Berechtigungen zeigen ihren Zustand live, Dock-Symbol wird wählbar
Die Einstellungen zeigten weiter "Anfragen", obwohl die Ortung längst lief: authorizationStatus ist eine Abfrage, keine Beobachtung, und ich hatte sie einmalig beim Öffnen gelesen. Jetzt meldet locationManagerDidChangeAuthorization die Änderung, WeatherModel führt daraus einen beobachtbaren Wert, und die Einstellungen aktualisieren sich, während das Fenster offen steht. Nach der Zustimmung wird sofort geladen statt beim nächsten Anlauf. Der Knopf "Systemeinstellungen öffnen" ganz unten zeigte fest auf den Kalenderbereich — wer ihn beim Standort drückte, landete am falschen Ort. Es gibt ihn nicht mehr; stattdessen führt jede Berechtigung in ihren eigenen Bereich, und der Knopf erscheint nur im Zustand "abgelehnt". Bei "noch nie gefragt" steht dort Anfragen, bei "erteilt" gar nichts: drei Zustände, drei Antworten. Genau daran ist die erste Fassung gescheitert. Dock-Symbol ist jetzt eine Einstellung, standardmäßig aus. Onyx läuft als LSUIElement — das war Absicht und keine Auslassung: es hat kein Hauptfenster und wäre im Dock ein Symbol, das beim Anklicken nichts öffnet, und es stünde im Programmumschalter zwischen den Programmen, mit denen man arbeitet. Wer es trotzdem will, schaltet es ein; die Wahl wird beim Start wieder angewendet.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
import AppKit
|
||||||
import OnyxNotch
|
import OnyxNotch
|
||||||
import OnyxWidgetKit
|
import OnyxWidgetKit
|
||||||
|
|
||||||
@@ -39,9 +40,24 @@ final class AppModel {
|
|||||||
didSet { defaults.set(showsMenuBarIcon, forKey: Keys.showsMenuBarIcon) }
|
didSet { defaults.set(showsMenuBarIcon, forKey: Keys.showsMenuBarIcon) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ob Onyx im Dock auftaucht.
|
||||||
|
///
|
||||||
|
/// Standardmäßig nicht: Onyx ist ein Werkzeug, das in der Notch und in der
|
||||||
|
/// Menüleiste lebt, hat kein Hauptfenster und wäre im Dock ein Symbol, das
|
||||||
|
/// beim Anklicken nichts öffnet. Es taucht dann auch im Programmumschalter
|
||||||
|
/// auf und steht dort zwischen den Programmen, mit denen man arbeitet.
|
||||||
|
var showsDockIcon: Bool {
|
||||||
|
didSet {
|
||||||
|
guard showsDockIcon != oldValue else { return }
|
||||||
|
defaults.set(showsDockIcon, forKey: Keys.showsDockIcon)
|
||||||
|
NSApp.setActivationPolicy(showsDockIcon ? .regular : .accessory)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private enum Keys {
|
private enum Keys {
|
||||||
static let displayPolicy = "onyx.displayPolicy"
|
static let displayPolicy = "onyx.displayPolicy"
|
||||||
static let showsMenuBarIcon = "onyx.showsMenuBarIcon"
|
static let showsMenuBarIcon = "onyx.showsMenuBarIcon"
|
||||||
|
static let showsDockIcon = "onyx.showsDockIcon"
|
||||||
}
|
}
|
||||||
|
|
||||||
init(store: LayoutStore = .standard(), defaults: UserDefaults = .standard) {
|
init(store: LayoutStore = .standard(), defaults: UserDefaults = .standard) {
|
||||||
@@ -57,6 +73,7 @@ final class AppModel {
|
|||||||
// Standardmäßig sichtbar: das Menüleistensymbol ist der einzige Weg
|
// Standardmäßig sichtbar: das Menüleistensymbol ist der einzige Weg
|
||||||
// zurück, wenn die Notch einmal nicht reagiert.
|
// zurück, wenn die Notch einmal nicht reagiert.
|
||||||
self.showsMenuBarIcon = defaults.object(forKey: Keys.showsMenuBarIcon) as? Bool ?? true
|
self.showsMenuBarIcon = defaults.object(forKey: Keys.showsMenuBarIcon) as? Bool ?? true
|
||||||
|
self.showsDockIcon = defaults.object(forKey: Keys.showsDockIcon) as? Bool ?? false
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Widgets
|
// MARK: - Widgets
|
||||||
|
|||||||
@@ -485,6 +485,38 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"settings.display.dockIcon": {
|
||||||
|
"localizations": {
|
||||||
|
"de": {
|
||||||
|
"stringUnit": {
|
||||||
|
"state": "translated",
|
||||||
|
"value": "Symbol im Dock anzeigen"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"en": {
|
||||||
|
"stringUnit": {
|
||||||
|
"state": "translated",
|
||||||
|
"value": "Show icon in the Dock"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"settings.display.dockIcon.hint": {
|
||||||
|
"localizations": {
|
||||||
|
"de": {
|
||||||
|
"stringUnit": {
|
||||||
|
"state": "translated",
|
||||||
|
"value": "Standardmäßig aus. Onyx lebt in der Notch und in der Menüleiste und hat kein Hauptfenster — im Dock wäre es ein Symbol, das beim Anklicken nichts öffnet, und es stünde im Programmumschalter zwischen den Programmen, mit denen du arbeitest."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"en": {
|
||||||
|
"stringUnit": {
|
||||||
|
"state": "translated",
|
||||||
|
"value": "Off by default. Onyx lives in the notch and the menu bar and has no main window — in the Dock it would be an icon that opens nothing, and it would sit in the app switcher among the apps you actually work with."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"version": "1.0"
|
"version": "1.0"
|
||||||
|
|||||||
@@ -52,6 +52,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
|
|
||||||
let model = AppModel()
|
let model = AppModel()
|
||||||
self.model = model
|
self.model = model
|
||||||
|
// Die gespeicherte Wahl gleich anwenden — sonst steht Onyx nach einem
|
||||||
|
// Neustart wieder ohne Dock-Symbol da, obwohl es eingeschaltet ist.
|
||||||
|
NSApp.setActivationPolicy(model.showsDockIcon ? .regular : .accessory)
|
||||||
|
|
||||||
let coordinator = NotchCoordinator(policy: model.displayPolicy) { _, presentation in
|
let coordinator = NotchCoordinator(policy: model.displayPolicy) { _, presentation in
|
||||||
NotchHostingView(
|
NotchHostingView(
|
||||||
|
|||||||
@@ -42,96 +42,108 @@ struct SettingsView: View {
|
|||||||
private struct PermissionSettings: View {
|
private struct PermissionSettings: View {
|
||||||
let calendarModel: CalendarModel
|
let calendarModel: CalendarModel
|
||||||
let weatherModel: WeatherModel
|
let weatherModel: WeatherModel
|
||||||
@State private var granted = false
|
@State private var calendarGranted = false
|
||||||
@State private var locationGranted = false
|
|
||||||
|
private var locationState: PermissionRow.State {
|
||||||
|
switch weatherModel.locationAuthorization {
|
||||||
|
case .granted: .granted
|
||||||
|
case .denied: .denied
|
||||||
|
case .undetermined: .undetermined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Form {
|
Form {
|
||||||
Section {
|
Section {
|
||||||
HStack {
|
PermissionRow(
|
||||||
Label("settings.permissions.calendar", systemImage: "calendar")
|
title: "settings.permissions.calendar",
|
||||||
Spacer()
|
symbol: "calendar",
|
||||||
if granted {
|
state: calendarGranted ? .granted
|
||||||
Label("settings.permissions.granted", systemImage: "checkmark.circle.fill")
|
: (calendarModel.authorization == .denied ? .denied : .undetermined),
|
||||||
.foregroundStyle(.green)
|
explanation: "settings.permissions.calendar.why",
|
||||||
.labelStyle(.titleAndIcon)
|
pane: .calendar,
|
||||||
} else {
|
request: { calendarGranted = await calendarModel.requestAccessNow() })
|
||||||
Button("settings.permissions.request") {
|
|
||||||
Task {
|
|
||||||
let ok = await calendarModel.requestAccessNow()
|
|
||||||
granted = ok
|
|
||||||
// macOS fragt nach einer Ablehnung nicht erneut.
|
|
||||||
// Dann hilft nur noch der Weg über die
|
|
||||||
// Systemeinstellungen — also dorthin führen.
|
|
||||||
if !ok { openPrivacySettings() }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Text("settings.permissions.calendar.why")
|
|
||||||
.font(.callout)
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
.fixedSize(horizontal: false, vertical: true)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
HStack {
|
PermissionRow(
|
||||||
Label("settings.permissions.location", systemImage: "location")
|
title: "settings.permissions.location",
|
||||||
Spacer()
|
symbol: "location",
|
||||||
if locationGranted {
|
// Direkt aus dem beobachtbaren Wert: der Zustand ändert
|
||||||
Label("settings.permissions.granted", systemImage: "checkmark.circle.fill")
|
// sich, während dieses Fenster offen steht.
|
||||||
.foregroundStyle(.green)
|
state: locationState,
|
||||||
.labelStyle(.titleAndIcon)
|
explanation: "settings.permissions.location.why",
|
||||||
} else if weatherModel.locationIsDenied {
|
pane: .location,
|
||||||
Button("settings.permissions.openSystem") { openLocationSettings() }
|
request: { weatherModel.requestLocationAuthorization() })
|
||||||
} else {
|
|
||||||
Button("settings.permissions.request") {
|
|
||||||
// Muss aus diesem Fenster kommen: macOS zeigt den
|
|
||||||
// Dialog nur, wenn eine App im Vordergrund ist.
|
|
||||||
weatherModel.requestLocationAuthorization()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Text("settings.permissions.location.why")
|
|
||||||
.font(.callout)
|
|
||||||
.foregroundStyle(.secondary)
|
|
||||||
.fixedSize(horizontal: false, vertical: true)
|
|
||||||
}
|
|
||||||
|
|
||||||
Section {
|
|
||||||
Button("settings.permissions.openSystem") { openPrivacySettings() }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.formStyle(.grouped)
|
.formStyle(.grouped)
|
||||||
.padding()
|
.padding()
|
||||||
.task {
|
.task {
|
||||||
granted = calendarModel.hasAccess
|
calendarGranted = calendarModel.hasAccess
|
||||||
guard !granted, !didAutoRequest,
|
guard !calendarGranted, calendarModel.authorization == .undetermined else { return }
|
||||||
calendarModel.authorization == .undetermined else { return }
|
// Kurz warten, bis das Fenster wirklich vorne steht: TCC zeigt den
|
||||||
didAutoRequest = true
|
// Dialog nur, wenn eine App im Vordergrund ist, und Aktivierung
|
||||||
|
// wirkt asynchron.
|
||||||
// Kurz warten, bis das Fenster wirklich vorne steht. TCC zeigt den
|
|
||||||
// Dialog nur, wenn eine App im Vordergrund ist, an die er gehören
|
|
||||||
// kann — und Aktivierung wirkt asynchron. Ohne diese Pause fragt
|
|
||||||
// Onyx, während es formal noch im Hintergrund liegt, und die
|
|
||||||
// Anfrage verpufft kommentarlos.
|
|
||||||
try? await Task.sleep(for: .milliseconds(400))
|
try? await Task.sleep(for: .milliseconds(400))
|
||||||
granted = await calendarModel.requestAccessNow()
|
calendarGranted = await calendarModel.requestAccessNow()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@State private var didAutoRequest = false
|
/// Eine Berechtigung mit ihrem Zustand und dem Weg, der von dort weiterführt.
|
||||||
|
///
|
||||||
|
/// Die drei Zustände brauchen drei verschiedene Antworten, und genau daran ist
|
||||||
|
/// die erste Fassung gescheitert: „noch nie gefragt" lässt sich mit einer
|
||||||
|
/// Anfrage lösen, „abgelehnt" nur über die Systemeinstellungen, und „erteilt"
|
||||||
|
/// braucht gar nichts.
|
||||||
|
private struct PermissionRow: View {
|
||||||
|
enum State { case granted, denied, undetermined }
|
||||||
|
enum Pane {
|
||||||
|
case calendar, location
|
||||||
|
|
||||||
private func openLocationSettings() {
|
/// Jede Berechtigung führt in ihren eigenen Bereich. Ein gemeinsamer
|
||||||
guard let url = URL(string:
|
/// Knopf, der immer im Kalender landet, ist schlimmer als keiner.
|
||||||
"x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices") else { return }
|
var url: URL? {
|
||||||
NSWorkspace.shared.open(url)
|
switch self {
|
||||||
|
case .calendar:
|
||||||
|
URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Calendars")
|
||||||
|
case .location:
|
||||||
|
URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_LocationServices")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func openPrivacySettings() {
|
let title: LocalizedStringKey
|
||||||
guard let url = URL(string:
|
let symbol: String
|
||||||
"x-apple.systempreferences:com.apple.preference.security?Privacy_Calendars") else { return }
|
let state: State
|
||||||
NSWorkspace.shared.open(url)
|
let explanation: LocalizedStringKey
|
||||||
|
let pane: Pane
|
||||||
|
let request: () async -> Void
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
HStack {
|
||||||
|
Label(title, systemImage: symbol)
|
||||||
|
Spacer()
|
||||||
|
switch state {
|
||||||
|
case .granted:
|
||||||
|
Label("settings.permissions.granted", systemImage: "checkmark.circle.fill")
|
||||||
|
.foregroundStyle(.green)
|
||||||
|
.labelStyle(.titleAndIcon)
|
||||||
|
case .undetermined:
|
||||||
|
Button("settings.permissions.request") { Task { await request() } }
|
||||||
|
case .denied:
|
||||||
|
// macOS fragt nach einer Ablehnung nicht mehr — ein
|
||||||
|
// Anfragen-Knopf wäre hier eine Lüge.
|
||||||
|
Button("settings.permissions.openSystem") {
|
||||||
|
if let url = pane.url { NSWorkspace.shared.open(url) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Text(explanation)
|
||||||
|
.font(.callout)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.fixedSize(horizontal: false, vertical: true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,6 +255,14 @@ private struct DisplaySettings: View {
|
|||||||
.font(.callout)
|
.font(.callout)
|
||||||
.foregroundStyle(.secondary)
|
.foregroundStyle(.secondary)
|
||||||
.fixedSize(horizontal: false, vertical: true)
|
.fixedSize(horizontal: false, vertical: true)
|
||||||
|
|
||||||
|
Divider()
|
||||||
|
|
||||||
|
Toggle("settings.display.dockIcon", isOn: $model.showsDockIcon)
|
||||||
|
Text("settings.display.dockIcon.hint")
|
||||||
|
.font(.callout)
|
||||||
|
.foregroundStyle(.secondary)
|
||||||
|
.fixedSize(horizontal: false, vertical: true)
|
||||||
}
|
}
|
||||||
.formStyle(.grouped)
|
.formStyle(.grouped)
|
||||||
.padding()
|
.padding()
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ final class LocationProvider: NSObject, CLLocationManagerDelegate {
|
|||||||
|
|
||||||
private let manager = CLLocationManager()
|
private let manager = CLLocationManager()
|
||||||
private var pending: [CheckedContinuation<CLLocation?, Never>] = []
|
private var pending: [CheckedContinuation<CLLocation?, Never>] = []
|
||||||
|
/// Wird gerufen, wenn der Nutzer die Erlaubnis erteilt oder entzieht.
|
||||||
|
var onAuthorizationChange: (() -> Void)?
|
||||||
|
|
||||||
override init() {
|
override init() {
|
||||||
super.init()
|
super.init()
|
||||||
@@ -59,6 +61,14 @@ final class LocationProvider: NSObject, CLLocationManagerDelegate {
|
|||||||
Task { @MainActor in self.resume(with: locations.last) }
|
Task { @MainActor in self.resume(with: locations.last) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Ohne diesen Rückruf merkt niemand, dass der Nutzer gerade zugestimmt
|
||||||
|
/// hat: `authorizationStatus` ist eine Abfrage, keine Beobachtung. Die
|
||||||
|
/// Einstellungen zeigten deshalb weiter „Anfragen", obwohl die Ortung
|
||||||
|
/// längst lief.
|
||||||
|
nonisolated func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
|
||||||
|
Task { @MainActor in self.onAuthorizationChange?() }
|
||||||
|
}
|
||||||
|
|
||||||
nonisolated func locationManager(_ manager: CLLocationManager,
|
nonisolated func locationManager(_ manager: CLLocationManager,
|
||||||
didFailWithError error: Error) {
|
didFailWithError error: Error) {
|
||||||
Task { @MainActor in
|
Task { @MainActor in
|
||||||
@@ -78,6 +88,12 @@ public final class WeatherKitSource {
|
|||||||
|
|
||||||
public init() {}
|
public init() {}
|
||||||
|
|
||||||
|
/// Weiterreichen, damit das Modell einen beobachtbaren Wert daraus machen kann.
|
||||||
|
public var onLocationAuthorizationChange: (() -> Void)? {
|
||||||
|
get { location.onAuthorizationChange }
|
||||||
|
set { location.onAuthorizationChange = newValue }
|
||||||
|
}
|
||||||
|
|
||||||
public var locationIsDenied: Bool { location.isDenied }
|
public var locationIsDenied: Bool { location.isDenied }
|
||||||
public var locationIsUndetermined: Bool { location.isUndetermined }
|
public var locationIsUndetermined: Bool { location.isUndetermined }
|
||||||
public var locationIsAuthorized: Bool { !location.isDenied && !location.isUndetermined }
|
public var locationIsAuthorized: Bool { !location.isDenied && !location.isUndetermined }
|
||||||
|
|||||||
@@ -3,12 +3,21 @@ import AppKit
|
|||||||
import OnyxDesign
|
import OnyxDesign
|
||||||
import OnyxWidgetKit
|
import OnyxWidgetKit
|
||||||
|
|
||||||
|
/// Der Berechtigungszustand der Ortung — als beobachtbarer Wert.
|
||||||
|
public enum LocationAuthorization: Equatable, Sendable {
|
||||||
|
case granted, denied, undetermined
|
||||||
|
}
|
||||||
|
|
||||||
/// Hält den Wetterstand für das Widget.
|
/// Hält den Wetterstand für das Widget.
|
||||||
@MainActor
|
@MainActor
|
||||||
@Observable
|
@Observable
|
||||||
public final class WeatherModel {
|
public final class WeatherModel {
|
||||||
|
|
||||||
public private(set) var state: WeatherState = .idle
|
public private(set) var state: WeatherState = .idle
|
||||||
|
/// Beobachtbar, damit die Einstellungen mitbekommen, wenn der Nutzer
|
||||||
|
/// zustimmt. Eine Abfrage auf `CLLocationManager` täte das nicht — sie
|
||||||
|
/// liefert nur den Wert zum Zeitpunkt des Lesens.
|
||||||
|
public private(set) var locationAuthorization: LocationAuthorization = .undetermined
|
||||||
public var place: WeatherPlace {
|
public var place: WeatherPlace {
|
||||||
didSet { guard place != oldValue else { return }; load(force: true) }
|
didSet { guard place != oldValue else { return }; load(force: true) }
|
||||||
}
|
}
|
||||||
@@ -20,22 +29,30 @@ public final class WeatherModel {
|
|||||||
public init(source: WeatherKitSource = WeatherKitSource(), place: WeatherPlace = .current) {
|
public init(source: WeatherKitSource = WeatherKitSource(), place: WeatherPlace = .current) {
|
||||||
self.source = source
|
self.source = source
|
||||||
self.place = place
|
self.place = place
|
||||||
|
syncAuthorization()
|
||||||
|
source.onLocationAuthorizationChange = { [weak self] in
|
||||||
|
guard let self else { return }
|
||||||
|
syncAuthorization()
|
||||||
|
// Nach der Zustimmung sofort laden statt auf den nächsten Anlauf
|
||||||
|
// zu warten.
|
||||||
|
if locationAuthorization == .granted { load(force: true) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public var locationIsDenied: Bool { source.locationIsDenied }
|
private func syncAuthorization() {
|
||||||
public var locationIsUndetermined: Bool { source.locationIsUndetermined }
|
locationAuthorization = if source.locationIsDenied { .denied }
|
||||||
public var locationIsAuthorized: Bool { source.locationIsAuthorized }
|
else if source.locationIsUndetermined { .undetermined }
|
||||||
|
else { .granted }
|
||||||
|
}
|
||||||
|
|
||||||
/// Fragt die Ortungsberechtigung an und lädt danach.
|
public var locationIsDenied: Bool { locationAuthorization == .denied }
|
||||||
|
public var locationIsUndetermined: Bool { locationAuthorization == .undetermined }
|
||||||
|
public var locationIsAuthorized: Bool { locationAuthorization == .granted }
|
||||||
|
|
||||||
|
/// Fragt die Ortungsberechtigung an. Das Ergebnis kommt über den
|
||||||
|
/// Delegaten zurück und aktualisiert `locationAuthorization` von selbst.
|
||||||
public func requestLocationAuthorization() {
|
public func requestLocationAuthorization() {
|
||||||
source.requestLocationAuthorization()
|
source.requestLocationAuthorization()
|
||||||
// Die Entscheidung fällt asynchron. Kurz warten und dann laden — die
|
|
||||||
// Alternative wäre ein Delegat quer durch drei Schichten für einen
|
|
||||||
// Vorgang, der einmal im Leben der App passiert.
|
|
||||||
Task { [weak self] in
|
|
||||||
try? await Task.sleep(for: .seconds(2))
|
|
||||||
self?.load(force: true)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lädt, wenn der Zwischenspeicher abgelaufen ist.
|
/// Lädt, wenn der Zwischenspeicher abgelaufen ist.
|
||||||
|
|||||||
Reference in New Issue
Block a user