Sources/Orgstar/AppIconSection.swift

42 lines · 1760 bytes

2 symbols in this file
 1import AppKit
 2import OrgPresentation
 3import SwiftUI
 4
 5/// Settings ▸ Appearance ▸ App Icon: the icon the Dock shows while orgstar runs.
 6struct AppIconSection: View {
 7    @AppStorage("appIcon") private var appIcon = "default"
 8
 9    var body: some View {
10        Section {
11            LazyVGrid(columns: [GridItem(.adaptive(minimum: 72), spacing: 12)], spacing: 12) {
12                ForEach(AppIcons.ids, id: \.self) { id in
13                    Button {
14                        appIcon = id
15                        DockIcon.apply()
16                    } label: {
17                        VStack(spacing: 4) {
18                            preview(id)
19                                .resizable()
20                                .frame(width: 56, height: 56)
21                                .padding(4)
22                                .background(id == appIcon ? Color.accentColor.opacity(0.25) : .clear, in: RoundedRectangle(cornerRadius: 10))
23                            Text(AppIcons.name(id)).font(.caption)
24                        }
25                    }
26                    .buttonStyle(.plain)
27                    .accessibilityAddTraits(id == appIcon ? .isSelected : [])
28                }
29            }
30        } header: {
31            Text("App Icon")
32        } footer: {
33            Text("The icon shows in the Dock while orgstar runs. Finder, and the Dock after orgstar quits, show the default icon.")
34                .settingsFooter()
35        }
36    }
37
38    private func preview(_ id: String) -> Image {
39        let url = Bundle.main.url(forResource: "\(id)-light", withExtension: "png", subdirectory: "IconPreviews")
40        return url.flatMap { NSImage(contentsOf: $0) }.map { Image(nsImage: $0) } ?? Image(nsImage: NSApp.applicationIconImage)
41    }
42}