Skip to content

Instantly share code, notes, and snippets.

@torarnv
Last active September 10, 2021 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torarnv/b50e5edf85c6a9cab5d28b896da84c5b to your computer and use it in GitHub Desktop.
Save torarnv/b50e5edf85c6a9cab5d28b896da84c5b to your computer and use it in GitHub Desktop.
//usr/bin/env clang++ $0 -o ${o=`mktemp`} -fmodules -fcxx-modules && echo $o && exec $o $* || exit
@import AppKit;
@interface View : NSView
@property (retain) NSTrackingArea* trackingArea;
@end
@implementation View
- (id)init
{
if((self = [super initWithFrame:NSZeroRect])) {
self.trackingArea = nil;
}
return self;
}
- (void)mouseEntered:(NSEvent *)event
{
NSLog(@"mouseEntered");
}
- (void)mouseMoved:(NSEvent *)event
{
NSLog(@"mouseMoved");
}
- (void)mouseExited:(NSEvent *)event
{
NSLog(@"mouseExited");
}
- (void)updateTrackingAreas
{
NSLog(@"updateTrackingAreas");
[self removeTrackingArea:self.trackingArea];
if (NSApp.active) {
NSUInteger trackingOptions = NSTrackingActiveAlways
| NSTrackingMouseEnteredAndExited | NSTrackingCursorUpdate
| NSTrackingMouseMoved | NSTrackingInVisibleRect;
self.trackingArea = [[[NSTrackingArea alloc] initWithRect:NSZeroRect
options:trackingOptions owner:self userInfo:nil] autorelease];
[self addTrackingArea:self.trackingArea];
}
[super updateTrackingAreas];
}
- (void)applicationDidBecomeActive:(NSNotification *)notification
{
NSLog(@"applicationDidBecomeActive");
[self updateTrackingAreas];
}
- (void)applicationDidResignActive:(NSNotification *)notification
{
NSLog(@"applicationDidResignActive");
dispatch_async(dispatch_get_main_queue(), ^{
[self 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, 1200, 300)
styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable
backing:NSBackingStoreBuffered defer:YES];
window.contentView = [[View alloc] init];
window.title = @"With mouse over window, quickly ⌘-Tab to Finder without triggering the App Switcher UI, " \
"then click app window. No mouse enter/exit or mouse move anymore.";
NSApp.delegate = window.contentView;
[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