File: pre_target_handler.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 (373 lines) | stat: -rw-r--r-- 12,707 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2024 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/ui/ash/editor_menu/utils/pre_target_handler.h"

#include <cstddef>

#include "base/containers/adapters.h"
#include "chrome/browser/ui/ash/editor_menu/utils/utils.h"
#include "ui/aura/env.h"
#include "ui/events/keycodes/keyboard_codes_posix.h"
#include "ui/views/controls/menu/menu_controller.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/focus/external_focus_tracker.h"
#include "ui/views/focus/focus_manager.h"
#include "ui/views/view.h"
#include "ui/views/view_tracker.h"
#include "ui/views/widget/tooltip_manager.h"
#include "ui/views/widget/widget.h"

namespace chromeos::editor_menu {

namespace {

// If true. Focus can be moved into the root view by using the tab key.
// Otherwise, users can move focus into the root view using up and down keys.
bool IsNavigatableUsingTabKey(CardType card_type) {
  return card_type == CardType::kEditorMenu ||
         card_type == CardType::kMahiDefaultMenu ||
         card_type == CardType::kMagicBoostOptInCard;
}

}  // namespace

PreTargetHandler::PreTargetHandler(Delegate& delegate, const CardType& type)
    : delegate_(delegate), card_type_(type) {
  Init();
}

PreTargetHandler::~PreTargetHandler() {
  aura::Env::GetInstance()->RemovePreTargetHandler(this);

  // Cancel any active context-menus, |view_| was a companion-view of which,
  // unless it had requested otherwise for some cases.
  if (dismiss_anchor_menu_on_view_closed_) {
    auto* active_menu_instance = views::MenuController::GetActiveInstance();
    if (active_menu_instance) {
      active_menu_instance->Cancel(views::MenuController::ExitType::kAll);
    }
  }
}

void PreTargetHandler::Init() {
  // QuickAnswersView is a companion view of a menu. Menu host widget sets
  // mouse capture as well as a pre-target handler, so we need to register one
  // here as well to intercept events for QuickAnswersView.
  aura::Env::GetInstance()->AddPreTargetHandler(
      this, ui::EventTarget::Priority::kSystem);
  context_menu_focus_tracker_ = std::make_unique<views::ExternalFocusTracker>(
      delegate_->GetRootView(), /*focus_manager=*/nullptr);
}

void PreTargetHandler::OnEvent(ui::Event* event) {
  if (event->IsKeyEvent()) {
    ProcessKeyEvent(static_cast<ui::KeyEvent*>(event));
    return;
  }

  if (!event->IsLocatedEvent()) {
    return;
  }

  // Filter scroll event due to potential timing issue (b/258750397).
  if (event->IsScrollEvent() || event->type() == ui::EventType::kMousewheel) {
    return;
  }

  auto* root_view = delegate_->GetRootView();

  CHECK(root_view);

  // Clone event to forward down the view-hierarchy.
  auto clone = event->Clone();
  ui::Event::DispatcherApi(clone.get()).set_target(event->target());
  auto* to_dispatch = clone->AsLocatedEvent();
  auto location = to_dispatch->target()->GetScreenLocation(*to_dispatch);
  bool contains_location = root_view->GetBoundsInScreen().Contains(location);

  // Use a local `ViewTracker` in case `this` is deleted after
  // `DoDispatchEvent()` is run.
  views::ViewTracker view_tracker(root_view);

  // `EventType::kMouseMoved` events outside the top-view's bounds are also
  // dispatched to clear any set hover-state.
  bool dispatch_event =
      (contains_location || to_dispatch->type() == ui::EventType::kMouseMoved);
  if (dispatch_event) {
    // Convert to local coordinates and forward to the top-view.
    views::View::ConvertPointFromScreen(root_view, &location);
    to_dispatch->set_location(location);
    ui::Event::DispatcherApi(to_dispatch).set_target(root_view);

    // Convert touch-event to gesture before dispatching since views do not
    // process touch-events.
    std::unique_ptr<ui::GestureEvent> gesture_event;
    if (to_dispatch->type() == ui::EventType::kTouchPressed) {
      gesture_event = std::make_unique<ui::GestureEvent>(
          to_dispatch->x(), to_dispatch->y(), ui::EF_NONE,
          base::TimeTicks::Now(),
          ui::GestureEventDetails(ui::EventType::kGestureTap));
      to_dispatch = gesture_event.get();
    }

    DoDispatchEvent(root_view, to_dispatch);

    // Clicks inside Quick-Answers views can dismiss the menu since they are
    // outside menu-bounds and are thus not propagated to it to prevent so.
    // Active menu instance will instead be cancelled when |root_view| is
    // deleted.
    if (event->type() != ui::EventType::kMouseMoved) {
      event->StopPropagation();
    }
  }

  // After `DoDispatchEvent()` is run, the event dispatch can cause `root_view`
  // and `this` to be deleted.
  if (!view_tracker) {
    return;
  }

  // Show tooltips.
  auto* tooltip_manager = root_view->GetWidget()->GetTooltipManager();
  if (tooltip_manager) {
    tooltip_manager->UpdateTooltip();
  }
}

// TODO(siabhijeet): Investigate using SendEventsToSink() for dispatching.
bool PreTargetHandler::DoDispatchEvent(views::View* view,
                                       ui::LocatedEvent* event) {
  DCHECK(view && event);

  // Out-of-bounds `EventType::kMouseMoved` events are allowed to sift through
  // to clear any set hover-state.
  // TODO (siabhijeet): Two-phased dispatching via widget should fix this.
  if (!view->HitTestPoint(event->location()) &&
      event->type() != ui::EventType::kMouseMoved) {
    return false;
  }

  views::ViewTracker view_tracker(view);

  // Post-order dispatch the event on child views in reverse Z-order.
  auto children = view->GetChildrenInZOrder();
  for (views::View* child : base::Reversed(children)) {
    // Dispatch a fresh event to preserve the |event| for the parent target.
    std::unique_ptr<ui::Event> to_dispatch;
    if (event->IsMouseEvent()) {
      to_dispatch =
          std::make_unique<ui::MouseEvent>(*event->AsMouseEvent(), view, child);
    } else if (event->IsGestureEvent()) {
      to_dispatch = std::make_unique<ui::GestureEvent>(*event->AsGestureEvent(),
                                                       view, child);
    } else {
      return false;
    }
    ui::Event::DispatcherApi(to_dispatch.get()).set_target(child);
    if (DoDispatchEvent(child, to_dispatch.get()->AsLocatedEvent())) {
      return true;
    }

    // `view` can be deleted after `child` handles the event.
    if (!view_tracker) {
      return false;
    }
  }

  view->OnEvent(event);
  return event->handled();
}

void PreTargetHandler::ProcessKeyEvent(ui::KeyEvent* key_event) {
  if (key_event->type() != ui::EventType::kKeyPressed) {
    return;
  }

  auto* root_view = delegate_->GetRootView();

  CHECK(root_view);

  auto* focus_manager = root_view->GetFocusManager();
  auto* currently_focused_view = focus_manager->GetFocusedView();
  bool view_has_pane_focus = root_view->Contains(currently_focused_view);

  // |view_| will insert itself between the cyclic keyboard traversal order of
  // the last and the first menu items of the active menu by commandeering the
  // selection from these terminal items.

  // - VKEY_UP/VKEY_DOWN will move focus between the tracked view and the
  // context menu UIs if the tracking view is of type `kDefault`.
  // - VKEY_LEFT/VKEY_RIGHT will move focus inside the tracked view if the view
  // is in focus.
  // - VKEY_TAB will move the focus to the tracked view if it is an Editor
  // Menu or Mahi default menu. Note that in this case the card will get
  // activated, and the context menu will be dismissed.
  auto key_code = key_event->key_code();
  switch (key_code) {
    case ui::VKEY_UP:
    case ui::VKEY_DOWN: {
      if (!IsNavigatableUsingTabKey(card_type_)) {
        ProcessKeyUpAndDown(key_event);
      }
      return;
    }
    case ui::VKEY_TAB: {
      if (IsNavigatableUsingTabKey(card_type_) && !view_has_pane_focus) {
        root_view->RequestFocus();
        key_event->StopPropagation();
      }
      return;
    }
    case ui::VKEY_RETURN: {
      if (view_has_pane_focus) {
        auto* button_in_focus = views::Button::AsButton(currently_focused_view);
        if (button_in_focus) {
          button_in_focus->OnKeyPressed(*key_event);
        }
        key_event->StopPropagation();
      }
      return;
    }
    case ui::VKEY_LEFT:
    case ui::VKEY_RIGHT: {
      if (view_has_pane_focus) {
        bool reverse = key_code == ui::VKEY_LEFT;
        focus_manager->AdvanceFocus(reverse);
        key_event->StopPropagation();
      }
      return;
    }
    default:
      return;
  }
}

void PreTargetHandler::ProcessKeyUpAndDown(ui::KeyEvent* key_event) {
  auto* active_menu = views::MenuController::GetActiveInstance();
  CHECK(active_menu);

  auto* root_view = delegate_->GetRootView();
  CHECK(root_view);

  std::vector<views::View*> views =
      delegate_->GetTraversableViewsByUpDownKeys();
  if (views.empty()) {
    return;
  }

  auto* focus_manager = root_view->GetFocusManager();
  auto* currently_focused_view = focus_manager->GetFocusedView();
  auto key_code = key_event->key_code();

  for (size_t index = 0; index < views.size(); ++index) {
    auto* view = views[index];
    CHECK(view);

    // Only handle the case that view has focus.
    if (view != currently_focused_view &&
        !view->Contains(currently_focused_view)) {
      continue;
    }

    // Move focus to context menu if needed if the view is the first or last
    // view on the list.
    if ((index == 0 && key_code == ui::VKEY_UP) ||
        (index == views.size() - 1 && key_code == ui::VKEY_DOWN)) {
      MoveFocusToContextMenu();
      return;
    }

    // Move focus to the previous/next view depending on the key code.
    MoveFocusTo(views[index + (key_code == ui::VKEY_UP ? -1 : 1)], key_event,
                focus_manager);
    return;
  }

  // Get the selected item, if any, in the currently active menu.
  auto* const selected_item = active_menu->GetSelectedMenuItem();
  if (!selected_item) {
    return;
  }

  auto* const parent = selected_item->GetParentMenuItem();

  if (!parent) {
    // The root menu item (which doesn't have a parent) can be marked as
    // selected when there are no nested menus and no items are visibly
    // selected. In this case, the first view in the list should gain focus for
    // Up-key press.
    if (key_code == ui::VKEY_UP) {
      MoveFocusTo(views[0], key_event, focus_manager);
    }
    return;
  }

  // Check if the item is within the outer-most menu, since we do not want
  // the selection to loop back to any views for submenus.
  if (parent->GetParentMenuItem()) {
    return;
  }

  // Handle key events that transition from context menu to the traversable
  // view.
  bool first_item_selected =
      selected_item == parent->GetSubmenu()->children().front();
  if (first_item_selected && key_code == ui::VKEY_UP) {
    MoveFocusTo(views.back(), key_event, focus_manager);
    return;
  }

  bool last_item_selected =
      selected_item == parent->GetSubmenu()->children().back();
  if (last_item_selected && key_code == ui::VKEY_DOWN) {
    MoveFocusTo(views.front(), key_event, focus_manager);
  }
}

void PreTargetHandler::MoveFocusToContextMenu() {
  // Moves the focus back to context menu using `context_menu_focus_tracker_`.
  // The logic of selecting a correct menu item is done by the context menu.
  context_menu_focus_tracker_->FocusLastFocusedExternalView();
  context_menu_focus_tracker_->SetFocusManager(nullptr);

  auto* root_view = delegate_->GetRootView();
  CHECK(root_view);

  auto* focus_manager = root_view->GetFocusManager();

  // Explicitly lose focus if restoring to last focused did not work.
  if (root_view->Contains(focus_manager->GetFocusedView())) {
    focus_manager->SetFocusedView(nullptr);
  }
}

void PreTargetHandler::MoveFocusTo(views::View* view_gain_focus,
                                   ui::KeyEvent* key_event,
                                   views::FocusManager* current_focus_manager) {
  if (!view_gain_focus) {
    return;
  }

  // Track currently focused view to restore back to later and send focus
  // to `view_gain_focus`.
  context_menu_focus_tracker_->SetFocusManager(current_focus_manager);
  view_gain_focus->RequestFocus();
  key_event->StopPropagation();

  auto* active_menu = views::MenuController::GetActiveInstance();
  CHECK(active_menu);
  auto* const selected_item = active_menu->GetSelectedMenuItem();
  CHECK(selected_item);
  auto* const parent = selected_item->GetParentMenuItem();

  // Reopen the sub-menu owned by `parent` to clear the currently selected
  // boundary menu-item.
  if (parent) {
    active_menu->SelectItemAndOpenSubmenu(parent);
  }
}

}  // namespace chromeos::editor_menu