From e93a6eb2e4497804157c7cb6a345223ac4de8878 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 8 Aug 2023 09:43:15 -0700 Subject: [PATCH] apprt/gtk: set icon name --- src/apprt/gtk.zig | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/apprt/gtk.zig b/src/apprt/gtk.zig index 8c671be4f..bbdbccd43 100644 --- a/src/apprt/gtk.zig +++ b/src/apprt/gtk.zig @@ -298,6 +298,9 @@ const Window = struct { /// The background CSS for the window (if any). css_window_background: ?[]u8 = null, + /// The resources directory for the icon (if any). + icon_search_dir: ?[:0]const u8 = null, + pub fn init(self: *Window, app: *App) !void { // Set up our own state self.* = .{ @@ -314,6 +317,31 @@ const Window = struct { c.gtk_window_set_title(gtk_window, "Ghostty"); c.gtk_window_set_default_size(gtk_window, 1000, 600); + // If we don't have the icon then we'll try to add our resources dir + // to the search path and see if we can find it there. + const icon_name = "com.mitchellh.ghostty"; + const icon_theme = c.gtk_icon_theme_get_for_display(c.gtk_widget_get_display(window)); + if (c.gtk_icon_theme_has_icon(icon_theme, icon_name) == 0) icon: { + const base = self.app.core_app.resources_dir orelse { + log.info("gtk app missing Ghostty icon and no resources dir detected", .{}); + log.info("gtk app will not have Ghostty icon", .{}); + break :icon; + }; + + // Note that this method for adding the icon search path is + // a fallback mechanism. The recommended mechanism is the + // Freedesktop Icon Theme Specification. We distribute a ".desktop" + // file in zig-out/share that should be installed to the proper + // place. + const dir = try std.fmt.allocPrintZ(app.core_app.alloc, "{s}/icons", .{base}); + self.icon_search_dir = dir; + c.gtk_icon_theme_add_search_path(icon_theme, dir.ptr); + if (c.gtk_icon_theme_has_icon(icon_theme, icon_name) == 0) { + log.warn("Ghostty icon for gtk app not found", .{}); + } + } + c.gtk_window_set_icon_name(gtk_window, icon_name); + // Apply background opacity if we have it if (app.config.@"background-opacity" < 1) { var css = try std.fmt.allocPrint( @@ -361,6 +389,7 @@ const Window = struct { pub fn deinit(self: *Window) void { if (self.css_window_background) |ptr| self.app.core_app.alloc.free(ptr); + if (self.icon_search_dir) |ptr| self.app.core_app.alloc.free(ptr); } /// Add a new tab to this window.