From df50cc08d6b58898a60640860ec1e1181037c92a Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Apr 2013 22:49:49 +0800 Subject: [PATCH] Expose interface of NativeWindow in javascript. --- browser/api/atom_api_window.cc | 281 +++++++++++++++++++++++++++++++++ browser/api/atom_api_window.h | 27 ++++ browser/default_app/main.js | 4 + browser/native_window.cc | 4 +- browser/native_window.h | 4 +- browser/native_window_mac.h | 4 +- browser/native_window_mac.mm | 10 +- vendor/node | 2 +- 8 files changed, 325 insertions(+), 11 deletions(-) diff --git a/browser/api/atom_api_window.cc b/browser/api/atom_api_window.cc index 0edb9f35bd4b..a74ab5b44c17 100644 --- a/browser/api/atom_api_window.cc +++ b/browser/api/atom_api_window.cc @@ -9,8 +9,12 @@ #include "browser/native_window.h" #include "common/v8_value_converter_impl.h" #include "content/public/browser/web_contents.h" +#include "ui/gfx/point.h" +#include "ui/gfx/rect.h" +#include "ui/gfx/size.h" using content::V8ValueConverter; +using node::ObjectWrap; namespace atom { @@ -49,6 +53,256 @@ v8::Handle Window::New(const v8::Arguments &args) { return args.This(); } +// static +v8::Handle Window::Destroy(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + delete self; + + return v8::Undefined(); +} + +// static +v8::Handle Window::Close(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->Close(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::Focus(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->Focus(args[0]->IsBoolean() ? args[0]->BooleanValue(): true); + + return v8::Undefined(); +} + +// static +v8::Handle Window::Show(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->Show(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::Hide(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->Hide(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::Maximize(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->Maximize(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::Unmaximize(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->Unmaximize(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::Minimize(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->Minimize(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::Restore(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->Restore(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::SetFullscreen(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 1 || !args[0]->IsBoolean()) + return node::ThrowTypeError("Bad argument"); + self->window_->SetFullscreen(args[0]->BooleanValue()); + + return v8::Undefined(); +} + +// static +v8::Handle Window::IsFullscreen(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + return v8::Boolean::New(self->window_->IsFullscreen()); +} + +// static +v8::Handle Window::SetSize(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 2) + return node::ThrowTypeError("Bad argument"); + self->window_->SetSize( + gfx::Size(args[0]->IntegerValue(), args[1]->IntegerValue())); + + return v8::Undefined(); +} + +// static +v8::Handle Window::GetSize(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + gfx::Size size = self->window_->GetSize(); + v8::Handle ret = v8::Array::New(2); + ret->Set(0, v8::Integer::New(size.width())); + ret->Set(1, v8::Integer::New(size.height())); + + return ret; +} + +// static +v8::Handle Window::SetMinimumSize(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 2) + return node::ThrowTypeError("Bad argument"); + self->window_->SetMinimumSize( + gfx::Size(args[0]->IntegerValue(), args[1]->IntegerValue())); + + return v8::Undefined(); +} + +// static +v8::Handle Window::SetMaximumSize(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 2) + return node::ThrowTypeError("Bad argument"); + self->window_->SetMaximumSize( + gfx::Size(args[0]->IntegerValue(), args[1]->IntegerValue())); + + return v8::Undefined(); +} + +// static +v8::Handle Window::SetResizable(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 1 || !args[0]->IsBoolean()) + return node::ThrowTypeError("Bad argument"); + self->window_->SetResizable(args[0]->BooleanValue()); + + return v8::Undefined(); +} + +// static +v8::Handle Window::SetAlwaysOnTop(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 1 || !args[0]->IsBoolean()) + return node::ThrowTypeError("Bad argument"); + self->window_->SetAlwaysOnTop(args[0]->BooleanValue()); + + return v8::Undefined(); +} + +// static +v8::Handle Window::SetPosition(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 2) + return node::ThrowTypeError("Bad argument"); + self->window_->SetPosition( + gfx::Point(args[0]->IntegerValue(), args[1]->IntegerValue())); + + return v8::Undefined(); +} + +// static +v8::Handle Window::GetPosition(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + gfx::Point pos = self->window_->GetPosition(); + v8::Handle ret = v8::Array::New(2); + ret->Set(0, v8::Integer::New(pos.x())); + ret->Set(1, v8::Integer::New(pos.y())); + + return ret; +} + +// static +v8::Handle Window::SetTitle(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 1 || !args[0]->IsString()) + return node::ThrowTypeError("Bad argument"); + self->window_->SetTitle(*v8::String::Utf8Value(args[0])); + + return v8::Undefined(); +} + +// static +v8::Handle Window::FlashFrame(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->FlashFrame( + args[0]->IsBoolean() ? args[0]->BooleanValue(): true); + + return v8::Undefined(); +} + +// static +v8::Handle Window::SetKiosk(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() < 1 || !args[0]->IsBoolean()) + return node::ThrowTypeError("Bad argument"); + self->window_->SetKiosk(args[0]->BooleanValue()); + + return v8::Undefined(); +} + +// static +v8::Handle Window::IsKiosk(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + return v8::Boolean::New(self->window_->IsKiosk()); +} + +// static +v8::Handle Window::ShowDevTools(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->ShowDevTools(); + + return v8::Undefined(); +} + +// static +v8::Handle Window::CloseDevTools(const v8::Arguments &args) { + Window *self = ObjectWrap::Unwrap(args.This()); + + self->window_->CloseDevTools(); + + return v8::Undefined(); +} + // static void Window::Initialize(v8::Handle target) { v8::HandleScope scope; @@ -56,6 +310,33 @@ void Window::Initialize(v8::Handle target) { v8::Local t = v8::FunctionTemplate::New(Window::New); t->InstanceTemplate()->SetInternalFieldCount(1); t->SetClassName(v8::String::NewSymbol("Window")); + + NODE_SET_PROTOTYPE_METHOD(t, "destroy" , Destroy); + NODE_SET_PROTOTYPE_METHOD(t, "close" , Close); + NODE_SET_PROTOTYPE_METHOD(t, "focus" , Focus); + NODE_SET_PROTOTYPE_METHOD(t, "show" , Show); + NODE_SET_PROTOTYPE_METHOD(t, "hide" , Hide); + NODE_SET_PROTOTYPE_METHOD(t, "maximize" , Maximize); + NODE_SET_PROTOTYPE_METHOD(t, "unmaximize" , Unmaximize); + NODE_SET_PROTOTYPE_METHOD(t, "minimize" , Minimize); + NODE_SET_PROTOTYPE_METHOD(t, "restore" , Restore); + NODE_SET_PROTOTYPE_METHOD(t, "setFullscreen" , SetFullscreen); + NODE_SET_PROTOTYPE_METHOD(t, "isFullscreen" , IsFullscreen); + NODE_SET_PROTOTYPE_METHOD(t, "setSize" , SetSize); + NODE_SET_PROTOTYPE_METHOD(t, "getSize" , GetSize); + NODE_SET_PROTOTYPE_METHOD(t, "setMinimumSize" , SetMinimumSize); + NODE_SET_PROTOTYPE_METHOD(t, "setMaximumSize" , SetMaximumSize); + NODE_SET_PROTOTYPE_METHOD(t, "setResizable" , SetResizable); + NODE_SET_PROTOTYPE_METHOD(t, "setAlwaysOnTop" , SetAlwaysOnTop); + NODE_SET_PROTOTYPE_METHOD(t, "setPosition" , SetPosition); + NODE_SET_PROTOTYPE_METHOD(t, "getPosition" , GetPosition); + NODE_SET_PROTOTYPE_METHOD(t, "setTitle" , SetTitle); + NODE_SET_PROTOTYPE_METHOD(t, "flashFrame" , FlashFrame); + NODE_SET_PROTOTYPE_METHOD(t, "setKiosk" , SetKiosk); + NODE_SET_PROTOTYPE_METHOD(t, "isKiosk" , IsKiosk); + NODE_SET_PROTOTYPE_METHOD(t, "showDevTools" , ShowDevTools); + NODE_SET_PROTOTYPE_METHOD(t, "closeDevTools" , CloseDevTools); + target->Set(v8::String::NewSymbol("Window"), t->GetFunction()); } diff --git a/browser/api/atom_api_window.h b/browser/api/atom_api_window.h index db6c508389f9..1bab825788b9 100644 --- a/browser/api/atom_api_window.h +++ b/browser/api/atom_api_window.h @@ -31,6 +31,33 @@ class Window : public EventEmitter { private: static v8::Handle New(const v8::Arguments &args); + static v8::Handle Destroy(const v8::Arguments &args); + + // APIs for NativeWindow. + static v8::Handle Close(const v8::Arguments &args); + static v8::Handle Focus(const v8::Arguments &args); + static v8::Handle Show(const v8::Arguments &args); + static v8::Handle Hide(const v8::Arguments &args); + static v8::Handle Maximize(const v8::Arguments &args); + static v8::Handle Unmaximize(const v8::Arguments &args); + static v8::Handle Minimize(const v8::Arguments &args); + static v8::Handle Restore(const v8::Arguments &args); + static v8::Handle SetFullscreen(const v8::Arguments &args); + static v8::Handle IsFullscreen(const v8::Arguments &args); + static v8::Handle SetSize(const v8::Arguments &args); + static v8::Handle GetSize(const v8::Arguments &args); + static v8::Handle SetMinimumSize(const v8::Arguments &args); + static v8::Handle SetMaximumSize(const v8::Arguments &args); + static v8::Handle SetResizable(const v8::Arguments &args); + static v8::Handle SetAlwaysOnTop(const v8::Arguments &args); + static v8::Handle SetPosition(const v8::Arguments &args); + static v8::Handle GetPosition(const v8::Arguments &args); + static v8::Handle SetTitle(const v8::Arguments &args); + static v8::Handle FlashFrame(const v8::Arguments &args); + static v8::Handle SetKiosk(const v8::Arguments &args); + static v8::Handle IsKiosk(const v8::Arguments &args); + static v8::Handle ShowDevTools(const v8::Arguments &args); + static v8::Handle CloseDevTools(const v8::Arguments &args); scoped_ptr window_; diff --git a/browser/default_app/main.js b/browser/default_app/main.js index 35e9881ac2a3..f0dd86bbf3f2 100644 --- a/browser/default_app/main.js +++ b/browser/default_app/main.js @@ -5,4 +5,8 @@ var mainWindow = null; atom.browserMainParts.preMainMessageLoopRun = function() { mainWindow = new Window({ width: 800, height: 600 }); + + setTimeout(function() { + mainWindow.destroy(); + }, 10000); } diff --git a/browser/native_window.cc b/browser/native_window.cc index 49d77f7d9138..8bf4433eb32e 100644 --- a/browser/native_window.cc +++ b/browser/native_window.cc @@ -43,12 +43,12 @@ void NativeWindow::InitFromOptions(base::DictionaryValue* options) { int min_height, min_width; if (options->GetInteger(switches::kMinHeight, &min_height) && options->GetInteger(switches::kMinWidth, &min_width)) { - SetMinimumSize(min_width, min_height); + SetMinimumSize(gfx::Size(min_width, min_height)); } int max_height, max_width; if (options->GetInteger(switches::kMaxHeight, &max_height) && options->GetInteger(switches::kMaxWidth, &max_width)) { - SetMaximumSize(max_width, max_height); + SetMaximumSize(gfx::Size(max_width, max_height)); } bool resizable; if (options->GetBoolean(switches::kResizable, &resizable)) { diff --git a/browser/native_window.h b/browser/native_window.h index 11a4443c4faf..ec2c3b865b40 100644 --- a/browser/native_window.h +++ b/browser/native_window.h @@ -55,8 +55,8 @@ class NativeWindow : public content::WebContentsDelegate { virtual bool IsFullscreen() = 0; virtual void SetSize(const gfx::Size& size) = 0; virtual gfx::Size GetSize() = 0; - virtual void SetMinimumSize(int width, int height) = 0; - virtual void SetMaximumSize(int width, int height) = 0; + virtual void SetMinimumSize(const gfx::Size& size) = 0; + virtual void SetMaximumSize(const gfx::Size& size) = 0; virtual void SetResizable(bool resizable) = 0; virtual void SetAlwaysOnTop(bool top) = 0; virtual void SetPosition(const std::string& position) = 0; diff --git a/browser/native_window_mac.h b/browser/native_window_mac.h index a10f5c74ce68..af1ec253e125 100644 --- a/browser/native_window_mac.h +++ b/browser/native_window_mac.h @@ -32,8 +32,8 @@ class NativeWindowMac : public NativeWindow { virtual bool IsFullscreen() OVERRIDE; virtual void SetSize(const gfx::Size& size) OVERRIDE; virtual gfx::Size GetSize() OVERRIDE; - virtual void SetMinimumSize(int width, int height) OVERRIDE; - virtual void SetMaximumSize(int width, int height) OVERRIDE; + virtual void SetMinimumSize(const gfx::Size& size) OVERRIDE; + virtual void SetMaximumSize(const gfx::Size& size) OVERRIDE; virtual void SetResizable(bool resizable) OVERRIDE; virtual void SetAlwaysOnTop(bool top) OVERRIDE; virtual void SetPosition(const std::string& position) OVERRIDE; diff --git a/browser/native_window_mac.mm b/browser/native_window_mac.mm index 8ee1d5d3ab8a..9f33b4be98a5 100644 --- a/browser/native_window_mac.mm +++ b/browser/native_window_mac.mm @@ -84,6 +84,8 @@ NativeWindowMac::NativeWindowMac(content::BrowserContext* browser_context, } NativeWindowMac::~NativeWindowMac() { + Close(); + [window() release]; } void NativeWindowMac::Close() { @@ -216,14 +218,14 @@ gfx::Size NativeWindowMac::GetSize() { return gfx::Size(frame.size.width, frame.size.height); } -void NativeWindowMac::SetMinimumSize(int width, int height) { - NSSize min_size = NSMakeSize(width, height); +void NativeWindowMac::SetMinimumSize(const gfx::Size& size) { + NSSize min_size = NSMakeSize(size.width(), size.height()); NSView* content = [window() contentView]; [window() setContentMinSize:[content convertSize:min_size toView:nil]]; } -void NativeWindowMac::SetMaximumSize(int width, int height) { - NSSize max_size = NSMakeSize(width, height); +void NativeWindowMac::SetMaximumSize(const gfx::Size& size) { + NSSize max_size = NSMakeSize(size.width(), size.height()); NSView* content = [window() contentView]; [window() setContentMaxSize:[content convertSize:max_size toView:nil]]; } diff --git a/vendor/node b/vendor/node index 44cac2fd2486..bfdcb5bae8f0 160000 --- a/vendor/node +++ b/vendor/node @@ -1 +1 @@ -Subproject commit 44cac2fd2486151f13b7ce3f033158154eedbfbd +Subproject commit bfdcb5bae8f01b877fbad84bb260a53ba7ca1e92