From 4ae2738ce5d559994dca4954e951a6855460b9f0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 7 Jan 2014 20:15:55 +0800 Subject: [PATCH] Add screen.getPrimaryDisplay() API. --- common/api/atom_api_screen.cc | 23 +++++++++++++++++++++++ common/api/atom_api_screen.h | 2 ++ spec/api-screen-spec.coffee | 11 +++++++++-- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/common/api/atom_api_screen.cc b/common/api/atom_api_screen.cc index 995a58476ed8..329767271f08 100644 --- a/common/api/atom_api_screen.cc +++ b/common/api/atom_api_screen.cc @@ -18,6 +18,20 @@ namespace atom { namespace api { +namespace { + +v8::Handle DisplayToV8Value(const gfx::Display& display) { + v8::Handle 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 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& args) { + UNWRAP_SCREEN_AND_CHECK; + gfx::Display display = self->screen_->GetPrimaryDisplay(); + args.GetReturnValue().Set(DisplayToV8Value(display)); +} + // static void Screen::Initialize(v8::Handle target) { v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); @@ -52,6 +74,7 @@ void Screen::Initialize(v8::Handle 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()); } diff --git a/common/api/atom_api_screen.h b/common/api/atom_api_screen.h index 7b1ad3befe28..df762f3e8d60 100644 --- a/common/api/atom_api_screen.h +++ b/common/api/atom_api_screen.h @@ -29,6 +29,8 @@ class Screen : public EventEmitter { static void GetCursorScreenPoint( const v8::FunctionCallbackInfo& args); + static void GetPrimaryDisplay( + const v8::FunctionCallbackInfo& args); gfx::Screen* screen_; diff --git a/spec/api-screen-spec.coffee b/spec/api-screen-spec.coffee index a3803fb158e7..341524fca81b 100644 --- a/spec/api-screen-spec.coffee +++ b/spec/api-screen-spec.coffee @@ -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