This source file includes following definitions.
- Register
- Unregister
- UnregisterAll
- Process
- GetCurrentTarget
- HasPriorityHandler
#include "ui/base/accelerators/accelerator_manager.h"
#include <algorithm>
#include "base/logging.h"
namespace ui {
AcceleratorManager::AcceleratorManager() {
}
AcceleratorManager::~AcceleratorManager() {
}
void AcceleratorManager::Register(const Accelerator& accelerator,
HandlerPriority priority,
AcceleratorTarget* target) {
AcceleratorTargetList& targets = accelerators_[accelerator].second;
DCHECK(std::find(targets.begin(), targets.end(), target) == targets.end())
<< "Registering the same target multiple times";
if (priority) {
DCHECK(!accelerators_[accelerator].first)
<< "Only one _priority_ handler can be registered";
targets.push_front(target);
accelerators_[accelerator].first = true;
return;
}
if (!accelerators_[accelerator].first)
targets.push_front(target);
else
targets.insert(++targets.begin(), target);
}
void AcceleratorManager::Unregister(const Accelerator& accelerator,
AcceleratorTarget* target) {
AcceleratorMap::iterator map_iter = accelerators_.find(accelerator);
if (map_iter == accelerators_.end()) {
NOTREACHED() << "Unregistering non-existing accelerator";
return;
}
AcceleratorTargetList* targets = &map_iter->second.second;
AcceleratorTargetList::iterator target_iter =
std::find(targets->begin(), targets->end(), target);
if (target_iter == targets->end()) {
NOTREACHED() << "Unregistering accelerator for wrong target";
return;
}
if (accelerators_[accelerator].first && target_iter == targets->begin()) {
accelerators_[accelerator].first = false;
}
targets->erase(target_iter);
}
void AcceleratorManager::UnregisterAll(AcceleratorTarget* target) {
for (AcceleratorMap::iterator map_iter = accelerators_.begin();
map_iter != accelerators_.end(); ++map_iter) {
AcceleratorTargetList* targets = &map_iter->second.second;
targets->remove(target);
}
}
bool AcceleratorManager::Process(const Accelerator& accelerator) {
bool result = false;
AcceleratorMap::iterator map_iter = accelerators_.find(accelerator);
if (map_iter != accelerators_.end()) {
AcceleratorTargetList targets(map_iter->second.second);
for (AcceleratorTargetList::iterator iter = targets.begin();
iter != targets.end(); ++iter) {
if ((*iter)->CanHandleAccelerators() &&
(*iter)->AcceleratorPressed(accelerator)) {
result = true;
break;
}
}
}
return result;
}
AcceleratorTarget* AcceleratorManager::GetCurrentTarget(
const Accelerator& accelerator) const {
AcceleratorMap::const_iterator map_iter = accelerators_.find(accelerator);
if (map_iter == accelerators_.end() || map_iter->second.second.empty())
return NULL;
return map_iter->second.second.front();
}
bool AcceleratorManager::HasPriorityHandler(
const Accelerator& accelerator) const {
AcceleratorMap::const_iterator map_iter = accelerators_.find(accelerator);
if (map_iter == accelerators_.end() || map_iter->second.second.empty())
return false;
if (!map_iter->second.first)
return false;
return map_iter->second.second.front()->CanHandleAccelerators();
}
}