From abf713feecc87500b011aead65c973819c08cca7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 15 Dec 2024 18:14:29 -0800 Subject: [PATCH] macos: support the macos-icon configurations --- macos/Sources/App/macOS/AppDelegate.swift | 18 ++++++++---- macos/Sources/Ghostty/Ghostty.Config.swift | 29 +++++++++++++++++++ macos/Sources/Ghostty/Package.swift | 8 ++++- macos/Sources/Helpers/OSColor+Extension.swift | 13 +++++++++ src/config/Config.zig | 4 +-- 5 files changed, 63 insertions(+), 9 deletions(-) diff --git a/macos/Sources/App/macOS/AppDelegate.swift b/macos/Sources/App/macOS/AppDelegate.swift index 9536b3867..438ccfda2 100644 --- a/macos/Sources/App/macOS/AppDelegate.swift +++ b/macos/Sources/App/macOS/AppDelegate.swift @@ -527,13 +527,19 @@ class AppDelegate: NSObject, GlobalEventTap.shared.disable() } - if let colorizedIcon = ColorizedGhosttyIcon( - screenColors: [], - ghostColor: .yellow - ).makeImage() { - self.appIcon = colorizedIcon - } else { + switch (config.macosIcon) { + case .official: self.appIcon = nil + break + + case .customColor: + guard let ghostColor = config.macosIconGhostColor else { break } + guard let screenColors = config.macosIconScreenColor else { break } + guard let icon = ColorizedGhosttyIcon( + screenColors: screenColors, + ghostColor: ghostColor + ).makeImage() else { break } + self.appIcon = icon } } diff --git a/macos/Sources/Ghostty/Ghostty.Config.swift b/macos/Sources/Ghostty/Ghostty.Config.swift index c52b247d5..6437e3bbd 100644 --- a/macos/Sources/Ghostty/Ghostty.Config.swift +++ b/macos/Sources/Ghostty/Ghostty.Config.swift @@ -252,6 +252,35 @@ extension Ghostty { return v } + var macosIcon: MacOSIcon { + let defaultValue = MacOSIcon.official + guard let config = self.config else { return defaultValue } + var v: UnsafePointer? = nil + let key = "macos-icon" + guard ghostty_config_get(config, &v, key, UInt(key.count)) else { return defaultValue } + guard let ptr = v else { return defaultValue } + let str = String(cString: ptr) + return MacOSIcon(rawValue: str) ?? defaultValue + } + + var macosIconGhostColor: OSColor? { + guard let config = self.config else { return nil } + var v: ghostty_config_color_s = .init() + let key = "macos-icon-ghost-color" + guard ghostty_config_get(config, &v, key, UInt(key.count)) else { return nil } + return .init(ghostty: v) + } + + var macosIconScreenColor: [OSColor]? { + guard let config = self.config else { return nil } + var v: ghostty_config_color_list_s = .init() + let key = "macos-icon-screen-color" + guard ghostty_config_get(config, &v, key, UInt(key.count)) else { return nil } + guard v.len > 0 else { return nil } + let buffer = UnsafeBufferPointer(start: v.colors, count: v.len) + return buffer.map { .init(ghostty: $0) } + } + var focusFollowsMouse : Bool { guard let config = self.config else { return false } var v = false; diff --git a/macos/Sources/Ghostty/Package.swift b/macos/Sources/Ghostty/Package.swift index e7d9d98fd..cb3615e2d 100644 --- a/macos/Sources/Ghostty/Package.swift +++ b/macos/Sources/Ghostty/Package.swift @@ -194,7 +194,13 @@ extension Ghostty { } } } - + + /// macos-icon + enum MacOSIcon: String { + case official + case customColor = "custom-color" + } + /// Enum for the macos-titlebar-proxy-icon config option enum MacOSTitlebarProxyIcon: String { case visible diff --git a/macos/Sources/Helpers/OSColor+Extension.swift b/macos/Sources/Helpers/OSColor+Extension.swift index 5a02af5ed..54b3e1fab 100644 --- a/macos/Sources/Helpers/OSColor+Extension.swift +++ b/macos/Sources/Helpers/OSColor+Extension.swift @@ -1,4 +1,5 @@ import Foundation +import GhosttyKit extension OSColor { var isLightColor: Bool { @@ -89,3 +90,15 @@ extension OSColor { ) } } + +// MARK: Ghostty Types + +extension OSColor { + /// Create a color from a Ghostty color. + convenience init(ghostty: ghostty_config_color_s) { + let red = Double(ghostty.r) / 255 + let green = Double(ghostty.g) / 255 + let blue = Double(ghostty.b) / 255 + self.init(red: red, green: green, blue: blue, alpha: 1) + } +} diff --git a/src/config/Config.zig b/src/config/Config.zig index 3081ac363..608a962ea 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -3704,8 +3704,8 @@ pub const ColorList = struct { const input = input_ orelse return error.ValueRequired; if (input.len == 0) return error.ValueRequired; - // Whenever a color list is set, we reset the list - self.colors.clearRetainingCapacity(); + // Always reset on parse + self.* = .{}; // Split the input by commas and parse each color var it = std.mem.tokenizeScalar(u8, input, ',');