import SwiftUI
|
import AppKit
|
import Carbon
|
|
// MARK: - Application Entry Point
|
|
@main
|
struct TagLauncherApp: App {
|
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
|
|
var body: some Scene {
|
Settings {
|
PreferencesView()
|
}
|
.defaultSize(width: 880, height: 460)
|
}
|
}
|
|
// MARK: - App Delegate (menubar + overlay window + hotkey)
|
|
final class OverlayPanel: NSPanel {
|
override var canBecomeKey: Bool { true }
|
override var canBecomeMain: Bool { true }
|
}
|
|
final class AppDelegate: NSObject, NSApplicationDelegate {
|
private var statusItem: NSStatusItem?
|
private var overlayWindow: NSWindow?
|
private var settingsWindow: NSWindow? // Track Settings window to keep it above overlay
|
private var hotkeyRef: EventHotKeyRef?
|
private var isInEditMode = false // Suppress auto-dismiss during editing
|
|
func applicationDidFinishLaunching(_ notification: Notification) {
|
L10n.setup()
|
migrateDefaultGroupName()
|
TagDatabase.seedDefaultTags()
|
let showDock = UserDefaults.standard.bool(forKey: "showDockIcon")
|
NSApp.setActivationPolicy(showDock ? .regular : .accessory)
|
setupMenuBar()
|
registerHotkey()
|
observeOtherWindows()
|
observeSettingsClose()
|
observeEditMode()
|
observeDockSetting()
|
observeLanguageChanges()
|
setupLaunchAtLogin()
|
}
|
|
/// Dock icon click → show overlay (same as menubar "Show TagLauncher")
|
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
|
showOverlay()
|
return false // Suppress default "unhide all windows" behavior
|
}
|
|
/// Ensure defaultGroupName is always the language-neutral key "Other".
|
/// Translates known old values back to "Other" so switching languages works.
|
private func migrateDefaultGroupName() {
|
let key = "defaultGroupName"
|
let stored = UserDefaults.standard.string(forKey: key)
|
// "Other" is the neutral key — nothing to do
|
if stored == nil || stored == "Other" { return }
|
// Check if the stored value is a translated version of "group.uncategorized"
|
for (code, _) in L10n.supported {
|
let loc = L10n.loadedTranslation("group.uncategorized", for: code)
|
if stored == loc {
|
UserDefaults.standard.set("Other", forKey: key)
|
return
|
}
|
}
|
// User has set a custom name — keep it
|
}
|
|
/// Observe Show in Dock changes so it takes effect immediately.
|
private func observeDockSetting() {
|
NotificationCenter.default.addObserver(
|
forName: UserDefaults.didChangeNotification,
|
object: nil, queue: .main
|
) { _ in
|
let show = UserDefaults.standard.bool(forKey: "showDockIcon")
|
NSApp.setActivationPolicy(show ? .regular : .accessory)
|
}
|
}
|
|
/// Keep app chrome in sync when language changes from any entry point.
|
private func observeLanguageChanges() {
|
NotificationCenter.default.addObserver(
|
forName: .appLanguageDidChange,
|
object: nil, queue: .main
|
) { [weak self] _ in
|
self?.setupMenuBar()
|
}
|
}
|
|
// MARK: - Launch at Login (LaunchAgent, zero permissions)
|
|
private static let launchAgentLabel = "com.apptag.launcher"
|
static var supportsLaunchAtLogin: Bool {
|
ProcessInfo.processInfo.environment["APP_SANDBOX_CONTAINER_ID"] == nil
|
}
|
|
private static var launchAgentURL: URL {
|
FileManager.default.homeDirectoryForCurrentUser
|
.appendingPathComponent("Library/LaunchAgents/\(launchAgentLabel).plist")
|
}
|
|
static func enableLaunchAtLogin() {
|
guard supportsLaunchAtLogin else { return }
|
let plist: [String: Any] = [
|
"Label": Self.launchAgentLabel,
|
"ProgramArguments": ["open", Bundle.main.bundlePath, "--hide"],
|
"RunAtLoad": true,
|
]
|
let dir = Self.launchAgentURL.deletingLastPathComponent()
|
try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
|
(plist as NSDictionary).write(to: Self.launchAgentURL, atomically: true)
|
|
let uid = getuid()
|
let task = Process()
|
task.launchPath = "/bin/launchctl"
|
task.arguments = ["bootstrap", "gui/\(uid)", Self.launchAgentURL.path]
|
task.launch()
|
}
|
|
static func disableLaunchAtLogin() {
|
guard supportsLaunchAtLogin else { return }
|
let uid = getuid()
|
let task = Process()
|
task.launchPath = "/bin/launchctl"
|
task.arguments = ["bootout", "gui/\(uid)/\(Self.launchAgentLabel)"]
|
task.launch()
|
try? FileManager.default.removeItem(at: Self.launchAgentURL)
|
}
|
|
/// On first launch, enable login item by default via LaunchAgent.
|
/// Does NOT require App Management permission.
|
private func setupLaunchAtLogin() {
|
guard Self.supportsLaunchAtLogin else {
|
if UserDefaults.standard.object(forKey: "launchAtLogin") == nil {
|
UserDefaults.standard.set(false, forKey: "launchAtLogin")
|
}
|
return
|
}
|
let key = "launchAtLogin"
|
if UserDefaults.standard.object(forKey: key) == nil {
|
UserDefaults.standard.set(true, forKey: key)
|
Self.enableLaunchAtLogin()
|
}
|
}
|
|
// MARK: - Menu Bar
|
|
private func setupMenuBar() {
|
if statusItem == nil {
|
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
|
}
|
guard let statusItem else { return }
|
|
if let button = statusItem.button {
|
let image = NSImage(
|
systemSymbolName: "tag.fill",
|
accessibilityDescription: "TagLauncher"
|
)
|
image?.isTemplate = true
|
button.image = image
|
button.imagePosition = .imageOnly
|
button.toolTip = "TagLauncher — Tag-based app launcher"
|
button.action = #selector(toggleOverlay)
|
button.target = self
|
}
|
|
let menu = NSMenu()
|
menu.addItem(
|
NSMenuItem(
|
title: "\(tr("menu.show")) ⇧⌥Space",
|
action: #selector(toggleOverlay),
|
keyEquivalent: ""
|
)
|
)
|
menu.addItem(.separator())
|
|
// Version display
|
let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "?"
|
let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "?"
|
let versionItem = NSMenuItem(
|
title: "\(tr("menu.version")) \(appVersion) (\(buildNumber))",
|
action: nil,
|
keyEquivalent: ""
|
)
|
versionItem.isEnabled = false
|
menu.addItem(versionItem)
|
|
menu.addItem(.separator())
|
let prefsItem = NSMenuItem(
|
title: tr("menu.preferences"),
|
action: #selector(openPreferences),
|
keyEquivalent: ","
|
)
|
prefsItem.keyEquivalentModifierMask = .command
|
menu.addItem(prefsItem)
|
menu.addItem(.separator())
|
|
// Language submenu
|
let langMenu = NSMenu()
|
let currentLang = L10n.currentCode
|
for (code, name) in L10n.supported {
|
let item = NSMenuItem(title: name, action: #selector(switchLanguage(_:)), keyEquivalent: "")
|
item.representedObject = code
|
item.state = (code == currentLang) ? .on : .off
|
langMenu.addItem(item)
|
}
|
let langItem = NSMenuItem(title: tr("menu.language"), action: nil, keyEquivalent: "")
|
langItem.submenu = langMenu
|
menu.addItem(langItem)
|
|
menu.addItem(.separator())
|
menu.addItem(
|
NSMenuItem(
|
title: tr("menu.quit"),
|
action: #selector(NSApplication.terminate(_:)),
|
keyEquivalent: "q"
|
)
|
)
|
statusItem.menu = menu
|
}
|
|
// MARK: - Overlay Window
|
|
@objc private func toggleOverlay() {
|
if overlayWindow?.isVisible == true {
|
hideOverlay()
|
} else {
|
showOverlay()
|
}
|
}
|
|
private func showOverlay() {
|
// Use the screen under the mouse cursor — works in fullscreen spaces
|
let mousePoint = NSEvent.mouseLocation
|
guard let screen = NSScreen.screens.first(where: {
|
NSMouseInRect(mousePoint, $0.frame, false)
|
}) ?? NSScreen.main ?? NSScreen.screens.first else { return }
|
|
overlayWindow?.orderOut(nil)
|
overlayWindow = makeOverlayWindow(on: screen)
|
overlayWindow?.setFrame(screen.frame, display: true)
|
overlayWindow?.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.maximumWindow)))
|
|
// Local key monitor: catch Escape while overlay is up
|
NSEvent.addLocalMonitorForEvents(matching: .keyDown) { [weak self] event in
|
if event.keyCode == 53 { // Escape
|
self?.hideOverlay()
|
return nil
|
}
|
return event
|
}
|
|
overlayWindow?.makeKeyAndOrderFront(nil)
|
overlayWindow?.orderFrontRegardless()
|
}
|
|
private func makeOverlayWindow(on screen: NSScreen) -> NSWindow {
|
let panel = OverlayPanel(
|
contentRect: screen.frame,
|
styleMask: [.borderless, .fullSizeContentView, .nonactivatingPanel],
|
backing: .buffered,
|
defer: false
|
)
|
panel.isFloatingPanel = true
|
panel.hidesOnDeactivate = false
|
panel.collectionBehavior = [
|
.moveToActiveSpace,
|
.fullScreenAuxiliary,
|
.stationary,
|
.transient,
|
.ignoresCycle
|
]
|
panel.isOpaque = false
|
panel.backgroundColor = .clear
|
panel.hasShadow = false
|
panel.titlebarAppearsTransparent = true
|
panel.titleVisibility = .hidden
|
panel.isReleasedWhenClosed = false
|
panel.contentView = DismissibleHostingView(
|
rootView: ContentView(hideOverlay: { [weak self] in
|
self?.hideOverlay(force: true)
|
}),
|
onBackdropTap: { [weak self] in
|
self?.hideOverlay()
|
}
|
)
|
return panel
|
}
|
|
private func hideOverlay(force: Bool = false) {
|
guard force || !isInEditMode else { return }
|
overlayWindow?.orderOut(nil)
|
if force {
|
overlayWindow = nil
|
}
|
}
|
|
// MARK: - Global Hotkey (Shift+Option+Space)
|
|
/// Carbon RegisterEventHotKey. If it fails (sandbox, etc.), falls back to menu bar only.
|
private func registerHotkey() {
|
var hotkeyID = EventHotKeyID()
|
hotkeyID.signature = OSType(0x41505447) // 'APTG'
|
hotkeyID.id = 1
|
|
let modifiers = UInt32(shiftKey | optionKey)
|
|
var ref: EventHotKeyRef?
|
let status = RegisterEventHotKey(
|
UInt32(kVK_Space),
|
modifiers,
|
hotkeyID,
|
GetApplicationEventTarget(),
|
0,
|
&ref
|
)
|
hotkeyRef = ref
|
|
if status != noErr {
|
print("[TagLauncher] Hotkey registration failed: \(status). Falling back to menu bar only.")
|
return
|
}
|
|
var eventSpec = EventTypeSpec(
|
eventClass: OSType(kEventClassKeyboard),
|
eventKind: UInt32(kEventHotKeyPressed)
|
)
|
|
let selfPtr = Unmanaged.passUnretained(self).toOpaque()
|
|
InstallEventHandler(
|
GetApplicationEventTarget(),
|
{ (_, _, userData) -> OSStatus in
|
guard let userData else { return noErr }
|
let delegate = Unmanaged<AppDelegate>
|
.fromOpaque(userData)
|
.takeUnretainedValue()
|
DispatchQueue.main.async {
|
delegate.toggleOverlay()
|
}
|
return noErr
|
},
|
1,
|
&eventSpec,
|
selfPtr,
|
nil
|
)
|
}
|
|
// MARK: - Preferences
|
|
/// Hide overlay when any other window becomes key (catches Cmd+, via SwiftUI Settings).
|
/// Suppressed while in edit mode to prevent false dismissals.
|
private func observeOtherWindows() {
|
NotificationCenter.default.addObserver(
|
forName: NSWindow.didBecomeKeyNotification,
|
object: nil,
|
queue: .main
|
) { [weak self] notification in
|
guard let self,
|
let keyWindow = notification.object as? NSWindow,
|
keyWindow != self.overlayWindow,
|
!self.isInEditMode
|
else { return }
|
|
// Settings/Preferences window -> float it above overlay for real-time preview.
|
if self.isSettingsWindowCandidate(keyWindow) {
|
self.prepareSettingsWindow(keyWindow)
|
return
|
}
|
|
self.hideOverlay()
|
}
|
}
|
|
/// Settings must always appear centered over the current overlay view and float above it.
|
private func prepareSettingsWindow(_ window: NSWindow) {
|
window.identifier = NSUserInterfaceItemIdentifier("TagLauncherPreferencesWindow")
|
let settingsSize = NSSize(width: 880, height: 460)
|
window.minSize = settingsSize
|
window.maxSize = settingsSize
|
if abs(window.frame.width - settingsSize.width) > 0.5 || abs(window.frame.height - settingsSize.height) > 0.5 {
|
window.setFrame(
|
NSRect(origin: window.frame.origin, size: settingsSize),
|
display: false
|
)
|
}
|
|
if let overlayWindow, overlayWindow.isVisible {
|
center(window, over: overlayWindow.frame)
|
window.level = NSWindow.Level(rawValue: overlayWindow.level.rawValue + 1)
|
} else if let screen = screenUnderMouse() {
|
center(window, over: screen.visibleFrame)
|
window.level = .floating
|
}
|
|
window.collectionBehavior.formUnion([.moveToActiveSpace, .fullScreenAuxiliary])
|
window.makeKeyAndOrderFront(nil)
|
window.orderFrontRegardless()
|
settingsWindow = window
|
}
|
|
private func isSettingsWindowCandidate(_ window: NSWindow) -> Bool {
|
if window == settingsWindow { return true }
|
if window.identifier?.rawValue == "TagLauncherPreferencesWindow" { return true }
|
guard NSApp.windows.contains(window),
|
window != overlayWindow,
|
window.isVisible,
|
!(window is NSPanel)
|
else { return false }
|
return true
|
}
|
|
private func center(_ window: NSWindow, over rect: NSRect) {
|
let frame = window.frame
|
let origin = NSPoint(
|
x: rect.midX - frame.width / 2,
|
y: rect.midY - frame.height / 2
|
)
|
window.setFrameOrigin(origin)
|
}
|
|
private func screenUnderMouse() -> NSScreen? {
|
let mousePoint = NSEvent.mouseLocation
|
return NSScreen.screens.first(where: {
|
NSMouseInRect(mousePoint, $0.frame, false)
|
}) ?? NSScreen.main ?? NSScreen.screens.first
|
}
|
|
/// Clean up settingsWindow reference when the Settings window closes.
|
private func observeSettingsClose() {
|
NotificationCenter.default.addObserver(
|
forName: NSWindow.willCloseNotification,
|
object: nil,
|
queue: .main
|
) { [weak self] notification in
|
guard let self,
|
let closingWindow = notification.object as? NSWindow,
|
closingWindow == self.settingsWindow
|
else { return }
|
self.settingsWindow = nil
|
}
|
}
|
|
/// Track whether the overlay is in edit mode to suppress auto-dismiss.
|
private func observeEditMode() {
|
NotificationCenter.default.addObserver(
|
forName: .tagLauncherEditModeChanged,
|
object: nil,
|
queue: .main
|
) { [weak self] notification in
|
self?.isInEditMode = (notification.userInfo?["active"] as? Bool) ?? false
|
}
|
}
|
|
@objc private func openPreferences() {
|
// Don't hide overlay — keep it visible for real-time setting preview.
|
NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
|
prepareSettingsWindowWhenAvailable(retries: 5)
|
}
|
|
private func prepareSettingsWindowWhenAvailable(retries: Int) {
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.03) { [weak self] in
|
guard let self else { return }
|
let candidates = NSApp.windows.filter { window in
|
self.isSettingsWindowCandidate(window)
|
}
|
candidates.forEach { self.prepareSettingsWindow($0) }
|
if candidates.isEmpty && retries > 0 {
|
self.prepareSettingsWindowWhenAvailable(retries: retries - 1)
|
}
|
}
|
}
|
|
@objc private func switchLanguage(_ sender: NSMenuItem) {
|
guard let code = sender.representedObject as? String else { return }
|
L10n.switchTo(code)
|
}
|
}
|
|
// MARK: - NSView-level backdrop dismiss (works even if SwiftUI rendering is slow)
|
|
final class DismissibleHostingView<Content: View>: NSHostingView<Content> {
|
private let onBackdropTap: () -> Void
|
|
@MainActor required init(rootView: Content) {
|
self.onBackdropTap = {}
|
super.init(rootView: rootView)
|
}
|
|
init(rootView: Content, onBackdropTap: @escaping () -> Void) {
|
self.onBackdropTap = onBackdropTap
|
super.init(rootView: rootView)
|
}
|
|
@available(*, unavailable)
|
required init?(coder: NSCoder) { fatalError() }
|
|
override func mouseDown(with event: NSEvent) {
|
let location = convert(event.locationInWindow, from: nil)
|
guard let hit = hitTest(location) else {
|
super.mouseDown(with: event)
|
return
|
}
|
if hit == self {
|
onBackdropTap()
|
return
|
}
|
// Recursively search hit subtree for TextFieldContainer or NSTextField.
|
// NSHostingView.hitTest may return a SwiftUI-internal wrapper — the
|
// actual AppKit subview may be nested deeper.
|
if let container = findTextFieldContainer(in: hit) {
|
container.mouseDown(with: event)
|
return
|
}
|
if let tf = findNSTextField(in: hit) {
|
tf.window?.makeFirstResponder(tf)
|
tf.mouseDown(with: event)
|
return
|
}
|
// Forward to any other NSControl we find
|
if let control = findNSControl(in: hit) {
|
control.mouseDown(with: event)
|
return
|
}
|
super.mouseDown(with: event)
|
}
|
|
private func findTextFieldContainer(in view: NSView) -> TextFieldContainer? {
|
if let container = view as? TextFieldContainer { return container }
|
for sub in view.subviews {
|
if let found = findTextFieldContainer(in: sub) { return found }
|
}
|
return nil
|
}
|
|
private func findNSTextField(in view: NSView) -> NSTextField? {
|
if let tf = view as? NSTextField, tf.isEditable { return tf }
|
for sub in view.subviews {
|
if let found = findNSTextField(in: sub) { return found }
|
}
|
return nil
|
}
|
|
private func findNSControl(in view: NSView) -> NSControl? {
|
if let control = view as? NSControl { return control }
|
for sub in view.subviews {
|
if let found = findNSControl(in: sub) { return found }
|
}
|
return nil
|
}
|
}
|
|
// MARK: - Preferences View
|
|
struct PreferencesView: View {
|
private let settingsWindowWidth: CGFloat = 880
|
private let settingsContentWidth: CGFloat = 820
|
private let settingsLabelWidth: CGFloat = 160
|
private let widePickerWidth: CGFloat = 650
|
private let compactPickerWidth: CGFloat = 320
|
|
@AppStorage("tagFontSize") private var tagFontSize: Double = 18
|
@AppStorage("iconSize") private var iconSize: Double = 56
|
@AppStorage("tagPosition") private var tagPosition = "left"
|
@AppStorage("defaultGroupName") private var defaultGroupName = "Other"
|
@AppStorage("displayMode") private var displayMode = "flat"
|
@AppStorage("hideAppNames") private var hideAppNames = false
|
@AppStorage("showDockIcon") private var showDockIcon = false
|
@AppStorage("launchAtLogin") private var launchAtLogin = true
|
@State private var selectedLanguage = L10n.currentCode
|
@State private var isRefreshingLanguage = false
|
@State private var allApps: [AppInfo] = []
|
@State private var tagColors: [String: Int] = [:]
|
|
private func scanApps() {
|
DispatchQueue.global(qos: .userInitiated).async {
|
var apps = AppIndexer.scan()
|
let store = TagDatabase.load()
|
apps = TagEditor.annotate(apps: apps)
|
let colors = store.tags.mapValues { $0.color }
|
DispatchQueue.main.async {
|
allApps = apps
|
tagColors = colors
|
}
|
}
|
}
|
|
private func exportTags() {
|
let panel = NSSavePanel()
|
panel.title = tr("settings.export")
|
panel.nameFieldStringValue = "TagLauncher-tags.json"
|
panel.allowedContentTypes = [.json]
|
panel.begin { response in
|
guard response == .OK, let url = panel.url else { return }
|
do {
|
try TagDatabase.exportTo(url)
|
} catch {
|
fputs("[TagLauncher] Export failed: \(error)\n", stderr)
|
showDataAlert(title: tr("settings.exportFailed"), message: error.localizedDescription)
|
}
|
}
|
}
|
|
private func importTags() {
|
let panel = NSOpenPanel()
|
panel.title = tr("settings.import")
|
panel.allowedContentTypes = [.json]
|
panel.allowsMultipleSelection = false
|
panel.begin { response in
|
guard response == .OK, let url = panel.url else { return }
|
do {
|
_ = try TagDatabase.importFrom(url)
|
scanApps()
|
} catch {
|
fputs("[TagLauncher] Import failed: \(error)\n", stderr)
|
showDataAlert(title: tr("settings.importFailed"), message: error.localizedDescription)
|
}
|
}
|
}
|
|
private func showDataAlert(title: String, message: String) {
|
let alert = NSAlert()
|
alert.messageText = title
|
alert.informativeText = message
|
alert.alertStyle = .warning
|
alert.runModal()
|
}
|
|
private var appVersion: String {
|
Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "?"
|
}
|
private var buildVersion: String {
|
Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "?"
|
}
|
private var languageColumns: [GridItem] {
|
[
|
GridItem(.flexible(minimum: 220), spacing: 18, alignment: .top),
|
GridItem(.flexible(minimum: 220), spacing: 18, alignment: .top),
|
]
|
}
|
|
var body: some View {
|
ZStack {
|
TabView {
|
// Tab 1: Language
|
VStack(alignment: .leading, spacing: 10) {
|
Text(tr("settings.language"))
|
.font(.headline)
|
Text(tr("settings.languageDesc"))
|
.font(.caption)
|
.foregroundStyle(.secondary)
|
|
ScrollView {
|
LazyVGrid(columns: languageColumns, alignment: .leading, spacing: 6) {
|
ForEach(L10n.supported, id: \.code) { language in
|
Button {
|
selectedLanguage = language.code
|
} label: {
|
HStack(spacing: 8) {
|
Image(systemName: selectedLanguage == language.code ? "checkmark" : "circle")
|
.font(.system(size: 12, weight: .semibold))
|
.foregroundStyle(selectedLanguage == language.code ? Color.accentColor : Color.secondary.opacity(0.28))
|
.frame(width: 16)
|
Text(language.name)
|
.foregroundStyle(.primary)
|
.lineLimit(1)
|
.truncationMode(.tail)
|
Spacer(minLength: 0)
|
}
|
.padding(.horizontal, 8)
|
.padding(.vertical, 5)
|
.contentShape(Rectangle())
|
}
|
.buttonStyle(.plain)
|
.accessibilityLabel("\(tr("settings.languagePicker")) \(language.name)")
|
}
|
}
|
.padding(.vertical, 4)
|
}
|
}
|
.frame(maxWidth: settingsContentWidth, alignment: .leading)
|
.tabItem { Label(tr("settings.language"), systemImage: "globe") }
|
.padding()
|
|
// Tab 2: General
|
VStack(alignment: .leading, spacing: 0) {
|
// Toggle row — centered as a rectangular block
|
HStack(spacing: 20) {
|
if AppDelegate.supportsLaunchAtLogin {
|
Toggle(tr("settings.launchAtLogin"), isOn: $launchAtLogin)
|
.onChange(of: launchAtLogin) { _, enabled in
|
if enabled {
|
AppDelegate.enableLaunchAtLogin()
|
} else {
|
AppDelegate.disableLaunchAtLogin()
|
}
|
}
|
}
|
Toggle(tr("settings.showInDock"), isOn: $showDockIcon)
|
Toggle(tr("settings.hideAppNames"), isOn: $hideAppNames)
|
}
|
.frame(maxWidth: .infinity, alignment: .center)
|
.padding(.bottom, 16)
|
|
Divider()
|
.padding(.bottom, 16)
|
|
// Two-column layout: each row label (right-aligned) + controls (left-aligned)
|
// All pickers and descriptions share the same left edge
|
VStack(spacing: 16) {
|
HStack(alignment: .top, spacing: 16) {
|
Text(tr("settings.appListStyle"))
|
.frame(width: settingsLabelWidth, alignment: .trailing)
|
VStack(alignment: .leading, spacing: 4) {
|
Picker("", selection: $displayMode) {
|
Text(tr("settings.flat")).tag("flat")
|
Text(tr("settings.container")).tag("container")
|
Text(tr("settings.coloredContainer")).tag("coloredContainer")
|
Text(tr("settings.gridContainer")).tag("gridContainer")
|
Text(tr("settings.coloredGridContainer")).tag("coloredGridContainer")
|
}
|
.pickerStyle(.segmented)
|
.frame(width: widePickerWidth, alignment: .leading)
|
Text(tr("settings.flatDesc"))
|
.font(.caption).foregroundStyle(.secondary)
|
.fixedSize(horizontal: false, vertical: true)
|
}
|
}
|
HStack(alignment: .top, spacing: 16) {
|
Text(tr("settings.tagPosition"))
|
.frame(width: settingsLabelWidth, alignment: .trailing)
|
VStack(alignment: .leading, spacing: 4) {
|
Picker("", selection: $tagPosition) {
|
Text(tr("settings.left")).tag("left")
|
Text(tr("settings.right")).tag("right")
|
Text(tr("settings.top")).tag("top")
|
}
|
.pickerStyle(.segmented)
|
.frame(width: compactPickerWidth, alignment: .leading)
|
Text(tr("settings.tagPosDesc"))
|
.font(.caption).foregroundStyle(.secondary)
|
.fixedSize(horizontal: false, vertical: true)
|
}
|
}
|
HStack(alignment: .top, spacing: 16) {
|
Text(tr("settings.tagFontSize"))
|
.frame(width: settingsLabelWidth, alignment: .trailing)
|
VStack(alignment: .leading, spacing: 4) {
|
Picker("", selection: $tagFontSize) {
|
ForEach([16.0, 18.0, 20.0, 22.0, 24.0, 26.0], id: \.self) { size in
|
Text("\(Int(size))").tag(size)
|
}
|
}
|
.pickerStyle(.segmented)
|
.frame(width: compactPickerWidth, alignment: .leading)
|
Text(tr("settings.tagFontDesc"))
|
.font(.caption).foregroundStyle(.secondary)
|
.fixedSize(horizontal: false, vertical: true)
|
}
|
}
|
HStack(alignment: .top, spacing: 16) {
|
Text(tr("settings.iconSize"))
|
.frame(width: settingsLabelWidth, alignment: .trailing)
|
VStack(alignment: .leading, spacing: 4) {
|
Picker("", selection: $iconSize) {
|
ForEach([40.0, 48.0, 56.0, 64.0, 72.0, 80.0], id: \.self) { size in
|
Text("\(Int(size))").tag(size)
|
}
|
}
|
.pickerStyle(.segmented)
|
.frame(width: compactPickerWidth, alignment: .leading)
|
Text(tr("settings.iconSizeDesc"))
|
.font(.caption).foregroundStyle(.secondary)
|
.fixedSize(horizontal: false, vertical: true)
|
}
|
}
|
}
|
}
|
.frame(maxWidth: settingsContentWidth, alignment: .center)
|
.padding()
|
.tabItem { Label(tr("settings.general"), systemImage: "gearshape") }
|
|
// Tab 3: Tags
|
VStack(spacing: 0) {
|
TagEditorView(
|
tagColors: $tagColors,
|
excludedTagNames: ["Mac自带", defaultGroupName],
|
onRefresh: { scanApps() }
|
)
|
}
|
.padding(.leading, 16)
|
.tabItem { Label(tr("settings.tags"), systemImage: "tag.fill") }
|
.onAppear { scanApps() }
|
|
// Tab 4: Data
|
Form {
|
Section {
|
HStack(spacing: 12) {
|
Button(tr("settings.export")) { exportTags() }
|
.buttonStyle(.bordered)
|
Button(tr("settings.import")) { importTags() }
|
.buttonStyle(.bordered)
|
}
|
Text(tr("settings.backupDesc"))
|
.font(.caption)
|
.foregroundStyle(.secondary)
|
} header: {
|
Text(tr("settings.backup"))
|
}
|
}
|
.tabItem { Label(tr("settings.data"), systemImage: "externaldrive.fill") }
|
.padding()
|
|
// Tab 5: About
|
Form {
|
Section {
|
HStack {
|
if let icon = NSImage(named: NSImage.applicationIconName) {
|
Image(nsImage: icon)
|
.resizable()
|
.frame(width: 128, height: 128)
|
}
|
VStack(alignment: .leading, spacing: 4) {
|
Text("TagLauncher")
|
.font(.title2)
|
.fontWeight(.semibold)
|
Text(tr("app.description"))
|
.font(.body)
|
.foregroundStyle(.secondary)
|
Text("\(tr("app.version")) \(appVersion) (\(tr("app.build")) \(buildVersion))")
|
.font(.callout)
|
.foregroundStyle(.tertiary)
|
|
Divider()
|
.padding(.vertical, 4)
|
|
Text("万物之中,希望最美")
|
.font(.caption)
|
.foregroundStyle(.secondary)
|
Text("永桔@2026-18602102518")
|
.font(.caption)
|
.foregroundStyle(.secondary)
|
}
|
}
|
.padding(.leading, 100)
|
}
|
}
|
.tabItem { Label(tr("settings.about"), systemImage: "info.circle") }
|
.padding()
|
}
|
.onChange(of: selectedLanguage) { _, code in
|
L10n.switchTo(code)
|
}
|
.onReceive(NotificationCenter.default.publisher(for: .appLanguageDidChange)) { notification in
|
if let code = notification.userInfo?["code"] as? String {
|
selectedLanguage = code
|
}
|
showLanguageRefresh()
|
}
|
|
if isRefreshingLanguage {
|
ZStack {
|
Color(nsColor: .windowBackgroundColor)
|
.opacity(0.84)
|
Image(systemName: "arrow.clockwise")
|
.font(.system(size: 30, weight: .regular))
|
.symbolRenderingMode(.hierarchical)
|
.foregroundStyle(.secondary)
|
}
|
.transition(.opacity)
|
}
|
}
|
.frame(width: settingsWindowWidth, height: 460)
|
}
|
|
private func showLanguageRefresh() {
|
isRefreshingLanguage = true
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.35) {
|
withAnimation(.easeOut(duration: 0.18)) {
|
isRefreshingLanguage = false
|
}
|
}
|
}
|
}
|