feedback-assistant / reports

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

FB11965330: It should be allowed to use OpenWindowAction directly in SwiftUI App struct #380

Open sindresorhus opened 1 year ago

sindresorhus commented 1 year ago

Description

Currently, if you try to use it in App, you get a warning: Accessing Environment's value outside of being installed on a View. This will always read the default value and will not update.

The use-cases for needing this are:

My specific use-case is to show a custom notification window at certain times. Currently, I have to use an AppKit window for this as it’s not possible to open a Window outside of being in a SwiftUI view, and my app is mixed SwiftUI and AppKit. Another use-case I have is to be able to open a Window from a AppKit style menu bar app (MenuBarExtra is still too limited to use for most use-cases).

The SwiftUI window system is currently only optimized for a perfect future where everything is inside a SwiftUI view lifecycle, but this won’t be reality for most apps for many years.

Alternatively, provide a method to open SwiftUI windows based on their identifier from anywhere, not from the openAction environment value.

Example:

@main
struct AppMain: App {
    @Environment(\.openWindow) private var openWindow

    init() {
        onSomeEvent {
            openWindow(id: "calculators")
        }
    }

    var body: some Scene {
        MenuBarExtra {
            // …
        }
        Window("Calculators", id: "calculators") {
            CalculatorsScreen()
        }
    }
}
tommcoates commented 3 months ago

Totally agree with this need. I have a Document-based MacOS app that also has a Splash-screen startup Window. I need that window to open at startup but it won't unless it is located first in body{} before the DocumentGroup. But if it is first, then other parts of UI (the Cmd-N shortcut on File->New menu) stop working. We either need the ability to control what opens at App startup, or we need the ability to programmatically open windows, as from the AppDelegate, or init(). Currently I can make this work but the system emits the serious looking warning every time the program starts up.

sindresorhus commented 3 months ago

@tommcoates This repo is a manual mirror of Feedback Assistant reports. Apple will not see your comment. Submit a duplicate of this report to Apple with your added comments.