From 5ba8780a48ec27695bd00dfbc9304e2ecdfdbd29 Mon Sep 17 00:00:00 2001 From: rich2014 Date: Fri, 29 Sep 2023 17:23:00 +0800 Subject: [PATCH] Cocoa: GetCocoaWindowAtPos() added ensure that gets the correct window at mouse pos 1. in Z-Order 2. on the active Space 3. in current App 4. is visible window 5. is not the misc window like Menu Bar --- lcl/interfaces/cocoa/cocoaint.pas | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lcl/interfaces/cocoa/cocoaint.pas b/lcl/interfaces/cocoa/cocoaint.pas index 8452a74e3d..80ffc9feb1 100644 --- a/lcl/interfaces/cocoa/cocoaint.pas +++ b/lcl/interfaces/cocoa/cocoaint.pas @@ -279,6 +279,8 @@ function CocoaPromptUser(const DialogCaption, DialogMessage: String; EscapeResult: Longint; sheetOfWindow: NSWindow = nil; modalSheet: Boolean = false): Longint; +function GetCocoaWindowAtPos(p: NSPoint): TCocoaWindow; + // The function tries to initialize the proper application class. // The desired application class can be specified in info.plit // by specifying NSPrincipalClass property. @@ -510,6 +512,34 @@ begin end; {$endif} +// ensure that gets the correct window at mouse pos +// 1. in Z-Order +// 2. on the active Space +// 3. in current App +// 4. is visible window +// 5. is not the misc window like Menu Bar +function GetCocoaWindowAtPos(p: NSPoint): TCocoaWindow; +var + windowNumber: NSInteger; + windowNumbers: NSArray; + window: NSWindow; +begin + Result := nil; + + // ensure 1 + windowNumber := NSWindow.windowNumberAtPoint_belowWindowWithWindowNumber(p,0); + windowNumbers := NSWindow.windowNumbersWithOptions(0); + + // ensure 2, 3, 4 + if not windowNumbers.containsObject(NSNumber.numberWithInt(windowNumber)) then + exit; + + // ensure 5 + window := NSApp.windowWithWindowNumber(windowNumber); + if Assigned(window) and window.isKindOfClass(TCocoaWindow) then + Result := TCocoaWindow(window); +end; + procedure ForwardMouseMove(app: NSApplication; theEvent: NSEvent); var w : NSWindow;