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"