Files
ghostty/macos/Sources/Features/Dock Tile Plugin/UserDefaults+AppIcon.swift
2026-02-24 08:58:15 +01:00

38 lines
1.2 KiB
Swift

import AppKit
extension UserDefaults {
private static let customIconKeyOld = "CustomGhosttyIcon"
private static let customIconKeyNew = "CustomGhosttyIcon2"
var appIcon: AppIcon? {
get {
// Always remove our old pre-docktileplugin values.
defer {
removeObject(forKey: Self.customIconKeyOld)
}
// If we have an old, pre-docktileplugin value, then we parse the
// the old value (try) and set it.
if let previous = string(forKey: Self.customIconKeyOld), let newIcon = AppIcon(string: previous) {
// update new storage once
self.appIcon = newIcon
return newIcon
}
// Check if we have the new key for our dock tile plugin format.
guard let data = data(forKey: Self.customIconKeyNew) else {
return nil
}
return try? JSONDecoder().decode(AppIcon.self, from: data)
}
set {
guard let newData = try? JSONEncoder().encode(newValue) else {
return
}
set(newData, forKey: Self.customIconKeyNew)
}
}
}