From f490c67b0fbb53a44bb8fcbbcb3d6cd447bda641 Mon Sep 17 00:00:00 2001 From: dmitry Date: Mon, 9 Nov 2020 00:58:59 +0000 Subject: [PATCH] cocoa: if app is inactive searching through available windows in order to find one by mouse position. bug #37877 git-svn-id: trunk@64117 - --- lcl/interfaces/cocoa/cocoawsmenus.pas | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lcl/interfaces/cocoa/cocoawsmenus.pas b/lcl/interfaces/cocoa/cocoawsmenus.pas index 6787787290..9e7e060bcf 100644 --- a/lcl/interfaces/cocoa/cocoawsmenus.pas +++ b/lcl/interfaces/cocoa/cocoawsmenus.pas @@ -831,6 +831,7 @@ var view : NSView; w : NSWindow; px, py: Integer; + wi: NSUInteger; begin if Assigned(APopupMenu) and (APopupMenu.Handle<>0) then begin @@ -847,6 +848,31 @@ begin py := y; view := nil; w :=NSApp.keyWindow; + if not Assigned(w) and (NSApp.windows.count>0) then + begin + // in macOS it's possible to "rightclick" without focusing a window + // so let's try to find the window + for wi := 0 to NSApp.windows.count-1 do + begin + w := NSWindow(NSApp.windows.objectAtIndex(wi)); + if not w.isVisible then Continue; + view := w.contentView; + view.lclScreenToLocal(px, py); + if (px >= 0) and (py >= 0) + and (px<=Round(view.frame.size.width)) + and (py<=Round(view.frame.size.height)) + then + begin + px := X; + py := Y; + Break; + end; + w := nil; + px := X; + py := Y; + end; + end; + if Assigned(w) then begin view := w.contentView;