feat: support dip <-> screen conversion on Linux X11 (#46211)

feat: support dip <-> screen conversion on Linux
This commit is contained in:
Shelley Vohr 2025-05-02 10:31:45 +02:00 committed by GitHub
commit ede84fc327
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 106 additions and 31 deletions

View file

@ -20,11 +20,18 @@
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/point_conversions.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/gfx/geometry/vector2d_conversions.h"
#if BUILDFLAG(IS_WIN)
#include "ui/display/win/screen_win.h"
#endif
#if BUILDFLAG(IS_LINUX)
#include "shell/browser/linux/x11_util.h"
#endif
#if defined(USE_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif
@ -102,14 +109,6 @@ static gfx::Rect DIPToScreenRect(electron::NativeWindow* window,
return display::win::GetScreenWin()->DIPToScreenRect(hwnd, rect);
}
static gfx::PointF ScreenToDIPPoint(const gfx::PointF& pixel_point) {
return display::win::GetScreenWin()->ScreenToDIPPoint(pixel_point);
}
static gfx::Point DIPToScreenPoint(const gfx::Point& dip_point) {
return display::win::GetScreenWin()->DIPToScreenPoint(dip_point);
}
#endif
void Screen::OnDisplayAdded(const display::Display& new_display) {
@ -134,6 +133,44 @@ void Screen::OnDisplayMetricsChanged(const display::Display& display,
MetricsToArray(changed_metrics)));
}
gfx::PointF Screen::ScreenToDIPPoint(const gfx::PointF& point_px) {
#if BUILDFLAG(IS_WIN)
return display::win::GetScreenWin()->ScreenToDIPPoint(point_px);
#elif BUILDFLAG(IS_LINUX)
if (x11_util::IsX11()) {
gfx::Point pt_px = gfx::ToFlooredPoint(point_px);
display::Display display = GetDisplayNearestPoint(pt_px);
gfx::Vector2d delta_px = pt_px - display.native_origin();
gfx::Vector2dF delta_dip =
gfx::ScaleVector2d(delta_px, 1.0 / display.device_scale_factor());
return gfx::PointF(display.bounds().origin()) + delta_dip;
} else {
return point_px;
}
#else
return point_px;
#endif
}
gfx::Point Screen::DIPToScreenPoint(const gfx::Point& point_dip) {
#if BUILDFLAG(IS_WIN)
return display::win::GetScreenWin()->DIPToScreenPoint(point_dip);
#elif BUILDFLAG(IS_LINUX)
if (x11_util::IsX11()) {
display::Display display = GetDisplayNearestPoint(point_dip);
gfx::Rect bounds_dip = display.bounds();
gfx::Vector2d delta_dip = point_dip - bounds_dip.origin();
gfx::Vector2d delta_px = gfx::ToFlooredVector2d(
gfx::ScaleVector2d(delta_dip, display.device_scale_factor()));
return display.native_origin() + delta_px;
} else {
return point_dip;
}
#else
return point_dip;
#endif
}
// static
v8::Local<v8::Value> Screen::Create(gin_helper::ErrorThrower error_thrower) {
if (!Browser::Get()->is_ready()) {
@ -161,9 +198,11 @@ gin::ObjectTemplateBuilder Screen::GetObjectTemplateBuilder(
.SetMethod("getPrimaryDisplay", &Screen::GetPrimaryDisplay)
.SetMethod("getAllDisplays", &Screen::GetAllDisplays)
.SetMethod("getDisplayNearestPoint", &Screen::GetDisplayNearestPoint)
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX)
.SetMethod("screenToDipPoint", &Screen::ScreenToDIPPoint)
.SetMethod("dipToScreenPoint", &Screen::DIPToScreenPoint)
#endif
#if BUILDFLAG(IS_WIN)
.SetMethod("screenToDipPoint", &ScreenToDIPPoint)
.SetMethod("dipToScreenPoint", &DIPToScreenPoint)
.SetMethod("screenToDipRect", &ScreenToDIPRect)
.SetMethod("dipToScreenRect", &DIPToScreenRect)
#endif