Cocoa: TCocoaWSPopupMenu.Popup() refactored

This commit is contained in:
rich2014 2023-10-21 00:33:37 +08:00
parent 275776771b
commit 7f7a01a35b

View File

@ -950,44 +950,28 @@ end;
class procedure TCocoaWSPopupMenu.Popup(const APopupMenu: TPopupMenu; const X, class procedure TCocoaWSPopupMenu.Popup(const APopupMenu: TPopupMenu; const X,
Y: Integer); Y: Integer);
var var
menu : TCocoaMenu; menu: TCocoaMenu;
view : NSView; point: TPoint;
w : NSWindow; screen: NSScreen;
px, py: Integer; mouseY: CGFloat;
menuY: Integer; menuY: CGFloat;
begin begin
if (not Assigned(APopupMenu)) or (APopupMenu.Handle=0) then if (not Assigned(APopupMenu)) or (APopupMenu.Handle=0) then
exit; exit;
px := x;
py := y;
menu := TCocoaMenu(APopupMenu.Handle); menu := TCocoaMenu(APopupMenu.Handle);
view := nil; point:= TPoint.Create( x, y );
w :=NSApp.keyWindow; screen:= getScreenFromHMonitor( CocoaWidgetSet.MonitorFromPoint(point, MONITOR_DEFAULTTONULL) );
// in macOS it's possible to "rightclick" without focusing a window
// so let's try to find the window
if not Assigned(w) then
w := GetCocoaWindowAtPos( NSMakePoint(px, Round(NSGlobalScreenHeight) - py) );
if Assigned(w) then mouseY:= NSGlobalScreenHeight - y;
begin if Assigned(screen) then begin
view := w.contentView; menuY:= screen.visibleFrame.origin.y + menu.size.height + 1;
if Assigned(view) then menuY:= max( mouseY, MenuY );
begin end else begin
// LCL Screen coordinate menuY:= mouseY;
menuY := Round(NSGlobalScreenHeight - w.screen.visibleFrame.origin.y - menu.size.height) - 1; end;
py := min(py, menuY);
view.lclScreenToLocal(px, py);
// have to flip again, because popUpMenuPositioningItem expects point
// to be in View coordinates and it does respect Flipped flag
if not view.isFlipped then
py := Round(view.frame.size.height) - py;
end;
end
else
py := Round(NSGlobalScreenHeight) - py;
menu.popUpMenuPositioningItem_atLocation_inView(nil, NSMakePoint(px, py), view); menu.popUpMenuPositioningItem_atLocation_inView(nil, NSMakePoint(x,menuY), nil);
APopupMenu.Close; // notify LCL popup menu APopupMenu.Close; // notify LCL popup menu
end; end;