Possible MacOS SwiftUI onFocusChange bug

I think I've found a bug with focusable(_: onFocusChange:) on MacOS with SwiftUI and wanted to sanity check it here before using feedback assistant. This is with Xcode 11.3.1


When I create the window, the VStack inside thinks it is in focus. But the onFocusChange closure never gets called to say it loses focus. As a result, both my VStacks in both my windows think they are infocus.


Demonstration project at https://github.com/darrellroot/focusBug/


Here's my ContentView which displays whether it thinks it is in focus:


struct ContentView: View {
    @State var inFocus = false
    let windowCount: Int
    var body: some View {
        VStack {
            Text("Focus Window \(windowCount)")
            inFocus ? Text("This window thinks it is in focus") : Text("This window does not think it is in focus")
        }.padding(50).focusable() { newFocus in
            debugPrint("onFocusChange: \(newFocus)")
            self.inFocus = newFocus
        }
    }
}


Here's my appDelegate code which spawns 1 window on launch and 1 more every time a particular menu is selected:


    var windows: [Int:NSWindow] = [:]
    var windowCount = 0

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        newWindow()
    }
    @IBAction func newFocusWindow(_ sender: NSMenuItem) {
        newWindow()
    }
    func newWindow() {
        windowCount = windowCount + 1
        let contentView = ContentView(windowCount: windowCount)
        let window = NSWindow(
            contentRect: NSRect(x: 100, y: 100, width: 1000, height: 1000),
            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
            backing: .buffered, defer: false)
        window.isReleasedWhenClosed = false
        windows[windowCount] = window
        window.title = "Focus Window \(self.windowCount)"
        window.tabbingMode = .disallowed
        window.center()
        //window.setFrameAutosaveName("Window \(self.windowCount)")
        window.contentView = NSHostingView(rootView: contentView)
        window.makeKeyAndOrderFront(nil)
    }


DebugPrint output shows that onFocusChange got called once per window:


2020-03-09 10:24:22.363001-0700 focusBug[2555:258395] Metal API Validation Enabled

"onFocusChange: true"

"onFocusChange: true"

Well, since nobody else ever said anything, I will: good catch, bro: excellently-documented, well reasoned, and an easily reproducible bug report.

Thank you for that.

Alas, I'm no longer a Dev with Apple. Now I'm just another Dev home-brewing something who stumbled into something tangentially similar.

But I did want you know someone DID notice, and DOES respect the extra effort. I can't fix the issue for myself, yet, much less for you (just started to investigate), but I've been a dev for well over 20 years, and I know when a report is actionable.

Ill update if I find a fix.

Yup the same bug ... A trivial feature that somehow does not work.

Possible MacOS SwiftUI onFocusChange bug
 
 
Q