event bridge

this is probably a totally shit strategy
master
Jordan Orelli 8 years ago
parent 8c328d3eac
commit 199c59b786

@ -29,6 +29,7 @@ func (bg *background) run() {
switch v := e.(type) { switch v := e.(type) {
case events.UserSelectedDirectory: case events.UserSelectedDirectory:
bg.setRoot(v.Path) bg.setRoot(v.Path)
bg.out <- events.SetRootEvent{Path: v.Path}
} }
} }
} }

@ -10,3 +10,8 @@ type backgroundEvent struct{ event }
func (e backgroundEvent) isBackgroundEvent() {} func (e backgroundEvent) isBackgroundEvent() {}
type SigIntEvent struct{ backgroundEvent } type SigIntEvent struct{ backgroundEvent }
type SetRootEvent struct {
Path string
backgroundEvent
}

@ -1,6 +1,12 @@
#import "AppDelegate.h" #import "AppDelegate.h"
#import "MainWindowController.h" #import "MainWindowController.h"
@interface AppDelegate ()
@property (nonatomic, strong) MainWindowController *windowController;
@end
@implementation AppDelegate @implementation AppDelegate
// Application Startup ------------------------------------------------------{{{ // Application Startup ------------------------------------------------------{{{
@ -59,14 +65,10 @@
- (void) createMainWindow { - (void) createMainWindow {
NSLog(@"[AppDelegate] Creating Main Window"); NSLog(@"[AppDelegate] Creating Main Window");
// TODO: make a singleton? retain in a property of appdelegate? self.windowController = [[MainWindowController alloc] init];
// MainWindowController *windowController = [[[MainWindowController alloc] init] retain]; [self.windowController showWindow:self];
MainWindowController *windowController = [[MainWindowController alloc] init];
// NSLog(@"Window loaded: %d", [windowController isWindowLoaded]);
[windowController showWindow:self];
} }
// --------------------------------------------------------------------------}}} // --------------------------------------------------------------------------}}}
@end @end

@ -0,0 +1,7 @@
#import <Cocoa/Cocoa.h>
#import "EventListener.h"
@interface EventBridge : NSObject
@property (assign) id <EventListener> listener;
+ (instancetype) shared;
@end

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

@ -0,0 +1,6 @@
#import <Cocoa/Cocoa.h>
@protocol EventListener
- (void) serverDidSetRoot:(NSString *)path;
@end

@ -1,4 +1,6 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "EventListener.h"
@interface MainViewController : NSViewController @interface MainViewController : NSViewController <EventListener>
- (void) serverDidSetRoot:(NSString *)path;
@end @end

@ -1,6 +1,7 @@
#import "_cgo_export.h" #import "_cgo_export.h"
#import "MainViewController.h" #import "MainViewController.h"
#import "MainView.h" #import "MainView.h"
#import "EventBridge.h"
@interface MainViewController () @interface MainViewController ()
@ -38,16 +39,27 @@
[self.view addSubview:self.selectDirectoryButton]; [self.view addSubview:self.selectDirectoryButton];
// create label to show selected directory // 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]; [self.view addSubview:self.selectedDirectoryText];
// setup constraints // place button in top right
[self.selectDirectoryButton.rightAnchor [self.selectDirectoryButton.rightAnchor
constraintEqualToAnchor:self.view.rightAnchor constraintEqualToAnchor:self.view.rightAnchor
constant:-8.0].active = YES; constant:-8.0].active = YES;
[self.selectDirectoryButton.topAnchor [self.selectDirectoryButton.topAnchor
constraintEqualToAnchor:self.view.topAnchor constraintEqualToAnchor:self.view.topAnchor
constant:8.0].active = YES; 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 { - (void) selectDirectory {
@ -93,4 +105,8 @@
return [super viewDidLayout]; return [super viewDidLayout];
} }
- (void) serverDidSetRoot:(NSString *)path {
[self.selectedDirectoryText setStringValue:path];
}
@end @end

@ -2,6 +2,7 @@ package ui
import ( import (
"log" "log"
"unsafe"
"github.com/jordanorelli/dws/events" "github.com/jordanorelli/dws/events"
) )
@ -9,6 +10,7 @@ import (
/* /*
#cgo CFLAGS: -x objective-c #cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa #cgo LDFLAGS: -framework Cocoa
#include <stdlib.h>
#include "ui_darwin.h" #include "ui_darwin.h"
*/ */
import "C" import "C"
@ -42,10 +44,14 @@ func (ui *cocoaUI) Run(out chan events.UserEvent, in chan events.BackgroundEvent
func (ui *cocoaUI) forwardEvents() { func (ui *cocoaUI) forwardEvents() {
for e := range ui.in { for e := range ui.in {
switch e.(type) { switch v := e.(type) {
case events.SigIntEvent: case events.SigIntEvent:
log.Println("Cocoa UI sees sig int, forwarding to NSApp") log.Println("Cocoa UI sees sig int, forwarding to NSApp")
C.shutdown() C.shutdown()
case events.SetRootEvent:
cs := C.CString(v.Path)
C.set_root(cs)
C.free(unsafe.Pointer(cs))
} }
} }
} }

@ -1,3 +1,4 @@
void initialize(); void initialize();
int run(); int run();
void shutdown(); void shutdown();
void set_root(char *);

@ -1,5 +1,6 @@
#include <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#include "AppDelegate.h" #import "AppDelegate.h"
#import "EventBridge.h"
id defaultAutoreleasePool; id defaultAutoreleasePool;
id appDelegate; id appDelegate;
@ -20,3 +21,8 @@ int run() {
void shutdown() { void shutdown() {
[[NSApplication sharedApplication] terminate:nil]; [[NSApplication sharedApplication] terminate:nil];
} }
void set_root(char *path) {
id listener = [[EventBridge shared] listener];
[listener serverDidSetRoot:[NSString stringWithUTF8String:path]];
}

Loading…
Cancel
Save