2014-10-31 18:17:05 +00:00
|
|
|
// Copyright (c) 2014 GitHub, Inc.
|
2014-07-31 04:22:29 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/atom_api_global_shortcut.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
#include "atom/common/native_mate_converters/accelerator_converter.h"
|
|
|
|
#include "base/stl_util.h"
|
2014-08-10 11:14:20 +00:00
|
|
|
#include "native_mate/callback.h"
|
2014-07-31 04:22:29 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
|
|
|
|
#include "atom/common/node_includes.h"
|
|
|
|
|
|
|
|
using extensions::GlobalShortcutListener;
|
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
|
|
|
GlobalShortcut::GlobalShortcut() {
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalShortcut::~GlobalShortcut() {
|
|
|
|
UnregisterAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
|
|
|
|
if (accelerator_callback_map_.find(accelerator) ==
|
|
|
|
accelerator_callback_map_.end()) {
|
|
|
|
// This should never occur, because if it does, GlobalGlobalShortcutListener
|
|
|
|
// notifes us with wrong accelerator.
|
|
|
|
NOTREACHED();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
accelerator_callback_map_[accelerator].Run();
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
|
|
|
|
const base::Closure& callback) {
|
2014-07-31 04:22:29 +00:00
|
|
|
if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(
|
|
|
|
accelerator, this)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-04 16:00:39 +00:00
|
|
|
|
2014-07-31 04:22:29 +00:00
|
|
|
accelerator_callback_map_[accelerator] = callback;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) {
|
|
|
|
if (!ContainsKey(accelerator_callback_map_, accelerator))
|
2014-07-31 04:22:29 +00:00
|
|
|
return;
|
2014-08-04 16:00:39 +00:00
|
|
|
|
2014-07-31 04:22:29 +00:00
|
|
|
accelerator_callback_map_.erase(accelerator);
|
|
|
|
GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
|
|
|
|
accelerator, this);
|
|
|
|
}
|
|
|
|
|
2014-08-04 16:00:39 +00:00
|
|
|
bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) {
|
|
|
|
return ContainsKey(accelerator_callback_map_, accelerator);
|
|
|
|
}
|
|
|
|
|
2014-07-31 04:22:29 +00:00
|
|
|
void GlobalShortcut::UnregisterAll() {
|
|
|
|
accelerator_callback_map_.clear();
|
|
|
|
GlobalShortcutListener::GetInstance()->UnregisterAccelerators(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
mate::ObjectTemplateBuilder GlobalShortcut::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return mate::ObjectTemplateBuilder(isolate)
|
2014-08-22 02:53:37 +00:00
|
|
|
.SetMethod("register", &GlobalShortcut::Register)
|
|
|
|
.SetMethod("isRegistered", &GlobalShortcut::IsRegistered)
|
|
|
|
.SetMethod("unregister", &GlobalShortcut::Unregister)
|
|
|
|
.SetMethod("unregisterAll", &GlobalShortcut::UnregisterAll);
|
2014-07-31 04:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
mate::Handle<GlobalShortcut> GlobalShortcut::Create(v8::Isolate* isolate) {
|
|
|
|
return CreateHandle(isolate, new GlobalShortcut);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-05-22 11:11:22 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context, void* priv) {
|
2014-07-31 04:22:29 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
|
|
|
dict.Set("globalShortcut", atom::api::GlobalShortcut::Create(isolate));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_global_shortcut, Initialize)
|