2014-10-31 18:17:05 +00:00
|
|
|
// 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"
|
|
|
|
|
2015-01-16 19:39:25 +00:00
|
|
|
#include <algorithm>
|
2015-01-16 20:02:32 +00:00
|
|
|
#include <string>
|
2015-01-16 19:39:25 +00:00
|
|
|
|
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"
|
2015-01-14 22:38:51 +00:00
|
|
|
#include "base/bind.h"
|
|
|
|
#include "native_mate/dictionary.h"
|
2015-01-15 00:19:22 +00:00
|
|
|
#include "native_mate/object_template_builder.h"
|
2016-07-04 06:08:55 +00:00
|
|
|
#include "ui/display/display.h"
|
|
|
|
#include "ui/display/screen.h"
|
|
|
|
#include "ui/gfx/geometry/point.h"
|
2015-01-14 22:38:51 +00:00
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#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 {
|
|
|
|
|
2015-01-16 19:39:25 +00:00
|
|
|
namespace {
|
|
|
|
|
2015-01-16 20:02:32 +00:00
|
|
|
// Find an item in container according to its ID.
|
2018-04-18 01:55:30 +00:00
|
|
|
template <class T>
|
2015-01-16 19:39:25 +00:00
|
|
|
typename T::iterator FindById(T* container, int id) {
|
2018-04-18 01:55:30 +00:00
|
|
|
auto predicate = [id](const typename T::value_type& item) -> bool {
|
2015-01-16 19:39:25 +00:00
|
|
|
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;
|
2016-07-04 06:08:55 +00:00
|
|
|
if (metrics & display::DisplayObserver::DISPLAY_METRIC_BOUNDS)
|
2015-01-16 20:02:32 +00:00
|
|
|
array.push_back("bounds");
|
2016-07-04 06:08:55 +00:00
|
|
|
if (metrics & display::DisplayObserver::DISPLAY_METRIC_WORK_AREA)
|
2015-01-16 20:02:32 +00:00
|
|
|
array.push_back("workArea");
|
2016-07-04 06:08:55 +00:00
|
|
|
if (metrics & display::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR)
|
2015-01-16 20:02:32 +00:00
|
|
|
array.push_back("scaleFactor");
|
2016-07-04 06:08:55 +00:00
|
|
|
if (metrics & display::DisplayObserver::DISPLAY_METRIC_ROTATION)
|
2015-11-29 18:22:53 +00:00
|
|
|
array.push_back("rotation");
|
2015-01-16 20:02:32 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:39:25 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
Screen::Screen(v8::Isolate* isolate, display::Screen* screen)
|
2016-04-25 01:17:54 +00:00
|
|
|
: screen_(screen) {
|
2015-01-16 19:39:25 +00:00
|
|
|
screen_->AddObserver(this);
|
2016-04-25 01:17:54 +00:00
|
|
|
Init(isolate);
|
2015-01-15 00:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Screen::~Screen() {
|
2015-01-16 19:39:25 +00:00
|
|
|
screen_->RemoveObserver(this);
|
2015-01-15 00:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Point Screen::GetCursorScreenPoint() {
|
|
|
|
return screen_->GetCursorScreenPoint();
|
|
|
|
}
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
display::Display Screen::GetPrimaryDisplay() {
|
2015-01-15 00:19:22 +00:00
|
|
|
return screen_->GetPrimaryDisplay();
|
|
|
|
}
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
std::vector<display::Display> Screen::GetAllDisplays() {
|
2016-04-28 15:46:41 +00:00
|
|
|
return screen_->GetAllDisplays();
|
2015-01-15 00:19:22 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
display::Display Screen::GetDisplayNearestPoint(const gfx::Point& point) {
|
2015-01-15 00:19:22 +00:00
|
|
|
return screen_->GetDisplayNearestPoint(point);
|
|
|
|
}
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
display::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) {
|
2015-01-15 00:19:22 +00:00
|
|
|
return screen_->GetDisplayMatching(match_rect);
|
|
|
|
}
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
void Screen::OnDisplayAdded(const display::Display& new_display) {
|
2015-01-16 20:02:32 +00:00
|
|
|
Emit("display-added", new_display);
|
2015-01-15 00:19:22 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
void Screen::OnDisplayRemoved(const display::Display& old_display) {
|
2015-01-16 20:02:32 +00:00
|
|
|
Emit("display-removed", old_display);
|
2015-01-15 00:19:22 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
void Screen::OnDisplayMetricsChanged(const display::Display& display,
|
2015-01-15 00:19:22 +00:00
|
|
|
uint32_t changed_metrics) {
|
2015-01-16 20:02:32 +00:00
|
|
|
Emit("display-metrics-changed", display, MetricsToArray(changed_metrics));
|
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) {
|
2015-01-15 00:37:48 +00:00
|
|
|
if (!Browser::Get()->is_ready()) {
|
2015-09-07 08:12:31 +00:00
|
|
|
isolate->ThrowException(v8::Exception::Error(mate::StringToV8(
|
2018-04-18 01:55:30 +00:00
|
|
|
isolate, "Cannot require \"screen\" module before app is ready")));
|
2015-03-26 03:34:41 +00:00
|
|
|
return v8::Null(isolate);
|
2015-01-15 00:37:48 +00:00
|
|
|
}
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
display::Screen* screen = display::Screen::GetScreen();
|
2015-01-15 00:19:22 +00:00
|
|
|
if (!screen) {
|
2018-04-18 01:55:30 +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
|
|
|
}
|
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
return mate::CreateHandle(isolate, new Screen(isolate, screen)).ToV8();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2018-04-18 01:55:30 +00:00
|
|
|
void Screen::BuildPrototype(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::FunctionTemplate> prototype) {
|
2016-08-02 10:28:12 +00:00
|
|
|
prototype->SetClassName(mate::StringToV8(isolate, "Screen"));
|
2016-08-02 09:08:12 +00:00
|
|
|
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
|
2016-04-25 01:17:54 +00:00
|
|
|
.SetMethod("getCursorScreenPoint", &Screen::GetCursorScreenPoint)
|
|
|
|
.SetMethod("getPrimaryDisplay", &Screen::GetPrimaryDisplay)
|
|
|
|
.SetMethod("getAllDisplays", &Screen::GetAllDisplays)
|
|
|
|
.SetMethod("getDisplayNearestPoint", &Screen::GetDisplayNearestPoint)
|
2017-05-30 21:26:02 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2017-05-24 02:24:56 +00:00
|
|
|
.SetMethod("getMenuBarHeight", &Screen::getMenuBarHeight)
|
2017-05-30 21:26:02 +00:00
|
|
|
#endif
|
2016-04-25 01:17:54 +00:00
|
|
|
.SetMethod("getDisplayMatching", &Screen::GetDisplayMatching);
|
2015-01-15 00:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
2014-04-15 08:25:39 +00:00
|
|
|
namespace {
|
2014-01-07 12:15:55 +00:00
|
|
|
|
2016-08-02 11:38:35 +00:00
|
|
|
using atom::api::Screen;
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
void Initialize(v8::Local<v8::Object> exports,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context,
|
|
|
|
void* priv) {
|
2016-08-02 11:38:35 +00:00
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
|
|
|
mate::Dictionary dict(isolate, exports);
|
|
|
|
dict.Set("screen", Screen::Create(isolate));
|
|
|
|
dict.Set("Screen", Screen::GetConstructor(isolate)->GetFunction());
|
2014-01-07 12:00:25 +00:00
|
|
|
}
|
|
|
|
|
2014-04-15 08:25:39 +00:00
|
|
|
} // namespace
|
2014-01-07 12:00:25 +00:00
|
|
|
|
2018-01-06 15:58:24 +00:00
|
|
|
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_common_screen, Initialize)
|