SwiftUI toolbar items flashing while in full screen

Viewed 131

I'm working on a MacOS SwiftUI App that involves a stopwatch. When the app the in fullscreen and the stopwatch is running, the two buttons in the toolbar "flash" every second or so. The buttons are above a translucent sidebar, and "flash" but turning solid white for a very short amount of time.

This is what it normally looks like:

enter image description here

But it occasionally flashes like this:

enter image description here

This is my code for the stopwatch that causes the flashes while it's running:

class Stopwatch: ObservableObject {
    var secondsElapsed = 0.0
    @Published var secondsElapsedString = "0.00"
    @Published var isRunning = false
    var startTime: Date?
    var timer = Timer()
    
    func start() {
        startTime = Date()
        isRunning = true
        timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
            self.secondsElapsed = Date().timeIntervalSince(self.startTime!)
            self.secondsElapsedString = String(format: "%.1f", self.secondsElapsed)
        }
    }
    
    func stop() {
        timer.invalidate()
        isRunning = false
        secondsElapsed = Date().timeIntervalSince(self.startTime!)
        secondsElapsedString = String(format: "%.2f", self.secondsElapsed)
    }
    
    func reset() {
        secondsElapsed = 0
        secondsElapsedString = "0.0"
    }
}

And here is the code for the toolbar:

.toolbar {
    ToolbarItemGroup() {
        Button(action: toggleSidebar, label: {
            Image(systemName: "sidebar.left")
        })
        .help("Toggle the sidebar")
        
        Button {
            showingAddInstanceSheet = true
        } label: {
            Image(systemName: "plus")
        }
        .help("Create new instance")

    }
}

Any ideas what might be going on? Thanks!

0 Answers
Related