| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/mus/accelerators/accelerator_controller_registrar.h" | 5 #include "ash/mus/accelerators/accelerator_controller_registrar.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "ash/common/accelerators/accelerator_controller.h" | 9 #include "ash/common/accelerators/accelerator_controller.h" |
| 10 #include "ash/common/accelerators/accelerator_router.h" | 10 #include "ash/common/accelerators/accelerator_router.h" |
| 11 #include "ash/common/wm/window_cycle_controller.h" |
| 11 #include "ash/common/wm_shell.h" | 12 #include "ash/common/wm_shell.h" |
| 12 #include "ash/mus/accelerators/accelerator_ids.h" | 13 #include "ash/mus/accelerators/accelerator_ids.h" |
| 13 #include "ash/mus/window_manager.h" | 14 #include "ash/mus/window_manager.h" |
| 14 #include "ash/public/interfaces/event_properties.mojom.h" | 15 #include "ash/public/interfaces/event_properties.mojom.h" |
| 15 #include "ash/shell.h" | 16 #include "ash/shell.h" |
| 16 #include "base/logging.h" | 17 #include "base/logging.h" |
| 17 #include "services/ui/common/accelerator_util.h" | 18 #include "services/ui/common/accelerator_util.h" |
| 18 #include "services/ui/public/cpp/property_type_converters.h" | 19 #include "services/ui/public/cpp/property_type_converters.h" |
| 20 #include "ui/base/accelerators/accelerator.h" |
| 19 #include "ui/base/accelerators/accelerator_history.h" | 21 #include "ui/base/accelerators/accelerator_history.h" |
| 20 | 22 |
| 21 namespace ash { | 23 namespace ash { |
| 22 namespace mus { | 24 namespace mus { |
| 23 namespace { | 25 namespace { |
| 24 | 26 |
| 25 // Callback from registering the accelerators. | 27 // Callback from registering the accelerators. |
| 26 void OnAcceleratorsAdded(const std::vector<ui::Accelerator>& accelerators, | 28 void OnAcceleratorsAdded(const std::vector<ui::Accelerator>& accelerators, |
| 27 bool added) { | 29 bool added) { |
| 28 // All our accelerators should be registered, so we expect |added| to be true. | 30 // All our accelerators should be registered, so we expect |added| to be true. |
| 29 DCHECK(added) << "Unexpected accelerator vector registration failure."; | 31 DCHECK(added) << "Unexpected accelerator vector registration failure."; |
| 30 } | 32 } |
| 31 | 33 |
| 32 } // namespace | 34 } // namespace |
| 33 | 35 |
| 34 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar( | 36 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar( |
| 35 WindowManager* window_manager, | 37 WindowManager* window_manager, |
| 36 uint16_t id_namespace) | 38 uint16_t id_namespace) |
| 37 : window_manager_(window_manager), | 39 : window_cycle_complete_accelerator_(ui::VKEY_MENU, |
| 40 ui::EF_NONE, |
| 41 ui::Accelerator::KeyState::RELEASED), |
| 42 window_cycle_cancel_accelerator_(ui::VKEY_ESCAPE, ui::EF_ALT_DOWN), |
| 43 window_manager_(window_manager), |
| 38 id_namespace_(id_namespace), | 44 id_namespace_(id_namespace), |
| 39 next_id_(0), | 45 next_id_(0), |
| 40 router_(new AcceleratorRouter) { | 46 router_(new AcceleratorRouter) { |
| 41 window_manager_->AddAcceleratorHandler(id_namespace, this); | 47 window_manager_->AddAcceleratorHandler(id_namespace, this); |
| 48 RegisterWindowCycleAccelerators(); |
| 42 } | 49 } |
| 43 | 50 |
| 44 AcceleratorControllerRegistrar::~AcceleratorControllerRegistrar() { | 51 AcceleratorControllerRegistrar::~AcceleratorControllerRegistrar() { |
| 45 window_manager_->RemoveAcceleratorHandler(id_namespace_); | 52 window_manager_->RemoveAcceleratorHandler(id_namespace_); |
| 46 | 53 |
| 47 if (!window_manager_->window_manager_client()) | 54 if (!window_manager_->window_manager_client()) |
| 48 return; | 55 return; |
| 49 | 56 |
| 50 // TODO(sky): consider not doing this. If we assume the destructor is called | 57 // TODO(sky): consider not doing this. If we assume the destructor is called |
| 51 // during shutdown, then this is unnecessary and results in a bunch of | 58 // during shutdown, then this is unnecessary and results in a bunch of |
| (...skipping 23 matching lines...) Expand all Loading... |
| 75 if (is_pre) { | 82 if (is_pre) { |
| 76 // TODO(sky): this does not exactly match ash code. In particular ash code | 83 // TODO(sky): this does not exactly match ash code. In particular ash code |
| 77 // is called for *all* key events, where as this is only called for | 84 // is called for *all* key events, where as this is only called for |
| 78 // registered accelerators. This means the previous accelerator isn't the | 85 // registered accelerators. This means the previous accelerator isn't the |
| 79 // same as it was in ash. We need to figure out exactly what is needed of | 86 // same as it was in ash. We need to figure out exactly what is needed of |
| 80 // previous accelerator so that we can either register for the right set of | 87 // previous accelerator so that we can either register for the right set of |
| 81 // accelerators, or make WS send the previous accelerator. | 88 // accelerators, or make WS send the previous accelerator. |
| 82 // http://crbug.com/630683. | 89 // http://crbug.com/630683. |
| 83 accelerator_controller->accelerator_history()->StoreCurrentAccelerator( | 90 accelerator_controller->accelerator_history()->StoreCurrentAccelerator( |
| 84 accelerator); | 91 accelerator); |
| 92 if (HandleWindowCycleAccelerator(accelerator)) |
| 93 return ui::mojom::EventResult::HANDLED; |
| 85 WmWindow* target_window = WmShell::Get()->GetFocusedWindow(); | 94 WmWindow* target_window = WmShell::Get()->GetFocusedWindow(); |
| 86 if (!target_window) | 95 if (!target_window) |
| 87 target_window = Shell::GetWmRootWindowForNewWindows(); | 96 target_window = Shell::GetWmRootWindowForNewWindows(); |
| 88 DCHECK(target_window); | 97 DCHECK(target_window); |
| 89 if (router_->ProcessAccelerator(target_window, *(event.AsKeyEvent()), | 98 if (router_->ProcessAccelerator(target_window, *(event.AsKeyEvent()), |
| 90 accelerator)) { | 99 accelerator)) { |
| 91 return ui::mojom::EventResult::HANDLED; | 100 return ui::mojom::EventResult::HANDLED; |
| 92 } | 101 } |
| 93 if (accelerator_controller->IsActionForAcceleratorEnabled(accelerator)) { | 102 if (accelerator_controller->IsActionForAcceleratorEnabled(accelerator)) { |
| 94 // We do have an accelerator for the key. Set a property so that the real | 103 // We do have an accelerator for the key. Set a property so that the real |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() { | 187 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() { |
| 179 DCHECK_LT(ids_.size(), std::numeric_limits<uint16_t>::max()); | 188 DCHECK_LT(ids_.size(), std::numeric_limits<uint16_t>::max()); |
| 180 // Common case is we never wrap once, so this is typically cheap. Additionally | 189 // Common case is we never wrap once, so this is typically cheap. Additionally |
| 181 // we expect there not to be too many accelerators. | 190 // we expect there not to be too many accelerators. |
| 182 while (ids_.count(next_id_) > 0) | 191 while (ids_.count(next_id_) > 0) |
| 183 ++next_id_; | 192 ++next_id_; |
| 184 ids_.insert(next_id_); | 193 ids_.insert(next_id_); |
| 185 return next_id_++; | 194 return next_id_++; |
| 186 } | 195 } |
| 187 | 196 |
| 197 void AcceleratorControllerRegistrar::RegisterWindowCycleAccelerators() { |
| 198 std::vector<ui::Accelerator> accelerators; |
| 199 accelerators.push_back(window_cycle_complete_accelerator_); |
| 200 accelerators.push_back(window_cycle_cancel_accelerator_); |
| 201 OnAcceleratorsRegistered(accelerators); |
| 202 } |
| 203 |
| 204 bool AcceleratorControllerRegistrar::HandleWindowCycleAccelerator( |
| 205 const ui::Accelerator& accelerator) { |
| 206 ash::WindowCycleController* window_cycle_controller = |
| 207 Shell::Get()->window_cycle_controller(); |
| 208 if (!window_cycle_controller->IsCycling()) |
| 209 return false; |
| 210 |
| 211 if (accelerator == window_cycle_complete_accelerator_) { |
| 212 window_cycle_controller->CompleteCycling(); |
| 213 return true; |
| 214 } |
| 215 |
| 216 if (accelerator == window_cycle_cancel_accelerator_) { |
| 217 window_cycle_controller->CancelCycling(); |
| 218 return true; |
| 219 } |
| 220 |
| 221 return false; |
| 222 } |
| 223 |
| 188 } // namespace mus | 224 } // namespace mus |
| 189 } // namespace ash | 225 } // namespace ash |
| OLD | NEW |