send selection back to Go

master
Jordan Orelli 8 years ago
parent 2a3219c370
commit 8431fc1f27

@ -21,23 +21,19 @@ func exit(status int, t string, args ...interface{}) {
os.Exit(status) 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() { func main() {
log.Println("Creating new Desktop UI") log.Println("Creating new Desktop UI")
ui, err := ui.NewDesktop() desktop := ui.Desktop()
if err != nil {
exit(1, "unable to create desktop ui: %s", err) c := make(chan ui.Event, 1)
go func() {
for e := range c {
log.Printf("UI Event: %v\n", e)
} }
log.Println("Initializing Desktop UI") }()
must(ui.Init, 1, "unable to initialize desktop ui")
log.Println("Running Desktop UI") 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)
}
} }

@ -1,4 +1,4 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
@interface MainViewController : NSViewController <NSOpenSavePanelDelegate> @interface MainViewController : NSViewController
@end @end

@ -1,3 +1,4 @@
#import "_cgo_export.h"
#import "MainViewController.h" #import "MainViewController.h"
#import "MainView.h" #import "MainView.h"
@ -27,12 +28,11 @@
self.selectDirectoryPanel = [NSOpenPanel openPanel]; self.selectDirectoryPanel = [NSOpenPanel openPanel];
[self.selectDirectoryPanel setCanChooseFiles:NO]; [self.selectDirectoryPanel setCanChooseFiles:NO];
[self.selectDirectoryPanel setCanChooseDirectories:YES]; [self.selectDirectoryPanel setCanChooseDirectories:YES];
[self.selectDirectoryPanel setDelegate:self];
// create select directory button // create select directory button
self.selectDirectoryButton = [NSButton buttonWithTitle:@"select directory" self.selectDirectoryButton = [NSButton buttonWithTitle:@"select directory"
target:self.selectDirectoryPanel target:self
action:@selector(runModal)]; action:@selector(selectDirectory)];
[self.selectDirectoryButton setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.selectDirectoryButton setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.selectDirectoryButton]; [self.view addSubview:self.selectDirectoryButton];
@ -45,9 +45,17 @@
constant:8.0].active = YES; constant:8.0].active = YES;
} }
- (void) panel:(id)sender didChangeToDirectoryURL:(NSURL *)url { - (void) selectDirectory {
NSLog(@"[MainViewController] panel: %@ didChangeToDirectoryURL: %@", sender, url); NSLog(@"[MainViewController] select directory start");
return [super viewWillAppear]; [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 { - (void) viewWillAppear {

@ -1,6 +1,18 @@
package ui package ui
type UI interface { type UI interface {
Init() error Run(chan Event) error
Run() error }
type Event interface {
isUIEvent()
}
type event struct{}
func (e event) isUIEvent() {}
type SelectDirectoryEvent struct {
Path string
event
} }

@ -12,21 +12,31 @@ import (
*/ */
import "C" import "C"
func NewDesktop() (UI, error) { var desktopUI *cocoaUI
runtime.LockOSThread()
func Desktop() UI {
if desktopUI != nil {
return desktopUI
}
log.Println("Creating new cocoaUI") log.Println("Creating new cocoaUI")
return new(cocoaUI), nil runtime.LockOSThread()
C.Initialize()
desktopUI = new(cocoaUI)
return desktopUI
} }
type cocoaUI struct { type cocoaUI struct {
out chan Event
} }
func (ui *cocoaUI) Init() error { func (ui *cocoaUI) Run(out chan Event) error {
C.Initialize() ui.out = out
C.Run()
return nil return nil
} }
func (ui *cocoaUI) Run() error { //export selectDirectory
C.Run() func selectDirectory(path string) {
return nil desktopUI.out <- SelectDirectoryEvent{Path: path}
} }

Loading…
Cancel
Save