Use native_mate to simplify screen api.
This commit is contained in:
parent
c25911db0d
commit
16e3397c32
3 changed files with 60 additions and 109 deletions
|
@ -2,9 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
#include "atom/common/api/atom_api_screen.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
|
|
||||||
#include "atom/common/v8/native_type_conversions.h"
|
|
||||||
#include "ui/gfx/screen.h"
|
#include "ui/gfx/screen.h"
|
||||||
|
|
||||||
#include "atom/common/v8/node_common.h"
|
#include "atom/common/v8/node_common.h"
|
||||||
|
@ -16,14 +14,7 @@
|
||||||
#include "ui/gfx/gtk_util.h"
|
#include "ui/gfx/gtk_util.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define UNWRAP_SCREEN_AND_CHECK \
|
namespace mate {
|
||||||
Screen* self = ObjectWrap::Unwrap<Screen>(args.This()); \
|
|
||||||
if (self == NULL) \
|
|
||||||
return node::ThrowError("Screen is already destroyed")
|
|
||||||
|
|
||||||
namespace atom {
|
|
||||||
|
|
||||||
namespace api {
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
@ -42,70 +33,76 @@ gfx::Display AdaptToWindowManager(const gfx::Display& display) {
|
||||||
return changed;
|
return changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
v8::Handle<v8::Object> DisplayToV8Value(const gfx::Display& raw) {
|
|
||||||
gfx::Display display(AdaptToWindowManager(raw));
|
|
||||||
v8::Handle<v8::Object> obj = v8::Object::New();
|
|
||||||
obj->Set(ToV8Value("bounds"), ToV8Value(display.bounds()));
|
|
||||||
obj->Set(ToV8Value("workArea"), ToV8Value(display.work_area()));
|
|
||||||
obj->Set(ToV8Value("size"), ToV8Value(display.size()));
|
|
||||||
obj->Set(ToV8Value("workAreaSize"), ToV8Value(display.work_area_size()));
|
|
||||||
obj->Set(ToV8Value("scaleFactor"), ToV8Value(display.device_scale_factor()));
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
Screen::Screen(v8::Handle<v8::Object> wrapper)
|
template<>
|
||||||
: EventEmitter(wrapper),
|
struct Converter<gfx::Point> {
|
||||||
screen_(gfx::Screen::GetNativeScreen()) {
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Screen::~Screen() {
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// static
|
template<>
|
||||||
void Screen::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
struct Converter<gfx::Rect> {
|
||||||
v8::HandleScope scope(args.GetIsolate());
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (!args.IsConstructCall())
|
template<>
|
||||||
return node::ThrowError("Require constructor call");
|
struct Converter<gfx::Display> {
|
||||||
|
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||||
|
const gfx::Display& val) {
|
||||||
|
gfx::Display display(AdaptToWindowManager(val));
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
new Screen(args.This());
|
} // namespace mate
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
namespace {
|
||||||
void Screen::GetCursorScreenPoint(
|
|
||||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
UNWRAP_SCREEN_AND_CHECK;
|
|
||||||
args.GetReturnValue().Set(ToV8Value(self->screen_->GetCursorScreenPoint()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
void Initialize(v8::Handle<v8::Object> exports) {
|
||||||
void Screen::GetPrimaryDisplay(
|
|
||||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
||||||
UNWRAP_SCREEN_AND_CHECK;
|
|
||||||
gfx::Display display = self->screen_->GetPrimaryDisplay();
|
|
||||||
args.GetReturnValue().Set(DisplayToV8Value(display));
|
|
||||||
}
|
|
||||||
|
|
||||||
// static
|
|
||||||
void Screen::Initialize(v8::Handle<v8::Object> target) {
|
|
||||||
#if defined(TOOLKIT_GTK)
|
#if defined(TOOLKIT_GTK)
|
||||||
gfx::GdkInitFromCommandLine(*CommandLine::ForCurrentProcess());
|
gfx::GdkInitFromCommandLine(*CommandLine::ForCurrentProcess());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
|
gfx::Screen* screen = gfx::Screen::GetNativeScreen();
|
||||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
|
||||||
t->SetClassName(v8::String::NewSymbol("Screen"));
|
|
||||||
|
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "getCursorScreenPoint", GetCursorScreenPoint);
|
mate::ObjectTemplateBuilder builder(v8::Isolate::GetCurrent());
|
||||||
NODE_SET_PROTOTYPE_METHOD(t, "getPrimaryDisplay", GetPrimaryDisplay);
|
builder.SetMethod("getCursorScreenPoint",
|
||||||
|
base::Bind(&gfx::Screen::GetCursorScreenPoint,
|
||||||
target->Set(v8::String::NewSymbol("Screen"), t->GetFunction());
|
base::Unretained(screen)))
|
||||||
|
.SetMethod("getPrimaryDisplay",
|
||||||
|
base::Bind(&gfx::Screen::GetPrimaryDisplay,
|
||||||
|
base::Unretained(screen)));
|
||||||
|
exports->SetPrototype(builder.Build()->NewInstance());
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace
|
||||||
|
|
||||||
} // namespace atom
|
NODE_MODULE(atom_common_screen, Initialize)
|
||||||
|
|
||||||
NODE_MODULE(atom_common_screen, atom::api::Screen::Initialize)
|
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style license that can be
|
|
||||||
// found in the LICENSE file.
|
|
||||||
|
|
||||||
#ifndef ATOM_COMMON_API_ATOM_API_SCREEN_H_
|
|
||||||
#define ATOM_COMMON_API_ATOM_API_SCREEN_H_
|
|
||||||
|
|
||||||
#include "atom/common/api/atom_api_event_emitter.h"
|
|
||||||
|
|
||||||
namespace gfx {
|
|
||||||
class Screen;
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace atom {
|
|
||||||
|
|
||||||
namespace api {
|
|
||||||
|
|
||||||
class Screen : public EventEmitter {
|
|
||||||
public:
|
|
||||||
virtual ~Screen();
|
|
||||||
|
|
||||||
static void Initialize(v8::Handle<v8::Object> target);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
explicit Screen(v8::Handle<v8::Object> wrapper);
|
|
||||||
|
|
||||||
private:
|
|
||||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
|
|
||||||
static void GetCursorScreenPoint(
|
|
||||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
static void GetPrimaryDisplay(
|
|
||||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
||||||
|
|
||||||
gfx::Screen* screen_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(Screen);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace api
|
|
||||||
|
|
||||||
} // namespace atom
|
|
||||||
|
|
||||||
#endif // ATOM_COMMON_API_ATOM_API_SCREEN_H_
|
|
|
@ -1,3 +1 @@
|
||||||
{Screen} = process.atomBinding 'screen'
|
module.exports = process.atomBinding 'screen'
|
||||||
|
|
||||||
module.exports = new Screen
|
|
||||||
|
|
Loading…
Reference in a new issue