Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow registering multiple shortcuts #15542

Merged
merged 7 commits into from Nov 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions atom/browser/api/atom_api_global_shortcut.cc
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions atom/browser/api/atom_api_global_shortcut.h
Expand Up @@ -7,6 +7,7 @@

#include <map>
#include <string>
#include <vector>

#include "atom/browser/api/trackable_object.h"
#include "base/callback.h"
Expand All @@ -33,10 +34,13 @@ class GlobalShortcut : public extensions::GlobalShortcutListener::Observer,
private:
typedef std::map<ui::Accelerator, base::Closure> AcceleratorCallbackMap;

bool RegisterAll(const std::vector<ui::Accelerator>& accelerators,
const base::Closure& callback);
bool Register(const ui::Accelerator& accelerator,
const base::Closure& callback);
bool IsRegistered(const ui::Accelerator& accelerator);
void Unregister(const ui::Accelerator& accelerator);
void UnregisterSome(const std::vector<ui::Accelerator>& accelerators);
void UnregisterAll();

// GlobalShortcutListener::Observer implementation.
Expand Down
11 changes: 11 additions & 0 deletions docs/api/global-shortcut.md
Expand Up @@ -54,6 +54,17 @@ When the accelerator is already taken by other applications, this call will
silently fail. This behavior is intended by operating systems, since they don't
want applications to fight for global shortcuts.

### `globalShortcut.registerAll(accelerators, callback)`

* `accelerators` String[] - an array of [Accelerator](accelerator.md)s.
* `callback` Function

Registers a global shortcut of all `accelerator` items in `accelerators`. The `callback` is called when any of the registered shortcuts are pressed by the user.

When a given accelerator is already taken by other applications, this call will
silently fail. This behavior is intended by operating systems, since they don't
want applications to fight for global shortcuts.

### `globalShortcut.isRegistered(accelerator)`

* `accelerator` [Accelerator](accelerator.md)
Expand Down
21 changes: 19 additions & 2 deletions spec/api-global-shortcut-spec.js
Expand Up @@ -18,8 +18,8 @@ describe('globalShortcut module', () => {
globalShortcut.unregisterAll()
})

it('can register and unregister accelerators', () => {
const accelerator = 'CommandOrControl+A+B+C'
it('can register and unregister single accelerators', () => {
const accelerator = 'CmdOrCtrl+A+B+C'

expect(globalShortcut.isRegistered(accelerator)).to.be.false()
globalShortcut.register(accelerator, () => {})
Expand All @@ -33,4 +33,21 @@ describe('globalShortcut module', () => {
globalShortcut.unregisterAll()
expect(globalShortcut.isRegistered(accelerator)).to.be.false()
})

it('can register and unregister multiple accelerators', () => {
const accelerators = ['CmdOrCtrl+X', 'CmdOrCtrl+Y']

expect(globalShortcut.isRegistered(accelerators[0])).to.be.false()
expect(globalShortcut.isRegistered(accelerators[1])).to.be.false()

globalShortcut.registerAll(accelerators, () => {})

expect(globalShortcut.isRegistered(accelerators[0])).to.be.true()
expect(globalShortcut.isRegistered(accelerators[1])).to.be.true()

globalShortcut.unregisterAll()

expect(globalShortcut.isRegistered(accelerators[0])).to.be.false()
expect(globalShortcut.isRegistered(accelerators[1])).to.be.false()
})
})