gtk: ensure that the content scale is always positive

If GTK reports a negative or zero scale, we ignore that and use 1.0 for
the scale.

Backport of #5954
This commit is contained in:
Jeffrey C. Ollie
2025-03-23 16:30:49 -05:00
parent f458d48f9b
commit 8fdb3e6e99

View File

@ -817,16 +817,23 @@ pub fn shouldClose(self: *const Surface) bool {
} }
pub fn getContentScale(self: *const Surface) !apprt.ContentScale { pub fn getContentScale(self: *const Surface) !apprt.ContentScale {
// Future: detect GTK version 4.12+ and use gdk_surface_get_scale so we const gtk_scale: f32 = scale: {
// can support fractional scaling. // Future: detect GTK version 4.12+ and use gdk_surface_get_scale so we
const gtk_scale: f32 = @floatFromInt(c.gtk_widget_get_scale_factor(@ptrCast(self.gl_area))); // can support fractional scaling.
const scale = c.gtk_widget_get_scale_factor(@ptrCast(self.gl_area));
if (scale <= 0) {
log.warn("gtk_widget_get_scale_factor returned a non-positive number: {d}", .{scale});
break :scale 1.0;
}
break :scale @floatFromInt(scale);
};
// Also scale using font-specific DPI, which is often exposed to the user // Also scale using font-specific DPI, which is often exposed to the user
// via DE accessibility settings (see https://docs.gtk.org/gtk4/class.Settings.html). // via DE accessibility settings (see https://docs.gtk.org/gtk4/class.Settings.html).
const xft_dpi_scale = xft_scale: { const xft_dpi_scale = xft_scale: {
// gtk-xft-dpi is font DPI multiplied by 1024. See // gtk-xft-dpi is font DPI multiplied by 1024. See
// https://docs.gtk.org/gtk4/property.Settings.gtk-xft-dpi.html // https://docs.gtk.org/gtk4/property.Settings.gtk-xft-dpi.html
const settings = c.gtk_settings_get_default(); const settings = c.gtk_settings_get_default() orelse break :xft_scale 1.0;
var value: c.GValue = std.mem.zeroes(c.GValue); var value: c.GValue = std.mem.zeroes(c.GValue);
defer c.g_value_unset(&value); defer c.g_value_unset(&value);
@ -834,11 +841,21 @@ pub fn getContentScale(self: *const Surface) !apprt.ContentScale {
c.g_object_get_property(@ptrCast(@alignCast(settings)), "gtk-xft-dpi", &value); c.g_object_get_property(@ptrCast(@alignCast(settings)), "gtk-xft-dpi", &value);
const gtk_xft_dpi = c.g_value_get_int(&value); const gtk_xft_dpi = c.g_value_get_int(&value);
// Use a value of 1.0 for the XFT DPI scale if the setting is <= 0
// See:
// https://gitlab.gnome.org/GNOME/libadwaita/-/commit/a7738a4d269bfdf4d8d5429ca73ccdd9b2450421
// https://gitlab.gnome.org/GNOME/libadwaita/-/commit/9759d3fd81129608dd78116001928f2aed974ead
// ensure that the scale is never negative
if (gtk_xft_dpi <= 0) {
log.warn("gtk-xft-dpi was not set, using default value", .{});
break :xft_scale 1.0;
}
// As noted above gtk-xft-dpi is multiplied by 1024, so we divide by // As noted above gtk-xft-dpi is multiplied by 1024, so we divide by
// 1024, then divide by the default value (96) to derive a scale. Note // 1024, then divide by the default value (96) to derive a scale. Note
// gtk-xft-dpi can be fractional, so we use floating point math here. // gtk-xft-dpi can be fractional, so we use floating point math here.
const xft_dpi: f32 = @as(f32, @floatFromInt(gtk_xft_dpi)) / 1024; const xft_dpi: f32 = @as(f32, @floatFromInt(gtk_xft_dpi)) / 1024.0;
break :xft_scale xft_dpi / 96; break :xft_scale xft_dpi / 96.0;
}; };
const scale = gtk_scale * xft_dpi_scale; const scale = gtk_scale * xft_dpi_scale;