File: switch_access_back_button_view.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (126 lines) | stat: -rw-r--r-- 4,546 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/system/accessibility/switch_access/switch_access_back_button_view.h"

#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/ash_color_id.h"
#include "ash/system/accessibility/floating_menu_button.h"
#include "ash/system/tray/tray_constants.h"
#include "base/functional/bind.h"
#include "cc/paint/paint_flags.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/mojom/ax_node_data.mojom-shared.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/events/event.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/layout/box_layout.h"

namespace ash {

namespace {
// The width of a single color focus ring, in density-independent pixels.
constexpr int kFocusRingSingleColorWidthDp = 2;
// Additional buffer needed to prevent clipping at the focus ring's edges.
constexpr int kFocusRingBufferDp = 1;

constexpr int kRadiusDp = 18;
}  // namespace

SwitchAccessBackButtonView::SwitchAccessBackButtonView(bool for_menu) {
  // Calculate the side length of the bounding box, with room for the two-color
  // focus ring on either side.
  int side_length = 2 * (kRadiusDp + GetFocusRingWidthPerSide());

  views::Builder<SwitchAccessBackButtonView>(this)
      .SetMainAxisAlignment(views::BoxLayout::MainAxisAlignment::kCenter)
      .AddChildren(
          views::Builder<FloatingMenuButton>()
              .CopyAddressTo(&back_button_)
              .SetVectorIcon(for_menu ? kSwitchAccessCloseIcon
                                      : kSwitchAccessBackIcon)
              .SetTooltipText(l10n_util::GetStringUTF16(
                  IDS_ASH_SWITCH_ACCESS_BACK_BUTTON_DESCRIPTION))
              .SetPreferredSize(gfx::Size(2 * kRadiusDp, 2 * kRadiusDp))
              .SetDrawHighlight(true)
              .SetCallback(base::BindRepeating(
                  &SwitchAccessBackButtonView::OnButtonPressed,
                  base::Unretained(this))))
      .SetSize(gfx::Size(side_length, side_length))
      .BuildChildren();

  GetViewAccessibility().SetRole(ax::mojom::Role::kButton);
}

int SwitchAccessBackButtonView::GetFocusRingWidthPerSide() {
  return 2 * kFocusRingSingleColorWidthDp + kFocusRingBufferDp;
}

void SwitchAccessBackButtonView::SetFocusRing(bool should_show) {
  if (show_focus_ring_ == should_show) {
    return;
  }
  show_focus_ring_ = should_show;
  SchedulePaint();
}

void SwitchAccessBackButtonView::SetForMenu(bool for_menu) {
  if (for_menu) {
    back_button_->SetVectorIcon(kSwitchAccessCloseIcon);
  } else {
    back_button_->SetVectorIcon(kSwitchAccessBackIcon);
  }
}

gfx::Size SwitchAccessBackButtonView::CalculatePreferredSize(
    const views::SizeBounds& available_size) const {
  gfx::Size preferred_size =
      views::BoxLayoutView::CalculatePreferredSize(available_size);
  if (available_size.width().is_bounded()) {
    preferred_size.set_height(available_size.width().value());
  }
  return preferred_size;
}

void SwitchAccessBackButtonView::OnPaint(gfx::Canvas* canvas) {
  auto* color_provider = GetColorProvider();
  gfx::Rect rect(GetContentsBounds());
  cc::PaintFlags flags;
  flags.setAntiAlias(true);
  flags.setColor(color_provider->GetColor(kColorAshShieldAndBase80));
  flags.setStyle(cc::PaintFlags::kFill_Style);
  canvas->DrawCircle(gfx::PointF(rect.CenterPoint()), kRadiusDp, flags);

  if (!show_focus_ring_) {
    return;
  }

  flags.setColor(
      color_provider->GetColor(kColorAshSwitchAccessInnerStrokeColor));
  flags.setStyle(cc::PaintFlags::kStroke_Style);
  flags.setStrokeWidth(kFocusRingSingleColorWidthDp);
  canvas->DrawCircle(gfx::PointF(rect.CenterPoint()),
                     kRadiusDp + kFocusRingSingleColorWidthDp, flags);

  flags.setColor(
      color_provider->GetColor(kColorAshSwitchAccessOuterStrokeColor));
  canvas->DrawCircle(gfx::PointF(rect.CenterPoint()),
                     kRadiusDp + (2 * kFocusRingSingleColorWidthDp), flags);
}

void SwitchAccessBackButtonView::OnButtonPressed() {
  NotifyAccessibilityEventDeprecated(ax::mojom::Event::kClicked,
                                     /*send_native_event=*/false);
}

BEGIN_METADATA(SwitchAccessBackButtonView)
END_METADATA

}  // namespace ash