Add screen.getPrimaryDisplay() API.

This commit is contained in:
Cheng Zhao 2014-01-07 20:15:55 +08:00
parent e09a2ff811
commit 4ae2738ce5
3 changed files with 34 additions and 2 deletions

View file

@ -18,6 +18,20 @@ namespace atom {
namespace api {
namespace {
v8::Handle<v8::Object> DisplayToV8Value(const gfx::Display& display) {
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
Screen::Screen(v8::Handle<v8::Object> wrapper)
: EventEmitter(wrapper),
screen_(gfx::Screen::GetNativeScreen()) {
@ -43,6 +57,14 @@ void Screen::GetCursorScreenPoint(
args.GetReturnValue().Set(ToV8Value(self->screen_->GetCursorScreenPoint()));
}
// static
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) {
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
@ -52,6 +74,7 @@ void Screen::Initialize(v8::Handle<v8::Object> target) {
t->SetClassName(v8::String::NewSymbol("Screen"));
NODE_SET_PROTOTYPE_METHOD(t, "getCursorScreenPoint", GetCursorScreenPoint);
NODE_SET_PROTOTYPE_METHOD(t, "getPrimaryDisplay", GetPrimaryDisplay);
target->Set(v8::String::NewSymbol("Screen"), t->GetFunction());
}

View file

@ -29,6 +29,8 @@ class Screen : public EventEmitter {
static void GetCursorScreenPoint(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPrimaryDisplay(
const v8::FunctionCallbackInfo<v8::Value>& args);
gfx::Screen* screen_;

View file

@ -5,5 +5,12 @@ describe 'screen module', ->
describe 'screen.getCursorScreenPoint()', ->
it 'returns a point object', ->
point = screen.getCursorScreenPoint()
assert typeof(point.x), 'number'
assert typeof(point.y), 'number'
assert.equal typeof(point.x), 'number'
assert.equal typeof(point.y), 'number'
describe 'screen.getPrimaryDisplay()', ->
it 'returns a display object', ->
display = screen.getPrimaryDisplay()
assert.equal typeof(display.scaleFactor), 'number'
assert display.size.width > 0
assert display.size.height > 0