electron/atom/browser/api/atom_api_screen.cc

147 lines
4.2 KiB
C++
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
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.
2015-01-15 00:19:22 +00:00
#include "atom/browser/api/atom_api_screen.h"
#include <algorithm>
2015-01-16 20:02:32 +00:00
#include <string>
2015-01-15 00:19:22 +00:00
#include "atom/browser/browser.h"
2014-10-24 04:48:52 +00:00
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "base/bind.h"
#include "native_mate/dictionary.h"
2015-01-15 00:19:22 +00:00
#include "native_mate/object_template_builder.h"
#include "ui/gfx/screen.h"
#include "atom/common/node_includes.h"
2014-01-07 12:00:25 +00:00
2015-01-15 00:19:22 +00:00
namespace atom {
namespace api {
namespace {
2015-01-16 20:02:32 +00:00
// Find an item in container according to its ID.
template<class T>
typename T::iterator FindById(T* container, int id) {
auto predicate = [id] (const typename T::value_type& item) -> bool {
return item.id() == id;
};
return std::find_if(container->begin(), container->end(), predicate);
}
2015-01-16 20:02:32 +00:00
// Convert the changed_metrics bitmask to string array.
std::vector<std::string> MetricsToArray(uint32_t metrics) {
std::vector<std::string> array;
if (metrics & gfx::DisplayObserver::DISPLAY_METRIC_BOUNDS)
array.push_back("bounds");
if (metrics & gfx::DisplayObserver::DISPLAY_METRIC_WORK_AREA)
array.push_back("workArea");
if (metrics & gfx::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR)
array.push_back("scaleFactor");
if (metrics & gfx::DisplayObserver::DISPLAY_METRIC_ROTATION)
array.push_back("rotaion");
return array;
}
} // namespace
2015-01-15 00:19:22 +00:00
Screen::Screen(gfx::Screen* screen) : screen_(screen) {
displays_ = screen_->GetAllDisplays();
screen_->AddObserver(this);
2015-01-15 00:19:22 +00:00
}
Screen::~Screen() {
screen_->RemoveObserver(this);
2015-01-15 00:19:22 +00:00
}
gfx::Point Screen::GetCursorScreenPoint() {
return screen_->GetCursorScreenPoint();
}
gfx::Display Screen::GetPrimaryDisplay() {
return screen_->GetPrimaryDisplay();
}
std::vector<gfx::Display> Screen::GetAllDisplays() {
return displays_;
2015-01-15 00:19:22 +00:00
}
gfx::Display Screen::GetDisplayNearestPoint(const gfx::Point& point) {
return screen_->GetDisplayNearestPoint(point);
}
gfx::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) {
return screen_->GetDisplayMatching(match_rect);
}
void Screen::OnDisplayAdded(const gfx::Display& new_display) {
displays_.push_back(new_display);
2015-01-16 20:02:32 +00:00
Emit("display-added", new_display);
2015-01-15 00:19:22 +00:00
}
void Screen::OnDisplayRemoved(const gfx::Display& old_display) {
auto iter = FindById(&displays_, old_display.id());
if (iter == displays_.end())
return;
displays_.erase(iter);
2015-01-16 20:02:32 +00:00
Emit("display-removed", old_display);
2015-01-15 00:19:22 +00:00
}
void Screen::OnDisplayMetricsChanged(const gfx::Display& display,
uint32_t changed_metrics) {
auto iter = FindById(&displays_, display.id());
if (iter == displays_.end())
return;
*iter = display;
2015-01-16 20:02:32 +00:00
Emit("display-metrics-changed", display, MetricsToArray(changed_metrics));
2015-01-15 00:19:22 +00:00
}
mate::ObjectTemplateBuilder Screen::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
.SetMethod("getCursorScreenPoint", &Screen::GetCursorScreenPoint)
.SetMethod("getPrimaryDisplay", &Screen::GetPrimaryDisplay)
.SetMethod("getAllDisplays", &Screen::GetAllDisplays)
.SetMethod("getDisplayNearestPoint", &Screen::GetDisplayNearestPoint)
.SetMethod("getDisplayMatching", &Screen::GetDisplayMatching);
2015-01-15 00:19:22 +00:00
}
// static
2015-05-22 11:11:22 +00:00
v8::Local<v8::Value> Screen::Create(v8::Isolate* isolate) {
if (!Browser::Get()->is_ready()) {
2015-09-07 08:12:31 +00:00
isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
isolate,
"Cannot initialize \"screen\" module before app is ready")));
2015-03-26 03:34:41 +00:00
return v8::Null(isolate);
}
2015-01-15 00:19:22 +00:00
gfx::Screen* screen = gfx::Screen::GetNativeScreen();
if (!screen) {
2015-09-07 08:12:31 +00:00
isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
isolate, "Failed to get screen information")));
2015-03-26 03:34:41 +00:00
return v8::Null(isolate);
2015-01-15 00:19:22 +00:00
}
return mate::CreateHandle(isolate, new Screen(screen)).ToV8();
2015-01-15 00:19:22 +00:00
}
} // namespace api
} // namespace atom
namespace {
2014-01-07 12:15:55 +00:00
2015-05-22 11:11:22 +00:00
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
2015-01-15 00:19:22 +00:00
dict.Set("screen", atom::api::Screen::Create(context->GetIsolate()));
2014-01-07 12:00:25 +00:00
}
} // namespace
2014-01-07 12:00:25 +00:00
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_common_screen, Initialize)