feat: allow registering multiple shortcuts (#15542)

This PR allows for multiple global shortcuts to be registered such that triggering any of them calls the same callback.
This commit is contained in:
Shelley Vohr 2018-11-07 09:40:38 -08:00 committed by GitHub
parent 20a540e680
commit e9ba26f50e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 2 deletions

View file

@ -5,6 +5,7 @@
#include "atom/browser/api/atom_api_global_shortcut.h"
#include <string>
#include <vector>
#include "atom/common/native_mate_converters/accelerator_converter.h"
#include "atom/common/native_mate_converters/callback.h"
@ -38,6 +39,24 @@ void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
accelerator_callback_map_[accelerator].Run();
}
bool GlobalShortcut::RegisterAll(
const std::vector<ui::Accelerator>& accelerators,
const base::Closure& callback) {
std::vector<ui::Accelerator> registered;
for (auto& accelerator : accelerators) {
GlobalShortcutListener* listener = GlobalShortcutListener::GetInstance();
if (!listener->RegisterAccelerator(accelerator, this)) {
// unregister all shortcuts if any failed
UnregisterSome(registered);
return false;
}
registered.push_back(accelerator);
accelerator_callback_map_[accelerator] = callback;
}
return true;
}
bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
const base::Closure& callback) {
if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(accelerator,
@ -58,6 +77,13 @@ void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) {
this);
}
void GlobalShortcut::UnregisterSome(
const std::vector<ui::Accelerator>& accelerators) {
for (auto& accelerator : accelerators) {
Unregister(accelerator);
}
}
bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) {
return ContainsKey(accelerator_callback_map_, accelerator);
}
@ -77,6 +103,7 @@ void GlobalShortcut::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "GlobalShortcut"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("registerAll", &GlobalShortcut::RegisterAll)
.SetMethod("register", &GlobalShortcut::Register)
.SetMethod("isRegistered", &GlobalShortcut::IsRegistered)
.SetMethod("unregister", &GlobalShortcut::Unregister)