commit 353929978e735bbf22001f78b851f5342cf52e6e Author: Jordan Orelli Date: Sat Jun 10 14:49:38 2017 -0500 hi diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a31b1b8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +dws + diff --git a/main.go b/main.go new file mode 100644 index 0000000..0e9f51d --- /dev/null +++ b/main.go @@ -0,0 +1,39 @@ +package main + +import ( + "fmt" + "os" + "strings" + + "github.com/jordanorelli/dws/ui" +) + +func exit(status int, t string, args ...interface{}) { + if !strings.HasSuffix(t, "\n") { + t = t + "\n" + } + if status == 0 { + fmt.Fprintf(os.Stdout, t, args...) + } else { + fmt.Fprintf(os.Stderr, t, args...) + } + os.Exit(status) +} + +type failable func() error + +func must(fn failable, status int, msg string) { + msg = strings.TrimSpace(msg) + if err := fn(); err != nil { + exit(status, "%s: %s", msg, err) + } +} + +func main() { + ui, err := ui.NewDesktop() + if err != nil { + exit(1, "unable to create desktop ui: %s", err) + } + must(ui.Init, 1, "unable to initialize desktop ui") + must(ui.Run, 1, "unable to run desktop ui") +} diff --git a/ui/ui.go b/ui/ui.go new file mode 100644 index 0000000..8d8a548 --- /dev/null +++ b/ui/ui.go @@ -0,0 +1,6 @@ +package ui + +type UI interface { + Init() error + Run() error +} diff --git a/ui/ui_darwin.go b/ui/ui_darwin.go new file mode 100644 index 0000000..e544046 --- /dev/null +++ b/ui/ui_darwin.go @@ -0,0 +1,30 @@ +package ui + +import ( + "runtime" +) + +/* +#cgo CFLAGS: -x objective-c +#cgo LDFLAGS: -framework Cocoa +#include "ui_darwin.h" +*/ +import "C" + +func NewDesktop() (UI, error) { + runtime.LockOSThread() + return new(cocoaUI), nil +} + +type cocoaUI struct { +} + +func (ui *cocoaUI) Init() error { + C.Initialize() + return nil +} + +func (ui *cocoaUI) Run() error { + C.Run() + return nil +} diff --git a/ui/ui_darwin.h b/ui/ui_darwin.h new file mode 100644 index 0000000..21275c4 --- /dev/null +++ b/ui/ui_darwin.h @@ -0,0 +1,2 @@ +void Initialize(void); +int Run(void); diff --git a/ui/ui_darwin.m b/ui/ui_darwin.m new file mode 100644 index 0000000..0adb7d3 --- /dev/null +++ b/ui/ui_darwin.m @@ -0,0 +1,35 @@ +#include + +void +Initialize(void) { + [NSAutoreleasePool new]; + [NSApplication sharedApplication]; + [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; +} + +int +Run(void) { + id menubar = [[NSMenu new] autorelease]; + id appMenuItem = [[NSMenuItem new] autorelease]; + [menubar addItem:appMenuItem]; + [NSApp setMainMenu:menubar]; + + id appMenu = [[NSMenu new] autorelease]; + id appName = [[NSProcessInfo processInfo] processName]; + id quitTitle = [@"Quit " stringByAppendingString:appName]; + id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle + action:@selector(terminate:) keyEquivalent:@"q"] + autorelease]; + [appMenu addItem:quitMenuItem]; + [appMenuItem setSubmenu:appMenu]; + + id window = [[[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 200, 200) + styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:NO] + autorelease]; + [window cascadeTopLeftFromPoint:NSMakePoint(20,20)]; + [window setTitle:appName]; + [window makeKeyAndOrderFront:nil]; + [NSApp activateIgnoringOtherApps:YES]; + [NSApp run]; + return 0; +}