Expose interface of NativeWindow in javascript.

This commit is contained in:
Cheng Zhao 2013-04-17 22:49:49 +08:00
parent 60528e53ee
commit df50cc08d6
8 changed files with 325 additions and 11 deletions

View file

@ -9,8 +9,12 @@
#include "browser/native_window.h" #include "browser/native_window.h"
#include "common/v8_value_converter_impl.h" #include "common/v8_value_converter_impl.h"
#include "content/public/browser/web_contents.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 content::V8ValueConverter;
using node::ObjectWrap;
namespace atom { namespace atom {
@ -49,6 +53,256 @@ v8::Handle<v8::Value> Window::New(const v8::Arguments &args) {
return args.This(); return args.This();
} }
// static
v8::Handle<v8::Value> Window::Destroy(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
delete self;
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Close(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Close();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Focus(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Focus(args[0]->IsBoolean() ? args[0]->BooleanValue(): true);
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Show(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Show();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Hide(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Hide();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Maximize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Maximize();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Unmaximize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Unmaximize();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Minimize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Minimize();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Restore(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Restore();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::SetFullscreen(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::IsFullscreen(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
return v8::Boolean::New(self->window_->IsFullscreen());
}
// static
v8::Handle<v8::Value> Window::SetSize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::GetSize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
gfx::Size size = self->window_->GetSize();
v8::Handle<v8::Array> 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<v8::Value> Window::SetMinimumSize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::SetMaximumSize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::SetResizable(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::SetAlwaysOnTop(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::SetPosition(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::GetPosition(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
gfx::Point pos = self->window_->GetPosition();
v8::Handle<v8::Array> 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<v8::Value> Window::SetTitle(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::FlashFrame(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->FlashFrame(
args[0]->IsBoolean() ? args[0]->BooleanValue(): true);
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::SetKiosk(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(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<v8::Value> Window::IsKiosk(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
return v8::Boolean::New(self->window_->IsKiosk());
}
// static
v8::Handle<v8::Value> Window::ShowDevTools(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->ShowDevTools();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::CloseDevTools(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->CloseDevTools();
return v8::Undefined();
}
// static // static
void Window::Initialize(v8::Handle<v8::Object> target) { void Window::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope scope; v8::HandleScope scope;
@ -56,6 +310,33 @@ void Window::Initialize(v8::Handle<v8::Object> target) {
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(Window::New); v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(Window::New);
t->InstanceTemplate()->SetInternalFieldCount(1); t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("Window")); 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()); target->Set(v8::String::NewSymbol("Window"), t->GetFunction());
} }

View file

@ -31,6 +31,33 @@ class Window : public EventEmitter {
private: private:
static v8::Handle<v8::Value> New(const v8::Arguments &args); static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> Destroy(const v8::Arguments &args);
// APIs for NativeWindow.
static v8::Handle<v8::Value> Close(const v8::Arguments &args);
static v8::Handle<v8::Value> Focus(const v8::Arguments &args);
static v8::Handle<v8::Value> Show(const v8::Arguments &args);
static v8::Handle<v8::Value> Hide(const v8::Arguments &args);
static v8::Handle<v8::Value> Maximize(const v8::Arguments &args);
static v8::Handle<v8::Value> Unmaximize(const v8::Arguments &args);
static v8::Handle<v8::Value> Minimize(const v8::Arguments &args);
static v8::Handle<v8::Value> Restore(const v8::Arguments &args);
static v8::Handle<v8::Value> SetFullscreen(const v8::Arguments &args);
static v8::Handle<v8::Value> IsFullscreen(const v8::Arguments &args);
static v8::Handle<v8::Value> SetSize(const v8::Arguments &args);
static v8::Handle<v8::Value> GetSize(const v8::Arguments &args);
static v8::Handle<v8::Value> SetMinimumSize(const v8::Arguments &args);
static v8::Handle<v8::Value> SetMaximumSize(const v8::Arguments &args);
static v8::Handle<v8::Value> SetResizable(const v8::Arguments &args);
static v8::Handle<v8::Value> SetAlwaysOnTop(const v8::Arguments &args);
static v8::Handle<v8::Value> SetPosition(const v8::Arguments &args);
static v8::Handle<v8::Value> GetPosition(const v8::Arguments &args);
static v8::Handle<v8::Value> SetTitle(const v8::Arguments &args);
static v8::Handle<v8::Value> FlashFrame(const v8::Arguments &args);
static v8::Handle<v8::Value> SetKiosk(const v8::Arguments &args);
static v8::Handle<v8::Value> IsKiosk(const v8::Arguments &args);
static v8::Handle<v8::Value> ShowDevTools(const v8::Arguments &args);
static v8::Handle<v8::Value> CloseDevTools(const v8::Arguments &args);
scoped_ptr<NativeWindow> window_; scoped_ptr<NativeWindow> window_;

View file

@ -5,4 +5,8 @@ var mainWindow = null;
atom.browserMainParts.preMainMessageLoopRun = function() { atom.browserMainParts.preMainMessageLoopRun = function() {
mainWindow = new Window({ width: 800, height: 600 }); mainWindow = new Window({ width: 800, height: 600 });
setTimeout(function() {
mainWindow.destroy();
}, 10000);
} }

View file

@ -43,12 +43,12 @@ void NativeWindow::InitFromOptions(base::DictionaryValue* options) {
int min_height, min_width; int min_height, min_width;
if (options->GetInteger(switches::kMinHeight, &min_height) && if (options->GetInteger(switches::kMinHeight, &min_height) &&
options->GetInteger(switches::kMinWidth, &min_width)) { options->GetInteger(switches::kMinWidth, &min_width)) {
SetMinimumSize(min_width, min_height); SetMinimumSize(gfx::Size(min_width, min_height));
} }
int max_height, max_width; int max_height, max_width;
if (options->GetInteger(switches::kMaxHeight, &max_height) && if (options->GetInteger(switches::kMaxHeight, &max_height) &&
options->GetInteger(switches::kMaxWidth, &max_width)) { options->GetInteger(switches::kMaxWidth, &max_width)) {
SetMaximumSize(max_width, max_height); SetMaximumSize(gfx::Size(max_width, max_height));
} }
bool resizable; bool resizable;
if (options->GetBoolean(switches::kResizable, &resizable)) { if (options->GetBoolean(switches::kResizable, &resizable)) {

View file

@ -55,8 +55,8 @@ class NativeWindow : public content::WebContentsDelegate {
virtual bool IsFullscreen() = 0; virtual bool IsFullscreen() = 0;
virtual void SetSize(const gfx::Size& size) = 0; virtual void SetSize(const gfx::Size& size) = 0;
virtual gfx::Size GetSize() = 0; virtual gfx::Size GetSize() = 0;
virtual void SetMinimumSize(int width, int height) = 0; virtual void SetMinimumSize(const gfx::Size& size) = 0;
virtual void SetMaximumSize(int width, int height) = 0; virtual void SetMaximumSize(const gfx::Size& size) = 0;
virtual void SetResizable(bool resizable) = 0; virtual void SetResizable(bool resizable) = 0;
virtual void SetAlwaysOnTop(bool top) = 0; virtual void SetAlwaysOnTop(bool top) = 0;
virtual void SetPosition(const std::string& position) = 0; virtual void SetPosition(const std::string& position) = 0;

View file

@ -32,8 +32,8 @@ class NativeWindowMac : public NativeWindow {
virtual bool IsFullscreen() OVERRIDE; virtual bool IsFullscreen() OVERRIDE;
virtual void SetSize(const gfx::Size& size) OVERRIDE; virtual void SetSize(const gfx::Size& size) OVERRIDE;
virtual gfx::Size GetSize() OVERRIDE; virtual gfx::Size GetSize() OVERRIDE;
virtual void SetMinimumSize(int width, int height) OVERRIDE; virtual void SetMinimumSize(const gfx::Size& size) OVERRIDE;
virtual void SetMaximumSize(int width, int height) OVERRIDE; virtual void SetMaximumSize(const gfx::Size& size) OVERRIDE;
virtual void SetResizable(bool resizable) OVERRIDE; virtual void SetResizable(bool resizable) OVERRIDE;
virtual void SetAlwaysOnTop(bool top) OVERRIDE; virtual void SetAlwaysOnTop(bool top) OVERRIDE;
virtual void SetPosition(const std::string& position) OVERRIDE; virtual void SetPosition(const std::string& position) OVERRIDE;

View file

@ -84,6 +84,8 @@ NativeWindowMac::NativeWindowMac(content::BrowserContext* browser_context,
} }
NativeWindowMac::~NativeWindowMac() { NativeWindowMac::~NativeWindowMac() {
Close();
[window() release];
} }
void NativeWindowMac::Close() { void NativeWindowMac::Close() {
@ -216,14 +218,14 @@ gfx::Size NativeWindowMac::GetSize() {
return gfx::Size(frame.size.width, frame.size.height); return gfx::Size(frame.size.width, frame.size.height);
} }
void NativeWindowMac::SetMinimumSize(int width, int height) { void NativeWindowMac::SetMinimumSize(const gfx::Size& size) {
NSSize min_size = NSMakeSize(width, height); NSSize min_size = NSMakeSize(size.width(), size.height());
NSView* content = [window() contentView]; NSView* content = [window() contentView];
[window() setContentMinSize:[content convertSize:min_size toView:nil]]; [window() setContentMinSize:[content convertSize:min_size toView:nil]];
} }
void NativeWindowMac::SetMaximumSize(int width, int height) { void NativeWindowMac::SetMaximumSize(const gfx::Size& size) {
NSSize max_size = NSMakeSize(width, height); NSSize max_size = NSMakeSize(size.width(), size.height());
NSView* content = [window() contentView]; NSView* content = [window() contentView];
[window() setContentMaxSize:[content convertSize:max_size toView:nil]]; [window() setContentMaxSize:[content convertSize:max_size toView:nil]];
} }

2
vendor/node vendored

@ -1 +1 @@
Subproject commit 44cac2fd2486151f13b7ce3f033158154eedbfbd Subproject commit bfdcb5bae8f01b877fbad84bb260a53ba7ca1e92