Make Accelerator a standalone JS type.
This makes menu and global-shortcut share the same code on accelerator.
This commit is contained in:
parent
28b9df24a6
commit
6dc01945af
9 changed files with 114 additions and 65 deletions
|
@ -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.";
|
||||
void GlobalShortcut::Unregister(const ui::Accelerator& accelerator) {
|
||||
if (!ContainsKey(accelerator_callback_map_, accelerator))
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue