feat(settings): 新增关于菜单和基于正式发布的更新检测

This commit is contained in:
2026-11-16 11:26:18 +08:00
parent b9abb970d9
commit 0442398407
16 changed files with 540 additions and 31 deletions
@@ -26,6 +26,7 @@ import Observation
private let visibility: (Bool) -> Void
private let menuTitle: (String) -> String
private let openSettings: () -> Void
private let openAbout: () -> Void
private var showingStatusMenu = false
private var isHiding = false
private var preferredContentHeight = PanelLayout.defaultHeight
@@ -34,11 +35,13 @@ import Observation
visibility: @escaping (Bool) -> Void,
menuTitle: @escaping (String) -> String = { $0 },
openSettings: @escaping () -> Void = {},
openAbout: @escaping () -> Void = {},
@ViewBuilder content: @escaping (PanelPresentation) -> Content) {
self.readings = readings
self.visibility = visibility
self.menuTitle = menuTitle
self.openSettings = openSettings
self.openAbout = openAbout
self.content = {
let host = NSHostingView(rootView: content($0))
// The controller owns the fixed panel frame; content must not negotiate window limits.
@@ -127,6 +130,9 @@ import Observation
let settings = NSMenuItem(title: menuTitle("settings"), action: #selector(settingsClicked), keyEquivalent: ",")
settings.target = self
menu.addItem(settings)
let about = NSMenuItem(title: menuTitle("about"), action: #selector(aboutClicked), keyEquivalent: "")
about.target = self
menu.addItem(about)
menu.addItem(.separator())
let quit = NSMenuItem(title: menuTitle("quit"), action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
quit.target = NSApp
@@ -134,6 +140,7 @@ import Observation
return menu
}
@objc private func aboutClicked() { hide(); openAbout() }
@objc private func settingsClicked() { hide(); openSettings() }
private func isOverStatusItem(_ screenPoint: NSPoint) -> Bool {
+10 -6
View File
@@ -5,9 +5,10 @@ struct MenuPanelView: View {
let metrics: MetricsStore
let preferences: AppearancePreferences
let keepAwake: KeepAwakeService
let settingsPresentation: SettingsPresentation
let updates: ReleaseUpdates
var presentation = PanelPresentation()
var onOpenSettings: () -> Void = {}
var onOpenAbout: () -> Void = {}
var onPreferredHeight: (CGFloat) -> Void = { _ in }
@Environment(\.locale) var locale
@State private var panelSize = PanelLayout.contentSize(visible: NSScreen.main?.visibleFrame ?? CGRect(x: 0, y: 0, width: 1000, height: 760))
@@ -205,11 +206,14 @@ struct MenuPanelView: View {
}
.help(metrics.isPaused ? Text("action.resume") : Text("action.pause"))
.accessibilityLabel(metrics.isPaused ? Text("action.resume") : Text("action.pause"))
Button {
NSApp.setActivationPolicy(.regular)
settingsPresentation.request()
onOpenSettings()
} label: {
if updates.available != nil {
Button(action: onOpenAbout) {
Image(systemName: "arrow.down.circle.fill").frame(width: 20, height: 20)
.foregroundStyle(Color.accentColor)
}
.help(Text("updates.available")).accessibilityLabel(Text("updates.available"))
}
Button(action: onOpenSettings) {
Image(systemName: "gearshape").frame(width: 20, height: 20)
}
.help(Text("action.settings")).accessibilityLabel(Text("action.settings"))
@@ -8,13 +8,13 @@ struct AppearanceSettingsView: View {
@Bindable var sampling: SamplingPreferences
let menuBar: MenuBarPreferences
let readings: () -> [MenuBarReading]
let presentation: SettingsPresentation
@Bindable var presentation: SettingsPresentation
@Bindable var updates: ReleaseUpdates
@Environment(\.locale) private var locale
@State private var selection = SettingsPage.general
var body: some View {
Group {
switch selection {
switch presentation.selection {
case .general: generalPage
case .menuBar: MenuBarSettingsPage(preferences: menuBar, readings: readings())
case .about: aboutPage
@@ -23,7 +23,7 @@ struct AppearanceSettingsView: View {
.padding(12)
.toolbar {
ToolbarItem(placement: .principal) {
NeutralTabPicker(title: "settings.title", selection: $selection,
NeutralTabPicker(title: "settings.title", selection: $presentation.selection,
options: [(.general, "settings.general"), (.menuBar, "settings.menuBar"), (.about, "settings.about")])
}
.sharedBackgroundVisibility(.hidden)
@@ -97,6 +97,29 @@ struct AppearanceSettingsView: View {
}
.padding(.vertical, 8)
}
Section {
Toggle("updates.automatic", isOn: $updates.automatic)
.toggleStyle(.checkbox)
HStack {
Button("updates.check") { updates.check() }
.disabled(updates.phase == .checking)
switch updates.phase {
case .idle: EmptyView()
case .checking: Text("updates.checking").foregroundStyle(.secondary)
case .failed: Text("updates.failed").foregroundStyle(.secondary)
case .checked:
if updates.available == nil { Text("updates.current").foregroundStyle(.secondary) }
}
}
if let release = updates.available {
let display = release.build.map { "\(release.tagName) (\($0))" } ?? release.tagName
LabeledContent {
Link("updates.releasePage", destination: release.page)
} label: {
Text("updates.new \(display)")
}
}
}
}
.formStyle(.grouped)
.padding(.horizontal, 12)
@@ -121,7 +144,3 @@ private extension AppAppearance {
}
}
private enum SettingsPage: Hashable {
case general, menuBar, about
}