+- (void)restoreToForeground:(id)sender {
+ ProcessSerialNumber psn = { 0, kCurrentProcess };
+ TransformProcessType(&psn, kProcessTransformToForegroundApplication);
+ [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
+ [window makeKeyAndOrderFront:sender];
+ [NSObject cancelPreviousPerformRequestsWithTarget:self
+ selector:@selector(transformIntoElement:)
+ object:self];
+ [NSUserDefaults.standardUserDefaults setBool:NO forKey:@"hidden in status item"];
+}
+
+- (void)applicationWillBecomeActive:(NSNotification *)notification {
+ if (window.isVisible)
+ [self restoreToForeground:notification];
+}
+
+- (void)transformIntoElement:(id)sender {
+ ProcessSerialNumber psn = { 0, kCurrentProcess };
+ TransformProcessType(&psn, kProcessTransformToUIElementApplication);
+ [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"hidden in status item"];
+}
+
+- (void)flashStatusItem {
+ if ([statusItem.image.name isEqualToString:@"Status Menu Icon"]) {
+ statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
+ } else {
+ statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
+ }
+
+}
+
+- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
+ [theApplication hide:theApplication];
+ // If we turn into a UIElement right away, the application cancels
+ // the deactivation events. The dock icon disappears, but an
+ // unresponsive menu bar remains until the user clicks somewhere.
+ // So delay just long enough to be past the end handling that.
+ [self performSelector:@selector(transformIntoElement:) withObject:self afterDelay:0.001];
+ return NO;
+}
+
+- (void)eventSimulationStarted:(NSNotification *)note {
+ statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];