I was alerted by Christian Tietze of a pretty bad usability bug in macOS High Sierra. If you are running a Mac app, click the “Help” menu, and then dismiss it, whatever UI element you were focused on in the app loses its focus and does not regain it after dismissing the menu.
The problem is so bad that tabbing, clicking other UI elements, even switching to another app and back does not restore focus on the window’s responders. If the focus was on an NSTextView, such as the editor in MarsEdit, then the blinking cursor continues to animate, but keystrokes are ignored and simply cause the app to beep.
Christian filed a bug, and shared a workaround: set the delegate of the Help menu to your app’s delegate, and listen for the “menuDidClose” delegate method. If it’s the Help menu, restore focus manually.
I generalized this workaround to an approach that should work for whatever window, and whatever responder is currently focused when the Help menu is opened. By saving the window and the responder at “menuWillOpen” time, it can be precisely restored afterwards:
private weak var lastKeyWindow: NSWindow? = nil private weak var lastResponder: NSResponder? = nil func menuWillOpen(_ menu: NSMenu) { if menu == NSApp.helpMenu { if let activeWindow = NSApp.keyWindow { self.lastKeyWindow = activeWindow if let activeResponder = activeWindow.firstResponder { self.lastResponder = activeResponder } } // If the responder is a field editor, then save // the delegate, which is e.g. the NSTextField being edited, // rather than the ephemeral NSTextView which will be // removed when editing stops. if let textView = lastResponder as? NSTextView, textView.isFieldEditor { if let realTarget = textView.delegate as? NSResponder { lastResponder = realTarget } } } } func menuDidClose(_ menu: NSMenu) { if menu == NSApp.helpMenu { if doWorkaround { if let actualKeyWindow = self.lastKeyWindow { actualKeyWindow.makeKeyAndOrderFront(nil) actualKeyWindow.makeFirstResponder(self.lastResponder) } } } }
Note that I didn’t clear out the weak var references to the lastKeyWindow and lastResponder. The reason is because part of the bug here involved NSMenu’s menuWillOpen and menuDidClose getting called more often than they probably should be. It’s probably the root issues that is causing the Help menu to excessively take control of the key window. It turns out we are going to get called on menuDidClose twice, so we need to be sure the desired target window and responder are still available the second time around.
As Christian points out, the workaround fixes the worst aspect of the bug: locking up the UI so that typing is ignored, but the focus ring around the target text field doesn’t always get redrawn as expected. My theory is that the focus ring animation is in the process of drawing when the second “menu will open” event is generated, causing the Help menu to reactive itself. The field being reactivate again very shortly after somehow doesn’t trigger the need to redraw the focus ring as you might expect.
I filed an additional bug, Radar #39436005, including a sample project that demonstrates both the bug and the workaround. Until Apple fixes this, Mac developers may want to implement a workaround along the lines demonstrated here. Given the horrible user experience associated with this bug, hopefully Apple will fix it promptly!