From 33dfb076f52cc96aaca5e1250d4d3abb47729b6e Mon Sep 17 00:00:00 2001 From: Tanin Na Nakorn Date: Sun, 2 Feb 2020 00:49:20 -0800 Subject: [PATCH] Avoid quitting the app (#22) --- mac-app/Tip/AppDelegate.m | 19 ++++++++++++++----- mac-app/Tip/Info.plist | 2 +- mac-app/Tip/Receiver.h | 2 ++ mac-app/Tip/Receiver.m | 29 ++++++++++++++++++++--------- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/mac-app/Tip/AppDelegate.m b/mac-app/Tip/AppDelegate.m index 5b9e737..106b8f8 100644 --- a/mac-app/Tip/AppDelegate.m +++ b/mac-app/Tip/AppDelegate.m @@ -35,11 +35,6 @@ - (instancetype) init { } - (void) applicationDidFinishLaunching:(NSNotification *)notification { - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(terminate) - name:NSPopoverDidCloseNotification - object:nil]; - _statusItem = [NSStatusBar.systemStatusBar statusItemWithLength:25]; _statusItem.button.cell.font = [NSFont fontWithName:@"Font Awesome 5 Free" size:14]; @@ -51,6 +46,8 @@ - (void) applicationDidFinishLaunching:(NSNotification *)notification { [_statusItem.menu addItemWithTitle:@"How to setup Tip" action:@selector(openInstallationUrl) keyEquivalent:@""]; [_statusItem.menu addItemWithTitle:@"Help & Documentation" action:@selector(openGithubProject) keyEquivalent:@""]; [_statusItem.menu addItem:NSMenuItem.separatorItem]; + [_statusItem.menu addItemWithTitle:@"Hide this menu" action:@selector(hide) keyEquivalent:@""]; + [_statusItem.menu addItem:NSMenuItem.separatorItem]; [_statusItem.menu addItemWithTitle:@"Quit" action:@selector(terminate) keyEquivalent:@""]; NSUserDefaults *args = [NSUserDefaults standardUserDefaults]; @@ -67,6 +64,10 @@ - (void)openInstallationUrl { [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/tanin47/tip#installation"]]; } +- (void) hide { + _statusItem.visible = NO; +} + - (void)openGithubProject { [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/tanin47/tip"]]; } @@ -75,5 +76,13 @@ - (void)terminate { [NSApp terminate:nil]; } +- (void)applicationDidBecomeActive:(NSNotification *)notification { + // Tip.app is activated by user double-clicking on the binary. + // If it's activated by Mac's service, the tooltip will show. + // This means there's a window in orderedWindows. + if ([NSApp orderedWindows].count == 0) { + _statusItem.visible = YES; + } +} @end diff --git a/mac-app/Tip/Info.plist b/mac-app/Tip/Info.plist index 4f36b39..6449a24 100644 --- a/mac-app/Tip/Info.plist +++ b/mac-app/Tip/Info.plist @@ -21,7 +21,7 @@ CFBundleShortVersionString 1.2.0 CFBundleVersion - 4 + 5 LSApplicationCategoryType public.app-category.productivity LSMinimumSystemVersion diff --git a/mac-app/Tip/Receiver.h b/mac-app/Tip/Receiver.h index 53f54b4..83aca13 100644 --- a/mac-app/Tip/Receiver.h +++ b/mac-app/Tip/Receiver.h @@ -16,6 +16,8 @@ NS_ASSUME_NONNULL_BEGIN @property TipTableController* controller; @property ExternalTipper* tipper; +@property (nonatomic, retain, nullable) NSWindow* window; +@property (nonatomic, retain, nullable) NSPopover* popover; - (id)initWithTipper:(ExternalTipper *)tipper_; - (void)showPopover: (NSString*) input; diff --git a/mac-app/Tip/Receiver.m b/mac-app/Tip/Receiver.m index ca7b111..0b5508a 100644 --- a/mac-app/Tip/Receiver.m +++ b/mac-app/Tip/Receiver.m @@ -18,10 +18,20 @@ - (id)initWithTipper:(ExternalTipper *)tipper_ { self = [super init]; if (self) { self.tipper = tipper_; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(popoverDidClose) + name:NSPopoverDidCloseNotification + object:nil]; } return self; } +- (void) popoverDidClose { + [self.window close]; + self.window = nil; + self.popover = nil; +} + - (NSString*) readInput:(NSPasteboard*)pboard userData:(NSString *)userData error:(NSString **)error { @@ -53,12 +63,13 @@ - (void)showPopover: (NSString*) input { NSPoint mouseLoc = [NSEvent mouseLocation]; NSRect frame = NSMakeRect(mouseLoc.x, mouseLoc.y-10, 1, 1); - NSWindow* window = [[NSWindow alloc] initWithContentRect:frame + self.window = [[NSWindow alloc] initWithContentRect:frame styleMask:NSWindowStyleMaskBorderless backing:NSBackingStoreBuffered defer:NO]; - [window setBackgroundColor:[NSColor blueColor]]; - [window makeKeyAndOrderFront:NSApp]; + [self.window setReleasedWhenClosed:false]; + [self.window setBackgroundColor:[NSColor colorWithRed:0 green:0 blue:0 alpha:0]]; + [self.window makeKeyAndOrderFront:NSApp]; @try { NSArray * items = [self.tipper makeTip:input]; @@ -68,13 +79,13 @@ - (void)showPopover: (NSString*) input { NSLog(@"Error: %@ %@", error, [error userInfo]); _controller.error = error; } - NSPopover *entryPopover = [[NSPopover alloc] init]; - entryPopover.contentViewController = _controller; - entryPopover.behavior = NSPopoverBehaviorTransient; - entryPopover.animates = YES; + self.popover = [[NSPopover alloc] init]; + self.popover.contentViewController = _controller; + self.popover.behavior = NSPopoverBehaviorTransient; + self.popover.animates = YES; - [entryPopover showRelativeToRect:window.contentView.bounds - ofView:window.contentView + [self.popover showRelativeToRect:self.window.contentView.bounds + ofView:self.window.contentView preferredEdge:NSMinYEdge]; }