Skip to content

MenuBarExtra does not work with Defaults #106

@Wouter01

Description

@Wouter01

macOS 13.0 introduced a new SwiftUI Scene for showing a menubar icon: MenuBarExtra.

When using the initializer with isInserted, the settings window will just hang and show a beachball if isInserted has a value from Defaults. If AppStorage is used, everything works as expected.

Broken code:

Note that showPreferencesWindow has been replaced by showSettingsWindow in macOS 13.0

struct TestApp: App {
    @Default(.showMenuBarIcon) private var showMenubarIcon
    var menuBar: some Scene {
        MenuBarExtra("test", systemImage: "plus", isInserted: $showMenubarIcon) {
            Button {
                NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
            } label: {
                Text("Preferences")
            }
            .keyboardShortcut(.init(",", modifiers: .command))
            Divider()
            Button {
                NSApp.terminate(nil)
            } label: {
                Text("Quit")
            }
            .keyboardShortcut(.init("q", modifiers: .command))
        }
    }
    
    var body: some Scene {
        menuBar
        
        Settings {
            PreferencesView()
        }
    }
}

Working code:

struct TestApp: App {
    @AppStorage("showMenuBarIcon") private var showMenubarIcon = true
    var menuBar: some Scene {
        MenuBarExtra("test", systemImage: "plus", isInserted: $showMenubarIcon) {
            Button {
                NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
            } label: {
                Text("Preferences")
            }
            .keyboardShortcut(.init(",", modifiers: .command))
            Divider()
            Button {
                NSApp.terminate(nil)
            } label: {
                Text("Quit")
            }
            .keyboardShortcut(.init("q", modifiers: .command))
        }
    }
    
    var body: some Scene {
        menuBar
        
        Settings {
            PreferencesView()
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions