]> khamidou — GitHub mirrors - electron.git/commitdiff
Add converters between gfx:: and v8
authorCheng Zhao <zcbenz@gmail.com>
Fri, 24 Oct 2014 04:48:52 +0000 (12:48 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Fri, 24 Oct 2014 04:48:52 +0000 (12:48 +0800)
atom.gyp
atom/browser/api/atom_api_window.cc
atom/common/api/atom_api_screen.cc
atom/common/native_mate_converters/gfx_converter.h [new file with mode: 0644]

index 86cb4e1ced906bf9652147eabdc173d01e74ea0c..d0eaf3565bc4d6517eb3f6b54979bec9531feca2 100644 (file)
--- a/atom.gyp
+++ b/atom.gyp
       'atom/common/native_mate_converters/accelerator_converter.cc',
       'atom/common/native_mate_converters/accelerator_converter.h',
       'atom/common/native_mate_converters/file_path_converter.h',
+      'atom/common/native_mate_converters/gfx_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',
index 55c4c584d33b46cf6bd06f2d3d288cdd1507d3ee..9be2862c190e65a847313cc26694fcc14aaf40b1 100644 (file)
@@ -6,13 +6,11 @@
 
 #include "atom/browser/api/atom_api_web_contents.h"
 #include "atom/browser/native_window.h"
+#include "atom/common/native_mate_converters/gfx_converter.h"
 #include "content/public/browser/render_process_host.h"
 #include "native_mate/callback.h"
 #include "native_mate/constructor.h"
 #include "native_mate/dictionary.h"
-#include "ui/gfx/point.h"
-#include "ui/gfx/rect.h"
-#include "ui/gfx/size.h"
 
 #include "atom/common/node_includes.h"
 
@@ -40,23 +38,6 @@ struct Converter<PrintSettings> {
   }
 };
 
-template<>
-struct Converter<gfx::Rect> {
-  static bool FromV8(v8::Isolate* isolate,
-                     v8::Handle<v8::Value> val,
-                     gfx::Rect* out) {
-    if (!val->IsObject())
-      return false;
-    mate::Dictionary dict(isolate, val->ToObject());
-    int x, y, width, height;
-    if (!dict.Get("x", &x) || !dict.Get("y", &y) ||
-        !dict.Get("width", &width) || !dict.Get("height", &height))
-      return false;
-    *out = gfx::Rect(x, y, width, height);
-    return true;
-  }
-};
-
 }  // namespace mate
 
 namespace atom {
index 2a10390a63b7db06aa030af2e582c3afacfa284c..25ac5395bdd730ccf12657152f5bbbcca1a9aee5 100644 (file)
@@ -2,61 +2,9 @@
 // Use of this source code is governed by the MIT license that can be
 // found in the LICENSE file.
 
-#include "native_mate/dictionary.h"
-#include "ui/gfx/screen.h"
-
+#include "atom/common/native_mate_converters/gfx_converter.h"
 #include "atom/common/node_includes.h"
 
-namespace mate {
-
-template<>
-struct Converter<gfx::Point> {
-  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
-                                    const gfx::Point& val) {
-    return mate::ObjectTemplateBuilder(isolate).SetValue("x", val.x())
-                                               .SetValue("y", val.y())
-                                               .Build()->NewInstance();
-  }
-};
-
-template<>
-struct Converter<gfx::Size> {
-  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
-                                    const gfx::Size& val) {
-    return mate::ObjectTemplateBuilder(isolate).SetValue("width", val.width())
-                                               .SetValue("height", val.height())
-                                               .Build()->NewInstance();
-  }
-};
-
-template<>
-struct Converter<gfx::Rect> {
-  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
-                                    const gfx::Rect& val) {
-    return mate::ObjectTemplateBuilder(isolate).SetValue("x", val.x())
-                                               .SetValue("y", val.y())
-                                               .SetValue("width", val.width())
-                                               .SetValue("height", val.height())
-                                               .Build()->NewInstance();
-  }
-};
-
-template<>
-struct Converter<gfx::Display> {
-  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
-                                    const gfx::Display& display) {
-    return mate::ObjectTemplateBuilder(isolate)
-        .SetValue("bounds", display.bounds())
-        .SetValue("workArea", display.work_area())
-        .SetValue("size", display.size())
-        .SetValue("workAreaSize", display.work_area_size())
-        .SetValue("scaleFactor", display.device_scale_factor())
-        .Build()->NewInstance();
-  }
-};
-
-}  // namespace mate
-
 namespace {
 
 void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
diff --git a/atom/common/native_mate_converters/gfx_converter.h b/atom/common/native_mate_converters/gfx_converter.h
new file mode 100644 (file)
index 0000000..4abc183
--- /dev/null
@@ -0,0 +1,96 @@
+// 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 "native_mate/dictionary.h"
+#include "ui/gfx/point.h"
+#include "ui/gfx/rect.h"
+#include "ui/gfx/screen.h"
+#include "ui/gfx/size.h"
+
+namespace mate {
+
+template<>
+struct Converter<gfx::Point> {
+  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+                                    const gfx::Point& val) {
+    mate::Dictionary dict(isolate, v8::Object::New(isolate));
+    dict.Set("x", val.x());
+    dict.Set("y", val.y());
+    return dict.GetHandle();
+  }
+  static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
+                     gfx::Point* out) {
+    mate::Dictionary dict;
+    if (!ConvertFromV8(isolate, val, &dict))
+      return false;
+    int x, y;
+    if (!dict.Get("x", &x) || !dict.Get("y", &y))
+      return false;
+    *out = gfx::Point(x, y);
+    return true;
+  }
+};
+
+template<>
+struct Converter<gfx::Size> {
+  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+                                    const gfx::Size& val) {
+    mate::Dictionary dict(isolate, v8::Object::New(isolate));
+    dict.Set("width", val.width());
+    dict.Set("height", val.height());
+    return dict.GetHandle();
+  }
+  static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
+                     gfx::Size* out) {
+    mate::Dictionary dict;
+    if (!ConvertFromV8(isolate, val, &dict))
+      return false;
+    int width, height;
+    if (!dict.Get("width", &width) || !dict.Get("height", &height))
+      return false;
+    *out = gfx::Size(width, height);
+    return true;
+  }
+};
+
+template<>
+struct Converter<gfx::Rect> {
+  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+                                    const gfx::Rect& val) {
+    mate::Dictionary dict(isolate, v8::Object::New(isolate));
+    dict.Set("x", val.x());
+    dict.Set("y", val.y());
+    dict.Set("width", val.width());
+    dict.Set("height", val.height());
+    return dict.GetHandle();
+  }
+  static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
+                     gfx::Rect* out) {
+    mate::Dictionary dict;
+    if (!ConvertFromV8(isolate, val, &dict))
+      return false;
+    int x, y, width, height;
+    if (!dict.Get("x", &x) || !dict.Get("y", &y) ||
+        !dict.Get("width", &width) || !dict.Get("height", &height))
+      return false;
+    *out = gfx::Rect(x, y, width, height);
+    return true;
+  }
+};
+
+template<>
+struct Converter<gfx::Display> {
+  static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
+                                    const gfx::Display& display) {
+    mate::Dictionary dict(isolate, v8::Object::New(isolate));
+    dict.Set("bounds", display.bounds());
+    dict.Set("workArea", display.work_area());
+    dict.Set("size", display.size());
+    dict.Set("workAreaSize", display.work_area_size());
+    dict.Set("scaleFactor", display.device_scale_factor());
+    return dict.GetHandle();
+  }
+};
+
+}  // namespace mate