Skip to content

Commit

Permalink
Avoid re-init view when settings change
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Apr 23, 2024
1 parent 6dae7dd commit 0c9340e
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions Aware/macOS/MenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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 {
Expand Down

0 comments on commit 0c9340e

Please sign in to comment.