Repository: electron/electron
File: shell/browser/api/electron_api_screen.cc (C++)

Code before:
}

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);
#endif
}

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;
#endif
}

// static
v8::Local<v8::Value> Screen::Create(gin_helper::ErrorThrower error_thrower) {
  if (!Browser::Get()->is_ready()) {
    error_thrower.ThrowError(
        "The 'screen' module can't be used before the app 'ready' event");
    return v8::Null(error_thrower.isolate());
  }

  display::Screen* screen = display::Screen::GetScreen();
  if (!screen) {
    error_thrower.ThrowError("Failed to get screen information");
    return v8::Null(error_thrower.isolate());
  }

  return gin::CreateHandle(error_thrower.isolate(),
                           new Screen(error_thrower.isolate(), screen))
      .ToV8();
}

Review comment (suggestion):
For our build config this will be true for either backends on linux https://source.chromium.org/chromium/chromium/src/+/main:build/config/ozone.gni;l=83-87

We should have a runtime check instead
```suggestion
#elif defined(IS_LINUX)
if (ui::GetOzonePlatformName() == "x11") {
```

Code after:
                                     uint32_t changed_metrics) {
  base::SingleThreadTaskRunner::GetCurrentDefault()->PostNonNestableTask(
      FROM_HERE, base::BindOnce(&DelayEmitWithMetrics, base::Unretained(this),
                                "display-metrics-changed", 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()) {
    error_thrower.ThrowError(
        "The 'screen' module can't be used before the app 'ready' event");
    return v8::Null(error_thrower.isolate());