File: mouse_keys_bubble_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (130 lines) | stat: -rw-r--r-- 4,453 bytes parent folder | download | duplicates (5)
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
127
128
129
130
// Copyright 2025 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/mouse_keys/mouse_keys_bubble_view.h"

#include <memory>
#include <string_view>
#include <vector>

#include "ash/public/cpp/accessibility_controller_enums.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/shell.h"
#include "ash/style/ash_color_id.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/views/accessibility/view_accessibility.h"

namespace ash {

namespace {
constexpr int kIconSizeDip = 16;
constexpr int kSpaceBetweenIconAndTextDip = 4;

std::unique_ptr<views::ImageView> CreateImageView(
    const gfx::VectorIcon& icon,
    raw_ptr<views::ImageView>* destination_view) {
  return views::Builder<views::ImageView>()
      .SetImage(ui::ImageModel::FromVectorIcon(icon, kColorAshTextColorPrimary,
                                               kIconSizeDip))
      .CopyAddressTo(destination_view)
      .Build();
}

std::unique_ptr<views::Label> CreateLabelView(
    const std::u16string& text,
    ui::ColorId enabled_color_id,
    raw_ptr<views::Label>* destination_view) {
  return views::Builder<views::Label>()
      .SetText(text)
      .SetEnabledColor(enabled_color_id)
      .SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_LEFT)
      .SetMultiLine(false)
      .CopyAddressTo(destination_view)
      .Build();
}

aura::Window* FindRootWindowAtMousePosition() {
  auto* screen = display::Screen::GetScreen();
  CHECK(screen);
  auto display = screen->GetDisplayNearestPoint(screen->GetCursorScreenPoint());
  return Shell::GetRootWindowForDisplayId(display.id());
}

}  // namespace

MouseKeysBubbleView::MouseKeysBubbleView() {
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));

  auto* screen = display::Screen::GetScreen();
  CHECK(screen);
  auto display = screen->GetDisplayNearestPoint(screen->GetCursorScreenPoint());

  set_parent_window(
      Shell::GetContainer(FindRootWindowAtMousePosition(),
                          kShellWindowId_AccessibilityBubbleContainer));

  // Set layout.
  std::unique_ptr<views::BoxLayout> layout = std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kHorizontal);
  layout->set_between_child_spacing(kSpaceBetweenIconAndTextDip);
  SetLayoutManager(std::move(layout));
  UseCompactMargins();

  // Add icons.
  AddChildView(
      CreateImageView(kSystemMenuMouseIcon, &mouse_button_change_icon_));
  AddChildView(CreateImageView(kMouseKeysDragIcon, &mouse_drag_icon_));

  // Add label.
  AddChildView(
      CreateLabelView(std::u16string(), kColorAshTextColorPrimary, &label_));

  GetViewAccessibility().SetRole(ax::mojom::Role::kGenericContainer);
  // Note: this static variable is used so that this view can be identified
  // from tests. Do not change this, as it will cause test failures.
  GetViewAccessibility().SetClassName("MouseKeysBubbleView");
}

MouseKeysBubbleView::~MouseKeysBubbleView() = default;

void MouseKeysBubbleView::Update(MouseKeysBubbleIconType icon,
                                 const std::optional<std::u16string>& text) {
  // Update icon visibility.
  mouse_button_change_icon_->SetVisible(
      icon == MouseKeysBubbleIconType::kButtonChanged);
  mouse_drag_icon_->SetVisible(icon == MouseKeysBubbleIconType::kMouseDrag);
  // Update label.
  label_->SetVisible(text.has_value());
  label_->SetText(text.has_value() ? text.value() : std::u16string());
  SizeToPreferredSize();
  SizeToContents();
}

void MouseKeysBubbleView::OnBeforeBubbleWidgetInit(
    views::Widget::InitParams* params,
    views::Widget* widget) const {
  params->type = views::Widget::InitParams::TYPE_BUBBLE;
  params->opacity = views::Widget::InitParams::WindowOpacity::kTranslucent;
  params->activatable = views::Widget::InitParams::Activatable::kNo;
  params->shadow_type = views::Widget::InitParams::ShadowType::kDrop;
  params->name = "MouseKeysBubbleView";
}

std::u16string_view MouseKeysBubbleView::GetTextForTesting() const {
  return label_->GetText();
}

views::ImageView* MouseKeysBubbleView::GetMouseButtonChangeIconForTesting()
    const {
  return mouse_button_change_icon_;
}

views::ImageView* MouseKeysBubbleView::GetMouseDragIconForTesting() const {
  return mouse_drag_icon_;
}

BEGIN_METADATA(MouseKeysBubbleView)
END_METADATA
}  // namespace ash