electron/atom/common/api/atom_api_screen.cc

107 lines
3.5 KiB
C++
Raw Normal View History

2014-01-07 12:00:25 +00:00
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
2014-01-07 12:00:25 +00:00
// found in the LICENSE file.
2014-04-16 07:31:59 +00:00
#include "native_mate/dictionary.h"
2014-01-07 12:00:25 +00:00
#include "ui/gfx/screen.h"
#include "atom/common/node_includes.h"
2014-01-07 12:00:25 +00:00
#if defined(TOOLKIT_GTK)
#include "base/command_line.h"
#include "base/environment.h"
#include "base/nix/xdg_util.h"
#include "ui/gfx/gtk_util.h"
#endif
namespace mate {
2014-01-07 12:00:25 +00:00
2014-01-07 12:15:55 +00:00
namespace {
gfx::Display AdaptToWindowManager(const gfx::Display& display) {
gfx::Display changed(display);
#if defined(TOOLKIT_GTK)
scoped_ptr<base::Environment> env(base::Environment::Create());
base::nix::DesktopEnvironment de(base::nix::GetDesktopEnvironment(env.get()));
if (de == base::nix::DESKTOP_ENVIRONMENT_UNITY) {
// Unity's 24px global menu bar should not be included in the work area.
gfx::Rect rect(changed.work_area());
rect.set_height(rect.height() - 24);
changed.set_work_area(rect);
}
#endif
return changed;
}
2014-01-07 12:15:55 +00:00
} // namespace
template<>
struct Converter<gfx::Point> {
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();
}
};
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();
}
};
template<>
struct Converter<gfx::Rect> {
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();
}
};
template<>
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();
}
};
2014-01-07 12:00:25 +00:00
} // namespace mate
2014-01-07 12:00:25 +00:00
namespace {
2014-01-07 12:15:55 +00:00
void Initialize(v8::Handle<v8::Object> exports) {
#if defined(TOOLKIT_GTK)
gfx::GdkInitFromCommandLine(*CommandLine::ForCurrentProcess());
#endif
gfx::Screen* screen = gfx::Screen::GetNativeScreen();
2014-04-16 07:31:59 +00:00
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
dict.SetMethod("getCursorScreenPoint",
base::Bind(&gfx::Screen::GetCursorScreenPoint,
base::Unretained(screen)));
dict.SetMethod("getPrimaryDisplay",
base::Bind(&gfx::Screen::GetPrimaryDisplay,
base::Unretained(screen)));
2014-01-07 12:00:25 +00:00
}
} // namespace
2014-01-07 12:00:25 +00:00
NODE_MODULE(atom_common_screen, Initialize)