From 8d22eeb3be7ce0b4d6c1393cf686f63fa3aa21c1 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 29 Jul 2015 14:25:12 +0800 Subject: [PATCH] Use DOM's way of telling modifiers --- atom/browser/api/atom_api_tray.cc | 31 +++++++++++++++++++++++++------ atom/browser/api/atom_api_tray.h | 4 +++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc index a849c03f59a0..82f78519aeb3 100644 --- a/atom/browser/api/atom_api_tray.cc +++ b/atom/browser/api/atom_api_tray.cc @@ -14,6 +14,7 @@ #include "atom/common/native_mate_converters/string16_converter.h" #include "native_mate/constructor.h" #include "native_mate/dictionary.h" +#include "ui/events/event_constants.h" #include "ui/gfx/image/image.h" #include "atom/common/node_includes.h" @@ -41,11 +42,24 @@ mate::Wrappable* Tray::New(v8::Isolate* isolate, const gfx::Image& image) { } void Tray::OnClicked(const gfx::Rect& bounds, int modifiers) { - Emit("clicked", bounds, modifiers); + v8::Locker locker(isolate()); + v8::HandleScope handle_scope(isolate()); + EmitCustomEvent("clicked", + ModifiersToObject(isolate(), modifiers), bounds); } void Tray::OnDoubleClicked(const gfx::Rect& bounds, int modifiers) { - Emit("double-clicked", bounds, modifiers); + v8::Locker locker(isolate()); + v8::HandleScope handle_scope(isolate()); + EmitCustomEvent("double-clicked", + ModifiersToObject(isolate(), modifiers), bounds); +} + +void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) { + v8::Locker locker(isolate()); + v8::HandleScope handle_scope(isolate()); + EmitCustomEvent("right-clicked", + ModifiersToObject(isolate(), modifiers), bounds); } void Tray::OnBalloonShow() { @@ -60,10 +74,6 @@ void Tray::OnBalloonClosed() { Emit("balloon-closed"); } -void Tray::OnRightClicked(const gfx::Rect& bounds, int modifiers) { - Emit("right-clicked", bounds, modifiers); -} - void Tray::OnDropFiles(const std::vector& files) { Emit("drop-files", files); } @@ -120,6 +130,15 @@ void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) { tray_icon_->SetContextMenu(menu->model()); } +v8::Local Tray::ModifiersToObject(v8::Isolate* isolate, + int modifiers) { + mate::Dictionary obj(isolate, v8::Object::New(isolate)); + obj.Set("shiftKey", static_cast(modifiers & ui::EF_SHIFT_DOWN)); + obj.Set("ctrlKey", static_cast(modifiers & ui::EF_CONTROL_DOWN)); + obj.Set("altKey", static_cast(modifiers & ui::EF_ALT_DOWN)); + return obj.GetHandle(); +} + // static void Tray::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h index 10340677813f..02f7418fe41d 100644 --- a/atom/browser/api/atom_api_tray.h +++ b/atom/browser/api/atom_api_tray.h @@ -44,10 +44,10 @@ class Tray : public mate::EventEmitter, // TrayIconObserver: void OnClicked(const gfx::Rect& bounds, int modifiers) override; void OnDoubleClicked(const gfx::Rect& bounds, int modifiers) override; + void OnRightClicked(const gfx::Rect& bounds, int modifiers) override; void OnBalloonShow() override; void OnBalloonClicked() override; void OnBalloonClosed() override; - void OnRightClicked(const gfx::Rect& bounds, int modifiers) override; void OnDropFiles(const std::vector& files) override; // mate::Wrappable: @@ -64,6 +64,8 @@ class Tray : public mate::EventEmitter, void SetContextMenu(mate::Arguments* args, Menu* menu); private: + v8::Local ModifiersToObject(v8::Isolate* isolate, int modifiers); + scoped_ptr tray_icon_; DISALLOW_COPY_AND_ASSIGN(Tray);