1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/accelerators/accelerator_delegate.h"
7 #include "ash/accelerators/accelerator_controller.h"
9 #include "ash/wm/window_state.h"
10 #include "ui/base/accelerators/accelerator.h"
11 #include "ui/events/event.h"
12 #include "ui/wm/core/window_util.h"
15 namespace {} // namespace
17 AcceleratorDelegate::AcceleratorDelegate() {
19 AcceleratorDelegate::~AcceleratorDelegate() {
22 bool AcceleratorDelegate::ProcessAccelerator(const ui::KeyEvent
& key_event
,
23 const ui::Accelerator
& accelerator
,
25 // Special hardware keys like brightness and volume are handled in
26 // special way. However, some windows can override this behavior
27 // (e.g. Chrome v1 apps by default and Chrome v2 apps with
28 // permission) by setting a window property.
29 if (key_type
== KEY_TYPE_SYSTEM
&& !CanConsumeSystemKeys(key_event
)) {
30 // System keys are always consumed regardless of whether they trigger an
31 // accelerator to prevent windows from seeing unexpected key up events.
32 Shell::GetInstance()->accelerator_controller()->Process(accelerator
);
35 if (!ShouldProcessAcceleratorNow(key_event
, accelerator
))
37 return Shell::GetInstance()->accelerator_controller()->Process(accelerator
);
40 // Uses the top level window so if the target is a web contents window the
41 // containing parent window will be checked for the property.
42 bool AcceleratorDelegate::CanConsumeSystemKeys(const ui::KeyEvent
& event
) {
43 aura::Window
* target
= static_cast<aura::Window
*>(event
.target());
45 aura::Window
* top_level
= ::wm::GetToplevelWindow(target
);
46 return top_level
&& wm::GetWindowState(top_level
)->can_consume_system_keys();
49 // Returns true if the |accelerator| should be processed now, inside Ash's env
51 bool AcceleratorDelegate::ShouldProcessAcceleratorNow(
52 const ui::KeyEvent
& event
,
53 const ui::Accelerator
& accelerator
) {
54 aura::Window
* target
= static_cast<aura::Window
*>(event
.target());
57 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
58 if (std::find(root_windows
.begin(), root_windows
.end(), target
) !=
62 aura::Window
* top_level
= ::wm::GetToplevelWindow(target
);
63 Shell
* shell
= Shell::GetInstance();
65 // Reserved accelerators (such as Power button) always have a prority.
66 if (shell
->accelerator_controller()->IsReserved(accelerator
))
69 // A full screen window has a right to handle all key events including the
71 if (top_level
&& wm::GetWindowState(top_level
)->IsFullscreen()) {
72 // On ChromeOS, fullscreen windows are either browser or apps, which
73 // send key events to a web content first, then will process keys
74 // if the web content didn't consume them.
78 // Handle preferred accelerators (such as ALT-TAB) before sending
80 if (shell
->accelerator_controller()->IsPreferred(accelerator
))
83 return shell
->GetAppListTargetVisibility();