Convert to mate::Handle<NativeImage> instead of gfx::Image
authorCheng Zhao <zcbenz@gmail.com>
Fri, 20 May 2016 07:14:40 +0000 (16:14 +0900)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 20 May 2016 07:14:40 +0000 (16:14 +0900)
atom/browser/api/atom_api_tray.cc
atom/browser/api/atom_api_tray.h
atom/common/api/atom_api_native_image.cc
atom/common/api/atom_api_native_image.h
atom/common/native_mate_converters/image_converter.cc
vendor/native_mate

index 34fe81b..a6d5080 100644 (file)
@@ -9,6 +9,7 @@
 #include "atom/browser/api/atom_api_menu.h"
 #include "atom/browser/browser.h"
 #include "atom/browser/ui/tray_icon.h"
+#include "atom/common/api/atom_api_native_image.h"
 #include "atom/common/native_mate_converters/gfx_converter.h"
 #include "atom/common/native_mate_converters/image_converter.h"
 #include "atom/common/native_mate_converters/string16_converter.h"
@@ -22,9 +23,10 @@ namespace atom {
 
 namespace api {
 
-Tray::Tray(v8::Isolate* isolate, const gfx::Image& image)
-    : tray_icon_(TrayIcon::Create()) {
-  tray_icon_->SetImage(image);
+Tray::Tray(v8::Isolate* isolate, mate::Handle<NativeImage> image)
+    : image_(isolate, image.ToV8()),
+      tray_icon_(TrayIcon::Create()) {
+  tray_icon_->SetImage(image->image());
   tray_icon_->AddObserver(this);
 }
 
@@ -32,7 +34,8 @@ Tray::~Tray() {
 }
 
 // static
-mate::WrappableBase* Tray::New(v8::Isolate* isolate, const gfx::Image& image) {
+mate::WrappableBase* Tray::New(v8::Isolate* isolate,
+                               mate::Handle<NativeImage> image) {
   if (!Browser::Get()->is_ready()) {
     isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
         isolate, "Cannot create Tray before app is ready")));
@@ -94,23 +97,26 @@ void Tray::OnDragEnded() {
   Emit("drag-end");
 }
 
-void Tray::SetImage(mate::Arguments* args, const gfx::Image& image) {
-  tray_icon_->SetImage(image);
+void Tray::SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image) {
+  image_.Reset(isolate, image.ToV8());
+  tray_icon_->SetImage(image->image());
 }
 
-void Tray::SetPressedImage(mate::Arguments* args, const gfx::Image& image) {
-  tray_icon_->SetPressedImage(image);
+void Tray::SetPressedImage(v8::Isolate* isolate,
+                           mate::Handle<NativeImage> image) {
+  pressed_image_.Reset(isolate, image.ToV8());
+  tray_icon_->SetPressedImage(image->image());
 }
 
-void Tray::SetToolTip(mate::Arguments* args, const std::string& tool_tip) {
+void Tray::SetToolTip(const std::string& tool_tip) {
   tray_icon_->SetToolTip(tool_tip);
 }
 
-void Tray::SetTitle(mate::Arguments* args, const std::string& title) {
+void Tray::SetTitle(const std::string& title) {
   tray_icon_->SetTitle(title);
 }
 
-void Tray::SetHighlightMode(mate::Arguments* args, bool highlight) {
+void Tray::SetHighlightMode(bool highlight) {
   tray_icon_->SetHighlightMode(highlight);
 }
 
@@ -136,7 +142,7 @@ void Tray::PopUpContextMenu(mate::Arguments* args) {
   tray_icon_->PopUpContextMenu(pos, menu.IsEmpty() ? nullptr : menu->model());
 }
 
-void Tray::SetContextMenu(mate::Arguments* args, Menu* menu) {
+void Tray::SetContextMenu(Menu* menu) {
   tray_icon_->SetContextMenu(menu->model());
 }
 
index 84e6822..2a794b5 100644 (file)
@@ -11,6 +11,7 @@
 #include "atom/browser/api/trackable_object.h"
 #include "atom/browser/ui/tray_icon_observer.h"
 #include "base/memory/scoped_ptr.h"
+#include "native_mate/handle.h"
 
 namespace gfx {
 class Image;
@@ -28,18 +29,19 @@ class TrayIcon;
 namespace api {
 
 class Menu;
+class NativeImage;
 
 class Tray : public mate::TrackableObject<Tray>,
              public TrayIconObserver {
  public:
   static mate::WrappableBase* New(
-      v8::Isolate* isolate, const gfx::Image& image);
+      v8::Isolate* isolate, mate::Handle<NativeImage> image);
 
   static void BuildPrototype(v8::Isolate* isolate,
                              v8::Local<v8::ObjectTemplate> prototype);
 
  protected:
-  Tray(v8::Isolate* isolate, const gfx::Image& image);
+  Tray(v8::Isolate* isolate, mate::Handle<NativeImage> image);
   ~Tray() override;
 
   // TrayIconObserver:
@@ -55,18 +57,20 @@ class Tray : public mate::TrackableObject<Tray>,
   void OnDragExited() override;
   void OnDragEnded() override;
 
-  void SetImage(mate::Arguments* args, const gfx::Image& image);
-  void SetPressedImage(mate::Arguments* args, const gfx::Image& image);
-  void SetToolTip(mate::Arguments* args, const std::string& tool_tip);
-  void SetTitle(mate::Arguments* args, const std::string& title);
-  void SetHighlightMode(mate::Arguments* args, bool highlight);
+  void SetImage(v8::Isolate* isolate, mate::Handle<NativeImage> image);
+  void SetPressedImage(v8::Isolate* isolate, mate::Handle<NativeImage> image);
+  void SetToolTip(const std::string& tool_tip);
+  void SetTitle(const std::string& title);
+  void SetHighlightMode(bool highlight);
   void DisplayBalloon(mate::Arguments* args, const mate::Dictionary& options);
   void PopUpContextMenu(mate::Arguments* args);
-  void SetContextMenu(mate::Arguments* args, Menu* menu);
+  void SetContextMenu(Menu* menu);
 
  private:
   v8::Local<v8::Object> ModifiersToObject(v8::Isolate* isolate, int modifiers);
 
+  v8::Global<v8::Object> image_;
+  v8::Global<v8::Object> pressed_image_;
   scoped_ptr<TrayIcon> tray_icon_;
 
   DISALLOW_COPY_AND_ASSIGN(Tray);
index 1bbb9d9..1b1b7ac 100644 (file)
@@ -341,6 +341,35 @@ void NativeImage::BuildPrototype(
 
 }  // namespace atom
 
+namespace mate {
+
+v8::Local<v8::Value> Converter<mate::Handle<atom::api::NativeImage>>::ToV8(
+    v8::Isolate* isolate,
+    const mate::Handle<atom::api::NativeImage>& val) {
+  return val.ToV8();
+}
+
+bool Converter<mate::Handle<atom::api::NativeImage>>::FromV8(
+    v8::Isolate* isolate, v8::Local<v8::Value> val,
+    mate::Handle<atom::api::NativeImage>* out) {
+  // Try converting from file path.
+  base::FilePath path;
+  if (ConvertFromV8(isolate, val, &path)) {
+    *out = atom::api::NativeImage::CreateFromPath(isolate, path);
+    // Should throw when failed to initialize from path.
+    return !(*out)->image().IsEmpty();
+  }
+
+  WrappableBase* wrapper = static_cast<WrappableBase*>(internal::FromV8Impl(
+      isolate, val));
+  if (!wrapper)
+    return false;
+
+  *out = CreateHandle(isolate, static_cast<atom::api::NativeImage*>(wrapper));
+  return true;
+}
+
+}  // namespace mate
 
 namespace {
 
index 981f3b8..faf00f3 100644 (file)
@@ -53,6 +53,9 @@ class NativeImage : public mate::Wrappable<NativeImage> {
                              v8::Local<v8::ObjectTemplate> prototype);
 
   const gfx::Image& image() const { return image_; }
+#if defined(OS_WIN)
+  HICON hicon() const { return hicon_.get(); }
+#endif
 
  protected:
   NativeImage(v8::Isolate* isolate, const gfx::Image& image);
@@ -89,4 +92,19 @@ class NativeImage : public mate::Wrappable<NativeImage> {
 
 }  // namespace atom
 
+namespace mate {
+
+// A custom converter that allows converting path to NativeImage.
+template<>
+struct Converter<mate::Handle<atom::api::NativeImage>> {
+  static v8::Local<v8::Value> ToV8(
+      v8::Isolate* isolate,
+      const mate::Handle<atom::api::NativeImage>& val);
+  static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
+                     mate::Handle<atom::api::NativeImage>* out);
+};
+
+}  // namespace mate
+
+
 #endif  // ATOM_COMMON_API_ATOM_API_NATIVE_IMAGE_H_
index 550bb7b..cfb1938 100644 (file)
@@ -28,16 +28,8 @@ bool Converter<gfx::Image>::FromV8(v8::Isolate* isolate,
     return true;
 
   Handle<atom::api::NativeImage> native_image;
-  if (!ConvertFromV8(isolate, val, &native_image)) {
-    // Try converting from file path.
-    base::FilePath path;
-    if (!Converter<base::FilePath>::FromV8(isolate, val, &path))
-      return false;
-
-    native_image = atom::api::NativeImage::CreateFromPath(isolate, path);
-    if (native_image->image().IsEmpty())
-      return false;
-  }
+  if (!ConvertFromV8(isolate, val, &native_image))
+    return false;
 
   *out = native_image->image();
   return true;
index ea07d4c..4ad6ecd 160000 (submodule)
@@ -1 +1 @@
-Subproject commit ea07d4c6c89d8d460e76b2d2ab9b0ebc51f9a432
+Subproject commit 4ad6ecd19617ac33c09e93ccb6d8e652ac1ac126