mirror of
https://github.com/vlang/v.git
synced 2025-09-10 16:00:31 -04:00
gg: fix screen_size() on macos with multiple displays
This commit is contained in:
parent
071ab6c362
commit
1e6fdbf815
@ -15,11 +15,43 @@ NSString* nsstring(string s) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gg__Size gg_get_screen_size() {
|
gg__Size gg_get_screen_size() {
|
||||||
NSScreen* screen = [NSScreen mainScreen];
|
NSScreen *currentScreen = nil;
|
||||||
NSDictionary* description = [screen deviceDescription];
|
NSWindow *mainWindow = [NSApp mainWindow];
|
||||||
|
// 1. Try screen containing the main window
|
||||||
|
if (mainWindow) {
|
||||||
|
currentScreen = [mainWindow screen];
|
||||||
|
}
|
||||||
|
// 2. If no main window, try the key window (might be different, e.g., a panel)
|
||||||
|
if (!currentScreen) {
|
||||||
|
NSWindow *keyWindow = [NSApp keyWindow];
|
||||||
|
if (keyWindow) {
|
||||||
|
currentScreen = [keyWindow screen];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 3. If no relevant window, find the screen containing the mouse cursor
|
||||||
|
if (!currentScreen) {
|
||||||
|
// Get mouse location in global screen coordinates (bottom-left origin)
|
||||||
|
NSPoint mouseLocation = [NSEvent mouseLocation];
|
||||||
|
NSArray<NSScreen *> *screens = [NSScreen screens];
|
||||||
|
for (NSScreen *screen in screens) {
|
||||||
|
// Check if the mouse location is within the screen's frame
|
||||||
|
// Note: Both mouseLocation and screen.frame use bottom-left origin coordinates
|
||||||
|
if (NSMouseInRect(mouseLocation, [screen frame], NO)) {
|
||||||
|
currentScreen = screen;
|
||||||
|
break; // Found the screen with the mouse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 4. As a last resort, fall back to the main screen
|
||||||
|
if (!currentScreen) {
|
||||||
|
NSLog(@"Warning: Could not determine current screen based on window or mouse. Falling back to mainScreen.");
|
||||||
|
currentScreen = [NSScreen mainScreen];
|
||||||
|
}
|
||||||
|
// Now get the size of the determined screen
|
||||||
|
NSDictionary *description = [currentScreen deviceDescription];
|
||||||
|
// Use NSDeviceSize to get pixel dimensions
|
||||||
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
|
NSSize displayPixelSize = [[description objectForKey:NSDeviceSize] sizeValue];
|
||||||
CGSize displayPhysicalSize =
|
// Create the V gg.Size object
|
||||||
CGDisplayScreenSize([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]);
|
|
||||||
gg__Size res;
|
gg__Size res;
|
||||||
res.width = displayPixelSize.width;
|
res.width = displayPixelSize.width;
|
||||||
res.height = displayPixelSize.height;
|
res.height = displayPixelSize.height;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user