Merge pull request #152 from atom/gfx-module

Add screen module
This commit is contained in:
Cheng Zhao 2014-01-07 04:46:24 -08:00
commit 367285f3f3
18 changed files with 222 additions and 33 deletions

View file

@ -28,6 +28,7 @@
'common/api/lib/clipboard.coffee', 'common/api/lib/clipboard.coffee',
'common/api/lib/crash-reporter.coffee', 'common/api/lib/crash-reporter.coffee',
'common/api/lib/id-weak-map.coffee', 'common/api/lib/id-weak-map.coffee',
'common/api/lib/screen.coffee',
'common/api/lib/shell.coffee', 'common/api/lib/shell.coffee',
'renderer/lib/init.coffee', 'renderer/lib/init.coffee',
'renderer/api/lib/ipc.coffee', 'renderer/api/lib/ipc.coffee',
@ -47,8 +48,6 @@
'browser/api/atom_api_dialog.h', 'browser/api/atom_api_dialog.h',
'browser/api/atom_api_event.cc', 'browser/api/atom_api_event.cc',
'browser/api/atom_api_event.h', 'browser/api/atom_api_event.h',
'browser/api/atom_api_event_emitter.cc',
'browser/api/atom_api_event_emitter.h',
'browser/api/atom_api_menu.cc', 'browser/api/atom_api_menu.cc',
'browser/api/atom_api_menu.h', 'browser/api/atom_api_menu.h',
'browser/api/atom_api_menu_mac.h', 'browser/api/atom_api_menu_mac.h',
@ -131,8 +130,12 @@
'common/api/atom_api_clipboard.h', 'common/api/atom_api_clipboard.h',
'common/api/atom_api_crash_reporter.cc', 'common/api/atom_api_crash_reporter.cc',
'common/api/atom_api_crash_reporter.h', 'common/api/atom_api_crash_reporter.h',
'common/api/atom_api_event_emitter.cc',
'common/api/atom_api_event_emitter.h',
'common/api/atom_api_id_weak_map.cc', 'common/api/atom_api_id_weak_map.cc',
'common/api/atom_api_id_weak_map.h', '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.cc',
'common/api/atom_api_shell.h', 'common/api/atom_api_shell.h',
'common/api/atom_api_v8_util.cc', 'common/api/atom_api_v8_util.cc',

View file

