OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/accelerators/accelerator_delegate.h" | 5 #include "ash/accelerators/accelerator_delegate.h" |
6 | 6 |
7 #include "ash/common/accelerators/accelerator_controller.h" | 7 #include "ash/aura/wm_window_aura.h" |
8 #include "ash/common/wm/window_state.h" | 8 #include "ash/common/accelerators/accelerator_router.h" |
9 #include "ash/common/wm_shell.h" | 9 #include "ui/aura/window.h" |
10 #include "ash/shell.h" | |
11 #include "ash/wm/window_state_aura.h" | |
12 #include "ui/base/accelerators/accelerator.h" | |
13 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
14 #include "ui/wm/core/window_util.h" | |
15 | 11 |
16 namespace ash { | 12 namespace ash { |
17 | 13 |
18 namespace { | 14 AcceleratorDelegate::AcceleratorDelegate() : router_(new AcceleratorRouter) {} |
19 | 15 |
20 // Returns true if |key_code| is a key usually handled directly by the shell. | |
21 bool IsSystemKey(ui::KeyboardCode key_code) { | |
22 #if defined(OS_CHROMEOS) | |
23 switch (key_code) { | |
24 case ui::VKEY_MEDIA_LAUNCH_APP2: // Fullscreen button. | |
25 case ui::VKEY_MEDIA_LAUNCH_APP1: // Overview button. | |
26 case ui::VKEY_BRIGHTNESS_DOWN: | |
27 case ui::VKEY_BRIGHTNESS_UP: | |
28 case ui::VKEY_KBD_BRIGHTNESS_DOWN: | |
29 case ui::VKEY_KBD_BRIGHTNESS_UP: | |
30 case ui::VKEY_VOLUME_MUTE: | |
31 case ui::VKEY_VOLUME_DOWN: | |
32 case ui::VKEY_VOLUME_UP: | |
33 case ui::VKEY_POWER: | |
34 return true; | |
35 default: | |
36 return false; | |
37 } | |
38 #endif // defined(OS_CHROMEOS) | |
39 return false; | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 AcceleratorDelegate::AcceleratorDelegate() {} | |
45 AcceleratorDelegate::~AcceleratorDelegate() {} | 16 AcceleratorDelegate::~AcceleratorDelegate() {} |
46 | 17 |
47 bool AcceleratorDelegate::ProcessAccelerator( | 18 bool AcceleratorDelegate::ProcessAccelerator( |
48 const ui::KeyEvent& key_event, | 19 const ui::KeyEvent& key_event, |
49 const ui::Accelerator& accelerator) { | 20 const ui::Accelerator& accelerator) { |
50 // Special hardware keys like brightness and volume are handled in | 21 return router_->ProcessAccelerator( |
51 // special way. However, some windows can override this behavior | 22 WmWindowAura::Get(static_cast<aura::Window*>(key_event.target())), |
52 // (e.g. Chrome v1 apps by default and Chrome v2 apps with | 23 key_event, accelerator); |
53 // permission) by setting a window property. | |
54 if (IsSystemKey(key_event.key_code()) && !CanConsumeSystemKeys(key_event)) { | |
55 // System keys are always consumed regardless of whether they trigger an | |
56 // accelerator to prevent windows from seeing unexpected key up events. | |
57 WmShell::Get()->accelerator_controller()->Process(accelerator); | |
58 return true; | |
59 } | |
60 if (!ShouldProcessAcceleratorNow(key_event, accelerator)) | |
61 return false; | |
62 return WmShell::Get()->accelerator_controller()->Process(accelerator); | |
63 } | |
64 | |
65 // Uses the top level window so if the target is a web contents window the | |
66 // containing parent window will be checked for the property. | |
67 bool AcceleratorDelegate::CanConsumeSystemKeys(const ui::KeyEvent& event) { | |
68 aura::Window* target = static_cast<aura::Window*>(event.target()); | |
69 DCHECK(target); | |
70 aura::Window* top_level = ::wm::GetToplevelWindow(target); | |
71 return top_level && wm::GetWindowState(top_level)->can_consume_system_keys(); | |
72 } | |
73 | |
74 // Returns true if the |accelerator| should be processed now, inside Ash's env | |
75 // event filter. | |
76 bool AcceleratorDelegate::ShouldProcessAcceleratorNow( | |
77 const ui::KeyEvent& event, | |
78 const ui::Accelerator& accelerator) { | |
79 // On ChromeOS, If the accelerator is Search+<key(s)> then it must never be | |
80 // intercepted by apps or windows. | |
81 if (accelerator.IsCmdDown()) | |
82 return true; | |
83 | |
84 aura::Window* target = static_cast<aura::Window*>(event.target()); | |
85 DCHECK(target); | |
86 | |
87 aura::Window::Windows root_windows = Shell::GetAllRootWindows(); | |
88 if (std::find(root_windows.begin(), root_windows.end(), target) != | |
89 root_windows.end()) | |
90 return true; | |
91 | |
92 aura::Window* top_level = ::wm::GetToplevelWindow(target); | |
93 AcceleratorController* accelerator_controller = | |
94 WmShell::Get()->accelerator_controller(); | |
95 | |
96 // Reserved accelerators (such as Power button) always have a prority. | |
97 if (accelerator_controller->IsReserved(accelerator)) | |
98 return true; | |
99 | |
100 // A full screen window has a right to handle all key events including the | |
101 // reserved ones. | |
102 if (top_level && wm::GetWindowState(top_level)->IsFullscreen()) { | |
103 // On ChromeOS, fullscreen windows are either browser or apps, which | |
104 // send key events to a web content first, then will process keys | |
105 // if the web content didn't consume them. | |
106 return false; | |
107 } | |
108 | |
109 // Handle preferred accelerators (such as ALT-TAB) before sending | |
110 // to the target. | |
111 if (accelerator_controller->IsPreferred(accelerator)) | |
112 return true; | |
113 | |
114 return WmShell::Get()->GetAppListTargetVisibility(); | |
115 } | 24 } |
116 | 25 |
117 } // namespace ash | 26 } // namespace ash |
OLD | NEW |