SwiftUI MenuBar App with Assertion on @main
Asked Answered
M

1

0

I have a SwiftUI Menu Bar App with no visible Windows on startup, except an entry in the Menu Bar with a Button showing a View on click.

This works great, but sporadically, the App throws an assertion and the MenuBar Button is not added, though its impossible to click it.

Here is what does not solve the issue:

  • Cleaning Build + Derived Data
  • Reboots

Code in AppDelegate (needed for the MenuBar)

@main // Assertion thrown here
struct MenuBarApp: App {
    
    @Environment(\.scenePhase) var scenePhase
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            // This surpresses opening an empty Window on launch
            ZStack{
                EmptyView()
            }.hidden() // removing .hidden() sometimes make the assertion go away, sometimes having .hidden() here triggers it

            // Sometimes, a second ZStack makes the assertion dissappear, sometimes it triggers it.
        }
    }
}

The assertion reads:

2022-05-24 12:58:06.263464+0200 MenuBar[7119:96419] *** Assertion failure in void _NSWindowSetFrameIvar(NSWindow *, NSRect)(), NSWindow.m:935

2022-05-24 13:04:40.477258+0200 MenuBar[7119:96419] Invalid parameter not satisfying: <SwiftUI.SwiftUIWindow: 0x159e63780>. "frame=CGRectContainsRect(CGRectMake((CGFloat)INT_MIN, (CGFloat)INT_MIN, (CGFloat)INT_MAX - (CGFloat)INT_MIN, (CGFloat)INT_MAX - (CGFloat)INT_MIN), frame)"

On other machines the build and launch of the same Code runs perfectly fine.

func applicationDidFinishLaunching(_ notification: Notification) is not getting called in the assert case

macOS 12.4, M1 Max, XCode 13.3.1

Messina answered 24/5, 2022 at 11:15 Comment(3)
Interesting Point from @vidian, before he deleted his answer.Messina
I deleted the answer because the statement I have a SwiftUI Menu Bar App with no Windows is a bit misleading. It's a window which causes the error.Interviewer
Well, got your point, but it is the setup above ( with no windows ) that triggers the assertion. It also happens with more Window Groups, but i wanted to make it as short as possible to understand the issue. Nevertheless i think your hint goes into the right direction, removing the Empty WindowGroup and just working with the othersm settings ect. helped, so i assume that Empty WindowGroup is part of the problem. Thank you for that.Messina
P
0

Code in AppDelegate (needed for the MenuBar)

The AppDelegate is no longer needed with the release of MenuBarExtra as of Xcode 14.2, macOS 13 Ventura. A MenuBarExtra scene renders a persistent control in the system menu bar with the native SwiftUI lifecycle.

@main
struct MyStatubarMenuApp: App {
    var body: some Scene {
        // -- no WindowGroup required --
        // -- no AppDelegate needed --
        MenuBarExtra("😎") { 
            Text("Hello Status Bar Menu!")
        }
    }
}
Prismatic answered 8/2, 2023 at 22:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.