feedback-assistant / reports

Open collection of Apple Feedback Assistant reports
213 stars 2 forks source link

FB10184971: There is no way to open the settings window from an app using only MenuBarExtra in SwiftUI #327

Open sindresorhus opened 2 years ago

sindresorhus commented 2 years ago

Description

When you make a menu bar-only app using the new MenuBarExtra scene in SwiftUI, there’s no way to open the settings window from the menu bar window or menu since the menu bar is not available (because you have LSUIElement in Info.plist)

For example:

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        Settings {
            Text("Hello")
        }
        MenuBarExtra {
            Button("Bar") {}
        } label: {
            Text("Foo")
        }
    }
}

Possible solutions:

  1. Add a SettingsButton view which is a button that simply opens the settings window.
  2. Add a openSettings environment value, similar to the new openWindow environment value *.

Having a way to open the settings window is important as most apps need some kind of settings.

https://developer.apple.com/documentation/swiftui/environmentvalues/openwindow/

zshannon commented 1 year ago

@sindresorhus try NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil). Eg:

import SwiftUI

@main
struct TestApp: App {
    var body: some Scene {
        Settings {
            Text("Hello")
        }
        MenuBarExtra {
            Button("Bar") {
                             NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
                         }
        } label: {
            Text("Foo")
        }
    }
}
sindresorhus commented 1 year ago

Thanks. I am aware of the workaround.

nahive commented 1 year ago

@sindresorhus From macOS 10.14 new SettingsLink is available to use. https://developer.apple.com/documentation/swiftui/settingslink