Introduce "Preferred" accelerators, which may be processed by fullscreen, but will...
[chromium-blink-merge.git] / ash / accelerators / accelerator_delegate.cc
blobbcdc75e0774d051e870bd530dc61319582f2cab3
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"
8 #include "ash/shell.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"
14 namespace ash {
15 namespace {} // namespace
17 AcceleratorDelegate::AcceleratorDelegate() {
19 AcceleratorDelegate::~AcceleratorDelegate() {
22 bool AcceleratorDelegate::ProcessAccelerator(const ui::KeyEvent& key_event,
23 const ui::Accelerator& accelerator,
24 KeyType key_type) {
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);
33 return true;
35 if (!ShouldProcessAcceleratorNow(key_event, accelerator))
36 return false;
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());
44 DCHECK(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
50 // event filter.
51 bool AcceleratorDelegate::ShouldProcessAcceleratorNow(
52 const ui::KeyEvent& event,
53 const ui::Accelerator& accelerator) {
54 aura::Window* target = static_cast<aura::Window*>(event.target());
55 DCHECK(target);
57 aura::Window::Windows root_windows = Shell::GetAllRootWindows();
58 if (std::find(root_windows.begin(), root_windows.end(), target) !=
59 root_windows.end())
60 return true;
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))
67 return true;
69 // A full screen window has a right to handle all key events including the
70 // reserved ones.
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.
75 return false;
78 // Handle preferred accelerators (such as ALT-TAB) before sending
79 // to the target.
80 if (shell->accelerator_controller()->IsPreferred(accelerator))
81 return true;
83 return shell->GetAppListTargetVisibility();
86 } // namespace ash