|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package ui |
| 4 | + |
| 5 | +/* |
| 6 | +#cgo CFLAGS: -x objective-c |
| 7 | +#cgo LDFLAGS: -framework Cocoa |
| 8 | +
|
| 9 | +#import <Cocoa/Cocoa.h> |
| 10 | +
|
| 11 | +// moveNSWindowTo moves the given NSWindow to (x, y) using top-left origin. |
| 12 | +// macOS uses bottom-left origin, so we flip Y relative to the screen that |
| 13 | +// contains the window. |
| 14 | +static void moveNSWindowTo(uintptr_t winHandle, int x, int y) { |
| 15 | + dispatch_async(dispatch_get_main_queue(), ^{ |
| 16 | + NSWindow *w = (__bridge NSWindow*)(void*)winHandle; |
| 17 | + NSScreen *screen = [w screen]; |
| 18 | + if (screen == nil) screen = [NSScreen mainScreen]; |
| 19 | + CGFloat screenHeight = [screen frame].size.height; |
| 20 | + CGFloat windowHeight = [w frame].size.height; |
| 21 | + CGFloat flippedY = screenHeight - (CGFloat)y - windowHeight; |
| 22 | + [w setFrameOrigin:NSMakePoint((CGFloat)x, flippedY)]; |
| 23 | + }); |
| 24 | +} |
| 25 | +
|
| 26 | +// getNSWindowPos returns the current top-left position of the NSWindow. |
| 27 | +static void getNSWindowPos(uintptr_t winHandle, int* outX, int* outY) { |
| 28 | + *outX = 0; |
| 29 | + *outY = 0; |
| 30 | + NSWindow *w = (__bridge NSWindow*)(void*)winHandle; |
| 31 | + NSScreen *screen = [w screen]; |
| 32 | + if (screen == nil) screen = [NSScreen mainScreen]; |
| 33 | + CGFloat screenHeight = [screen frame].size.height; |
| 34 | + NSRect frame = [w frame]; |
| 35 | + *outX = (int)frame.origin.x; |
| 36 | + *outY = (int)(screenHeight - frame.origin.y - frame.size.height); |
| 37 | +} |
| 38 | +
|
| 39 | +// getMainScreenSize returns the main screen dimensions. |
| 40 | +static void getMainScreenSize(int* w, int* h) { |
| 41 | + NSScreen *screen = [NSScreen mainScreen]; |
| 42 | + NSRect frame = [screen frame]; |
| 43 | + *w = (int)frame.size.width; |
| 44 | + *h = (int)frame.size.height; |
| 45 | +} |
| 46 | +
|
| 47 | +// getScreenCount returns the number of screens. |
| 48 | +static int getScreenCount(void) { |
| 49 | + return (int)[[NSScreen screens] count]; |
| 50 | +} |
| 51 | +
|
| 52 | +// getScreenBounds returns the visible frame (excluding menu bar/dock) for the |
| 53 | +// screen at the given index. Coordinates use top-left origin. |
| 54 | +static void getScreenBounds(int index, int* outX, int* outY, int* outW, int* outH) { |
| 55 | + NSArray *screens = [NSScreen screens]; |
| 56 | + if (index < 0 || index >= (int)[screens count]) { |
| 57 | + index = 0; |
| 58 | + } |
| 59 | + NSScreen *screen = [screens objectAtIndex:index]; |
| 60 | + NSRect visible = [screen visibleFrame]; |
| 61 | + NSRect full = [screen frame]; |
| 62 | +
|
| 63 | + *outX = (int)visible.origin.x; |
| 64 | + // Convert Y from bottom-left to top-left origin. |
| 65 | + *outY = (int)(full.size.height - visible.origin.y - visible.size.height); |
| 66 | + *outW = (int)visible.size.width; |
| 67 | + *outH = (int)visible.size.height; |
| 68 | +} |
| 69 | +*/ |
| 70 | +import "C" |
| 71 | + |
| 72 | +import ( |
| 73 | + "log" |
| 74 | + "time" |
| 75 | + |
| 76 | + "fyne.io/fyne/v2" |
| 77 | + "fyne.io/fyne/v2/driver" |
| 78 | +) |
| 79 | + |
| 80 | +// darwinWidgetWindow holds a reference to the widget window so we can |
| 81 | +// retrieve its native NSWindow pointer without relying on title search. |
| 82 | +var darwinWidgetWindow fyne.Window |
| 83 | + |
| 84 | +// registerDarwinWindow stores the widget window reference for later use |
| 85 | +// by moveWindow and getWindowPosition. Must be called once after the window |
| 86 | +// is created. |
| 87 | +func registerDarwinWindow(w fyne.Window) { |
| 88 | + darwinWidgetWindow = w |
| 89 | +} |
| 90 | + |
| 91 | +// getNSWindowHandle returns the native NSWindow handle (uintptr) for the |
| 92 | +// stored widget window using Fyne's driver.NativeWindow interface. |
| 93 | +func getNSWindowHandle() C.uintptr_t { |
| 94 | + if darwinWidgetWindow == nil { |
| 95 | + log.Println("macOS: getNSWindowHandle — darwinWidgetWindow is nil") |
| 96 | + return 0 |
| 97 | + } |
| 98 | + nativeWin, ok := darwinWidgetWindow.(driver.NativeWindow) |
| 99 | + if !ok { |
| 100 | + log.Printf("macOS: window type %T does not implement driver.NativeWindow", darwinWidgetWindow) |
| 101 | + return 0 |
| 102 | + } |
| 103 | + var handle uintptr |
| 104 | + nativeWin.RunNative(func(ctx any) { |
| 105 | + // Fyne passes MacWindowContext by value, not pointer. |
| 106 | + if macCtx, ok := ctx.(driver.MacWindowContext); ok { |
| 107 | + handle = macCtx.NSWindow |
| 108 | + } |
| 109 | + }) |
| 110 | + return C.uintptr_t(handle) |
| 111 | +} |
| 112 | + |
| 113 | +// applyToolWindowStyle is a no-op on macOS. |
| 114 | +// Window decorations are already removed via CreateSplashWindow. |
| 115 | +func applyToolWindowStyle(_ string) {} |
| 116 | + |
| 117 | +// getScreenSize returns the main screen dimensions on macOS. |
| 118 | +func getScreenSize() (int, int) { |
| 119 | + var w, h C.int |
| 120 | + C.getMainScreenSize(&w, &h) |
| 121 | + return int(w), int(h) |
| 122 | +} |
| 123 | + |
| 124 | +// moveWindow repositions the widget window to the given screen coordinates. |
| 125 | +// It moves immediately and retries after short delays to handle the case where |
| 126 | +// Fyne/GLFW repositions the window after Show(). |
| 127 | +func moveWindow(_ fyne.Window, x, y int) { |
| 128 | + notifyDarwinMoveByUs() |
| 129 | + handle := getNSWindowHandle() |
| 130 | + if handle == 0 { |
| 131 | + log.Println("macOS: moveWindow — could not get NSWindow handle") |
| 132 | + return |
| 133 | + } |
| 134 | + log.Printf("macOS: moveWindow x=%d y=%d", x, y) |
| 135 | + C.moveNSWindowTo(handle, C.int(x), C.int(y)) |
| 136 | + go func(h C.uintptr_t, px, py C.int) { |
| 137 | + for _, delay := range []int{150, 400, 900} { |
| 138 | + time.Sleep(time.Duration(delay) * time.Millisecond) |
| 139 | + notifyDarwinMoveByUs() |
| 140 | + C.moveNSWindowTo(h, px, py) |
| 141 | + } |
| 142 | + }(handle, C.int(x), C.int(y)) |
| 143 | +} |
| 144 | + |
| 145 | +// getWindowPosition returns the current top-left position of the widget window. |
| 146 | +func getWindowPosition() (int, int) { |
| 147 | + handle := getNSWindowHandle() |
| 148 | + if handle == 0 { |
| 149 | + return 0, 0 |
| 150 | + } |
| 151 | + var x, y C.int |
| 152 | + C.getNSWindowPos(handle, &x, &y) |
| 153 | + return int(x), int(y) |
| 154 | +} |
| 155 | + |
| 156 | +// setWindowOpacity is a no-op on macOS for now. |
| 157 | +func setWindowOpacity(_ int) {} |
| 158 | + |
| 159 | +// getMonitorCount returns the number of display monitors on macOS. |
| 160 | +func getMonitorCount() int { |
| 161 | + return int(C.getScreenCount()) |
| 162 | +} |
| 163 | + |
| 164 | +// getMonitorBounds returns the visible work-area rectangle for the monitor |
| 165 | +// at the given index. Coordinates use top-left origin. |
| 166 | +func getMonitorBounds(index int) (int, int, int, int) { |
| 167 | + var x, y, w, h C.int |
| 168 | + C.getScreenBounds(C.int(index), &x, &y, &w, &h) |
| 169 | + return int(x), int(y), int(w), int(h) |
| 170 | +} |
0 commit comments