File: delete_edit_shortcut.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (176 lines) | stat: -rw-r--r-- 7,118 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/arc/input_overlay/ui/delete_edit_shortcut.h"

#include <memory>

#include "ash/strings/grit/ash_strings.h"
#include "ash/style/icon_button.h"
#include "chrome/app/vector_icons/vector_icons.h"
#include "chrome/browser/ash/arc/input_overlay/actions/action.h"
#include "chrome/browser/ash/arc/input_overlay/arc_input_overlay_metrics.h"
#include "chrome/browser/ash/arc/input_overlay/constants.h"
#include "chrome/browser/ash/arc/input_overlay/display_overlay_controller.h"
#include "chrome/browser/ash/arc/input_overlay/ui/action_view_list_item.h"
#include "chrome/browser/ash/arc/input_overlay/util.h"
#include "chromeos/strings/grit/chromeos_strings.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"

namespace arc::input_overlay {

namespace {

constexpr int kSpaceToEditingList = 8;
constexpr char kDeleteEditShortcut[] = "DeleteEditShortcut";

}  // namespace

DeleteEditShortcut::DeleteEditShortcut(DisplayOverlayController* controller,
                                       ActionViewListItem* anchor_view)
    : views::BubbleDialogDelegateView(anchor_view,
                                      views::BubbleBorder::LEFT_CENTER,
                                      // TODO(b/329895423): Add shadow.
                                      views::BubbleBorder::NO_SHADOW),
      controller_(controller) {
  SetBackgroundColor(cros_tokens::kCrosSysSystemBaseElevatedOpaque);
  set_margins(gfx::Insets(12));
  set_corner_radius(20);
  set_close_on_deactivate(false);
  set_focus_traversable_from_anchor_view(true);
  set_internal_name(kDeleteEditShortcut);
  set_parent_window(anchor_view->GetWidget()->GetNativeWindow());
  SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
  SetEnableArrowKeyTraversal(true);

  // BubbleDialogDelegate::GetAccessibleWindowRole() is a final method which
  // can't override. If the window role is `kWindow`, it will force set it to
  // alert dialog and reads all the tooltips inside.
  // SetAccessibleWindowRole(kDialog) can prevent it.
  // SetAccessibleWindowRole(ax::mojom::Role::kMenu) results in screenreader to
  // announce the menu having only one item.
  SetAccessibleWindowRole(ax::mojom::Role::kDialog);

  // Set root view to menu.
  GetViewAccessibility().SetRole(ax::mojom::Role::kMenu);
  SetAccessibleTitle(
      l10n_util::GetStringUTF16(IDS_INPUT_OVERLAY_SHORTCUT_MENU_A11Y_LABEL));

  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical,
      /*inside_border_insets=*/gfx::Insets(),
      /*between_child_spacing=*/12));

  // Create the buttons with empty accessibility names. They will be updated in
  // `UpdateTooltipText`.
  edit_button_ = AddChildView(std::make_unique<ash::IconButton>(
      base::BindRepeating(&DeleteEditShortcut::OnEditButtonPressed,
                          base::Unretained(this)),
      ash::IconButton::Type::kMedium, &kGameControlsEditPenIcon, u"",
      /*is_togglable=*/false, /*has_border=*/false));
  edit_button_->GetViewAccessibility().SetRole(ax::mojom::Role::kMenuItem);

  delete_button_ = AddChildView(std::make_unique<ash::IconButton>(
      base::BindRepeating(&DeleteEditShortcut::OnDeleteButtonPressed,
                          base::Unretained(this)),
      ash::IconButton::Type::kMedium, &kGameControlsDeleteIcon, u"",
      /*is_togglable=*/false, /*has_border=*/false));
  delete_button_->GetViewAccessibility().SetRole(ax::mojom::Role::kMenuItem);

  UpdateTooltipText(anchor_view);
}

DeleteEditShortcut::~DeleteEditShortcut() = default;

void DeleteEditShortcut::UpdateAnchorView(ActionViewListItem* anchor_view) {
  // Reset the highlight of previous anchor view.
  SetHighlightedButton(anchor_view);

  SetAnchorView(anchor_view);
  UpdateTooltipText(anchor_view);

  // Clear focus when changing anchor view.
  if (auto* focus_manager = GetFocusManager()) {
    focus_manager->ClearFocus();
  }
}

void DeleteEditShortcut::UpdateTooltipText(ActionViewListItem* anchor_view) {
  const std::u16string action_name = anchor_view->CalculateActionName();
  // e.g. "Edit Button m"
  const std::u16string edit_text = l10n_util::GetStringFUTF16(
      IDS_INPUT_OVERLAY_SHORTCUT_EDIT_A11Y_LABEL_TEMPLATE, action_name);
  edit_button_->SetTooltipText(edit_text);

  // e.g. "Delete Joystick wasd"
  const std::u16string delete_text = l10n_util::GetStringFUTF16(
      IDS_INPUT_OVERLAY_SHORTCUT_DELETE_A11Y_LABEL_TEMPLATE, action_name);
  delete_button_->SetTooltipText(delete_text);
}

void DeleteEditShortcut::OnEditButtonPressed() {
  RecordEditDeleteMenuFunctionTriggered(controller_->GetPackageName(),
                                        EditDeleteMenuFunction::kEdit);
  if (auto* anchor_view =
          views::AsViewClass<ActionViewListItem>(GetAnchorView())) {
    controller_->AddButtonOptionsMenuWidget(anchor_view->action());
  }
}

void DeleteEditShortcut::OnDeleteButtonPressed() {
  RecordEditDeleteMenuFunctionTriggered(controller_->GetPackageName(),
                                        EditDeleteMenuFunction::kDelete);
  if (auto* anchor_view =
          views::AsViewClass<ActionViewListItem>(GetAnchorView())) {
    controller_->RemoveAction(anchor_view->action());
    controller_->RemoveDeleteEditShortcutWidget();
  }
}

std::unique_ptr<views::NonClientFrameView>
DeleteEditShortcut::CreateNonClientFrameView(views::Widget* widget) {
  // Create the customized bubble border.
  auto bubble_border =
      std::make_unique<views::BubbleBorder>(arrow(), GetShadow());
  bubble_border->SetColor(background_color());
  if (GetParams().round_corners) {
    bubble_border->set_rounded_corners(gfx::RoundedCornersF(GetCornerRadius()));
  }
  bubble_border->set_avoid_shadow_overlap(true);
  bubble_border->set_insets(
      gfx::Insets::VH(0, kSpaceToEditingList + kEditingListInsideBorderInsets));

  auto frame =
      views::BubbleDialogDelegateView::CreateNonClientFrameView(widget);
  if (auto* frame_view =
          views::AsViewClass<views::BubbleFrameView>(frame.get())) {
    frame_view->SetBubbleBorder(std::move(bubble_border));
  }
  return frame;
}

void DeleteEditShortcut::OnMouseExited(const ui::MouseEvent& event) {
  if (auto* widget = GetWidget();
      widget->IsMouseEventsEnabled() && !IsMouseHovered()) {
    controller_->RemoveDeleteEditShortcutWidget();
  }
}

BEGIN_METADATA(DeleteEditShortcut)
END_METADATA

}  // namespace arc::input_overlay