File: menu_pre_target_handler_aura.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 (132 lines) | stat: -rw-r--r-- 4,072 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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/menu/menu_pre_target_handler_aura.h"

#include <memory>

#include "ui/aura/env.h"
#include "ui/aura/window.h"
#include "ui/views/controls/menu/menu_controller.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/public/activation_client.h"

namespace views {

namespace {

aura::Window* GetOwnerRootWindow(views::Widget* owner) {
  return owner ? owner->GetNativeWindow()->GetRootWindow() : nullptr;
}

}  // namespace

MenuPreTargetHandlerAura::MenuPreTargetHandlerAura(MenuController* controller,
                                                   Widget* owner)
    : controller_(controller), root_(GetOwnerRootWindow(owner)) {
  if (root_) {
    wm::GetActivationClient(root_)->AddObserver(this);
    root_->AddObserver(this);
  } else {
    // This should only happen in cases like when context menus are shown for
    // Windows OS system tray items and there is no parent window.
  }
  aura::Env::GetInstance()->AddPreTargetHandler(
      this, ui::EventTarget::Priority::kSystem);
}

MenuPreTargetHandlerAura::~MenuPreTargetHandlerAura() {
  aura::Env::GetInstance()->RemovePreTargetHandler(this);
  Cleanup();
}

void MenuPreTargetHandlerAura::OnWindowActivated(
    wm::ActivationChangeObserver::ActivationReason reason,
    aura::Window* gained_active,
    aura::Window* lost_active) {
  if (!controller_->drag_in_progress()) {
    controller_->Cancel(MenuController::ExitType::kAll);
  }
}

void MenuPreTargetHandlerAura::OnWindowDestroying(aura::Window* window) {
  Cleanup();
}

void MenuPreTargetHandlerAura::OnCancelMode(ui::CancelModeEvent* event) {
  controller_->Cancel(MenuController::ExitType::kAll);
}

void MenuPreTargetHandlerAura::OnKeyEvent(ui::KeyEvent* event) {
  // Fully exit the menu, if the key event is supposed to perform some task.
  if (ShouldCancelMenuForEvent(*event)) {
    controller_->Cancel(MenuController::ExitType::kAll);
    return;
  }
  controller_->OnWillDispatchKeyEvent(event);
}

bool MenuPreTargetHandlerAura::ShouldCancelMenuForEvent(
    const ui::KeyEvent& event) {
  const ui::KeyboardCode key_code = event.key_code();
  switch (key_code) {
    case ui::VKEY_ESCAPE:
      // Fully exit the menu when Shift-Esc is pressed because it is
      // supposed to open the Task Manager on Windows and Linux
      if (event.IsShiftDown()) {
        return true;
      }
      break;
    case ui::VKEY_J:
      // Fully exit the menu when Ctrl+J is pressed because it is supposed
      // to open the downloads page on Windows and Linux.
      if (event.IsControlDown()) {
        return true;
      }
      break;
    case ui::VKEY_H:
    case ui::VKEY_R:
    case ui::VKEY_N:
    case ui::VKEY_T:
    case ui::VKEY_P:
    case ui::VKEY_S:
      // Fully exit the menu when:
      // Ctrl+H is pressed because it is supposed to open the history page.
      // Ctrl+R is pressed because it is supposed to reload the current page.
      // Ctrl+N/Ctrl+Shift+N is pressed because it is supposed to open the
      // new window/new incognito window.
      // Ctrl+T is pressed because it is supposed to open the new tab.
      // Ctrl+P is pressed because it is supposed to print the current page.
      // Ctrl+S is pressed because it is supposed to save the current web page.
      if (event.IsControlDown()) {
        return true;
      }
      break;
    default:
      break;
  }
  return false;
}

void MenuPreTargetHandlerAura::Cleanup() {
  if (!root_) {
    return;
  }
  // The ActivationClient may have been destroyed by the time we get here.
  wm::ActivationClient* client = wm::GetActivationClient(root_);
  if (client) {
    client->RemoveObserver(this);
  }
  root_->RemoveObserver(this);
  root_ = nullptr;
}

// static
std::unique_ptr<MenuPreTargetHandler> MenuPreTargetHandler::Create(
    MenuController* controller,
    Widget* owner) {
  return std::make_unique<MenuPreTargetHandlerAura>(controller, owner);
}

}  // namespace views