From 0c9340e259780283d02f3b704d5d1038132cb687 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Tue, 23 Apr 2024 11:33:46 -0700 Subject: [PATCH] Avoid re-init view when settings change --- Aware/macOS/MenuBar.swift | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/Aware/macOS/MenuBar.swift b/Aware/macOS/MenuBar.swift index d6c7074..918efe6 100644 --- a/Aware/macOS/MenuBar.swift +++ b/Aware/macOS/MenuBar.swift @@ -16,29 +16,28 @@ private nonisolated(unsafe) let logger = Logger( ) struct MenuBar: Scene { - // User configurable idle time in seconds (defaults to 2 minutes) - @AppStorage("userIdleSeconds") private var userIdleSeconds: Int = 120 - - @AppStorage("formatStyle") private var timerFormatStyle: TimerFormatStyle.Style = .condensedAbbreviated - @AppStorage("showSeconds") private var showSeconds: Bool = false - - var userIdle: Duration { - .seconds(max(1, userIdleSeconds)) - } - var body: some Scene { MenuBarExtra { MenuBarContentView() } label: { - TimerMenuBarLabel(userIdle: userIdle, timerFormatStyle: timerFormatStyle, showSeconds: showSeconds) + TimerMenuBarLabel() } } } struct TimerMenuBarLabel: View { - let userIdle: Duration - var timerFormatStyle: TimerFormatStyle.Style - var showSeconds: Bool + @State private var timerState = TimerState() + @State private var statusBarButton: NSStatusBarButton? + + // User configurable idle time in seconds (defaults to 2 minutes) + @AppStorage("userIdleSeconds") private var userIdleSeconds: Int = 120 + + @AppStorage("formatStyle") private var timerFormatStyle: TimerFormatStyle.Style = .condensedAbbreviated + @AppStorage("showSeconds") private var showSeconds: Bool = false + + var userIdle: Duration { + .seconds(max(1, userIdleSeconds)) + } /// Set text refresh rate to 60 seconds, when minutes are shown private var textRefreshRate: TimeInterval { showSeconds ? 1.0 : 60.0 } @@ -51,9 +50,6 @@ struct TimerMenuBarLabel: View { ActivityMonitor.Configuration(userIdle: userIdle) } - @State private var timerState = TimerState() - @State private var statusBarButton: NSStatusBarButton? - var body: some View { Group { if let start = timerState.start {