github.com/gop9/olt@v0.0.0-20200202132135-d956aad50b08/gio/app/internal/window/os_macos.m (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  // +build darwin,!ios
     4  
     5  @import AppKit;
     6  
     7  #include "os_macos.h"
     8  #include "_cgo_export.h"
     9  
    10  @interface GioDelegate : NSObject<NSApplicationDelegate, NSWindowDelegate>
    11  @property (strong,nonatomic) NSWindow *window;
    12  @end
    13  
    14  @implementation GioDelegate
    15  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    16  	[[NSRunningApplication currentApplication] activateWithOptions:(NSApplicationActivateAllWindows | NSApplicationActivateIgnoringOtherApps)];
    17  	[self.window makeKeyAndOrderFront:self];
    18  	gio_onShow((__bridge CFTypeRef)self.window.contentView);
    19  }
    20  - (void)applicationDidHide:(NSNotification *)aNotification {
    21  	gio_onHide((__bridge CFTypeRef)self.window.contentView);
    22  }
    23  - (void)applicationWillUnhide:(NSNotification *)notification {
    24  	gio_onShow((__bridge CFTypeRef)self.window.contentView);
    25  }
    26  - (void)windowWillMiniaturize:(NSNotification *)notification {
    27  	gio_onHide((__bridge CFTypeRef)self.window.contentView);
    28  }
    29  - (void)windowDidDeminiaturize:(NSNotification *)notification {
    30  	gio_onShow((__bridge CFTypeRef)self.window.contentView);
    31  }
    32  - (void)windowDidChangeScreen:(NSNotification *)notification {
    33  	CGDirectDisplayID dispID = [[[self.window screen] deviceDescription][@"NSScreenNumber"] unsignedIntValue];
    34  	CFTypeRef view = (__bridge CFTypeRef)self.window.contentView;
    35  	gio_updateDisplayLink(view, dispID);
    36  }
    37  - (void)windowDidBecomeKey:(NSNotification *)notification {
    38  	gio_onFocus((__bridge CFTypeRef)self.window.contentView, YES);
    39  }
    40  - (void)windowDidResignKey:(NSNotification *)notification {
    41  	gio_onFocus((__bridge CFTypeRef)self.window.contentView, NO);
    42  }
    43  - (void)windowWillClose:(NSNotification *)notification {
    44  	gio_onTerminate((__bridge CFTypeRef)self.window.contentView);
    45  	self.window.delegate = nil;
    46  	[NSApp terminate:nil];
    47  }
    48  @end
    49  
    50  CGFloat gio_viewHeight(CFTypeRef viewRef) {
    51  	NSView *view = (__bridge NSView *)viewRef;
    52  	return [view bounds].size.height;
    53  }
    54  
    55  CGFloat gio_viewWidth(CFTypeRef viewRef) {
    56  	NSView *view = (__bridge NSView *)viewRef;
    57  	return [view bounds].size.width;
    58  }
    59  
    60  CGFloat gio_getViewBackingScale(CFTypeRef viewRef) {
    61  	NSView *view = (__bridge NSView *)viewRef;
    62  	return [view.window backingScaleFactor];
    63  }
    64  
    65  void gio_main(CFTypeRef viewRef, const char *title, CGFloat width, CGFloat height) {
    66  	@autoreleasepool {
    67  		NSView *view = (NSView *)CFBridgingRelease(viewRef);
    68  		[NSApplication sharedApplication];
    69  		[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    70  
    71  		NSMenuItem *mainMenu = [NSMenuItem new];
    72  
    73  		NSMenu *menu = [NSMenu new];
    74  		NSMenuItem *hideMenuItem = [[NSMenuItem alloc] initWithTitle:@"Hide"
    75  															  action:@selector(hide:)
    76  													   keyEquivalent:@"h"];
    77  		[menu addItem:hideMenuItem];
    78  		NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit"
    79  															  action:@selector(terminate:)
    80  													   keyEquivalent:@"q"];
    81  		[menu addItem:quitMenuItem];
    82  		[mainMenu setSubmenu:menu];
    83  		NSMenu *menuBar = [NSMenu new];
    84  		[menuBar addItem:mainMenu];
    85  		[NSApp setMainMenu:menuBar];
    86  
    87  		NSRect rect = NSMakeRect(0, 0, width, height);
    88  		NSUInteger styleMask = NSTitledWindowMask |
    89  			NSResizableWindowMask |
    90  			NSMiniaturizableWindowMask |
    91  			NSClosableWindowMask;
    92  		NSWindow* window = [[NSWindow alloc] initWithContentRect:rect
    93  													   styleMask:styleMask
    94  														 backing:NSBackingStoreBuffered
    95  														   defer:NO];
    96  		window.title = [NSString stringWithUTF8String: title];
    97  		[window cascadeTopLeftFromPoint:NSMakePoint(20,20)];
    98  		[window setAcceptsMouseMovedEvents:YES];
    99  
   100  		gio_onCreate((__bridge CFTypeRef)view);
   101  		GioDelegate *del = [[GioDelegate alloc] init];
   102  		del.window = window;
   103  		[window setDelegate:del];
   104  		[NSApp setDelegate:del];
   105  		[window setContentView:view];
   106  		[window makeFirstResponder:view];
   107  
   108  
   109  		[NSApp run];
   110  	}
   111  }