Last active
August 29, 2015 14:13
-
-
Save porty/6b94f7f908ed7af0f070 to your computer and use it in GitHub Desktop.
OSX Tray Icon in Go
This file contains hidden or 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
package main | |
// http://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html | |
// https://github.com/TooTallNate/NodObjC/blob/master/examples/NodeCocoaHelloWorld.app/Contents/MacOS/app.js | |
// https://github.com/TooTallNate/NodObjC/issues/21 | |
// http://www.gnustep.it/nicola/Tutorials/FirstGUIApplication/node3.html | |
/* | |
#cgo CFLAGS: -x objective-c | |
#cgo LDFLAGS: -framework Cocoa | |
#import <Cocoa/Cocoa.h> | |
void addTrayIconMenuItems(); | |
@interface FooAppDelegate : NSObject <NSApplicationDelegate> | |
@property (strong, nonatomic) NSStatusItem *statusItem; | |
@end | |
@interface FooAppDelegate () | |
@end | |
@implementation FooAppDelegate | |
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification | |
{ | |
printf("%s", "applicationDidFinishLaunching() fired\n"); | |
addTrayIconMenuItems(); | |
} | |
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender | |
{ | |
printf("%s", "I am terminating!\n"); | |
return NSTerminateNow; | |
} | |
- (void)openFeedbin:(id) sender | |
{ | |
printf("%s", "You pressed a thing!\n"); | |
} | |
@end | |
NSStatusItem * _statusItem; | |
void createApplication() { | |
[NSAutoreleasePool new]; | |
[NSApplication sharedApplication]; | |
// In Snow Leopard, programs without application bundles and Info.plist files | |
// don't get a menubar and can't be brought to the front unless the | |
// presentation option is changed | |
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; | |
} | |
void createTrayIcon() { | |
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; | |
// The text that will be shown in the menu bar | |
_statusItem.title = @""; | |
// The image that will be shown in the menu bar, a 16x16 black png works best | |
//_statusItem.image = [NSImage imageNamed:@"feedbin-logo"]; | |
_statusItem.image = [[NSImage alloc] initByReferencingFile:@"lightning-inactive.png"]; | |
// The highlighted image, use a white version of the normal image | |
//_statusItem.alternateImage = [NSImage imageNamed:@"feedbin-logo-alt"]; | |
_statusItem.alternateImage = [[NSImage alloc] initByReferencingFile:@"lightning-active.png"]; | |
// The image gets a blue background when the item is selected | |
_statusItem.highlightMode = YES; | |
} | |
void addTrayIconMenuItems() { | |
NSMenu *menu = [[NSMenu alloc] init]; | |
[menu addItemWithTitle:@"Do a thing" action:@selector(openFeedbin:) keyEquivalent:@""]; | |
[menu addItem:[NSMenuItem separatorItem]]; // A thin grey line | |
[menu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@""]; | |
// [menu addItemWithTitle:@"Refresh" action:@selector(getUnreadEntries:) keyEquivalent:@""]; | |
// if ([[[KMFeedbinCredentialStorage sharedCredentialStorage] credential] hasPassword]) { | |
// [menu addItemWithTitle:@"Log Out" action:@selector(logOut:) keyEquivalent:@""]; | |
// } else { | |
// [menu addItemWithTitle:@"Log In" action:@selector(logIn:) keyEquivalent:@""]; | |
// } | |
// [menu addItem:[NSMenuItem separatorItem]]; // A thin grey line | |
// [menu addItemWithTitle:@"Quit Feedbin Notifier" action:@selector(terminate:) keyEquivalent:@""]; | |
_statusItem.menu = menu; | |
} | |
void windowLoop() { | |
FooAppDelegate * p = [FooAppDelegate alloc]; | |
[NSApp setDelegate:p]; | |
[NSApp run]; | |
} | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
C.createApplication() | |
C.createTrayIcon() | |
go C.windowLoop() | |
fmt.Println("Sleeping for 10 seconds to let the app loop run") | |
time.Sleep(10 * time.Second) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment