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

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

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

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

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

Loading…
Cancel
Save