Chromium Code Reviews| 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 "athena/input/accelerator_manager_impl.h" | 5 #include "athena/input/accelerator_manager_impl.h" |
| 6 | 6 |
| 7 #include "athena/common/switches.h" | 7 #include "athena/common/switches.h" |
| 8 #include "athena/input/public/input_manager.h" | 8 #include "athena/input/public/input_manager.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "ui/aura/window.h" | 10 #include "ui/aura/window.h" |
| 11 #include "ui/base/accelerators/accelerator_manager.h" | 11 #include "ui/base/accelerators/accelerator_manager.h" |
| 12 #include "ui/events/event.h" | 12 #include "ui/events/event.h" |
| 13 #include "ui/events/event_target.h" | 13 #include "ui/events/event_target.h" |
| 14 #include "ui/views/focus/focus_manager.h" | 14 #include "ui/views/focus/focus_manager.h" |
| 15 #include "ui/views/focus/focus_manager_delegate.h" | 15 #include "ui/views/focus/focus_manager_delegate.h" |
| 16 #include "ui/views/focus/focus_manager_factory.h" | 16 #include "ui/views/focus/focus_manager_factory.h" |
| 17 #include "ui/wm/core/accelerator_delegate.h" | 17 #include "ui/wm/core/accelerator_delegate.h" |
| 18 #include "ui/wm/core/accelerator_filter.h" | 18 #include "ui/wm/core/accelerator_filter.h" |
| 19 #include "ui/wm/core/nested_accelerator_controller.h" | 19 #include "ui/wm/core/nested_accelerator_controller.h" |
| 20 #include "ui/wm/core/nested_accelerator_delegate.h" | 20 #include "ui/wm/core/nested_accelerator_delegate.h" |
| 21 #include "ui/wm/public/dispatcher_client.h" | 21 #include "ui/wm/public/dispatcher_client.h" |
| 22 | 22 |
| 23 namespace athena { | 23 namespace athena { |
| 24 | 24 |
| 25 // This wrapper interface provides a common interface that handles global | |
| 26 // accelerators as well as local accelerators. | |
| 27 class AcceleratorManagerImpl::AcceleratorWrapper { | |
| 28 public: | |
| 29 virtual ~AcceleratorWrapper() {} | |
| 30 virtual void Register(const ui::Accelerator& accelerator, | |
| 31 ui::AcceleratorTarget* target) = 0; | |
| 32 virtual bool Process(const ui::Accelerator& accelerator) = 0; | |
| 33 virtual ui::AcceleratorTarget* GetCurrentTarget( | |
| 34 const ui::Accelerator& accelertor) const = 0; | |
| 35 }; | |
| 36 | |
| 25 namespace { | 37 namespace { |
| 26 | 38 |
| 27 // Accelerators inside nested message loop are handled by | 39 // Accelerators inside nested message loop are handled by |
| 28 // wm::NestedAcceleratorController while accelerators in normal case are | 40 // wm::NestedAcceleratorController while accelerators in normal case are |
| 29 // handled by wm::AcceleratorFilter. These delegates act bridges in these | 41 // handled by wm::AcceleratorFilter. These delegates act bridges in these |
| 30 // two different environment so that AcceleratorManagerImpl can handle | 42 // two different environment so that AcceleratorManagerImpl can handle |
| 31 // accelerators in an uniform way. | 43 // accelerators in an uniform way. |
| 32 | 44 |
| 33 class NestedAcceleratorDelegate : public wm::NestedAcceleratorDelegate { | 45 class NestedAcceleratorDelegate : public wm::NestedAcceleratorDelegate { |
| 34 public: | 46 public: |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 56 : accelerator_manager_(accelerator_manager) {} | 68 : accelerator_manager_(accelerator_manager) {} |
| 57 virtual ~AcceleratorDelegate() {} | 69 virtual ~AcceleratorDelegate() {} |
| 58 | 70 |
| 59 private: | 71 private: |
| 60 // wm::AcceleratorDelegate: | 72 // wm::AcceleratorDelegate: |
| 61 virtual bool ProcessAccelerator(const ui::KeyEvent& event, | 73 virtual bool ProcessAccelerator(const ui::KeyEvent& event, |
| 62 const ui::Accelerator& accelerator, | 74 const ui::Accelerator& accelerator, |
| 63 KeyType key_type) OVERRIDE { | 75 KeyType key_type) OVERRIDE { |
| 64 aura::Window* target = static_cast<aura::Window*>(event.target()); | 76 aura::Window* target = static_cast<aura::Window*>(event.target()); |
| 65 if (!target->IsRootWindow() && | 77 if (!target->IsRootWindow() && |
| 66 !accelerator_manager_->IsReserved(accelerator)) { | 78 !accelerator_manager_->IsRegistered(accelerator, AF_RESERVED)) { |
| 67 // TODO(oshima): do the same when the active window is in fullscreen. | 79 // TODO(oshima): do the same when the active window is in fullscreen. |
| 68 return false; | 80 return false; |
| 69 } | 81 } |
| 70 return accelerator_manager_->Process(accelerator); | 82 return accelerator_manager_->Process(accelerator); |
| 71 } | 83 } |
| 72 | 84 |
| 73 AcceleratorManagerImpl* accelerator_manager_; | 85 AcceleratorManagerImpl* accelerator_manager_; |
| 74 DISALLOW_COPY_AND_ASSIGN(AcceleratorDelegate); | 86 DISALLOW_COPY_AND_ASSIGN(AcceleratorDelegate); |
| 75 }; | 87 }; |
| 76 | 88 |
| 77 class FocusManagerDelegate : public views::FocusManagerDelegate { | 89 class FocusManagerDelegate : public views::FocusManagerDelegate { |
| 78 public: | 90 public: |
| 79 explicit FocusManagerDelegate(AcceleratorManagerImpl* accelerator_manager) | 91 explicit FocusManagerDelegate(AcceleratorManagerImpl* accelerator_manager) |
| 80 : accelerator_manager_(accelerator_manager) {} | 92 : accelerator_manager_(accelerator_manager) {} |
| 81 virtual ~FocusManagerDelegate() {} | 93 virtual ~FocusManagerDelegate() {} |
| 82 | 94 |
| 83 virtual bool ProcessAccelerator(const ui::Accelerator& accelerator) OVERRIDE { | 95 virtual bool ProcessAccelerator(const ui::Accelerator& accelerator) OVERRIDE { |
| 84 return accelerator_manager_->Process(accelerator); | 96 return accelerator_manager_->Process(accelerator); |
| 85 } | 97 } |
| 86 | 98 |
| 87 virtual ui::AcceleratorTarget* GetCurrentTargetForAccelerator( | 99 virtual ui::AcceleratorTarget* GetCurrentTargetForAccelerator( |
| 88 const ui::Accelerator& accelerator) const OVERRIDE { | 100 const ui::Accelerator& accelerator) const OVERRIDE { |
| 89 return accelerator_manager_->IsRegistered(accelerator) | 101 return accelerator_manager_->IsRegistered(accelerator, AF_NONE) |
| 90 ? accelerator_manager_ | 102 ? accelerator_manager_ |
| 91 : NULL; | 103 : NULL; |
| 92 } | 104 } |
| 93 | 105 |
| 94 private: | 106 private: |
| 95 AcceleratorManagerImpl* accelerator_manager_; | 107 AcceleratorManagerImpl* accelerator_manager_; |
| 96 | 108 |
| 97 DISALLOW_COPY_AND_ASSIGN(FocusManagerDelegate); | 109 DISALLOW_COPY_AND_ASSIGN(FocusManagerDelegate); |
| 98 }; | 110 }; |
| 99 | 111 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 114 widget, | 126 widget, |
| 115 desktop_widget ? NULL : new FocusManagerDelegate(accelerator_manager_)); | 127 desktop_widget ? NULL : new FocusManagerDelegate(accelerator_manager_)); |
| 116 } | 128 } |
| 117 | 129 |
| 118 private: | 130 private: |
| 119 AcceleratorManagerImpl* accelerator_manager_; | 131 AcceleratorManagerImpl* accelerator_manager_; |
| 120 | 132 |
| 121 DISALLOW_COPY_AND_ASSIGN(FocusManagerFactory); | 133 DISALLOW_COPY_AND_ASSIGN(FocusManagerFactory); |
| 122 }; | 134 }; |
| 123 | 135 |
| 136 class UIAcceleratorManagerWrapper | |
| 137 : public AcceleratorManagerImpl::AcceleratorWrapper { | |
| 138 public: | |
| 139 UIAcceleratorManagerWrapper() | |
| 140 : ui_accelerator_manager_(new ui::AcceleratorManager) {} | |
| 141 virtual ~UIAcceleratorManagerWrapper() {} | |
| 142 | |
| 143 virtual void Register(const ui::Accelerator& accelerator, | |
| 144 ui::AcceleratorTarget* target) OVERRIDE { | |
| 145 return ui_accelerator_manager_->Register( | |
| 146 accelerator, ui::AcceleratorManager::kNormalPriority, target); | |
| 147 } | |
| 148 | |
| 149 virtual bool Process(const ui::Accelerator& accelerator) OVERRIDE { | |
| 150 return ui_accelerator_manager_->Process(accelerator); | |
| 151 } | |
| 152 | |
| 153 virtual ui::AcceleratorTarget* GetCurrentTarget( | |
| 154 const ui::Accelerator& accelerator) const OVERRIDE { | |
| 155 return ui_accelerator_manager_->GetCurrentTarget(accelerator); | |
| 156 } | |
| 157 | |
| 158 private: | |
| 159 scoped_ptr<ui::AcceleratorManager> ui_accelerator_manager_; | |
| 160 | |
| 161 DISALLOW_COPY_AND_ASSIGN(UIAcceleratorManagerWrapper); | |
| 162 }; | |
| 163 | |
| 164 class FocusManagerWrapper : public AcceleratorManagerImpl::AcceleratorWrapper { | |
| 165 public: | |
| 166 explicit FocusManagerWrapper(views::FocusManager* focus_manager) | |
|
Jun Mukai
2014/06/11 22:07:51
Can we make FocusManager being a subclass of ui::A
oshima
2014/06/11 22:46:43
what do you mean? Focus manager is defined in view
Jun Mukai
2014/06/11 23:13:22
Why not? views::FocusManager creates its own Acce
Jun Mukai
2014/06/11 23:16:10
Or -- stop the AcceleratorWrapper class, but creat
oshima
2014/06/12 17:06:38
AcceleratorManager is internals state because Focu
oshima
2014/06/12 17:06:38
Using inheritance for has-a relation is considered
| |
| 167 : focus_manager_(focus_manager) {} | |
| 168 virtual ~FocusManagerWrapper() {} | |
| 169 | |
| 170 virtual void Register(const ui::Accelerator& accelerator, | |
| 171 ui::AcceleratorTarget* target) OVERRIDE { | |
| 172 return focus_manager_->RegisterAccelerator( | |
| 173 accelerator, ui::AcceleratorManager::kNormalPriority, target); | |
| 174 } | |
| 175 | |
| 176 virtual bool Process(const ui::Accelerator& accelerator) OVERRIDE { | |
| 177 NOTREACHED(); | |
| 178 return true; | |
| 179 } | |
| 180 | |
| 181 virtual ui::AcceleratorTarget* GetCurrentTarget( | |
| 182 const ui::Accelerator& accelerator) const OVERRIDE { | |
| 183 return focus_manager_->GetCurrentTargetForAccelerator(accelerator); | |
| 184 } | |
| 185 | |
| 186 private: | |
| 187 views::FocusManager* focus_manager_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(FocusManagerWrapper); | |
| 190 }; | |
| 191 | |
| 124 } // namespace | 192 } // namespace |
| 125 | 193 |
| 126 class AcceleratorManagerImpl::InternalData { | 194 class AcceleratorManagerImpl::InternalData { |
| 127 public: | 195 public: |
| 128 InternalData(int command_id, AcceleratorHandler* handler, int flags) | 196 InternalData(int command_id, AcceleratorHandler* handler, int flags) |
| 129 : command_id_(command_id), handler_(handler), flags_(flags) {} | 197 : command_id_(command_id), handler_(handler), flags_(flags) {} |
| 130 | 198 |
| 131 bool IsNonAutoRepeatable() const { return flags_ & AF_NON_AUTO_REPEATABLE; } | 199 bool IsNonAutoRepeatable() const { return flags_ & AF_NON_AUTO_REPEATABLE; } |
| 132 bool IsDebug() const { return flags_ & AF_DEBUG; } | 200 bool IsDebug() const { return flags_ & AF_DEBUG; } |
| 133 bool IsReserved() const { return flags_ & AF_RESERVED; } | 201 int flags() const { return flags_; } |
| 134 | 202 |
| 135 bool IsCommandEnabled() const { | 203 bool IsCommandEnabled() const { |
| 136 return handler_->IsCommandEnabled(command_id_); | 204 return handler_->IsCommandEnabled(command_id_); |
| 137 } | 205 } |
| 138 | 206 |
| 139 bool OnAcceleratorFired(const ui::Accelerator& accelerator) { | 207 bool OnAcceleratorFired(const ui::Accelerator& accelerator) { |
| 140 return handler_->OnAcceleratorFired(command_id_, accelerator); | 208 return handler_->OnAcceleratorFired(command_id_, accelerator); |
| 141 } | 209 } |
| 142 | 210 |
| 143 private: | 211 private: |
| 144 int command_id_; | 212 int command_id_; |
| 145 AcceleratorHandler* handler_; | 213 AcceleratorHandler* handler_; |
| 146 int flags_; | 214 int flags_; |
| 147 | 215 |
| 148 // This class is copyable by design. | 216 // This class is copyable by design. |
| 149 }; | 217 }; |
| 150 | 218 |
| 151 AcceleratorManagerImpl::AcceleratorManagerImpl() | 219 // static |
| 152 : accelerator_manager_(new ui::AcceleratorManager), | 220 AcceleratorManagerImpl* |
| 221 AcceleratorManagerImpl::CreateGlobalAcceleratorManager() { | |
| 222 return new AcceleratorManagerImpl(new UIAcceleratorManagerWrapper()); | |
| 223 } | |
| 224 | |
| 225 AcceleratorManagerImpl::AcceleratorManagerImpl( | |
| 226 AcceleratorWrapper* accelerator_wrapper) | |
| 227 : accelerator_wrapper_(accelerator_wrapper), | |
| 153 debug_accelerators_enabled_(switches::IsDebugAcceleratorsEnabled()) { | 228 debug_accelerators_enabled_(switches::IsDebugAcceleratorsEnabled()) { |
| 154 } | 229 } |
| 155 | 230 |
| 156 AcceleratorManagerImpl::~AcceleratorManagerImpl() { | 231 AcceleratorManagerImpl::~AcceleratorManagerImpl() { |
| 157 nested_accelerator_controller_.reset(); | 232 nested_accelerator_controller_.reset(); |
| 158 accelerator_filter_.reset(); | 233 accelerator_filter_.reset(); |
| 159 // Reset to use the default focus manager because the athena's | 234 // Reset to use the default focus manager because the athena's |
| 160 // FocusManager has the reference to this object. | 235 // FocusManager has the reference to this object. |
| 161 views::FocusManagerFactory::Install(NULL); | 236 views::FocusManagerFactory::Install(NULL); |
| 162 } | 237 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 174 accelerator_filter_.reset( | 249 accelerator_filter_.reset( |
| 175 new wm::AcceleratorFilter(accelerator_delegate.Pass())); | 250 new wm::AcceleratorFilter(accelerator_delegate.Pass())); |
| 176 toplevel->AddPreTargetHandler(accelerator_filter_.get()); | 251 toplevel->AddPreTargetHandler(accelerator_filter_.get()); |
| 177 } | 252 } |
| 178 | 253 |
| 179 void AcceleratorManagerImpl::OnRootWindowCreated(aura::Window* root_window) { | 254 void AcceleratorManagerImpl::OnRootWindowCreated(aura::Window* root_window) { |
| 180 aura::client::SetDispatcherClient(root_window, | 255 aura::client::SetDispatcherClient(root_window, |
| 181 nested_accelerator_controller_.get()); | 256 nested_accelerator_controller_.get()); |
| 182 } | 257 } |
| 183 | 258 |
| 184 bool AcceleratorManagerImpl::IsRegistered( | 259 bool AcceleratorManagerImpl::Process(const ui::Accelerator& accelerator) { |
| 185 const ui::Accelerator& accelerator) const { | 260 return accelerator_wrapper_->Process(accelerator); |
| 186 return accelerator_manager_->GetCurrentTarget(accelerator) != NULL; | |
| 187 } | 261 } |
| 188 | 262 |
| 189 bool AcceleratorManagerImpl::IsReserved( | 263 bool AcceleratorManagerImpl::IsRegistered(const ui::Accelerator& accelerator, |
| 190 const ui::Accelerator& accelerator) const { | 264 int flags) const { |
| 191 std::map<ui::Accelerator, InternalData>::const_iterator iter = | 265 std::map<ui::Accelerator, InternalData>::const_iterator iter = |
| 192 accelerators_.find(accelerator); | 266 accelerators_.find(accelerator); |
| 193 if (iter == accelerators_.end()) | 267 if (iter == accelerators_.end()) |
| 194 return false; | 268 return false; |
| 195 return iter->second.IsReserved(); | 269 DCHECK(accelerator_wrapper_->GetCurrentTarget(accelerator)); |
| 196 } | 270 return flags == AF_NONE || iter->second.flags() & flags; |
| 197 | |
| 198 bool AcceleratorManagerImpl::Process(const ui::Accelerator& accelerator) { | |
| 199 return accelerator_manager_->Process(accelerator); | |
| 200 } | 271 } |
| 201 | 272 |
| 202 void AcceleratorManagerImpl::RegisterAccelerators( | 273 void AcceleratorManagerImpl::RegisterAccelerators( |
| 203 const AcceleratorData accelerators[], | 274 const AcceleratorData accelerators[], |
| 204 size_t num_accelerators, | 275 size_t num_accelerators, |
| 205 AcceleratorHandler* handler) { | 276 AcceleratorHandler* handler) { |
| 206 for (size_t i = 0; i < num_accelerators; ++i) | 277 for (size_t i = 0; i < num_accelerators; ++i) |
| 207 RegisterAccelerator(accelerators[i], handler); | 278 RegisterAccelerator(accelerators[i], handler); |
| 208 } | 279 } |
| 209 | 280 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 231 } | 302 } |
| 232 | 303 |
| 233 void AcceleratorManagerImpl::RegisterAccelerator( | 304 void AcceleratorManagerImpl::RegisterAccelerator( |
| 234 const AcceleratorData& accelerator_data, | 305 const AcceleratorData& accelerator_data, |
| 235 AcceleratorHandler* handler) { | 306 AcceleratorHandler* handler) { |
| 236 ui::Accelerator accelerator(accelerator_data.keycode, | 307 ui::Accelerator accelerator(accelerator_data.keycode, |
| 237 accelerator_data.keyevent_flags); | 308 accelerator_data.keyevent_flags); |
| 238 accelerator.set_type(accelerator_data.trigger_event == TRIGGER_ON_PRESS | 309 accelerator.set_type(accelerator_data.trigger_event == TRIGGER_ON_PRESS |
| 239 ? ui::ET_KEY_PRESSED | 310 ? ui::ET_KEY_PRESSED |
| 240 : ui::ET_KEY_RELEASED); | 311 : ui::ET_KEY_RELEASED); |
| 241 accelerator_manager_->Register( | 312 accelerator_wrapper_->Register(accelerator, this); |
| 242 accelerator, ui::AcceleratorManager::kNormalPriority, this); | |
| 243 accelerators_.insert( | 313 accelerators_.insert( |
| 244 std::make_pair(accelerator, | 314 std::make_pair(accelerator, |
| 245 InternalData(accelerator_data.command_id, | 315 InternalData(accelerator_data.command_id, |
| 246 handler, | 316 handler, |
| 247 accelerator_data.accelerator_flags))); | 317 accelerator_data.accelerator_flags))); |
| 248 } | 318 } |
| 249 | 319 |
| 250 // static | 320 // static |
| 251 AcceleratorManager* AcceleratorManager::Get() { | 321 AcceleratorManager* AcceleratorManager::Get() { |
| 252 return InputManager::Get()->GetAcceleratorManager(); | 322 return InputManager::Get()->GetAcceleratorManager(); |
| 253 } | 323 } |
| 254 | 324 |
| 325 // static | |
| 326 scoped_ptr<AcceleratorManager> AcceleratorManager::CreateForFocusManager( | |
| 327 views::FocusManager* focus_manager) { | |
| 328 return scoped_ptr<AcceleratorManager>( | |
| 329 new AcceleratorManagerImpl(new FocusManagerWrapper(focus_manager))); | |
| 330 } | |
| 331 | |
| 255 } // namespace athena | 332 } // namespace athena |
| OLD | NEW |