From 1ab850fa948bc146be72dfa1e247feef12792189 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 11 Sep 2024 09:21:39 -0700 Subject: [PATCH] apprt/gtk: move adw enabling and version checks into shared file --- src/apprt/gtk/Window.zig | 20 ++++++++++++----- src/apprt/gtk/adwaita.zig | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/apprt/gtk/adwaita.zig diff --git a/src/apprt/gtk/Window.zig b/src/apprt/gtk/Window.zig index 3e6ecc1fd..ed489a98d 100644 --- a/src/apprt/gtk/Window.zig +++ b/src/apprt/gtk/Window.zig @@ -21,6 +21,7 @@ const Color = configpkg.Config.Color; const Surface = @import("Surface.zig"); const Tab = @import("Tab.zig"); const c = @import("c.zig").c; +const adwaita = @import("adwaita.zig"); const Notebook = @import("./notebook.zig").Notebook; const log = std.log.scoped(.gtk); @@ -59,10 +60,11 @@ pub fn init(self: *Window, app: *App) !void { .context_menu = undefined, }; - const adwaita = build_options.libadwaita and app.config.@"gtk-adwaita"; - // Create the window - const adw_window = adwaita and app.config.@"gtk-titlebar" and c.ADW_MINOR_VERSION >= 4; + const adw_window = adwaita.enabled(&app.config) and + app.config.@"gtk-titlebar" and + comptime adwaita.versionAtLeast(1, 4, 0) and + adwaita.versionAtLeast(1, 4, 0); const window: *c.GtkWidget = if (adw_window) c.adw_application_window_new(app.app) else @@ -125,7 +127,10 @@ pub fn init(self: *Window, app: *App) !void { // This is a really common issue where people build from source in debug and performance is really bad. if (comptime std.debug.runtime_safety) { const warning_text = "⚠️ You're running a debug build of Ghostty! Performance will be degraded."; - if (adwaita and c.ADW_MINOR_VERSION >= 3) { + if (adwaita.enabled(&app.config) and + comptime adwaita.versionAtLeast(1, 3, 0) and + adwaita.versionAtLeast(1, 3, 0)) + { const banner = c.adw_banner_new(warning_text); c.gtk_box_append(@ptrCast(box), @ptrCast(banner)); } else { @@ -155,7 +160,12 @@ pub fn init(self: *Window, app: *App) !void { // Our actions for the menu initActions(self); - if (build_options.libadwaita and app.config.@"gtk-adwaita" and app.config.@"gtk-titlebar" and header != null and c.ADW_MINOR_VERSION >= 4) { + if (adwaita.enabled(&app.config) and + app.config.@"gtk-titlebar" and + header != null and + comptime adwaita.versionAtLeast(1, 4, 0) and + adwaita.versionAtLeast(1, 4, 0)) + { const toolbar_view: *c.AdwToolbarView = @ptrCast(c.adw_toolbar_view_new()); const header_widget: *c.GtkWidget = @ptrCast(@alignCast(header.?)); diff --git a/src/apprt/gtk/adwaita.zig b/src/apprt/gtk/adwaita.zig new file mode 100644 index 000000000..c1efd34e3 --- /dev/null +++ b/src/apprt/gtk/adwaita.zig @@ -0,0 +1,46 @@ +const std = @import("std"); +const c = @import("c.zig").c; +const build_options = @import("build_options"); +const Config = @import("../../config.zig").Config; + +/// Returns true if Ghostty is configured to build with libadwaita and +/// the configuration has enabled adwaita. +pub fn enabled(config: *const Config) bool { + return build_options.libadwaita and + config.@"gtk-adwaita"; +} + +/// Verifies that the running libadwaita version is at least the given +/// version. This will return false if Ghostty is configured to +/// not build with libadwaita. +/// +/// This can be run in both a comptime and runtime context. If it +/// is run in a comptime context, it will only check the version +/// in the headers. If it is run in a runtime context, it will +/// check the actual version of the library we are linked against. +/// So generally you probably want to do both checks! +pub fn versionAtLeast( + comptime major: u16, + comptime minor: u16, + comptime micro: u16, +) bool { + if (comptime !build_options.libadwaita) return false; + + // If our header has lower versions than the given version, + // we can return false immediately. This prevents us from + // compiling against unknown symbols and makes runtime checks + // very slightly faster. + if (comptime c.ADW_MAJOR_VERSION < major or + c.ADW_MINOR_VERSION < minor or + c.ADW_MICRO_VERSION < micro) return false; + + // If we're in comptime then we can't check the runtime version. + if (@inComptime()) return true; + + // We use the functions instead of the constants such as + // c.ADW_MINOR_VERSION because the function gets the actual + // runtime version. + return c.adw_get_major_version() >= major and + c.adw_get_minor_version() >= minor and + c.adw_get_micro_version() >= micro; +}