event bridge

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

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

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

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

@ -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 "EventListener.h"
@interface MainViewController : NSViewController
@interface MainViewController : NSViewController <EventListener>
- (void) serverDidSetRoot:(NSString *)path;
@end

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

@ -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 <stdlib.h>
#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))
}
}
}

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

@ -1,5 +1,6 @@
#include <Cocoa/Cocoa.h>
#include "AppDelegate.h"
#import <Cocoa/Cocoa.h>
#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]];
}

Loading…
Cancel
Save