Conditional compilation against different GTK versions

Adds a comptime function to enable conditional compilation against
different GTK versions that have added new API calls in newer versions
of GTK.

Use this function to get fractional scaling information for GTK
surfaces, which is only available with GTK 4.12+.
This commit is contained in:
Jeffrey C. Ollie
2024-08-07 12:36:52 -05:00
committed by Mitchell Hashimoto
parent f1aea10a84
commit b6c943386c
2 changed files with 14 additions and 3 deletions

View File

@ -822,9 +822,15 @@ 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. if (comptime c.gtkVersionMinimum(4, 12)) {
const gtk_scale: f32 = @floatFromInt(c.gtk_widget_get_scale_factor(@ptrCast(self.gl_area))); const native = c.gtk_widget_get_native(@ptrCast(self.gl_area));
const surface = c.gtk_native_get_surface(native);
break :scale @floatCast(c.gdk_surface_get_scale(surface));
} else {
break :scale @floatFromInt(c.gtk_widget_get_scale_factor(@ptrCast(self.gl_area)));
}
};
// If we are on X11, we also have to scale using Xft.dpi // If we are on X11, we also have to scale using Xft.dpi
const xft_dpi_scale = if (!x11.is_current_display_server()) 1.0 else xft_scale: { const xft_dpi_scale = if (!x11.is_current_display_server()) 1.0 else xft_scale: {

View File

@ -17,3 +17,8 @@ pub const c = @cImport({
// compatibility // compatibility
@cInclude("ghostty_gtk_compat.h"); @cInclude("ghostty_gtk_compat.h");
}); });
pub fn gtkVersionMinimum(comptime major: c_int, comptime minor: c_int) bool {
return (c.GTK_MAJOR_VERSION > major or
(c.GTK_MAJOR_VERSION == major and c.GTK_MINOR_VERSION >= minor));
}