From 8431fc1f27e5a0a30e0284e2283f420108cb826b Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 17 Jun 2017 12:53:22 -0500 Subject: [PATCH] send selection back to Go --- main.go | 28 ++++++++++++---------------- ui/MainViewController.h | 2 +- ui/MainViewController.m | 20 ++++++++++++++------ ui/ui.go | 16 ++++++++++++++-- ui/ui_darwin.go | 26 ++++++++++++++++++-------- 5 files changed, 59 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index bee3b6b..38429de 100644 --- a/main.go +++ b/main.go @@ -21,23 +21,19 @@ func exit(status int, t string, args ...interface{}) { 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() { log.Println("Creating new Desktop UI") - ui, err := ui.NewDesktop() - if err != nil { - exit(1, "unable to create desktop ui: %s", err) - } - log.Println("Initializing Desktop UI") - must(ui.Init, 1, "unable to initialize desktop ui") + desktop := ui.Desktop() + + c := make(chan ui.Event, 1) + go func() { + for e := range c { + log.Printf("UI Event: %v\n", e) + } + }() + log.Println("Running Desktop UI") - must(ui.Run, 1, "unable to run desktop ui") + if err := desktop.Run(c); err != nil { + exit(1, "UI Error: %v", err) + } } diff --git a/ui/MainViewController.h b/ui/MainViewController.h index 0e1bb77..c370fca 100644 --- a/ui/MainViewController.h +++ b/ui/MainViewController.h @@ -1,4 +1,4 @@ #import -@interface MainViewController : NSViewController +@interface MainViewController : NSViewController @end diff --git a/ui/MainViewController.m b/ui/MainViewController.m index 79c9725..eba8548 100644 --- a/ui/MainViewController.m +++ b/ui/MainViewController.m @@ -1,3 +1,4 @@ +#import "_cgo_export.h" #import "MainViewController.h" #import "MainView.h" @@ -27,12 +28,11 @@ self.selectDirectoryPanel = [NSOpenPanel openPanel]; [self.selectDirectoryPanel setCanChooseFiles:NO]; [self.selectDirectoryPanel setCanChooseDirectories:YES]; - [self.selectDirectoryPanel setDelegate:self]; // create select directory button self.selectDirectoryButton = [NSButton buttonWithTitle:@"select directory" - target:self.selectDirectoryPanel - action:@selector(runModal)]; + target:self + action:@selector(selectDirectory)]; [self.selectDirectoryButton setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addSubview:self.selectDirectoryButton]; @@ -45,9 +45,17 @@ constant:8.0].active = YES; } -- (void) panel:(id)sender didChangeToDirectoryURL:(NSURL *)url { - NSLog(@"[MainViewController] panel: %@ didChangeToDirectoryURL: %@", sender, url); - return [super viewWillAppear]; +- (void) selectDirectory { + NSLog(@"[MainViewController] select directory start"); + [self.selectDirectoryPanel beginWithCompletionHandler:^(NSInteger result) { + if (result != NSFileHandlingPanelOKButton) { + NSLog(@"[MainViewController] user canceled select directory window"); + return; + } + NSURL *selected = [[self.selectDirectoryPanel URLs] objectAtIndex:0]; + NSString *path = [selected path]; + selectDirectory((GoString){path.UTF8String, path.length}); + }]; } - (void) viewWillAppear { diff --git a/ui/ui.go b/ui/ui.go index 8d8a548..67831e7 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -1,6 +1,18 @@ package ui type UI interface { - Init() error - Run() error + Run(chan Event) error +} + +type Event interface { + isUIEvent() +} + +type event struct{} + +func (e event) isUIEvent() {} + +type SelectDirectoryEvent struct { + Path string + event } diff --git a/ui/ui_darwin.go b/ui/ui_darwin.go index bb3e91c..52200d4 100644 --- a/ui/ui_darwin.go +++ b/ui/ui_darwin.go @@ -12,21 +12,31 @@ import ( */ import "C" -func NewDesktop() (UI, error) { - runtime.LockOSThread() +var desktopUI *cocoaUI + +func Desktop() UI { + if desktopUI != nil { + return desktopUI + } + log.Println("Creating new cocoaUI") - return new(cocoaUI), nil + runtime.LockOSThread() + C.Initialize() + desktopUI = new(cocoaUI) + return desktopUI } type cocoaUI struct { + out chan Event } -func (ui *cocoaUI) Init() error { - C.Initialize() +func (ui *cocoaUI) Run(out chan Event) error { + ui.out = out + C.Run() return nil } -func (ui *cocoaUI) Run() error { - C.Run() - return nil +//export selectDirectory +func selectDirectory(path string) { + desktopUI.out <- SelectDirectoryEvent{Path: path} }