]> khamidou — GitHub mirrors - electron.git/commitdiff
Make Accelerator a standalone JS type.
authorCheng Zhao <zcbenz@gmail.com>
Mon, 4 Aug 2014 16:00:39 +0000 (00:00 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 4 Aug 2014 16:03:58 +0000 (00:03 +0800)
This makes menu and global-shortcut share the same code on accelerator.

atom.gyp
atom/browser/api/atom_api_global_shortcut.cc
atom/browser/api/atom_api_global_shortcut.h
atom/browser/api/atom_api_menu.cc
atom/common/native_mate_converters/accelerator_converter.cc [new file with mode: 0644]
atom/common/native_mate_converters/accelerator_converter.h [new file with mode: 0644]
docs/api/accelerator.md [new file with mode: 0644]
docs/api/global-shortcut.md
docs/api/menu-item.md

index 2b9197a8b7da7e40248e765d7478d607c8e1884e..4fbfcf95bdee1d071832c0d18ceb7d7ab9c8c808 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
       'atom/common/draggable_region.cc',
       'atom/common/draggable_region.h',
       'atom/common/linux/application_info.cc',
+      'atom/common/native_mate_converters/accelerator_converter.cc',
+      'atom/common/native_mate_converters/accelerator_converter.h',
       'atom/common/native_mate_converters/file_path_converter.h',
       'atom/common/native_mate_converters/function_converter.h',
       'atom/common/native_mate_converters/gurl_converter.h',
index bb4de6cf16686815f052bbb3ffc75af34d5d42ae..827cd01ecb8363b9befeeb6dbe54feaf0bc107ec 100644 (file)
@@ -6,8 +6,9 @@
 
 #include <string>
 
-#include "atom/browser/ui/accelerator_util.h"
+#include "atom/common/native_mate_converters/accelerator_converter.h"
 #include "atom/common/native_mate_converters/function_converter.h"
+#include "base/stl_util.h"
 #include "native_mate/dictionary.h"
 
 #include "atom/common/node_includes.h"
@@ -36,52 +37,35 @@ void GlobalShortcut::OnKeyPressed(const ui::Accelerator& accelerator) {
   accelerator_callback_map_[accelerator].Run();
 }
 
-bool GlobalShortcut::Register(const std::string& keycode,
-    const base::Closure& callback) {
-  ui::Accelerator accelerator;
-  if (!accelerator_util::StringToAccelerator(keycode, &accelerator)) {
-    LOG(ERROR) << keycode << " is invalid.";
-    return false;
-  }
+bool GlobalShortcut::Register(const ui::Accelerator& accelerator,
+                              const base::Closure& callback) {
   if (!GlobalShortcutListener::GetInstance()->RegisterAccelerator(
       accelerator, this)) {
     return false;
   }
+
   accelerator_callback_map_[accelerator] = callback;
   return true;
 }
 
-void GlobalShortcut::Unregister(const std::string& keycode) {
-  ui::Accelerator accelerator;
-  if (!accelerator_util::StringToAccelerator(keycode, &accelerator)) {
-    LOG(ERROR) << "The keycode: " << keycode << " is invalid.";
-    return;
-  }
-  if (accelerator_callback_map_.find(accelerator) ==
-      accelerator_callback_map_.end()) {
-    LOG(ERROR) << "The keycode: " << keycode << " isn't registered yet!";
+void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) {
+  if (!ContainsKey(accelerator_callback_map_, accelerator))
     return;
-  }
+
   accelerator_callback_map_.erase(accelerator);
   GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
       accelerator, this);
 }
 
+bool GlobalShortcut::IsRegistered(const ui::Accelerator& accelerator) {
+  return ContainsKey(accelerator_callback_map_, accelerator);
+}
+
 void GlobalShortcut::UnregisterAll() {
   accelerator_callback_map_.clear();
   GlobalShortcutListener::GetInstance()->UnregisterAccelerators(this);
 }
 
-bool GlobalShortcut::IsRegistered(const std::string& keycode) {
-  ui::Accelerator accelerator;
-  if (!accelerator_util::StringToAccelerator(keycode, &accelerator)) {
-    LOG(ERROR) << "The keycode: " << keycode << " is invalid.";
-    return false;
-  }
-  return accelerator_callback_map_.find(accelerator) !=
-      accelerator_callback_map_.end();
-}
-
 // static
 mate::ObjectTemplateBuilder GlobalShortcut::GetObjectTemplateBuilder(
     v8::Isolate* isolate) {
index cb731c665d3d67f0daf8ffbfe6787d3c56b155ae..b9d04e29fe5bb1e9b2f1f4213b91a781e523b073 100644 (file)
@@ -34,9 +34,10 @@ class GlobalShortcut : public extensions::GlobalShortcutListener::Observer,
  private:
   typedef std::map<ui::Accelerator, base::Closure> AcceleratorCallbackMap;
 
-  bool Register(const std::string& keycode, const base::Closure& callback);
-  bool IsRegistered(const std::string& keycode);
-  void Unregister(const std::string& keycode);
+  bool Register(const ui::Accelerator& accelerator,
+                const base::Closure& callback);
+  bool IsRegistered(const ui::Accelerator& accelerator);
+  void Unregister(const ui::Accelerator& accelerator);
   void UnregisterAll();
 
   // GlobalShortcutListener::Observer implementation.
index 0a4847668147d4ce95a2664dddb87b7cc223e571..a08070f196039a290da429e27e2735145a4c4e77 100644 (file)
@@ -5,7 +5,7 @@
 #include "atom/browser/api/atom_api_menu.h"
 
 #include "atom/browser/native_window.h"
-#include "atom/browser/ui/accelerator_util.h"
+#include "atom/common/native_mate_converters/accelerator_converter.h"
 #include "atom/common/native_mate_converters/string16_converter.h"
 #include "native_mate/constructor.h"
 #include "native_mate/dictionary.h"
@@ -92,12 +92,7 @@ bool Menu::GetAcceleratorForCommandId(int command_id,
                                                 GetWrapper(isolate),
                                                 "getAcceleratorForCommandId",
                                                 command_id);
-  if (shortcut->IsString()) {
-    std::string shortcut_str = mate::V8ToString(shortcut);
-    return accelerator_util::StringToAccelerator(shortcut_str, accelerator);
-  }
-
-  return false;
+  return mate::ConvertFromV8(isolate, shortcut, accelerator);
 }
 
 bool Menu::IsItemForCommandIdDynamic(int command_id) const {
diff --git a/atom/common/native_mate_converters/accelerator_converter.cc b/atom/common/native_mate_converters/accelerator_converter.cc
new file mode 100644 (file)
index 0000000..e860dc1
--- /dev/null
@@ -0,0 +1,20 @@
+// Copyright (c) 2014 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/common/native_mate_converters/accelerator_converter.h"
+
+#include "atom/browser/ui/accelerator_util.h"
+
+namespace mate {
+
+// static
+bool Converter<ui::Accelerator>::FromV8(
+    v8::Isolate* isolate, v8::Handle<v8::Value> val, ui::Accelerator* out) {
+  std::string keycode;
+  if (!ConvertFromV8(isolate, val, &keycode))
+    return false;
+  return accelerator_util::StringToAccelerator(keycode, out);
+}
+
+}  // namespace mate
diff --git a/atom/common/native_mate_converters/accelerator_converter.h b/atom/common/native_mate_converters/accelerator_converter.h
new file mode 100644 (file)
index 0000000..a60345e
--- /dev/null
@@ -0,0 +1,24 @@
+// Copyright (c) 2014 GitHub, Inc. All rights reserved.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_ACCELERATOR_CONVERTER_H_
+#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_ACCELERATOR_CONVERTER_H_
+
+#include "native_mate/converter.h"
+
+namespace ui {
+class Accelerator;
+}
+
+namespace mate {
+
+template<>
+struct Converter<ui::Accelerator> {
+  static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
+                     ui::Accelerator* out);
+};
+
+}  // namespace mate
+
+#endif  // ATOM_COMMON_NATIVE_MATE_CONVERTERS_ACCELERATOR_CONVERTER_H_
diff --git a/docs/api/accelerator.md b/docs/api/accelerator.md
new file mode 100644 (file)
index 0000000..0cfb2d2
--- /dev/null
@@ -0,0 +1,41 @@
+# Accelerator
+
+An accelerator is string that represents a keyboard shortcut, it can contain
+multiple modifiers and key codes, combined by the `+` character.
+
+Examples:
+
+* `Command+A`
+* `Ctrl+Shift+Z`
+
+## Platform notice
+
+On Linux and Windows, the `Command` key would not have any effect, you can
+use `CommandOrControl` which represents `Command` on OS X and `Control` on
+Linux and Windows to define some accelerators.
+
+## Available modifiers
+
+* `Command` (or `Cmd` for short)
+* `Control` (or `Ctrl` for short)
+* `CommandOrControl` (or `CmdOrCtrl` for short)
+* `Alt`
+* `Shift`
+
+## Available key codes
+
+* `0` to `9`
+* `A` to `Z`
+* `F1` to `F24`
+* Punctuations like `~`, `!`, `@`, `#`, `$`, etc.
+* `Space`
+* `Backspace`
+* `Delete`
+* `Insert`
+* `Return` (or `Enter` as alias)
+* `Up`, `Down`, `Left` and `Right`
+* `Home` and `End`
+* `PageUp` and `PageDown`
+* `Escape` (or `Esc` for short)
+* `VolumeUp`, `VolumeDown` and `VolumeMute`
+* `MediaNextTrack`, `MediaPreviousTrack`, `MediaStop` and `MediaPlayPause`
index 7f08051c40bf430e809a4442bc57ea8b5dc12c6f..285c26995afb87ef5744381188a23ee8f9eb857e 100644 (file)
@@ -22,33 +22,23 @@ globalShortcut.unregister('ctrl+x');
 globalShortcut.unregisterAll();
 ```
 
-## globalShortcut.register(keycode, callback)
+## globalShortcut.register(accelerator, callback)
 
-* `keycode` String
+* `accelerator` [Accelerator](accelerator.md)
 * `callback` Function
 
-Registers a global shortcut of `keycode`, the `callback` would be called when
+Registers a global shortcut of `accelerator`, the `callback` would be called when
 the registered shortcut is pressed by user.
 
-`keycode` is a string to specify shortcut key, such as "ctrl+shift+a".
+## globalShortcut.isRegistered(accelerator)
 
-A `keycode` consists of modifier and key two parts:
+* `accelerator` [Accelerator](accelerator.md)
 
-__Modifiers__: control(ctrl), command(cmd), alt, shift, commandorcontrol(cmdorctrl).
+Returns whether shortcut of `accelerator` is registered.
 
-__Supported keys__: 0-9, a-z, up, down, left, right, home, end, pagedown, pageup,
-insert, delete, esc, space, backspace, tab, f1-f12, volumeup, volumedown, media 
-keys(medianextrack, mediaprevioustrack, mediastop, mediaplaypause).
+## globalShortcut.unregister(accelerator)
 
-## globalShortcut.isRegistered(keycode)
-
-* `keycode` String
-
-Return whether the shortcut is registered.
-
-## globalShortcut.unregister(keycode)
-
-* `keycode` String
+* `accelerator` [Accelerator](accelerator.md)
 
 Unregisters the global shortcut of `keycode`.
 
index ad2e42b69625cd7803d5d2a664952fc55902e57c..f1a351211478faff76715174a2d59dcc97cbf344 100644 (file)
      `radio`
   * `label` String
   * `sublabel` String
-  * `accelerator` String - In the form of `Command+R`, `Ctrl+C`,
-    `Shift+Command+D`, `D`, etc.
+  * `accelerator` [Accelerator](accelerator.md)
   * `enabled` Boolean
   * `visible` Boolean
   * `checked` Boolean
   * `submenu` Menu - Should be specified for `submenu` type menu item, when
      it's specified the `type: 'submenu'` can be omitted for the menu item
-
-## Notes on accelerator
-
-On Linux and Windows, the `Command` key would not have any effect, you can
-use `CommandOrControl` which represents `Command` on OS X and `Control` on
-Linux and Windows to define some accelerators, you can also use its short
-alias `CmdOrCtrl`.