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 // A full screen window should be able to handle all key events including the
64 aura::Window
* top_level
= ::wm::GetToplevelWindow(target
);
66 if (top_level
&& wm::GetWindowState(top_level
)->IsFullscreen()) {
67 // TODO(yusukes): On Chrome OS, only browser and flash windows can be full
68 // screen. Launching an app in "open full-screen" mode is not supported yet.
69 // That makes the IsWindowFullscreen() check above almost meaningless
70 // because a browser and flash window do handle Ash accelerators anyway
71 // before they're passed to a page or flash content.
75 if (Shell::GetInstance()->GetAppListTargetVisibility())
78 // Unless |target| is in the full screen state, handle reserved accelerators
79 // such as Alt+Tab now.
80 return Shell::GetInstance()->accelerator_controller()->IsReservedAccelerator(