]> khamidou — GitHub mirrors - electron.git/commitdiff
Bind TrayIcon to JS.
authorCheng Zhao <zcbenz@gmail.com>
Fri, 30 May 2014 15:57:54 +0000 (23:57 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 30 May 2014 15:57:54 +0000 (23:57 +0800)
13 files changed:
atom.gyp
atom/browser/api/atom_api_menu.h
atom/browser/api/atom_api_tray.cc [new file with mode: 0644]
atom/browser/api/atom_api_tray.h [new file with mode: 0644]
atom/browser/api/lib/tray.coffee [new file with mode: 0644]
atom/browser/ui/tray_icon.h
atom/browser/ui/tray_icon_cocoa.h
atom/browser/ui/tray_icon_cocoa.mm
atom/browser/ui/tray_icon_gtk.cc
atom/browser/ui/tray_icon_gtk.h
atom/browser/ui/tray_icon_win.cc
atom/browser/ui/tray_icon_win.h
atom/common/api/atom_extensions.h

index 426d9bf054314227bbab627a706b0a826148fcfd..3e01136324500ad515433fb2594ada0370ab9b24 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
@@ -24,6 +24,7 @@
       'atom/browser/api/lib/menu-item.coffee',
       'atom/browser/api/lib/power-monitor.coffee',
       'atom/browser/api/lib/protocol.coffee',
+      'atom/browser/api/lib/tray.coffee',
       'atom/browser/api/lib/web-contents.coffee',
       'atom/browser/lib/init.coffee',
       'atom/browser/lib/objects-registry.coffee',
@@ -62,6 +63,8 @@
       '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_tray.cc',
+      'atom/browser/api/atom_api_tray.h',
       'atom/browser/api/atom_api_web_contents.cc',
       'atom/browser/api/atom_api_web_contents.h',
       'atom/browser/api/atom_api_window.cc',
       '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',
+      'atom/common/native_mate_converters/image_converter.cc',
+      'atom/common/native_mate_converters/image_converter.h',
       'atom/common/native_mate_converters/string16_converter.h',
       'atom/common/native_mate_converters/v8_value_converter.cc',
       'atom/common/native_mate_converters/v8_value_converter.h',
index a309e1b1eb4055e369f5455adc8f809ace3ac4d7..2a212d0145a1ed5ee917b0333b13b5225b5950b5 100644 (file)
@@ -34,6 +34,8 @@ class Menu : public mate::Wrappable,
   static void SendActionToFirstResponder(const std::string& action);
 #endif
 
+  ui::SimpleMenuModel* model() const { return model_.get(); }
+
  protected:
   Menu();
   virtual ~Menu();
diff --git a/atom/browser/api/atom_api_tray.cc b/atom/browser/api/atom_api_tray.cc
new file mode 100644 (file)
index 0000000..41e1d18
--- /dev/null
@@ -0,0 +1,76 @@
+// 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_tray.h"
+
+#include "atom/browser/api/atom_api_menu.h"
+#include "atom/browser/ui/tray_icon.h"
+#include "atom/common/native_mate_converters/image_converter.h"
+#include "native_mate/constructor.h"
+#include "native_mate/dictionary.h"
+
+#include "atom/common/node_includes.h"
+
+namespace atom {
+
+namespace api {
+
+Tray::Tray(const gfx::ImageSkia& image)
+    : tray_icon_(TrayIcon::Create()) {
+  tray_icon_->SetImage(image);
+}
+
+Tray::~Tray() {
+}
+
+// static
+mate::Wrappable* Tray::New(const gfx::ImageSkia& image) {
+  return new Tray(image);
+}
+
+void Tray::SetImage(const gfx::ImageSkia& image) {
+  tray_icon_->SetImage(image);
+}
+
+void Tray::SetPressedImage(const gfx::ImageSkia& image) {
+  tray_icon_->SetPressedImage(image);
+}
+
+void Tray::SetToolTip(const std::string& tool_tip) {
+  tray_icon_->SetToolTip(tool_tip);
+}
+
+void Tray::SetContextMenu(Menu* menu) {
+  tray_icon_->SetContextMenu(menu->model());
+}
+
+// static
+void Tray::BuildPrototype(v8::Isolate* isolate,
+                          v8::Handle<v8::ObjectTemplate> prototype) {
+  mate::ObjectTemplateBuilder(isolate, prototype)
+      .SetMethod("setImage", &Tray::SetImage)
+      .SetMethod("setPressedImage", &Tray::SetPressedImage)
+      .SetMethod("setToolTip", &Tray::SetToolTip)
+      .SetMethod("_setContextMenu", &Tray::SetContextMenu);
+}
+
+}  // namespace api
+
+}  // namespace atom
+
+
+namespace {
+
+void Initialize(v8::Handle<v8::Object> exports) {
+  using atom::api::Tray;
+  v8::Isolate* isolate = v8::Isolate::GetCurrent();
+  v8::Handle<v8::Function> constructor = mate::CreateConstructor<Tray>(
+      isolate, "Tray", base::Bind(&Tray::New));
+  mate::Dictionary dict(isolate, exports);
+  dict.Set("Tray", static_cast<v8::Handle<v8::Value>>(constructor));
+}
+
+}  // namespace
+
+NODE_MODULE(atom_browser_tray, Initialize)
diff --git a/atom/browser/api/atom_api_tray.h b/atom/browser/api/atom_api_tray.h
new file mode 100644 (file)
index 0000000..1ea95d9
--- /dev/null
@@ -0,0 +1,49 @@
+// 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_TRAY_H_
+#define ATOM_BROWSER_API_ATOM_API_TRAY_H_
+
+#include "atom/browser/api/event_emitter.h"
+#include "base/memory/scoped_ptr.h"
+
+namespace gfx {
+class ImageSkia;
+}
+
+namespace atom {
+
+class TrayIcon;
+
+namespace api {
+
+class Menu;
+
+class Tray : public mate::EventEmitter {
+ public:
+  static mate::Wrappable* New(const gfx::ImageSkia& image);
+
+  static void BuildPrototype(v8::Isolate* isolate,
+                             v8::Handle<v8::ObjectTemplate> prototype);
+
+ protected:
+  Tray(const gfx::ImageSkia& image);
+  virtual ~Tray();
+
+  void SetImage(const gfx::ImageSkia& image);
+  void SetPressedImage(const gfx::ImageSkia& image);
+  void SetToolTip(const std::string& tool_tip);
+  void SetContextMenu(Menu* menu);
+
+ private:
+  scoped_ptr<TrayIcon> tray_icon_;
+
+  DISALLOW_COPY_AND_ASSIGN(Tray);
+};
+
+}  // namespace api
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_API_ATOM_API_TRAY_H_
diff --git a/atom/browser/api/lib/tray.coffee b/atom/browser/api/lib/tray.coffee
new file mode 100644 (file)
index 0000000..7d158a9
--- /dev/null
@@ -0,0 +1,10 @@
+EventEmitter = require('events').EventEmitter
+bindings = process.atomBinding 'tray'
+
+Tray = bindings.Tray
+Tray::__proto__ = EventEmitter.prototype
+Tray::setContextMenu = (menu) ->
+  @_setContextMenu menu
+  @menu = menu  # Keep a strong reference of menu.
+
+module.exports = Tray
index ed96653bcdabed562adc9b5d68cba1d112a1ad14..67f3150d40e29f7d1aff8d98a2ead0c2e9fddf8d 100644 (file)
@@ -15,6 +15,8 @@ class TrayIcon {
  public:
   static TrayIcon* Create();
 
+  virtual ~TrayIcon();
+
   // Sets the image associated with this status icon.
   virtual void SetImage(const gfx::ImageSkia& image) = 0;
 
@@ -32,7 +34,6 @@ class TrayIcon {
 
  protected:
   TrayIcon();
-  virtual ~TrayIcon();
 
  private:
   DISALLOW_COPY_AND_ASSIGN(TrayIcon);
index eb18c15db1ee5ae802e69951490d1097e75e5b7e..c030a1f98445825b08139a8e3a7e027deae31c2c 100644 (file)
@@ -18,15 +18,15 @@ namespace atom {
 
 class TrayIconCocoa : public TrayIcon {
  public:
+  TrayIconCocoa();
+  virtual ~TrayIconCocoa();
+
   virtual void SetImage(const gfx::ImageSkia& image) OVERRIDE;
   virtual void SetPressedImage(const gfx::ImageSkia& image) OVERRIDE;
   virtual void SetToolTip(const std::string& tool_tip) OVERRIDE;
   virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) OVERRIDE;
 
  private:
-  TrayIconCocoa();
-  virtual ~TrayIconCocoa();
-
   base::scoped_nsobject<NSStatusItem> item_;
 
   // Status menu shown when right-clicking the system icon.
index ee42c69e44deda71ef98ef5984e82f5b1be08d2d..f490015c97ee3b1afa951af200c315c09aa78c65 100644 (file)
@@ -41,9 +41,13 @@ void TrayIconCocoa::SetToolTip(const std::string& tool_tip) {
 }
 
 void TrayIconCocoa::SetContextMenu(ui::SimpleMenuModel* menu_model) {
-  TrayIcon::SetContextMenu(menu_model);
   menu_.reset([[AtomMenuController alloc] initWithModel:menu_model]);
   [item_ setMenu:[menu_ menu]];
 }
 
+// static
+TrayIcon* TrayIcon::Create() {
+  return new TrayIconCocoa;
+}
+
 }  // namespace atom
index 40c5bbbb4abb704225cda7016fcbafe3bc56417f..9069c0749b3ba9fad117209bc8387b69c96c2db9 100644 (file)
@@ -24,4 +24,9 @@ void TrayIconGtk::SetToolTip(const std::string& tool_tip) {
 void TrayIconGtk::SetContextMenu(ui::SimpleMenuModel* menu_model) {
 }
 
+// static
+TrayIcon* TrayIcon::Create() {
+  return new TrayIconGtk;
+}
+
 }  // namespace atom
index da398e4eee94f3c7282f35db18acd61aaa492351..cc66819cab29c2d1beac6cb86298ca1768a37912 100644 (file)
@@ -13,15 +13,15 @@ namespace atom {
 
 class TrayIconGtk : public TrayIcon {
  public:
+  TrayIconGtk();
+  virtual ~TrayIconGtk();
+
   virtual void SetImage(const gfx::ImageSkia& image) OVERRIDE;
   virtual void SetPressedImage(const gfx::ImageSkia& image) OVERRIDE;
   virtual void SetToolTip(const std::string& tool_tip) OVERRIDE;
   virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) OVERRIDE;
 
  private:
-  TrayIconGtk();
-  virtual ~TrayIconGtk();
-
   DISALLOW_COPY_AND_ASSIGN(TrayIconGtk);
 };
 
index 0ef5b0fdef7935d0d6caee3bf3a6f41a61e318ac..3c8e36ebc910267ab40af2c2e59fad86e33d1db4 100644 (file)
@@ -24,4 +24,9 @@ void TrayIconWin::SetToolTip(const std::string& tool_tip) {
 void TrayIconWin::SetContextMenu(ui::SimpleMenuModel* menu_model) {
 }
 
+// static
+TrayIcon* TrayIcon::Create() {
+  return new TrayIconWin;
+}
+
 }  // namespace atom
index b7077037d404030fb05024de96d93e75c0e5930a..dcff1c4807912b44f5bf57b35c1b0df5d833c84f 100644 (file)
@@ -13,15 +13,15 @@ namespace atom {
 
 class TrayIconWin : public TrayIcon {
  public:
+  TrayIconWin();
+  virtual ~TrayIconWin();
+
   virtual void SetImage(const gfx::ImageSkia& image) OVERRIDE;
   virtual void SetPressedImage(const gfx::ImageSkia& image) OVERRIDE;
   virtual void SetToolTip(const std::string& tool_tip) OVERRIDE;
   virtual void SetContextMenu(ui::SimpleMenuModel* menu_model) OVERRIDE;
 
  private:
-  TrayIconWin();
-  virtual ~TrayIconWin();
-
   DISALLOW_COPY_AND_ASSIGN(TrayIconWin);
 };
 
index 6d0a9166c1916da15290d355721ea22fe895dab0..39a4159c555e4749b2d5e09f8c73f0b03738f743 100644 (file)
@@ -15,6 +15,7 @@ NODE_EXT_LIST_ITEM(atom_browser_dialog)
 NODE_EXT_LIST_ITEM(atom_browser_menu)
 NODE_EXT_LIST_ITEM(atom_browser_power_monitor)
 NODE_EXT_LIST_ITEM(atom_browser_protocol)
+NODE_EXT_LIST_ITEM(atom_browser_tray)
 NODE_EXT_LIST_ITEM(atom_browser_window)
 
 // Module names start with `atom_renderer_` can only be used by renderer