@ -6,8 +6,8 @@
#define ATOM_BROWSER_API_ATOM_API_APP_H_ #define ATOM_BROWSER_API_ATOM_API_APP_H_
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "browser/api/atom_api_event_emitter.h"
#include "browser/browser_observer.h" #include "browser/browser_observer.h"
#include "common/api/atom_api_event_emitter.h"
namespace atom { namespace atom {

View file

@ -7,8 +7,8 @@
#include "base/callback.h" #include "base/callback.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "browser/api/atom_api_event_emitter.h"
#include "browser/auto_updater_delegate.h" #include "browser/auto_updater_delegate.h"
#include "common/api/atom_api_event_emitter.h"
namespace atom { namespace atom {

View file

@ -6,7 +6,7 @@
#define ATOM_BROWSER_API_ATOM_API_MENU_H_ #define ATOM_BROWSER_API_ATOM_API_MENU_H_
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "browser/api/atom_api_event_emitter.h" #include "common/api/atom_api_event_emitter.h"
#include "ui/base/models/simple_menu_model.h" #include "ui/base/models/simple_menu_model.h"
namespace atom { namespace atom {

View file

@ -5,10 +5,9 @@
#ifndef ATOM_BROWSER_API_ATOM_API_POWER_MONITOR_H_ #ifndef ATOM_BROWSER_API_ATOM_API_POWER_MONITOR_H_
#define ATOM_BROWSER_API_ATOM_API_POWER_MONITOR_H_ #define ATOM_BROWSER_API_ATOM_API_POWER_MONITOR_H_
#include "browser/api/atom_api_event_emitter.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/power_monitor/power_observer.h" #include "base/power_monitor/power_observer.h"
#include "common/api/atom_api_event_emitter.h"
namespace atom { namespace atom {

View file

@ -8,8 +8,8 @@
#include <vector> #include <vector>
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "browser/api/atom_api_event_emitter.h"
#include "browser/native_window_observer.h" #include "browser/native_window_observer.h"
#include "common/api/atom_api_event_emitter.h"
#include "common/v8/scoped_persistent.h" #include "common/v8/scoped_persistent.h"
namespace base { namespace base {

View file

@ -2,7 +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 "browser/api/atom_api_event_emitter.h" #include "common/api/atom_api_event_emitter.h"
#include <vector> #include <vector>

View file

@ -2,8 +2,8 @@
// 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.
#ifndef ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_ #ifndef ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_
#define ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_ #define ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_
#include <string> #include <string>
@ -40,4 +40,4 @@ class EventEmitter : public node::ObjectWrap {
} // namespace atom } // namespace atom
#endif // ATOM_BROWSER_API_ATOM_API_EVENT_EMITTER_H_ #endif // ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_

View file

@ -0,0 +1,86 @@
// 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<Screen>(args.This()); \
if (self == NULL) \
return node::ThrowError("Screen is already destroyed")
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()) {
}
Screen::~Screen() {
}
// static
void Screen::New(const v8::FunctionCallbackInfo<v8::Value>& 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<v8::Value>& args) {
UNWRAP_SCREEN_AND_CHECK;
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());
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
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());
}
} // namespace api
} // namespace atom
NODE_MODULE(atom_common_screen, atom::api::Screen::Initialize)

View file

@ -0,0 +1,44 @@
// 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<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_

View file

@ -27,6 +27,7 @@ NODE_EXT_LIST_ITEM(atom_renderer_ipc)
NODE_EXT_LIST_ITEM(atom_common_clipboard) NODE_EXT_LIST_ITEM(atom_common_clipboard)
NODE_EXT_LIST_ITEM(atom_common_crash_reporter) NODE_EXT_LIST_ITEM(atom_common_crash_reporter)
NODE_EXT_LIST_ITEM(atom_common_id_weak_map) 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_shell)
NODE_EXT_LIST_ITEM(atom_common_v8_util) NODE_EXT_LIST_ITEM(atom_common_v8_util)

View file

@ -0,0 +1,3 @@
{Screen} = process.atomBinding 'screen'
module.exports = new Screen

View file

@ -17,8 +17,9 @@
#include "common/swap_or_assign.h" #include "common/swap_or_assign.h"
#include "common/v8/scoped_persistent.h" #include "common/v8/scoped_persistent.h"
#include "common/v8/v8_value_converter.h" #include "common/v8/v8_value_converter.h"
#include "content/public/renderer/v8_value_converter.h" #include "ui/gfx/point.h"
#include "ui/gfx/rect.h" #include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
#include "url/gurl.h" #include "url/gurl.h"
// Convert V8 value to arbitrary supported types. // Convert V8 value to arbitrary supported types.
@ -71,8 +72,9 @@ struct FromV8Value {
converter->FromV8Value(value_, v8::Context::GetCurrent())); converter->FromV8Value(value_, v8::Context::GetCurrent()));
} }
operator std::vector<std::string>() { template<class T>
std::vector<std::string> array; operator std::vector<T>() {
std::vector<T> array;
v8::Handle<v8::Array> v8_array = v8::Handle<v8::Array>::Cast(value_); v8::Handle<v8::Array> v8_array = v8::Handle<v8::Array>::Cast(value_);
for (uint32_t i = 0; i < v8_array->Length(); ++i) for (uint32_t i = 0; i < v8_array->Length(); ++i)
array.push_back(FromV8Value(v8_array->Get(i))); array.push_back(FromV8Value(v8_array->Get(i)));
@ -80,14 +82,15 @@ struct FromV8Value {
return array; return array;
} }
operator std::map<std::string, std::string>() { template<class K, class V>
std::map<std::string, std::string> dict; operator std::map<K, V>() {
std::map<K, V> dict;
v8::Handle<v8::Object> v8_dict = value_->ToObject(); v8::Handle<v8::Object> v8_dict = value_->ToObject();
v8::Handle<v8::Array> v8_keys = v8_dict->GetOwnPropertyNames(); v8::Handle<v8::Array> v8_keys = v8_dict->GetOwnPropertyNames();
for (uint32_t i = 0; i < v8_keys->Length(); ++i) { for (uint32_t i = 0; i < v8_keys->Length(); ++i) {
v8::Handle<v8::Value> v8_key = v8_keys->Get(i); v8::Handle<v8::Value> v8_key = v8_keys->Get(i);
std::string key = FromV8Value(v8_key); K key = FromV8Value(v8_key);
dict[key] = std::string(FromV8Value(v8_dict->Get(v8_key))); dict[key] = V(FromV8Value(v8_dict->Get(v8_key)));
} }
return dict; return dict;
@ -121,6 +124,14 @@ inline v8::Handle<v8::Value> ToV8Value(bool b) {
return v8::Boolean::New(b); return v8::Boolean::New(b);
} }
inline v8::Handle<v8::Value> ToV8Value(float f) {
return v8::Number::New(f);
}
inline v8::Handle<v8::Value> ToV8Value(double f) {
return v8::Number::New(f);
}
inline v8::Handle<v8::Value> ToV8Value(const char* s) { inline v8::Handle<v8::Value> ToV8Value(const char* s) {
return v8::String::New(s); return v8::String::New(s);
} }
@ -146,14 +157,37 @@ inline v8::Handle<v8::Value> ToV8Value(void* whatever) {
return v8::Undefined(); return v8::Undefined();
} }
inline template<class T> inline
v8::Handle<v8::Value> ToV8Value(const std::vector<base::FilePath>& paths) { v8::Handle<v8::Value> ToV8Value(const std::vector<T>& arr) {
v8::Handle<v8::Array> result = v8::Array::New(paths.size()); v8::Handle<v8::Array> result = v8::Array::New(arr.size());
for (size_t i = 0; i < paths.size(); ++i) for (size_t i = 0; i < arr.size(); ++i)
result->Set(i, ToV8Value(paths[i])); result->Set(i, ToV8Value(arr[i]));
return result; return result;
} }
inline v8::Handle<v8::Value> ToV8Value(const gfx::Point& point) {
v8::Handle<v8::Object> obj = v8::Object::New();
obj->Set(ToV8Value("x"), ToV8Value(point.x()));
obj->Set(ToV8Value("y"), ToV8Value(point.y()));
return obj;
}
inline v8::Handle<v8::Value> ToV8Value(const gfx::Rect& rect) {
v8::Handle<v8::Object> obj = v8::Object::New();
obj->Set(ToV8Value("x"), ToV8Value(rect.x()));
obj->Set(ToV8Value("y"), ToV8Value(rect.y()));
obj->Set(ToV8Value("width"), ToV8Value(rect.width()));
obj->Set(ToV8Value("height"), ToV8Value(rect.height()));
return obj;
}
inline v8::Handle<v8::Value> ToV8Value(const gfx::Size& size) {
v8::Handle<v8::Object> obj = v8::Object::New();
obj->Set(ToV8Value("width"), ToV8Value(size.width()));
obj->Set(ToV8Value("height"), ToV8Value(size.height()));
return obj;
}
// Check if a V8 Value is of specified type. // Check if a V8 Value is of specified type.
template<class T> inline template<class T> inline
bool V8ValueCanBeConvertedTo(v8::Handle<v8::Value> value) { bool V8ValueCanBeConvertedTo(v8::Handle<v8::Value> value) {

View file

@ -35,4 +35,5 @@ Common modules:
* [clipboard](api/common/clipboard.md) * [clipboard](api/common/clipboard.md)
* [crash-reporter](api/common/crash-reporter.md) * [crash-reporter](api/common/crash-reporter.md)
* [screen](api/common/screen.md)
* [shell](api/common/shell.md) * [shell](api/common/shell.md)

11
docs/api/common/screen.md Normal file
View file

@ -0,0 +1,11 @@
# screen
Gets various info about screen size, displays, cursor position, etc.
## screen.getCursorScreenPoint()
Returns the current absolute position of the mouse pointer.
## screen.getPrimaryDisplay()
Returns the primary display.

View file

@ -0,0 +1,16 @@
assert = require 'assert'
screen = require 'screen'
describe 'screen module', ->
describe 'screen.getCursorScreenPoint()', ->
it 'returns a point object', ->
point = screen.getCursorScreenPoint()
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

View file

@ -6,14 +6,6 @@
</head> </head>
<body> <body>
<style type="text/css" media="screen">
.text:active {
-webkit-backface-visibility: hidden;
}
</style>
<div class="text">Welcome to Husk, the Atom shell example app.</div>
<div id="mocha"></div> <div id="mocha"></div>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">

View file

@ -138,7 +138,6 @@ app.on('ready', function() {
window = new BrowserWindow({ window = new BrowserWindow({
title: 'atom-shell tests', title: 'atom-shell tests',
show: false, show: false,
frame: false,
width: 800, width: 800,
height: 600 height: 600
}); });