Sources/Orgstar/DockIcon.swift
28 lines · 1013 bytes
1import AppKit
2import OrgPresentation
3
4/// The icon chosen in Settings ▸ Appearance, shown in the Dock while orgstar runs, in the
5/// variant for the current appearance.
6@MainActor
7enum DockIcon {
8 private static var observation: NSKeyValueObservation?
9
10 static func apply() {
11 let id = UserDefaults.standard.string(forKey: "appIcon") ?? "default"
12 let dark = NSApp.effectiveAppearance.bestMatch(from: [.aqua, .darkAqua]) == .darkAqua
13 guard let file = AppIcons.dockFile(id, dark: dark),
14 let url = Bundle.main.url(forResource: file, withExtension: "png", subdirectory: "IconPreviews") else {
15 NSApp.applicationIconImage = nil
16 return
17 }
18 NSApp.applicationIconImage = NSImage(contentsOf: url)
19 }
20
21 /// Applies now, and again whenever the appearance changes.
22 static func start() {
23 apply()
24 observation = NSApp.observe(\.effectiveAppearance) { _, _ in
25 Task { @MainActor in apply() }
26 }
27 }
28}