Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ protected int getDeviceZoom() {
long surface = GTK4.gtk_native_get_surface(GTK4.gtk_widget_get_native(shellHandle));
monitor = GDK.gdk_display_get_monitor_at_surface(display, surface);
} else {
monitor = GDK.gdk_display_get_monitor_at_point(display, 0, 0);
monitor = getPrimaryMonitor(display);
}

// GDK can return null monitor in some cases thus play safe
Expand All @@ -1120,4 +1120,18 @@ protected int getDeviceZoom() {
return DPIUtil.mapDPIToZoom (dpi);
}

/**
* Returns the GDK primary monitor handle, falling back to the monitor at
* virtual coordinate (0,0) when no primary monitor is reported (e.g. on Wayland).
*
* @noreference This method is not intended to be referenced by clients.
*/
protected long getPrimaryMonitor(long display) {
long monitor = GDK.gdk_display_get_primary_monitor(display);
if (monitor == 0) {
monitor = GDK.gdk_display_get_monitor_at_point(display, 0, 0);
}
return monitor;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2712,7 +2712,7 @@ public Monitor[] getMonitors() {
int scaleFactor = (int) GDK.gdk_monitor_get_scale_factor(gdkMonitor);
monitor.zoom = scaleFactor * 100;
} else {
monitor.zoom = Display._getDeviceZoom(monitor.handle);
monitor.zoom = _getDeviceZoom(monitor.handle);
}

/* workarea was defined in GTK 3.4. If present, it will return the best results
Expand Down Expand Up @@ -6256,16 +6256,19 @@ long gdk_device_get_surface_at_position (double[] win_x, double[] win_y) {
return GDK.gdk_device_get_surface_at_position (device, win_x, win_y);
}

static int _getDeviceZoom (long monitor_num) {
int _getDeviceZoom (long monitor) {
/*
* We can hard-code 96 as gdk_screen_get_resolution will always return -1
* if gdk_screen_set_resolution has not been called.
*/
int dpi = 96;
long display = GDK.gdk_display_get_default();
long monitor = GDK.gdk_display_get_monitor_at_point(display, 0, 0);
int scale = GDK.gdk_monitor_get_scale_factor(monitor);
dpi = dpi * scale;
if (monitor == 0) {
monitor = getPrimaryMonitor(GDK.gdk_display_get_default());
}
Comment on lines +6265 to +6267
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic for determining the default monitor is duplicated in Device.getDeviceZoom(). To improve maintainability, consider extracting this logic into a protected static helper method in the Device class and reusing it in both places.

For example, you could add the following method to Device.java:

protected static long getDefaultMonitor(long display) {
    long monitor = GDK.gdk_display_get_primary_monitor(display);
    if (monitor == 0) {
        monitor = GDK.gdk_display_get_monitor_at_point(display, 0, 0);
    }
    return monitor;
}

This new helper method could then be used here and in Device.getDeviceZoom() to simplify the code and remove the duplication.

if (monitor != 0) {
int scale = GDK.gdk_monitor_get_scale_factor(monitor);
dpi = dpi * scale;
}
return DPIUtil.mapDPIToZoom (dpi);
}

Expand Down
Loading