atom_api_global_shortcut.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) 2014 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/api/atom_api_global_shortcut.h"
  5. #include <string>
  6. #include "atom/common/native_mate_converters/accelerator_converter.h"
  7. #include "atom/common/native_mate_converters/callback.h"
  8. #include "base/stl_util.h"
  9. #include "native_mate/dictionary.h"
  10. #include "atom/common/node_includes.h"
  11. using extensions::GlobalShortcutListener;
  12. namespace atom {
  13. namespace api {
  14. GlobalShortcut::GlobalShortcut(v8::Isolate* isolate) {
  15. Init(isolate);
  16. }
  17. GlobalShortcut::~GlobalShortcut() {
  18. UnregisterAll();
  19. }
  20. void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
  21. if (accelerator_callback_map_.find(accelerator) ==
  22. accelerator_callback_map_.end()) {
  23. // This should never occur, because if it does, GlobalGlobalShortcutListener
  24. // notifes us with wrong accelerator.
  25. NOTREACHED();
  26. return;
  27. }
  28. accelerator_callback_map_[accelerator].Run();
  29. }
  30. bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
  31. const base::Closure& callback) {
  32. if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(accelerator,
  33. this)) {
  34. return false;
  35. }
  36. accelerator_callback_map_[accelerator] = callback;
  37. return true;
  38. }
  39. void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) {
  40. if (!ContainsKey(accelerator_callback_map_, accelerator))
  41. return;
  42. accelerator_callback_map_.erase(accelerator);
  43. GlobalShortcutListener::GetInstance()->UnregisterAccelerator(accelerator,
  44. this);
  45. }
  46. bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) {
  47. return ContainsKey(accelerator_callback_map_, accelerator);
  48. }
  49. void GlobalShortcut::UnregisterAll() {
  50. accelerator_callback_map_.clear();
  51. GlobalShortcutListener::GetInstance()->UnregisterAccelerators(this);
  52. }
  53. // static
  54. mate::Handle<GlobalShortcut> GlobalShortcut::Create(v8::Isolate* isolate) {
  55. return mate::CreateHandle(isolate, new GlobalShortcut(isolate));
  56. }
  57. // static
  58. void GlobalShortcut::BuildPrototype(v8::Isolate* isolate,
  59. v8::Local<v8::FunctionTemplate> prototype) {
  60. prototype->SetClassName(mate::StringToV8(isolate, "GlobalShortcut"));
  61. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  62. .SetMethod("register", &GlobalShortcut::Register)
  63. .SetMethod("isRegistered", &GlobalShortcut::IsRegistered)
  64. .SetMethod("unregister", &GlobalShortcut::Unregister)
  65. .SetMethod("unregisterAll", &GlobalShortcut::UnregisterAll);
  66. }
  67. } // namespace api
  68. } // namespace atom
  69. namespace {
  70. void Initialize(v8::Local<v8::Object> exports,
  71. v8::Local<v8::Value> unused,
  72. v8::Local<v8::Context> context,
  73. void* priv) {
  74. v8::Isolate* isolate = context->GetIsolate();
  75. mate::Dictionary dict(isolate, exports);
  76. dict.Set("globalShortcut", atom::api::GlobalShortcut::Create(isolate));
  77. }
  78. } // namespace
  79. NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_global_shortcut, Initialize)