TPopupMenu pops at wrong place (y is inverted) if form not focused
Original Reporter info from Mantis: Clover
-
Reporter name: David Gray
Original Reporter info from Mantis: Clover
- Reporter name: David Gray
Description:
A TPopupMenu placed on a TPaintBox can be popped via a right-click from within a MouseDown event as follows:
TPaintBox.MouseDown (... x,y:Integer ...) // The usual MouseDown invoked via a right-click
begin
PopupMenu1.Popup (PaintBox1.ClientToScreen(Point(X, Y));
end;
This works fine if the app's window has focus/active. The popup menu appears where the user right-clicked.
However, it goes wrong if the app's window doesn't have focus. For example the user has left-clicked the desktop. Or a ShowMessage has just been dismissed. You can tell because the app's window has grayed out 'traffic light' buttons.
With the app in this state, a right-click causes the popup to appear with correct x but incorrect y. The y is inverted (clicking near the top of the form pops the menu near the bottom and vice versa).
The error is in TCocoaWSPopupMenu.Popup in module TCocoaWSMenus.
The line w := NSApp.keyWindow fails if the window is not focused. The subsequent NSView is never initialized.
Left clicking the app restores normal operation.
Steps to reproduce:
Create a form and a TPopupMenu.
Add a MouseDown event.
In the MouseDown event add this line of code:
PopupMenu1.Popup (ClientToScreen(Point(X, Y));
Run the app and verify PopupMenu1 appears wherever you right click.
Now click away from the app, such that the traffic light buttons are grayed out.
While the app is in this mode, right click the app again and verify that PopupMenu1 appears in the wrong place, wrt the y axis. Y coordinate appears to be 'flipped'.
Additional information:
I was using a TPaintBox to contain the TPopupMenu but it would surely go wrong with just a TForm.
Tested using Catalina and Big Sur.
Carbon did not have this issue.
Mantis conversion info:
- Mantis ID: 37877
- OS: macOS
- OS Build: 11.0 Beta 8
- Build: Stable release
- Platform: Big Sur
- Version: 2.0.10
- Fixed in revision: 64117 (#f490c67b)
- Monitored by: » Clover (David Gray)