From 41aa9ac2ff54cbbdbcab122ef9fe750080393613 Mon Sep 17 00:00:00 2001 From: Tristan Partin Date: Thu, 28 Dec 2023 17:09:04 -0600 Subject: [PATCH 1/2] Use adw_application_new() instead of adw_init() This will enable further libadwaita improvements in the future, but will have no effect today. --- src/apprt/gtk/App.zig | 47 ++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index d746f2d8a..db7261714 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -69,20 +69,6 @@ pub fn init(core_app: *CoreApp, opts: Options) !App { } } - // Initialize libadwaita - if (build_options.libadwaita and config.@"gtk-adwaita") { - log.debug("initializing libadwaita", .{}); - c.adw_init(); - c.adw_style_manager_set_color_scheme( - c.adw_style_manager_get_default(), - switch (config.@"window-theme") { - .system => c.ADW_COLOR_SCHEME_PREFER_LIGHT, - .dark => c.ADW_COLOR_SCHEME_FORCE_DARK, - .light => c.ADW_COLOR_SCHEME_FORCE_LIGHT, - }, - ); - } - // The "none" cursor is used for hiding the cursor const cursor_none = c.gdk_cursor_new_from_name("none", null); errdefer if (cursor_none) |cursor| c.g_object_unref(cursor); @@ -121,10 +107,35 @@ pub fn init(core_app: *CoreApp, opts: Options) !App { app_id, single_instance, }); - const app = @as(?*c.GtkApplication, @ptrCast(c.gtk_application_new( - app_id.ptr, - app_flags, - ))) orelse return error.GtkInitFailed; + + const app: *c.GtkApplication = app: { + if (build_options.libadwaita and config.@"gtk-adwaita") { + const adw_app = @as(?*c.AdwApplication, @ptrCast(c.adw_application_new( + app_id.ptr, + app_flags, + ))) orelse return error.GtkInitFailed; + + const style_manager = c.adw_application_get_style_manager(adw_app); + c.adw_style_manager_set_color_scheme( + style_manager, + switch (config.@"window-theme") { + .system => c.ADW_COLOR_SCHEME_PREFER_LIGHT, + .dark => c.ADW_COLOR_SCHEME_FORCE_DARK, + .light => c.ADW_COLOR_SCHEME_FORCE_LIGHT, + }, + ); + + break :app @ptrCast(adw_app); + } else { + const app = @as(?*c.GtkApplication, @ptrCast(c.gtk_application_new( + app_id.ptr, + app_flags, + ))) orelse return error.GtkInitFailed; + + break :app app; + } + }; + errdefer c.g_object_unref(app); _ = c.g_signal_connect_data( app, From e43919cb9e2ba4c93389c191b2c64936c25f2886 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 29 Dec 2023 09:10:49 -0800 Subject: [PATCH 2/2] apprt/gtk: stylistic changes to adwaita init --- src/apprt/gtk/App.zig | 56 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/apprt/gtk/App.zig b/src/apprt/gtk/App.zig index db7261714..f687c68a0 100644 --- a/src/apprt/gtk/App.zig +++ b/src/apprt/gtk/App.zig @@ -103,37 +103,39 @@ pub fn init(core_app: *CoreApp, opts: Options) !App { }; // Create our GTK Application which encapsulates our process. - log.debug("creating GTK application id={s} single-instance={}", .{ - app_id, - single_instance, - }); - const app: *c.GtkApplication = app: { - if (build_options.libadwaita and config.@"gtk-adwaita") { - const adw_app = @as(?*c.AdwApplication, @ptrCast(c.adw_application_new( - app_id.ptr, - app_flags, - ))) orelse return error.GtkInitFailed; + const adwaita = build_options.libadwaita and config.@"gtk-adwaita"; - const style_manager = c.adw_application_get_style_manager(adw_app); - c.adw_style_manager_set_color_scheme( - style_manager, - switch (config.@"window-theme") { - .system => c.ADW_COLOR_SCHEME_PREFER_LIGHT, - .dark => c.ADW_COLOR_SCHEME_FORCE_DARK, - .light => c.ADW_COLOR_SCHEME_FORCE_LIGHT, - }, - ); + log.debug("creating GTK application id={s} single-instance={} adwaita={}", .{ + app_id, + single_instance, + adwaita, + }); - break :app @ptrCast(adw_app); - } else { - const app = @as(?*c.GtkApplication, @ptrCast(c.gtk_application_new( - app_id.ptr, - app_flags, - ))) orelse return error.GtkInitFailed; + // If not libadwaita, create a standard GTK application. + if (!adwaita) break :app @as(?*c.GtkApplication, @ptrCast(c.gtk_application_new( + app_id.ptr, + app_flags, + ))) orelse return error.GtkInitFailed; - break :app app; - } + // Use libadwaita if requested. Using an AdwApplication lets us use + // Adwaita widgets and access things such as the color scheme. + const adw_app = @as(?*c.AdwApplication, @ptrCast(c.adw_application_new( + app_id.ptr, + app_flags, + ))) orelse return error.GtkInitFailed; + + const style_manager = c.adw_application_get_style_manager(adw_app); + c.adw_style_manager_set_color_scheme( + style_manager, + switch (config.@"window-theme") { + .system => c.ADW_COLOR_SCHEME_PREFER_LIGHT, + .dark => c.ADW_COLOR_SCHEME_FORCE_DARK, + .light => c.ADW_COLOR_SCHEME_FORCE_LIGHT, + }, + ); + + break :app @ptrCast(adw_app); }; errdefer c.g_object_unref(app);