apprt/gtk: move adw enabling and version checks into shared file

This commit is contained in:
Mitchell Hashimoto
2024-09-11 09:21:39 -07:00
parent 7aa6b0008a
commit 1ab850fa94
2 changed files with 61 additions and 5 deletions

View File

@ -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.?));

46
src/apprt/gtk/adwaita.zig Normal file
View File

@ -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;
}