Browse Source

chore: address review feedback

Shelley Vohr 3 weeks ago
parent
commit
cfec681e12

+ 1 - 0
BUILD.gn

@@ -670,6 +670,7 @@ source_set("electron_lib") {
     sources += [
       "shell/browser/certificate_manager_model.cc",
       "shell/browser/certificate_manager_model.h",
+      "shell/browser/linux/x11_util.h",
       "shell/browser/ui/gtk_util.cc",
       "shell/browser/ui/gtk_util.h",
     ]

+ 4 - 0
docs/api/screen.md

@@ -133,6 +133,8 @@ Returns [`Point`](structures/point.md)
 Converts a screen physical point to a screen DIP point.
 The DPI scale is performed relative to the display containing the physical point.
 
+Not currently supported on Wayland.
+
 ### `screen.dipToScreenPoint(point)` _Windows_ _Linux_
 
 * `point` [Point](structures/point.md)
@@ -142,6 +144,8 @@ Returns [`Point`](structures/point.md)
 Converts a screen DIP point to a screen physical point.
 The DPI scale is performed relative to the display containing the DIP point.
 
+Not currently supported on Wayland.
+
 ### `screen.screenToDipRect(window, rect)` _Windows_
 
 * `window` [BrowserWindow](browser-window.md) | null

+ 24 - 19
shell/browser/api/electron_api_screen.cc

@@ -27,6 +27,10 @@
 #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
@@ -131,32 +135,33 @@ void Screen::OnDisplayMetricsChanged(const display::Display& display,
 gfx::Point Screen::ScreenToDIPPoint(const gfx::PointF& point_px) {
 #if BUILDFLAG(IS_WIN)
   return display::win::ScreenWin::ScreenToDIPPoint(point_px);
-#elif defined(IS_OZONE_X11)
-  display::Display display =
-      GetDisplayNearestPoint(gfx::ToFlooredPoint(point_px));
-  gfx::Vector2d delta_px = point_px - display.native_origin();
-  gfx::Vector2dF delta_dip =
-      gfx::ScaleVector2d(delta_px, 1.0 / display.device_scale_factor());
-  return display.bounds().origin() + delta_dip;
-#else  // Wayland
-  return gfx::ToFlooredPoint(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 display.bounds().origin() + delta_dip;
+  }
 #endif
+  return gfx::ToFlooredPoint(point_px);
 }
 
 gfx::Point Screen::DIPToScreenPoint(const gfx::Point& point_dip) {
 #if BUILDFLAG(IS_WIN)
   return display::win::ScreenWin::DIPToScreenPoint(point_dip);
-#elif defined(IS_OZONE_X11)
-  display::Display display =
-      GetDisplayNearestPoint(gfx::ToFlooredPoint(point_dip));
-  gfx::Rect bounds_dip = display.bounds();
-  gfx::Vector2d delta_dip = point_dip - bounds_dip.origin();
-  gfx::Vector2dF delta_px =
-      gfx::ScaleVector2d(delta_dip, display.device_scale_factor());
-  return display.native_origin() + delta_px;
-#else  // Wayland
-  return 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::Vector2dF delta_px =
+        gfx::ScaleVector2d(delta_dip, display.device_scale_factor());
+    return display.native_origin() + delta_px;
+  }
 #endif
+  return gfx::ToFlooredPoint(point_dip);
 }
 
 // static

+ 20 - 0
shell/browser/linux/x11_util.h

@@ -0,0 +1,20 @@
+// Copyright (c) 2025 Microsoft GmbH.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ELECTRON_SHELL_BROWSER_LINUX_X11_UTIL_H_
+#define ELECTRON_SHELL_BROWSER_LINUX_X11_UTIL_H_
+
+#include "ui/ozone/public/ozone_platform.h"
+
+namespace x11_util {
+
+bool IsX11() {
+  return ui::OzonePlatform::GetInstance()
+      ->GetPlatformProperties()
+      .electron_can_call_x11;
+}
+
+}  // namespace x11_util
+
+#endif  // ELECTRON_SHELL_BROWSER_LINUX_X11_UTIL_H_

+ 11 - 16
shell/browser/native_window_views.cc

@@ -54,6 +54,7 @@
 #include "base/strings/string_util.h"
 #include "shell/browser/browser.h"
 #include "shell/browser/linux/unity_service.h"
+#include "shell/browser/linux/x11_util.h"
 #include "shell/browser/ui/electron_desktop_window_tree_host_linux.h"
 #include "shell/browser/ui/views/client_frame_view_linux.h"
 #include "shell/browser/ui/views/native_frame_view.h"
@@ -163,12 +164,6 @@ gfx::Size WindowSizeToContentSizeBuggy(HWND hwnd, const gfx::Size& size) {
 
 #endif
 
-[[maybe_unused]] bool IsX11() {
-  return ui::OzonePlatform::GetInstance()
-      ->GetPlatformProperties()
-      .electron_can_call_x11;
-}
-
 class NativeWindowClientView : public views::ClientView {
  public:
   NativeWindowClientView(views::Widget* widget,
@@ -330,7 +325,7 @@ NativeWindowViews::NativeWindowViews(const gin_helper::Dictionary& options,
   if (parent)
     SetParentWindow(parent);
 
-  if (IsX11()) {
+  if (x11_util::IsX11()) {
     // Before the window is mapped the SetWMSpecState can not work, so we have
     // to manually set the _NET_WM_STATE.
     std::vector<x11::Atom> state_atom_list;
@@ -464,7 +459,7 @@ NativeWindowViews::~NativeWindowViews() {
 
 void NativeWindowViews::SetGTKDarkThemeEnabled(bool use_dark_theme) {
 #if BUILDFLAG(IS_LINUX)
-  if (IsX11()) {
+  if (x11_util::IsX11()) {
     const std::string color = use_dark_theme ? "dark" : "light";
     auto* connection = x11::Connection::Get();
     connection->SetStringProperty(
@@ -531,7 +526,7 @@ void NativeWindowViews::Show() {
 
   // On X11, setting Z order before showing the window doesn't take effect,
   // so we have to call it again.
-  if (IsX11())
+  if (x11_util::IsX11())
     widget()->SetZOrderLevel(widget()->GetZOrderLevel());
 #endif
 }
@@ -547,7 +542,7 @@ void NativeWindowViews::ShowInactive() {
 
   // On X11, setting Z order before showing the window doesn't take effect,
   // so we have to call it again.
-  if (IsX11())
+  if (x11_util::IsX11())
     widget()->SetZOrderLevel(widget()->GetZOrderLevel());
 #endif
 }
@@ -592,7 +587,7 @@ bool NativeWindowViews::IsEnabled() const {
 #if BUILDFLAG(IS_WIN)
   return ::IsWindowEnabled(GetAcceleratedWidget());
 #elif BUILDFLAG(IS_LINUX)
-  if (IsX11())
+  if (x11_util::IsX11())
     return !event_disabler_.get();
   NOTIMPLEMENTED();
   return true;
@@ -632,7 +627,7 @@ void NativeWindowViews::SetEnabledInternal(bool enable) {
 #if BUILDFLAG(IS_WIN)
   ::EnableWindow(GetAcceleratedWidget(), enable);
 #else
-  if (IsX11()) {
+  if (x11_util::IsX11()) {
     views::DesktopWindowTreeHostPlatform* tree_host =
         views::DesktopWindowTreeHostLinux::GetHostForWidget(
             GetAcceleratedWidget());
@@ -954,7 +949,7 @@ bool NativeWindowViews::MoveAbove(const std::string& sourceId) {
                  0, 0, 0,
                  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
 #else
-  if (IsX11()) {
+  if (x11_util::IsX11()) {
     if (!IsWindowValid(static_cast<x11::Window>(id.id)))
       return false;
 
@@ -976,7 +971,7 @@ void NativeWindowViews::MoveTop() {
                  size.width(), size.height(),
                  SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
 #else
-  if (IsX11())
+  if (x11_util::IsX11())
     electron::MoveWindowToForeground(
         static_cast<x11::Window>(GetAcceleratedWidget()));
 #endif
@@ -1290,7 +1285,7 @@ void NativeWindowViews::SetIgnoreMouseEvents(bool ignore, bool forward) {
     SetForwardMouseMessages(forward);
   }
 #else
-  if (IsX11()) {
+  if (x11_util::IsX11()) {
     auto* connection = x11::Connection::Get();
     if (ignore) {
       x11::Rectangle r{0, 0, 1, 1};
@@ -1411,7 +1406,7 @@ void NativeWindowViews::SetParentWindow(NativeWindow* parent) {
   NativeWindow::SetParentWindow(parent);
 
 #if BUILDFLAG(IS_LINUX)
-  if (IsX11()) {
+  if (x11_util::IsX11()) {
     auto* connection = x11::Connection::Get();
     connection->SetProperty(
         static_cast<x11::Window>(GetAcceleratedWidget()),