Interesting Bug Pertaining to NSTableView and Context Menus
I am seeing what I believe to be an interesting bug on macOS 14.5 (23F79).
If you have an NSTableView
with a context menu, opening that context menu via right click while the app is in the background and then clicking elsewhere in the table view to dismiss the context menu brings the window to the foreground (as expected). However the table view will not respond to any clicks until the app is moved to the background and brought back into the foreground.
I see this behavior in Apple Mail, the Finder, and in other apps including my own. To reproduce this:
-
Check out this sample app using
git clone https://github.com/jbrayton/ActivateBug.git
. Observe that it creates a simpleNSTableView
with three rows. Run the app. -
Click the table view rows. Observe that they can be selected by clicking them as you would expect. Right-click in the table view and observe that you get a context menu.
-
Open Safari. Keep the two windows next two each other on the same screen. Make Safari the active app.
-
Right-click in the table view of the sample app.
-
Dismiss the resulting context menu by clicking elsewhere in the table view.
-
Try to select other rows by clicking them.
Expected result: I would expect clicking a table row to select it.
Actual result: Clicking in the table view has no effect.
Possibly related: If the NSApplicationDelegate
has an applicationDidBecomeActive
method, I would expect it to be called when bringing an app to the foreground following these steps. It is not.
This video demonstrates the issue with my sample app:
This video demonstrates the issue using Apple Mail:
I filed this as FB13806870.
I can think of a couple possible workarounds, but neither is great:
-
Create a local NSEvent monitor for
.rightMouseDown
events that callsNSApplication.shared.activate()
. But this method “doesn’t guarantee app activation”, so this will only work sometimes. -
Similarly create a local NSEvent monitor for
.rightMouseDown
events that simply ignores right clicks by returningnil
when the app is in the background. But that would result in context menus simply being unavailable when the app is in the background.