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

* feat: support dip <-> screen conversion on Linux

* chore: fix build
This commit is contained in:
Robo 2025-05-22 01:57:32 +09:00 committed by GitHub
commit a4dfd9b6f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 107 additions and 24 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
@ -126,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::ScreenWin::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::ScreenWin::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()) {
@ -153,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", &display::win::ScreenWin::ScreenToDIPPoint)
.SetMethod("dipToScreenPoint", &display::win::ScreenWin::DIPToScreenPoint)
.SetMethod("screenToDipRect", &ScreenToDIPRect)
.SetMethod("dipToScreenRect", &DIPToScreenRect)
#endif