From 7253a354551115c6c820083063e998800b314950 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 7 Jan 2014 20:00:25 +0800 Subject: [PATCH] Add initial screen module. --- atom.gyp | 3 ++ common/api/atom_api_screen.cc | 63 +++++++++++++++++++++++++++++++++++ common/api/atom_api_screen.h | 42 +++++++++++++++++++++++ common/api/atom_extensions.h | 1 + common/api/lib/screen.coffee | 3 ++ spec/api-screen-spec.coffee | 9 +++++ 6 files changed, 121 insertions(+) create mode 100644 common/api/atom_api_screen.cc create mode 100644 common/api/atom_api_screen.h create mode 100644 common/api/lib/screen.coffee create mode 100644 spec/api-screen-spec.coffee diff --git a/atom.gyp b/atom.gyp index c1d0b8eb..f9b4a011 100644 --- a/atom.gyp +++ b/atom.gyp @@ -28,6 +28,7 @@ 'common/api/lib/clipboard.coffee', 'common/api/lib/crash-reporter.coffee', 'common/api/lib/id-weak-map.coffee', + 'common/api/lib/screen.coffee', 'common/api/lib/shell.coffee', 'renderer/lib/init.coffee', 'renderer/api/lib/ipc.coffee', @@ -133,6 +134,8 @@ 'common/api/atom_api_event_emitter.h', 'common/api/atom_api_id_weak_map.cc', 'common/api/atom_api_id_weak_map.h', + 'common/api/atom_api_screen.cc', + 'common/api/atom_api_screen.h', 'common/api/atom_api_shell.cc', 'common/api/atom_api_shell.h', 'common/api/atom_api_v8_util.cc', diff --git a/common/api/atom_api_screen.cc b/common/api/atom_api_screen.cc new file mode 100644 index 00000000..995a5847 --- /dev/null +++ b/common/api/atom_api_screen.cc @@ -0,0 +1,63 @@ +// 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. + +#include "common/api/atom_api_screen.h" + +#include "common/v8/native_type_conversions.h" +#include "ui/gfx/screen.h" + +#include "common/v8/node_common.h" + +#define UNWRAP_SCREEN_AND_CHECK \ + Screen* self = ObjectWrap::Unwrap(args.This()); \ + if (self == NULL) \ + return node::ThrowError("Screen is already destroyed") + +namespace atom { + +namespace api { + +Screen::Screen(v8::Handle wrapper) + : EventEmitter(wrapper), + screen_(gfx::Screen::GetNativeScreen()) { +} + +Screen::~Screen() { +} + +// static +void Screen::New(const v8::FunctionCallbackInfo& args) { + v8::HandleScope scope(args.GetIsolate()); + + if (!args.IsConstructCall()) + return node::ThrowError("Require constructor call"); + + new Screen(args.This()); +} + +// static +void Screen::GetCursorScreenPoint( + const v8::FunctionCallbackInfo& args) { + UNWRAP_SCREEN_AND_CHECK; + args.GetReturnValue().Set(ToV8Value(self->screen_->GetCursorScreenPoint())); +} + +// static +void Screen::Initialize(v8::Handle target) { + v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); + + v8::Local t = v8::FunctionTemplate::New(New); + t->InstanceTemplate()->SetInternalFieldCount(1); + t->SetClassName(v8::String::NewSymbol("Screen")); + + NODE_SET_PROTOTYPE_METHOD(t, "getCursorScreenPoint", GetCursorScreenPoint); + + target->Set(v8::String::NewSymbol("Screen"), t->GetFunction()); +} + +} // namespace api + +} // namespace atom + +NODE_MODULE(atom_common_screen, atom::api::Screen::Initialize) diff --git a/common/api/atom_api_screen.h b/common/api/atom_api_screen.h new file mode 100644 index 00000000..7b1ad3be --- /dev/null +++ b/common/api/atom_api_screen.h @@ -0,0 +1,42 @@ +// 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 "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 target); + + protected: + explicit Screen(v8::Handle wrapper); + + private: + static void New(const v8::FunctionCallbackInfo& args); + + static void GetCursorScreenPoint( + const v8::FunctionCallbackInfo& args); + + gfx::Screen* screen_; + + DISALLOW_COPY_AND_ASSIGN(Screen); +}; + +} // namespace api + +} // namespace atom + +#endif // ATOM_COMMON_API_ATOM_API_SCREEN_H_ diff --git a/common/api/atom_extensions.h b/common/api/atom_extensions.h index c373d49a..5bba348c 100644 --- a/common/api/atom_extensions.h +++ b/common/api/atom_extensions.h @@ -27,6 +27,7 @@ NODE_EXT_LIST_ITEM(atom_renderer_ipc) NODE_EXT_LIST_ITEM(atom_common_clipboard) NODE_EXT_LIST_ITEM(atom_common_crash_reporter) NODE_EXT_LIST_ITEM(atom_common_id_weak_map) +NODE_EXT_LIST_ITEM(atom_common_screen) NODE_EXT_LIST_ITEM(atom_common_shell) NODE_EXT_LIST_ITEM(atom_common_v8_util) diff --git a/common/api/lib/screen.coffee b/common/api/lib/screen.coffee new file mode 100644 index 00000000..fa859f1a --- /dev/null +++ b/common/api/lib/screen.coffee @@ -0,0 +1,3 @@ +{Screen} = process.atomBinding 'screen' + +module.exports = new Screen diff --git a/spec/api-screen-spec.coffee b/spec/api-screen-spec.coffee new file mode 100644 index 00000000..a3803fb1 --- /dev/null +++ b/spec/api-screen-spec.coffee @@ -0,0 +1,9 @@ +assert = require 'assert' +screen = require 'screen' + +describe 'screen module', -> + describe 'screen.getCursorScreenPoint()', -> + it 'returns a point object', -> + point = screen.getCursorScreenPoint() + assert typeof(point.x), 'number' + assert typeof(point.y), 'number' -- 2.43.0