Skip to content

Commit

Permalink
Update GlobalShortcut API design.
Browse files Browse the repository at this point in the history
* Rename Shortcut API to GlobalShortcut for better suite.
* Implement the new design interfaces.
* Put the chromium related source code to the same directory as in chrome.
  • Loading branch information
hokein committed Jul 31, 2014
1 parent b221747 commit 0356790
Show file tree
Hide file tree
Showing 16 changed files with 288 additions and 302 deletions.
22 changes: 11 additions & 11 deletions atom.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
'atom/browser/api/lib/auto-updater.coffee',
'atom/browser/api/lib/browser-window.coffee',
'atom/browser/api/lib/dialog.coffee',
'atom/browser/api/lib/global-shortcut.coffee',
'atom/browser/api/lib/ipc.coffee',
'atom/browser/api/lib/menu.coffee',
'atom/browser/api/lib/menu-item.coffee',
'atom/browser/api/lib/power-monitor.coffee',
'atom/browser/api/lib/protocol.coffee',
'atom/browser/api/lib/shortcut.coffee',
'atom/browser/api/lib/tray.coffee',
'atom/browser/api/lib/web-contents.coffee',
'atom/browser/lib/init.coffee',
Expand Down Expand Up @@ -53,6 +53,8 @@
'atom/browser/api/atom_api_auto_updater.cc',
'atom/browser/api/atom_api_auto_updater.h',
'atom/browser/api/atom_api_dialog.cc',
'atom/browser/api/atom_api_global_shortcut.cc',
'atom/browser/api/atom_api_global_shortcut.h',
'atom/browser/api/atom_api_menu.cc',
'atom/browser/api/atom_api_menu.h',
'atom/browser/api/atom_api_menu_views.cc',
Expand All @@ -63,8 +65,6 @@
'atom/browser/api/atom_api_power_monitor.h',
'atom/browser/api/atom_api_protocol.cc',
'atom/browser/api/atom_api_protocol.h',
'atom/browser/api/atom_api_shortcut.cc',
'atom/browser/api/atom_api_shortcut.h',
'atom/browser/api/atom_api_tray.cc',
'atom/browser/api/atom_api_tray.h',
'atom/browser/api/atom_api_web_contents.cc',
Expand Down Expand Up @@ -226,6 +226,14 @@
'atom/renderer/atom_render_view_observer.h',
'atom/renderer/atom_renderer_client.cc',
'atom/renderer/atom_renderer_client.h',
'chromium_src/chrome/browser/extensions/global_shortcut_listener.cc',
'chromium_src/chrome/browser/extensions/global_shortcut_listener.h',
'chromium_src/chrome/browser/extensions/global_shortcut_listener_mac.mm',
'chromium_src/chrome/browser/extensions/global_shortcut_listener_mac.h',
'chromium_src/chrome/browser/extensions/global_shortcut_listener_x11.cc',
'chromium_src/chrome/browser/extensions/global_shortcut_listener_x11.h',
'chromium_src/chrome/browser/extensions/global_shortcut_listener_win.cc',
'chromium_src/chrome/browser/extensions/global_shortcut_listener_win.h',
'chromium_src/chrome/browser/ui/libgtk2ui/app_indicator_icon_menu.cc',
'chromium_src/chrome/browser/ui/libgtk2ui/app_indicator_icon_menu.h',
'chromium_src/chrome/browser/ui/libgtk2ui/gtk2_status_icon.cc',
Expand All @@ -234,14 +242,6 @@
'chromium_src/chrome/browser/ui/views/frame/global_menu_bar_registrar_x11.h',
'chromium_src/chrome/browser/ui/views/status_icons/status_tray_state_changer_win.cc',
'chromium_src/chrome/browser/ui/views/status_icons/status_tray_state_changer_win.h',
'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener.cc',
'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener.h',
'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_mac.mm',
'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_mac.h',
'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_x11.cc',
'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_x11.h',
'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.cc',
'chromium_src/chrome/browser/ui/shortcut/global_shortcut_listener_win.h',
'<@(native_mate_files)',
],
'framework_sources': [
Expand Down
123 changes: 123 additions & 0 deletions atom/browser/api/atom_api_global_shortcut.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// 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/browser/api/atom_api_global_shortcut.h"

#include <string>

#include "atom/browser/ui/accelerator_util.h"
#include "atom/common/native_mate_converters/function_converter.h"
#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();
}

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;
}
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!";
return;
}
accelerator_callback_map_.erase(accelerator);
GlobalShortcutListener::GetInstance()->UnregisterAccelerator(
accelerator, this);
}

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) {
return mate::ObjectTemplateBuilder(isolate)
.SetMethod("register",
base::Bind(&GlobalShortcut::Register,
base::Unretained(this)))
.SetMethod("isRegistered",
base::Bind(&GlobalShortcut::IsRegistered,
base::Unretained(this)))
.SetMethod("unregister",
base::Bind(&GlobalShortcut::Unregister,
base::Unretained(this)))
.SetMethod("unregisterAll",
base::Bind(&GlobalShortcut::UnregisterAll,
base::Unretained(this)));
}

// static
mate::Handle<GlobalShortcut> GlobalShortcut::Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new GlobalShortcut);
}

} // namespace api

} // namespace atom

namespace {

void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
v8::Handle<v8::Context> context, void* priv) {
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)
54 changes: 54 additions & 0 deletions atom/browser/api/atom_api_global_shortcut.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// 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_BROWSER_API_ATOM_API_GLOBAL_SHORTCUT_H_
#define ATOM_BROWSER_API_ATOM_API_GLOBAL_SHORTCUT_H_

#include <map>
#include <string>

#include "base/callback.h"
#include "chrome/browser/extensions/global_shortcut_listener.h"
#include "native_mate/wrappable.h"
#include "native_mate/handle.h"
#include "ui/base/accelerators/accelerator.h"

namespace atom {

namespace api {

class GlobalShortcut : public extensions::GlobalShortcutListener::Observer,
public mate::Wrappable {
public:
static mate::Handle<GlobalShortcut> Create(v8::Isolate* isolate);

protected:
GlobalShortcut();
virtual ~GlobalShortcut();

// mate::Wrappable implementations:
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) OVERRIDE;

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);
void UnregisterAll();

// GlobalShortcutListener::Observer implementation.
virtual void OnKeyPressed(const ui::Accelerator& accelerator) OVERRIDE;

AcceleratorCallbackMap accelerator_callback_map_;

DISALLOW_COPY_AND_ASSIGN(GlobalShortcut);
};

} // namespace api

} // namespace atom

#endif // ATOM_BROWSER_API_ATOM_API_GLOBAL_SHORTCUT_H_
110 changes: 0 additions & 110 deletions atom/browser/api/atom_api_shortcut.cc

This file was deleted.

Loading

0 comments on commit 0356790

Please sign in to comment.