From 199c59b786892e826363025eeaac4f49d3d7292e Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 17 Jun 2017 20:43:35 -0500 Subject: [PATCH] event bridge this is probably a totally shit strategy --- bg/background.go | 1 + events/background_event.go | 5 +++++ ui/AppDelegate.m | 14 ++++++++------ ui/EventBridge.h | 7 +++++++ ui/EventBridge.m | 16 ++++++++++++++++ ui/EventListener.h | 6 ++++++ ui/MainViewController.h | 4 +++- ui/MainViewController.m | 20 ++++++++++++++++++-- ui/ui_darwin.go | 8 +++++++- ui/ui_darwin.h | 1 + ui/ui_darwin.m | 10 ++++++++-- 11 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 ui/EventBridge.h create mode 100644 ui/EventBridge.m create mode 100644 ui/EventListener.h diff --git a/bg/background.go b/bg/background.go index f55757f..76c6edc 100644 --- a/bg/background.go +++ b/bg/background.go @@ -29,6 +29,7 @@ func (bg *background) run() { switch v := e.(type) { case events.UserSelectedDirectory: bg.setRoot(v.Path) + bg.out <- events.SetRootEvent{Path: v.Path} } } } diff --git a/events/background_event.go b/events/background_event.go index e18c66e..268a13b 100644 --- a/events/background_event.go +++ b/events/background_event.go @@ -10,3 +10,8 @@ type backgroundEvent struct{ event } func (e backgroundEvent) isBackgroundEvent() {} type SigIntEvent struct{ backgroundEvent } + +type SetRootEvent struct { + Path string + backgroundEvent +} diff --git a/ui/AppDelegate.m b/ui/AppDelegate.m index e4ce0a8..b563c98 100644 --- a/ui/AppDelegate.m +++ b/ui/AppDelegate.m @@ -1,6 +1,12 @@ #import "AppDelegate.h" #import "MainWindowController.h" +@interface AppDelegate () + +@property (nonatomic, strong) MainWindowController *windowController; + +@end + @implementation AppDelegate // Application Startup ------------------------------------------------------{{{ @@ -59,14 +65,10 @@ - (void) createMainWindow { NSLog(@"[AppDelegate] Creating Main Window"); - // TODO: make a singleton? retain in a property of appdelegate? - // MainWindowController *windowController = [[[MainWindowController alloc] init] retain]; - MainWindowController *windowController = [[MainWindowController alloc] init]; - // NSLog(@"Window loaded: %d", [windowController isWindowLoaded]); - [windowController showWindow:self]; + self.windowController = [[MainWindowController alloc] init]; + [self.windowController showWindow:self]; } // --------------------------------------------------------------------------}}} - @end diff --git a/ui/EventBridge.h b/ui/EventBridge.h new file mode 100644 index 0000000..6db8205 --- /dev/null +++ b/ui/EventBridge.h @@ -0,0 +1,7 @@ +#import +#import "EventListener.h" + +@interface EventBridge : NSObject +@property (assign) id listener; ++ (instancetype) shared; +@end diff --git a/ui/EventBridge.m b/ui/EventBridge.m new file mode 100644 index 0000000..3fae18a --- /dev/null +++ b/ui/EventBridge.m @@ -0,0 +1,16 @@ +#import "EventBridge.h" + +@implementation EventBridge + ++ (instancetype) shared { + static id instance; + static dispatch_once_t once; + + dispatch_once(&once, ^{ + instance = [EventBridge new]; + }); + + return instance; +} + +@end diff --git a/ui/EventListener.h b/ui/EventListener.h new file mode 100644 index 0000000..7bf9e12 --- /dev/null +++ b/ui/EventListener.h @@ -0,0 +1,6 @@ +#import + +@protocol EventListener +- (void) serverDidSetRoot:(NSString *)path; +@end + diff --git a/ui/MainViewController.h b/ui/MainViewController.h index c370fca..5ae97d8 100644 --- a/ui/MainViewController.h +++ b/ui/MainViewController.h @@ -1,4 +1,6 @@ #import +#import "EventListener.h" -@interface MainViewController : NSViewController +@interface MainViewController : NSViewController +- (void) serverDidSetRoot:(NSString *)path; @end diff --git a/ui/MainViewController.m b/ui/MainViewController.m index 7555e14..ac7f12f 100644 --- a/ui/MainViewController.m +++ b/ui/MainViewController.m @@ -1,6 +1,7 @@ #import "_cgo_export.h" #import "MainViewController.h" #import "MainView.h" +#import "EventBridge.h" @interface MainViewController () @@ -38,16 +39,27 @@ [self.view addSubview:self.selectDirectoryButton]; // create label to show selected directory - self.selectedDirectoryText = [NSTextField labelWithString:@"(none)"]; + self.selectedDirectoryText = [NSTextField labelWithString:@"no directory selected"]; + [self.selectedDirectoryText setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addSubview:self.selectedDirectoryText]; - // setup constraints + // place button in top right [self.selectDirectoryButton.rightAnchor constraintEqualToAnchor:self.view.rightAnchor constant:-8.0].active = YES; [self.selectDirectoryButton.topAnchor constraintEqualToAnchor:self.view.topAnchor constant:8.0].active = YES; + + // place directory selection in top left + [self.selectedDirectoryText.leftAnchor + constraintEqualToAnchor:self.view.leftAnchor + constant:8.0].active = YES; + [self.selectedDirectoryText.topAnchor + constraintEqualToAnchor:self.view.topAnchor + constant:8.0].active = YES; + + [[EventBridge shared] setListener:self]; } - (void) selectDirectory { @@ -93,4 +105,8 @@ return [super viewDidLayout]; } +- (void) serverDidSetRoot:(NSString *)path { + [self.selectedDirectoryText setStringValue:path]; +} + @end diff --git a/ui/ui_darwin.go b/ui/ui_darwin.go index f647d62..566a2c3 100644 --- a/ui/ui_darwin.go +++ b/ui/ui_darwin.go @@ -2,6 +2,7 @@ package ui import ( "log" + "unsafe" "github.com/jordanorelli/dws/events" ) @@ -9,6 +10,7 @@ import ( /* #cgo CFLAGS: -x objective-c #cgo LDFLAGS: -framework Cocoa +#include #include "ui_darwin.h" */ import "C" @@ -42,10 +44,14 @@ func (ui *cocoaUI) Run(out chan events.UserEvent, in chan events.BackgroundEvent func (ui *cocoaUI) forwardEvents() { for e := range ui.in { - switch e.(type) { + switch v := e.(type) { case events.SigIntEvent: log.Println("Cocoa UI sees sig int, forwarding to NSApp") C.shutdown() + case events.SetRootEvent: + cs := C.CString(v.Path) + C.set_root(cs) + C.free(unsafe.Pointer(cs)) } } } diff --git a/ui/ui_darwin.h b/ui/ui_darwin.h index 48a1c96..8ed096a 100644 --- a/ui/ui_darwin.h +++ b/ui/ui_darwin.h @@ -1,3 +1,4 @@ void initialize(); int run(); void shutdown(); +void set_root(char *); diff --git a/ui/ui_darwin.m b/ui/ui_darwin.m index 1989b8e..941fc6d 100644 --- a/ui/ui_darwin.m +++ b/ui/ui_darwin.m @@ -1,5 +1,6 @@ -#include -#include "AppDelegate.h" +#import +#import "AppDelegate.h" +#import "EventBridge.h" id defaultAutoreleasePool; id appDelegate; @@ -20,3 +21,8 @@ int run() { void shutdown() { [[NSApplication sharedApplication] terminate:nil]; } + +void set_root(char *path) { + id listener = [[EventBridge shared] listener]; + [listener serverDidSetRoot:[NSString stringWithUTF8String:path]]; +}