macos: support the macos-icon configurations

This commit is contained in:
Mitchell Hashimoto
2024-12-15 18:14:29 -08:00
parent 29929a473d
commit abf713feec
5 changed files with 63 additions and 9 deletions

View File

@ -527,13 +527,19 @@ class AppDelegate: NSObject,
GlobalEventTap.shared.disable() GlobalEventTap.shared.disable()
} }
if let colorizedIcon = ColorizedGhosttyIcon( switch (config.macosIcon) {
screenColors: [], case .official:
ghostColor: .yellow
).makeImage() {
self.appIcon = colorizedIcon
} else {
self.appIcon = nil 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
} }
} }

View File

@ -252,6 +252,35 @@ extension Ghostty {
return v return v
} }
var macosIcon: MacOSIcon {
let defaultValue = MacOSIcon.official
guard let config = self.config else { return defaultValue }
var v: UnsafePointer<Int8>? = 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 { var focusFollowsMouse : Bool {
guard let config = self.config else { return false } guard let config = self.config else { return false }
var v = false; var v = false;

View File

@ -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 for the macos-titlebar-proxy-icon config option
enum MacOSTitlebarProxyIcon: String { enum MacOSTitlebarProxyIcon: String {
case visible case visible

View File

@ -1,4 +1,5 @@
import Foundation import Foundation
import GhosttyKit
extension OSColor { extension OSColor {
var isLightColor: Bool { 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)
}
}

View File

@ -3704,8 +3704,8 @@ pub const ColorList = struct {
const input = input_ orelse return error.ValueRequired; const input = input_ orelse return error.ValueRequired;
if (input.len == 0) return error.ValueRequired; if (input.len == 0) return error.ValueRequired;
// Whenever a color list is set, we reset the list // Always reset on parse
self.colors.clearRetainingCapacity(); self.* = .{};
// Split the input by commas and parse each color // Split the input by commas and parse each color
var it = std.mem.tokenizeScalar(u8, input, ','); var it = std.mem.tokenizeScalar(u8, input, ',');