]> khamidou — GitHub mirrors - electron.git/commitdiff
Add support to convert gfx::Rect and gfx::Size to v8 value.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 7 Jan 2014 12:15:23 +0000 (20:15 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 7 Jan 2014 12:15:23 +0000 (20:15 +0800)
common/v8/native_type_conversions.h

index 8da7238300e561697c3ffb3c1157c71c8a99e011..05bc883a901a6adcbf44b0e733d66811032fd3c9 100644 (file)
@@ -20,6 +20,7 @@
 #include "content/public/renderer/v8_value_converter.h"
 #include "ui/gfx/point.h"
 #include "ui/gfx/rect.h"
+#include "ui/gfx/size.h"
 #include "url/gurl.h"
 
 // Convert V8 value to arbitrary supported types.
@@ -122,6 +123,14 @@ inline v8::Handle<v8::Value> ToV8Value(bool b) {
   return v8::Boolean::New(b);
 }
 
+inline v8::Handle<v8::Value> ToV8Value(float f) {
+  return v8::Number::New(f);
+}
+
+inline v8::Handle<v8::Value> ToV8Value(double f) {
+  return v8::Number::New(f);
+}
+
 inline v8::Handle<v8::Value> ToV8Value(const char* s) {
   return v8::String::New(s);
 }
@@ -162,6 +171,22 @@ inline v8::Handle<v8::Value> ToV8Value(const gfx::Point& point) {
   return obj;
 }
 
+inline v8::Handle<v8::Value> ToV8Value(const gfx::Rect& rect) {
+  v8::Handle<v8::Object> obj = v8::Object::New();
+  obj->Set(ToV8Value("x"), ToV8Value(rect.x()));
+  obj->Set(ToV8Value("y"), ToV8Value(rect.y()));
+  obj->Set(ToV8Value("width"), ToV8Value(rect.width()));
+  obj->Set(ToV8Value("height"), ToV8Value(rect.height()));
+  return obj;
+}
+
+inline v8::Handle<v8::Value> ToV8Value(const gfx::Size& size) {
+  v8::Handle<v8::Object> obj = v8::Object::New();
+  obj->Set(ToV8Value("width"), ToV8Value(size.width()));
+  obj->Set(ToV8Value("height"), ToV8Value(size.height()));
+  return obj;
+}
+
 // Check if a V8 Value is of specified type.
 template<class T> inline
 bool V8ValueCanBeConvertedTo(v8::Handle<v8::Value> value) {