Skip to content

Commit

Permalink
Avoid quitting the app (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanin47 authored Feb 2, 2020
1 parent f4556a7 commit 33dfb07
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
19 changes: 14 additions & 5 deletions mac-app/Tip/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand All @@ -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"]];
}
Expand All @@ -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
2 changes: 1 addition & 1 deletion mac-app/Tip/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>CFBundleShortVersionString</key>
<string>1.2.0</string>
<key>CFBundleVersion</key>
<string>4</string>
<string>5</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.productivity</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 2 additions & 0 deletions mac-app/Tip/Receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 20 additions & 9 deletions mac-app/Tip/Receiver.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<TipItem *> * items = [self.tipper makeTip:input];
Expand All @@ -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];
}

Expand Down

0 comments on commit 33dfb07

Please sign in to comment.