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.
|
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/api/electron_api_screen.h"
|
2015-01-15 00:19:22 +00:00
|
|
|
|
2015-01-16 20:02:32 +00:00
|
|
|
#include <string>
|
2015-01-16 19:39:25 +00:00
|
|
|
|
2015-01-14 22:38:51 +00:00
|
|
|
#include "base/bind.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "gin/dictionary.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
#include "gin/handle.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/browser.h"
|
2019-09-06 05:52:54 +00:00
|
|
|
#include "shell/common/gin_converters/callback_converter.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
#include "shell/common/gin_converters/gfx_converter.h"
|
|
|
|
#include "shell/common/gin_converters/native_window_converter.h"
|
2020-03-31 21:49:15 +00:00
|
|
|
#include "shell/common/gin_helper/dictionary.h"
|
2019-09-19 15:09:15 +00:00
|
|
|
#include "shell/common/gin_helper/object_template_builder.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/common/node_includes.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
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-05-16 09:34:09 +00:00
|
|
|
#include "ui/display/win/screen_win.h"
|
|
|
|
#endif
|
|
|
|
|
2022-09-05 08:10:50 +00:00
|
|
|
#if defined(USE_OZONE)
|
|
|
|
#include "ui/ozone/public/ozone_platform.h"
|
|
|
|
#endif
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
namespace electron::api {
|
2015-01-15 00:19:22 +00:00
|
|
|
|
2020-07-28 18:04:01 +00:00
|
|
|
gin::WrapperInfo Screen::kWrapperInfo = {gin::kEmbedderNativeGin};
|
|
|
|
|
2015-01-16 19:39:25 +00:00
|
|
|
namespace {
|
|
|
|
|
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)
|
2019-09-13 14:26:59 +00:00
|
|
|
array.emplace_back("bounds");
|
2016-07-04 06:08:55 +00:00
|
|
|
if (metrics & display::DisplayObserver::DISPLAY_METRIC_WORK_AREA)
|
2019-09-13 14:26:59 +00:00
|
|
|
array.emplace_back("workArea");
|
2016-07-04 06:08:55 +00:00
|
|
|
if (metrics & display::DisplayObserver::DISPLAY_METRIC_DEVICE_SCALE_FACTOR)
|
2019-09-13 14:26:59 +00:00
|
|
|
array.emplace_back("scaleFactor");
|
2016-07-04 06:08:55 +00:00
|
|
|
if (metrics & display::DisplayObserver::DISPLAY_METRIC_ROTATION)
|
2019-09-13 14:26:59 +00:00
|
|
|
array.emplace_back("rotation");
|
2015-01-16 20:02:32 +00:00
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2019-06-28 02:01:28 +00:00
|
|
|
void DelayEmit(Screen* screen,
|
2019-07-25 15:19:04 +00:00
|
|
|
base::StringPiece name,
|
2019-06-28 02:01:28 +00:00
|
|
|
const display::Display& display) {
|
|
|
|
screen->Emit(name, display);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DelayEmitWithMetrics(Screen* screen,
|
2019-07-25 15:19:04 +00:00
|
|
|
base::StringPiece name,
|
2019-06-28 02:01:28 +00:00
|
|
|
const display::Display& display,
|
|
|
|
const std::vector<std::string>& metrics) {
|
|
|
|
screen->Emit(name, display, metrics);
|
|
|
|
}
|
|
|
|
|
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);
|
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
|
|
|
}
|
|
|
|
|
2022-09-05 08:10:50 +00:00
|
|
|
gfx::Point Screen::GetCursorScreenPoint(v8::Isolate* isolate) {
|
|
|
|
#if defined(USE_OZONE)
|
|
|
|
// Wayland will crash unless a window is created prior to calling
|
|
|
|
// GetCursorScreenPoint.
|
|
|
|
if (!ui::OzonePlatform::IsInitialized()) {
|
|
|
|
gin_helper::ErrorThrower thrower(isolate);
|
|
|
|
thrower.ThrowError(
|
|
|
|
"screen.getCursorScreenPoint() cannot be called before a window has "
|
|
|
|
"been created.");
|
|
|
|
return gfx::Point();
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-15 00:19:22 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-05-16 09:34:09 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
static gfx::Rect ScreenToDIPRect(electron::NativeWindow* window,
|
2018-05-16 09:34:09 +00:00
|
|
|
const gfx::Rect& rect) {
|
|
|
|
HWND hwnd = window ? window->GetAcceleratedWidget() : nullptr;
|
|
|
|
return display::win::ScreenWin::ScreenToDIPRect(hwnd, rect);
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
static gfx::Rect DIPToScreenRect(electron::NativeWindow* window,
|
2018-05-16 09:34:09 +00:00
|
|
|
const gfx::Rect& rect) {
|
|
|
|
HWND hwnd = window ? window->GetAcceleratedWidget() : nullptr;
|
|
|
|
return display::win::ScreenWin::DIPToScreenRect(hwnd, rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-07-04 06:08:55 +00:00
|
|
|
void Screen::OnDisplayAdded(const display::Display& new_display) {
|
2019-06-28 02:01:28 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
|
2021-01-25 01:27:40 +00:00
|
|
|
FROM_HERE, base::BindOnce(&DelayEmit, base::Unretained(this),
|
|
|
|
"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) {
|
2019-06-28 02:01:28 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
|
2021-01-25 01:27:40 +00:00
|
|
|
FROM_HERE, base::BindOnce(&DelayEmit, base::Unretained(this),
|
|
|
|
"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) {
|
2019-06-28 02:01:28 +00:00
|
|
|
base::ThreadTaskRunnerHandle::Get()->PostNonNestableTask(
|
2021-01-25 01:27:40 +00:00
|
|
|
FROM_HERE, base::BindOnce(&DelayEmitWithMetrics, base::Unretained(this),
|
|
|
|
"display-metrics-changed", display,
|
|
|
|
MetricsToArray(changed_metrics)));
|
2015-01-15 00:19:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2019-09-19 15:09:15 +00:00
|
|
|
v8::Local<v8::Value> Screen::Create(gin_helper::ErrorThrower error_thrower) {
|
2015-01-15 00:37:48 +00:00
|
|
|
if (!Browser::Get()->is_ready()) {
|
2019-09-19 15:09:15 +00:00
|
|
|
error_thrower.ThrowError(
|
|
|
|
"The 'screen' module can't be used before the app 'ready' event");
|
|
|
|
return v8::Null(error_thrower.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) {
|
2019-09-19 15:09:15 +00:00
|
|
|
error_thrower.ThrowError("Failed to get screen information");
|
|
|
|
return v8::Null(error_thrower.isolate());
|
2015-01-15 00:19:22 +00:00
|
|
|
}
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
return gin::CreateHandle(error_thrower.isolate(),
|
|
|
|
new Screen(error_thrower.isolate(), screen))
|
2019-09-19 15:09:15 +00:00
|
|
|
.ToV8();
|
2016-04-25 01:17:54 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 18:04:01 +00:00
|
|
|
gin::ObjectTemplateBuilder Screen::GetObjectTemplateBuilder(
|
|
|
|
v8::Isolate* isolate) {
|
|
|
|
return gin_helper::EventEmitterMixin<Screen>::GetObjectTemplateBuilder(
|
|
|
|
isolate)
|
2016-04-25 01:17:54 +00:00
|
|
|
.SetMethod("getCursorScreenPoint", &Screen::GetCursorScreenPoint)
|
|
|
|
.SetMethod("getPrimaryDisplay", &Screen::GetPrimaryDisplay)
|
|
|
|
.SetMethod("getAllDisplays", &Screen::GetAllDisplays)
|
|
|
|
.SetMethod("getDisplayNearestPoint", &Screen::GetDisplayNearestPoint)
|
2022-02-10 02:58:52 +00:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2018-05-16 09:34:09 +00:00
|
|
|
.SetMethod("screenToDipPoint", &display::win::ScreenWin::ScreenToDIPPoint)
|
|
|
|
.SetMethod("dipToScreenPoint", &display::win::ScreenWin::DIPToScreenPoint)
|
|
|
|
.SetMethod("screenToDipRect", &ScreenToDIPRect)
|
|
|
|
.SetMethod("dipToScreenRect", &DIPToScreenRect)
|
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
|
|
|
}
|
|
|
|
|
2020-07-28 18:04:01 +00:00
|
|
|
const char* Screen::GetTypeName() {
|
|
|
|
return "Screen";
|
|
|
|
}
|
|
|
|
|
2022-06-29 19:55:47 +00:00
|
|
|
} // namespace electron::api
|
2015-01-15 00:19:22 +00:00
|
|
|
|
2014-04-15 08:25:39 +00:00
|
|
|
namespace {
|
2014-01-07 12:15:55 +00:00
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
using electron::api::Screen;
|
2016-08-02 11:38:35 +00:00
|
|
|
|
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();
|
2020-03-31 21:49:15 +00:00
|
|
|
gin_helper::Dictionary dict(isolate, exports);
|
|
|
|
dict.SetMethod("createScreen", base::BindRepeating(&Screen::Create));
|
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
|
|
|
|
2022-06-21 07:34:56 +00:00
|
|
|
NODE_LINKED_MODULE_CONTEXT_AWARE(electron_browser_screen, Initialize)
|