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