github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/darwin/screen.go (about) 1 //go:build darwin 2 // +build darwin 3 4 package darwin 5 6 /* 7 #cgo CFLAGS: -x objective-c 8 #cgo LDFLAGS: -framework Foundation -framework Cocoa -framework WebKit -framework AppKit 9 #import <Foundation/Foundation.h> 10 #include <AppKit/AppKit.h> 11 #include <stdlib.h> 12 13 #import "Application.h" 14 #import "WailsContext.h" 15 16 typedef struct Screen { 17 int isCurrent; 18 int isPrimary; 19 int height; 20 int width; 21 int pHeight; 22 int pWidth; 23 } Screen; 24 25 26 int GetNumScreens(){ 27 return [[NSScreen screens] count]; 28 } 29 30 int screenUniqueID(NSScreen *screen){ 31 // adapted from https://stackoverflow.com/a/1237490/4188138 32 NSDictionary* screenDictionary = [screen deviceDescription]; 33 NSNumber* screenID = [screenDictionary objectForKey:@"NSScreenNumber"]; 34 CGDirectDisplayID aID = [screenID unsignedIntValue]; 35 return aID; 36 } 37 38 Screen GetNthScreen(int nth, void *inctx){ 39 WailsContext *ctx = (__bridge WailsContext*) inctx; 40 NSArray<NSScreen *> *screens = [NSScreen screens]; 41 NSScreen* nthScreen = [screens objectAtIndex:nth]; 42 NSScreen* currentScreen = [ctx getCurrentScreen]; 43 44 Screen returnScreen; 45 returnScreen.isCurrent = (int)(screenUniqueID(currentScreen)==screenUniqueID(nthScreen)); 46 // TODO properly handle screen mirroring 47 // from apple documentation: 48 // https://developer.apple.com/documentation/appkit/nsscreen/1388393-screens?language=objc 49 // The screen at index 0 in the returned array corresponds to the primary screen of the user’s system. This is the screen that contains the menu bar and whose origin is at the point (0, 0). In the case of mirroring, the first screen is the largest drawable display; if all screens are the same size, it is the screen with the highest pixel depth. This primary screen may not be the same as the one returned by the mainScreen method, which returns the screen with the active window. 50 returnScreen.isPrimary = nth==0; 51 returnScreen.height = (int) nthScreen.frame.size.height; 52 returnScreen.width = (int) nthScreen.frame.size.width; 53 54 returnScreen.pWidth = 0; 55 returnScreen.pHeight = 0; 56 57 // https://stackoverflow.com/questions/13859109/how-to-programmatically-determine-native-pixel-resolution-of-retina-macbook-pro 58 CGDirectDisplayID sid = ((NSNumber *)[nthScreen.deviceDescription 59 objectForKey:@"NSScreenNumber"]).unsignedIntegerValue; 60 61 CFArrayRef ms = CGDisplayCopyAllDisplayModes(sid, NULL); 62 CFIndex n = CFArrayGetCount(ms); 63 for (int i = 0; i < n; i++) { 64 CGDisplayModeRef m = (CGDisplayModeRef) CFArrayGetValueAtIndex(ms, i); 65 if (CGDisplayModeGetIOFlags(m) & kDisplayModeNativeFlag) { 66 // This corresponds with "System Settings" -> General -> About -> Displays 67 returnScreen.pWidth = CGDisplayModeGetPixelWidth(m); 68 returnScreen.pHeight = CGDisplayModeGetPixelHeight(m); 69 break; 70 } 71 } 72 CFRelease(ms); 73 74 if (returnScreen.pWidth == 0 || returnScreen.pHeight == 0) { 75 // If there was no native resolution take a best fit approach and use the backing pixel size. 76 NSRect pSize = [nthScreen convertRectToBacking:nthScreen.frame]; 77 returnScreen.pHeight = (int) pSize.size.height; 78 returnScreen.pWidth = (int) pSize.size.width; 79 } 80 return returnScreen; 81 } 82 83 */ 84 import "C" 85 86 import ( 87 "unsafe" 88 89 "github.com/secoba/wails/v2/internal/frontend" 90 ) 91 92 func GetAllScreens(wailsContext unsafe.Pointer) ([]frontend.Screen, error) { 93 err := error(nil) 94 screens := []frontend.Screen{} 95 numScreens := int(C.GetNumScreens()) 96 for screeNum := 0; screeNum < numScreens; screeNum++ { 97 screenNumC := C.int(screeNum) 98 cScreen := C.GetNthScreen(screenNumC, wailsContext) 99 100 screen := frontend.Screen{ 101 Height: int(cScreen.height), 102 Width: int(cScreen.width), 103 IsCurrent: cScreen.isCurrent == C.int(1), 104 IsPrimary: cScreen.isPrimary == C.int(1), 105 106 Size: frontend.ScreenSize{ 107 Height: int(cScreen.height), 108 Width: int(cScreen.width), 109 }, 110 PhysicalSize: frontend.ScreenSize{ 111 Height: int(cScreen.pHeight), 112 Width: int(cScreen.pWidth), 113 }, 114 } 115 screens = append(screens, screen) 116 } 117 return screens, err 118 }