A command modifying SceneStorage property for currently focused window (on macOS)?

I'd like to implement a command (application menu item) that would modify @SceneStorage property for currently selected window or tab.

My understanding is, because it's a SceneStorage, it has to be defined in my (main) View. And in my case it must be SceneStorage because each (main) View, regardless if it's a window or a tab may have this value different (different look and feel for given window - enum backed by @SceneStorage).

And it looks I'm stuck.

I found a sample here. Unfortunately FocusedBinding seems to work only for TextEditor view (as someone stated there), and even in this case it doesn't work if TextEditor is a ToolbarItem. And I need to make it work for an enum feeding View's ToolbarItem Picker.

Has anyone have any idea how I could solve this requirement?
I'd really like to give users the ability to modify window L&F using commands/menu, not only by clicking on UI elements.

BR,
Krystian
I just spent half a day struggling with a similar issue 🤬
I, too, read the other thread where an Apple engineer posted a sample.

I have finally found a solution based on this GitHub repo: https://github.com/jbmorley/swiftui-focus-demo
That repo provides a view modifier that installs a background NSView, which can become the first responder for the window.

Then this works:
Code Block swift
Text("Message: \(message.text)")
            .frame(width: 200, height: 200)
            .acceptsFirstResponder(isFirstResponder: $isFirst)
            .focusedValue(\.message, $message)


Note that focusedValue() has to be after the acceptsFirstResponder!


Dear Apple,
if you're reading this—please focus on documentation and samples for the next WWDC!
And while at it, please try using some of your own technologies. Try writing a complete application in SwiftUI (just the ColorPicker is not enough).
Thank you!





A command modifying SceneStorage property for currently focused window (on macOS)?
 
 
Q