master
Jordan Orelli 8 years ago
commit 353929978e

2
.gitignore vendored

@ -0,0 +1,2 @@
dws

@ -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")
}

@ -0,0 +1,6 @@
package ui
type UI interface {
Init() error
Run() error
}

@ -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
}

@ -0,0 +1,2 @@
void Initialize(void);
int Run(void);

@ -0,0 +1,35 @@
#include <Cocoa/Cocoa.h>
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;
}
Loading…
Cancel
Save