Created
September 10, 2021 08:28
-
-
Save torarnv/42d39da9616c9bedee3eeb57e9c9813e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//usr/bin/env clang++ $0 -o ${o=`mktemp`} -fmodules -fcxx-modules && echo $o && exec $o $* || exit | |
@import AppKit; | |
@interface View : NSView | |
@end | |
@implementation View | |
- (id)init | |
{ | |
if((self = [super initWithFrame:NSZeroRect])) { | |
NSUInteger trackingOptions = NSTrackingActiveInActiveApp | |
| NSTrackingMouseEnteredAndExited | NSTrackingCursorUpdate | |
| NSTrackingMouseMoved; | |
// Using NSTrackingInVisibleRect means AppKit will automatically synchronize the | |
// tracking rect with changes in the view's visible area, so leave it undefined. | |
trackingOptions |= NSTrackingInVisibleRect; | |
static const NSRect trackingRect = NSZeroRect; | |
[self addTrackingArea:[[[NSTrackingArea alloc] initWithRect:trackingRect | |
options:trackingOptions owner:self userInfo:nil] autorelease]]; | |
} | |
return self; | |
} | |
- (void)mouseEntered:(NSEvent *)event | |
{ | |
NSLog(@"mouseEntered"); | |
} | |
- (void)mouseMoved:(NSEvent *)event | |
{ | |
NSLog(@"mouseMoved"); | |
} | |
- (void)mouseExited:(NSEvent *)event | |
{ | |
NSLog(@"mouseExited"); | |
} | |
- (void)updateTrackingAreas | |
{ | |
NSLog(@"updateTrackingAreas"); | |
[super updateTrackingAreas]; | |
} | |
@end | |
int main() | |
{ | |
@autoreleasepool { | |
[NSApplication sharedApplication]; | |
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; | |
id appMenu = [[NSMenu new] autorelease]; | |
[appMenu addItem:[[[NSMenuItem alloc] initWithTitle:@"Quit" | |
action:@selector(terminate:) keyEquivalent:@"q"] autorelease]]; | |
id appMenuItem = [[NSMenuItem new] autorelease]; | |
[appMenuItem setSubmenu:appMenu]; | |
NSApp.mainMenu = [[NSMenu new] autorelease]; | |
[NSApp.mainMenu addItem:appMenuItem]; | |
NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 500, 500) | |
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | |
backing:NSBackingStoreBuffered defer:YES]; | |
window.contentView = [[View alloc] init]; | |
window.title = @"NSTrackingArea testcase"; | |
[window center]; | |
[window makeKeyAndOrderFront:nil]; | |
[NSApp activateIgnoringOtherApps:YES]; | |
[NSApp run]; | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment