apprt/gtk: set icon name

This commit is contained in:
Mitchell Hashimoto
2023-08-08 09:43:15 -07:00
parent 6b45d931c3
commit e93a6eb2e4

View File

@ -298,6 +298,9 @@ const Window = struct {
/// The background CSS for the window (if any). /// The background CSS for the window (if any).
css_window_background: ?[]u8 = null, 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 { pub fn init(self: *Window, app: *App) !void {
// Set up our own state // Set up our own state
self.* = .{ self.* = .{
@ -314,6 +317,31 @@ const Window = struct {
c.gtk_window_set_title(gtk_window, "Ghostty"); c.gtk_window_set_title(gtk_window, "Ghostty");
c.gtk_window_set_default_size(gtk_window, 1000, 600); 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 // Apply background opacity if we have it
if (app.config.@"background-opacity" < 1) { if (app.config.@"background-opacity" < 1) {
var css = try std.fmt.allocPrint( var css = try std.fmt.allocPrint(
@ -361,6 +389,7 @@ const Window = struct {
pub fn deinit(self: *Window) void { pub fn deinit(self: *Window) void {
if (self.css_window_background) |ptr| self.app.core_app.alloc.free(ptr); 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. /// Add a new tab to this window.