mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-09-12 09:39:16 +02:00
Cocoa: show full popup menu by calculating the most appropriate popup position
This commit is contained in:
parent
aff96ceae0
commit
664c88804c
@ -28,6 +28,7 @@ uses
|
|||||||
MacOSAll,
|
MacOSAll,
|
||||||
// RTL
|
// RTL
|
||||||
sysutils,
|
sysutils,
|
||||||
|
math,
|
||||||
// LCL
|
// LCL
|
||||||
Controls, Forms, Menus, Graphics, LCLType, LMessages, LCLProc, Classes,
|
Controls, Forms, Menus, Graphics, LCLType, LMessages, LCLProc, Classes,
|
||||||
LCLMessageGlue, LCLStrConsts,
|
LCLMessageGlue, LCLStrConsts,
|
||||||
@ -945,48 +946,44 @@ 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;
|
||||||
view : NSView;
|
view : NSView;
|
||||||
w : NSWindow;
|
w : NSWindow;
|
||||||
px, py: Integer;
|
px, py: Integer;
|
||||||
|
menuY: Integer;
|
||||||
begin
|
begin
|
||||||
if Assigned(APopupMenu) and (APopupMenu.Handle<>0) then
|
if (not Assigned(APopupMenu)) or (APopupMenu.Handle=0) then
|
||||||
|
exit;
|
||||||
|
|
||||||
|
px := x;
|
||||||
|
py := y;
|
||||||
|
menu := TCocoaMenu(APopupMenu.Handle);
|
||||||
|
view := nil;
|
||||||
|
w :=NSApp.keyWindow;
|
||||||
|
// 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, py) );
|
||||||
|
|
||||||
|
if Assigned(w) then
|
||||||
begin
|
begin
|
||||||
// old method which doesn't consider position but supports 10.0+ (useless since we target 10.6+)
|
view := w.contentView;
|
||||||
{w:=NSApp.keyWindow;
|
if Assigned(view) then
|
||||||
if Assigned(w) then
|
|
||||||
begin
|
begin
|
||||||
NSMenu.popUpContextMenu_withEvent_forView( TCocoaMenu(APopupMenu.Handle),
|
menuY := round(w.screen.frame.size.height - w.screen.visibleFrame.origin.y - menu.size.height) - 1;
|
||||||
NSApp.currentEvent, NSView(w.contentView));
|
py := min(py, menuY);
|
||||||
end;}
|
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(NSScreenZeroHeight - py);
|
||||||
|
|
||||||
// New method for 10.6+
|
menu.popUpMenuPositioningItem_atLocation_inView(nil, NSMakePoint(px, py), view);
|
||||||
px := x;
|
APopupMenu.Close; // notify LCL popup menu
|
||||||
py := y;
|
|
||||||
view := nil;
|
|
||||||
w :=NSApp.keyWindow;
|
|
||||||
if not Assigned(w) then
|
|
||||||
// in macOS it's possible to "rightclick" without focusing a window
|
|
||||||
// so let's try to find the window
|
|
||||||
w := GetCocoaWindowAtPos( NSMakePoint(px, py) );
|
|
||||||
|
|
||||||
if Assigned(w) then
|
|
||||||
begin
|
|
||||||
view := w.contentView;
|
|
||||||
if Assigned(view) then
|
|
||||||
begin
|
|
||||||
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(NSScreenZeroHeight - py);
|
|
||||||
TCocoaMenu(APopupMenu.Handle).popUpMenuPositioningItem_atLocation_inView(
|
|
||||||
nil, NSMakePoint(px, py), view);
|
|
||||||
APopupMenu.Close; // notify LCL popup menu
|
|
||||||
end;
|
|
||||||
end;
|
end;
|
||||||
|
|
||||||
procedure ShortcutToKeyEquivalent(const AShortCut: TShortcut; out Key: NSString; out shiftKeyMask: NSUInteger);
|
procedure ShortcutToKeyEquivalent(const AShortCut: TShortcut; out Key: NSString; out shiftKeyMask: NSUInteger);
|
||||||
|
Loading…
Reference in New Issue
Block a user