Chromium Code Reviews| 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_shell.h" | 11 #include "ash/common/wm_shell.h" |
| 12 #include "ash/mus/accelerators/accelerator_ids.h" | 12 #include "ash/mus/accelerators/accelerator_ids.h" |
| 13 #include "ash/mus/bridge/wm_window_mus.h" | 13 #include "ash/mus/bridge/wm_window_mus.h" |
| 14 #include "ash/mus/window_manager.h" | 14 #include "ash/mus/window_manager.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "services/ui/common/accelerator_util.h" | 16 #include "services/ui/common/accelerator_util.h" |
| 17 #include "ui/base/accelerators/accelerator_history.h" | 17 #include "ui/base/accelerators/accelerator_history.h" |
| 18 | 18 |
| 19 namespace ash { | 19 namespace ash { |
| 20 namespace mus { | 20 namespace mus { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Callback from registering the accelerator. | 23 // Callback from registering the accelerator. |
| 24 void OnAcceleratorAdded(const ui::Accelerator& accelerator, bool added) { | 24 void OnAcceleratorAdded(const ui::Accelerator& accelerator, bool added) { |
| 25 // All our accelerators should be registered, so we expect |added| to be true. | 25 // All our accelerators should be registered, so we expect |added| to be true. |
| 26 DCHECK(added) << "duplicate accelerator key_code=" << accelerator.key_code() | 26 DCHECK(added) << "duplicate accelerator key_code=" << accelerator.key_code() |
|
mfomitchev
2016/12/22 01:04:50
This debug info may no longer be correct if called
mfomitchev
2016/12/22 21:18:55
Not sure if you missed this comment?
thanhph
2016/12/24 18:52:16
Thanks for the reminder. I put the line
DVLOG(2)
| |
| 27 << " type=" << accelerator.type() | 27 << " type=" << accelerator.type() |
| 28 << " modifiers=" << accelerator.modifiers(); | 28 << " modifiers=" << accelerator.modifiers(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 // Callback from registering the accelerators. | |
| 32 void OnAcceleratorsAdded(const std::vector<ui::Accelerator>& ui_accelerators, | |
| 33 bool added) { | |
| 34 for (auto iter = ui_accelerators.begin(); iter != ui_accelerators.end(); | |
| 35 ++iter) | |
| 36 OnAcceleratorAdded(*iter, added); | |
| 31 } // namespace | 37 } // namespace |
| 38 } | |
| 32 | 39 |
| 33 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar( | 40 AcceleratorControllerRegistrar::AcceleratorControllerRegistrar( |
| 34 WindowManager* window_manager, | 41 WindowManager* window_manager, |
| 35 uint16_t id_namespace) | 42 uint16_t id_namespace) |
| 36 : window_manager_(window_manager), | 43 : window_manager_(window_manager), |
| 37 id_namespace_(id_namespace), | 44 id_namespace_(id_namespace), |
| 38 next_id_(0), | 45 next_id_(0), |
| 39 router_(new AcceleratorRouter) { | 46 router_(new AcceleratorRouter) { |
| 40 window_manager_->AddAcceleratorHandler(id_namespace, this); | 47 window_manager_->AddAcceleratorHandler(id_namespace, this); |
| 41 } | 48 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 ? ui::mojom::EventResult::HANDLED | 99 ? ui::mojom::EventResult::HANDLED |
| 93 : ui::mojom::EventResult::UNHANDLED; | 100 : ui::mojom::EventResult::UNHANDLED; |
| 94 } | 101 } |
| 95 DCHECK_EQ(GetAcceleratorLocalId(id), ids.post_id); | 102 DCHECK_EQ(GetAcceleratorLocalId(id), ids.post_id); |
| 96 // NOTE: for post return value doesn't really matter. | 103 // NOTE: for post return value doesn't really matter. |
| 97 return WmShell::Get()->accelerator_controller()->Process(accelerator) | 104 return WmShell::Get()->accelerator_controller()->Process(accelerator) |
| 98 ? ui::mojom::EventResult::HANDLED | 105 ? ui::mojom::EventResult::HANDLED |
| 99 : ui::mojom::EventResult::UNHANDLED; | 106 : ui::mojom::EventResult::UNHANDLED; |
| 100 } | 107 } |
| 101 | 108 |
| 109 void AcceleratorControllerRegistrar::OnAcceleratorsRegistered( | |
| 110 const std::vector<ui::Accelerator>& accelerators) { | |
| 111 accelerator_ptrs_.clear(); | |
|
mfomitchev
2016/12/22 01:04:50
I think this all could be done a bit cleaner:
Don'
thanhph
2016/12/22 17:48:44
If we get rid of |add_accelerator_immediately|, we
| |
| 112 for (auto iter = accelerators.begin(); iter != accelerators.end(); ++iter) | |
| 113 OnAcceleratorRegistered(*iter, false); | |
| 114 | |
| 115 window_manager_->window_manager_client()->AddAccelerators( | |
| 116 std::move(accelerator_ptrs_), | |
| 117 base::Bind(OnAcceleratorsAdded, accelerators)); | |
| 118 } | |
| 119 | |
| 120 // add_accelerator_immediately default set to true. | |
| 102 void AcceleratorControllerRegistrar::OnAcceleratorRegistered( | 121 void AcceleratorControllerRegistrar::OnAcceleratorRegistered( |
| 103 const ui::Accelerator& accelerator) { | 122 const ui::Accelerator& accelerator, |
| 123 bool add_accelerator_immediately) { | |
| 104 Ids ids; | 124 Ids ids; |
| 105 if (!GenerateIds(&ids)) { | 125 if (!GenerateIds(&ids)) { |
| 106 DVLOG(1) << "max number of accelerators registered, dropping request"; | 126 DVLOG(1) << "max number of accelerators registered, dropping request"; |
| 107 return; | 127 return; |
| 108 } | 128 } |
| 109 DCHECK_EQ(0u, accelerator_to_ids_.count(accelerator)); | 129 DCHECK_EQ(0u, accelerator_to_ids_.count(accelerator)); |
| 110 accelerator_to_ids_[accelerator] = ids; | 130 accelerator_to_ids_[accelerator] = ids; |
| 111 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size()); | 131 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size()); |
| 112 | 132 |
| 113 ui::mojom::EventMatcherPtr event_matcher = ui::CreateKeyMatcher( | 133 ui::mojom::EventMatcherPtr event_matcher = ui::CreateKeyMatcher( |
| 114 static_cast<ui::mojom::KeyboardCode>(accelerator.key_code()), | 134 static_cast<ui::mojom::KeyboardCode>(accelerator.key_code()), |
| 115 accelerator.modifiers()); | 135 accelerator.modifiers()); |
| 116 event_matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; | 136 event_matcher->accelerator_phase = ui::mojom::AcceleratorPhase::PRE_TARGET; |
| 117 DCHECK(accelerator.type() == ui::ET_KEY_PRESSED || | 137 DCHECK(accelerator.type() == ui::ET_KEY_PRESSED || |
| 118 accelerator.type() == ui::ET_KEY_RELEASED); | 138 accelerator.type() == ui::ET_KEY_RELEASED); |
| 119 event_matcher->type_matcher->type = accelerator.type() == ui::ET_KEY_PRESSED | 139 event_matcher->type_matcher->type = accelerator.type() == ui::ET_KEY_PRESSED |
| 120 ? ui::mojom::EventType::KEY_PRESSED | 140 ? ui::mojom::EventType::KEY_PRESSED |
| 121 : ui::mojom::EventType::KEY_RELEASED; | 141 : ui::mojom::EventType::KEY_RELEASED; |
| 122 | 142 |
| 123 ui::mojom::EventMatcherPtr post_event_matcher = event_matcher.Clone(); | 143 ui::mojom::EventMatcherPtr post_event_matcher = event_matcher.Clone(); |
| 124 post_event_matcher->accelerator_phase = | 144 post_event_matcher->accelerator_phase = |
| 125 ui::mojom::AcceleratorPhase::POST_TARGET; | 145 ui::mojom::AcceleratorPhase::POST_TARGET; |
| 126 | 146 |
| 127 window_manager_->window_manager_client()->AddAccelerators( | 147 if (add_accelerator_immediately) { |
| 128 ui::CreateAcceleratorVector( | 148 window_manager_->window_manager_client()->AddAccelerators( |
|
mfomitchev
2016/12/22 01:04:50
Can we create an array vector the accelerators we
thanhph
2016/12/22 20:36:01
Done.
| |
| 129 ComputeAcceleratorId(id_namespace_, ids.pre_id), | 149 ui::CreateAcceleratorVector( |
| 130 std::move(event_matcher)), | 150 ComputeAcceleratorId(id_namespace_, ids.pre_id), |
| 131 base::Bind(OnAcceleratorAdded, accelerator)); | 151 std::move(event_matcher)), |
| 152 base::Bind(OnAcceleratorAdded, accelerator)); | |
| 132 | 153 |
| 133 window_manager_->window_manager_client()->AddAccelerators( | 154 window_manager_->window_manager_client()->AddAccelerators( |
| 134 ui::CreateAcceleratorVector( | 155 ui::CreateAcceleratorVector( |
| 135 ComputeAcceleratorId(id_namespace_, ids.post_id), | 156 ComputeAcceleratorId(id_namespace_, ids.post_id), |
| 136 std::move(post_event_matcher)), | 157 std::move(post_event_matcher)), |
| 137 base::Bind(OnAcceleratorAdded, accelerator)); | 158 base::Bind(OnAcceleratorAdded, accelerator)); |
| 159 } else { | |
| 160 accelerator_ptrs_.push_back( | |
| 161 ui::CreateAccelerator(ComputeAcceleratorId(id_namespace_, ids.pre_id), | |
| 162 std::move(event_matcher))); | |
| 163 accelerator_ptrs_.push_back( | |
| 164 ui::CreateAccelerator(ComputeAcceleratorId(id_namespace_, ids.post_id), | |
| 165 std::move(post_event_matcher))); | |
| 166 } | |
| 138 } | 167 } |
| 139 | 168 |
| 140 void AcceleratorControllerRegistrar::OnAcceleratorUnregistered( | 169 void AcceleratorControllerRegistrar::OnAcceleratorUnregistered( |
| 141 const ui::Accelerator& accelerator) { | 170 const ui::Accelerator& accelerator) { |
| 142 auto iter = accelerator_to_ids_.find(accelerator); | 171 auto iter = accelerator_to_ids_.find(accelerator); |
| 143 DCHECK(iter != accelerator_to_ids_.end()); | 172 DCHECK(iter != accelerator_to_ids_.end()); |
| 144 Ids ids = iter->second; | 173 Ids ids = iter->second; |
| 145 accelerator_to_ids_.erase(iter); | 174 accelerator_to_ids_.erase(iter); |
| 146 ids_.erase(ids.pre_id); | 175 ids_.erase(ids.pre_id); |
| 147 ids_.erase(ids.post_id); | 176 ids_.erase(ids.post_id); |
| 148 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size()); | 177 DCHECK_EQ(accelerator_to_ids_.size() * 2, ids_.size()); |
| 149 window_manager_->window_manager_client()->RemoveAccelerator( | 178 window_manager_->window_manager_client()->RemoveAccelerator( |
| 150 ComputeAcceleratorId(id_namespace_, ids.pre_id)); | 179 ComputeAcceleratorId(id_namespace_, ids.pre_id)); |
| 151 window_manager_->window_manager_client()->RemoveAccelerator( | 180 window_manager_->window_manager_client()->RemoveAccelerator( |
| 152 ComputeAcceleratorId(id_namespace_, ids.post_id)); | 181 ComputeAcceleratorId(id_namespace_, ids.post_id)); |
| 153 } | 182 } |
| 154 | 183 |
| 184 void AcceleratorControllerRegistrar::OnAcceleratorsUnregistered( | |
| 185 std::vector<ui::Accelerator>& accelerators) { | |
| 186 for (auto iter = accelerators.begin(); iter != accelerators.end(); ++iter) { | |
| 187 OnAcceleratorUnregistered(*iter); | |
| 188 } | |
| 189 } | |
| 190 | |
| 155 bool AcceleratorControllerRegistrar::GenerateIds(Ids* ids) { | 191 bool AcceleratorControllerRegistrar::GenerateIds(Ids* ids) { |
| 156 if (ids_.size() + 2 >= std::numeric_limits<uint16_t>::max()) | 192 if (ids_.size() + 2 >= std::numeric_limits<uint16_t>::max()) |
| 157 return false; | 193 return false; |
| 158 ids->pre_id = GetNextLocalAcceleratorId(); | 194 ids->pre_id = GetNextLocalAcceleratorId(); |
| 159 ids->post_id = GetNextLocalAcceleratorId(); | 195 ids->post_id = GetNextLocalAcceleratorId(); |
| 160 return true; | 196 return true; |
| 161 } | 197 } |
| 162 | 198 |
| 163 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() { | 199 uint16_t AcceleratorControllerRegistrar::GetNextLocalAcceleratorId() { |
| 164 DCHECK_LT(ids_.size(), std::numeric_limits<uint16_t>::max()); | 200 DCHECK_LT(ids_.size(), std::numeric_limits<uint16_t>::max()); |
| 165 // Common case is we never wrap once, so this is typically cheap. Additionally | 201 // Common case is we never wrap once, so this is typically cheap. Additionally |
| 166 // we expect there not to be too many accelerators. | 202 // we expect there not to be too many accelerators. |
| 167 while (ids_.count(next_id_) > 0) | 203 while (ids_.count(next_id_) > 0) |
| 168 ++next_id_; | 204 ++next_id_; |
| 169 ids_.insert(next_id_); | 205 ids_.insert(next_id_); |
| 170 return next_id_++; | 206 return next_id_++; |
| 171 } | 207 } |
| 172 | 208 |
| 173 } // namespace mus | 209 } // namespace mus |
| 174 } // namespace ash | 210 } // namespace ash |
| OLD | NEW |