From 1f57a1aa5d551ad9b783251dfad8030b1589eda5 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sun, 18 Jun 2017 22:07:35 -0500 Subject: [PATCH] put cocoa ui in a package --- ui/{ => cocoa}/AppDelegate.h | 0 ui/{ => cocoa}/AppDelegate.m | 0 ui/{ => cocoa}/EventBridge.h | 0 ui/{ => cocoa}/EventBridge.m | 0 ui/{ => cocoa}/EventListener.h | 2 +- ui/{ => cocoa}/MainView.h | 0 ui/{ => cocoa}/MainView.m | 0 ui/{ => cocoa}/MainViewController.h | 0 ui/{ => cocoa}/MainViewController.m | 0 ui/{ => cocoa}/MainWindowController.h | 0 ui/{ => cocoa}/MainWindowController.m | 0 ui/{ => cocoa}/RequestHistory.h | 2 +- ui/{ => cocoa}/RequestHistory.m | 0 ui/{ => cocoa}/RequestHistoryItem.h | 2 +- ui/{ => cocoa}/RequestHistoryItem.m | 0 ui/cocoa/ui.go | 79 +++++++++++++++++++++++++++ ui/{ui_darwin.h => cocoa/ui.h} | 0 ui/{ui_darwin.m => cocoa/ui.m} | 2 +- ui/ui_darwin.go | 75 +------------------------ 19 files changed, 85 insertions(+), 77 deletions(-) rename ui/{ => cocoa}/AppDelegate.h (100%) rename ui/{ => cocoa}/AppDelegate.m (100%) rename ui/{ => cocoa}/EventBridge.h (100%) rename ui/{ => cocoa}/EventBridge.m (100%) rename ui/{ => cocoa}/EventListener.h (90%) rename ui/{ => cocoa}/MainView.h (100%) rename ui/{ => cocoa}/MainView.m (100%) rename ui/{ => cocoa}/MainViewController.h (100%) rename ui/{ => cocoa}/MainViewController.m (100%) rename ui/{ => cocoa}/MainWindowController.h (100%) rename ui/{ => cocoa}/MainWindowController.m (100%) rename ui/{ => cocoa}/RequestHistory.h (89%) rename ui/{ => cocoa}/RequestHistory.m (100%) rename ui/{ => cocoa}/RequestHistoryItem.h (94%) rename ui/{ => cocoa}/RequestHistoryItem.m (100%) create mode 100644 ui/cocoa/ui.go rename ui/{ui_darwin.h => cocoa/ui.h} (100%) rename ui/{ui_darwin.m => cocoa/ui.m} (97%) diff --git a/ui/AppDelegate.h b/ui/cocoa/AppDelegate.h similarity index 100% rename from ui/AppDelegate.h rename to ui/cocoa/AppDelegate.h diff --git a/ui/AppDelegate.m b/ui/cocoa/AppDelegate.m similarity index 100% rename from ui/AppDelegate.m rename to ui/cocoa/AppDelegate.m diff --git a/ui/EventBridge.h b/ui/cocoa/EventBridge.h similarity index 100% rename from ui/EventBridge.h rename to ui/cocoa/EventBridge.h diff --git a/ui/EventBridge.m b/ui/cocoa/EventBridge.m similarity index 100% rename from ui/EventBridge.m rename to ui/cocoa/EventBridge.m diff --git a/ui/EventListener.h b/ui/cocoa/EventListener.h similarity index 90% rename from ui/EventListener.h rename to ui/cocoa/EventListener.h index 10e377f..47962e6 100644 --- a/ui/EventListener.h +++ b/ui/cocoa/EventListener.h @@ -1,5 +1,5 @@ #import -#import "ui_darwin.h" +#import "ui.h" @protocol EventListener - (void) serverDidSetRoot:(NSString *)path; diff --git a/ui/MainView.h b/ui/cocoa/MainView.h similarity index 100% rename from ui/MainView.h rename to ui/cocoa/MainView.h diff --git a/ui/MainView.m b/ui/cocoa/MainView.m similarity index 100% rename from ui/MainView.m rename to ui/cocoa/MainView.m diff --git a/ui/MainViewController.h b/ui/cocoa/MainViewController.h similarity index 100% rename from ui/MainViewController.h rename to ui/cocoa/MainViewController.h diff --git a/ui/MainViewController.m b/ui/cocoa/MainViewController.m similarity index 100% rename from ui/MainViewController.m rename to ui/cocoa/MainViewController.m diff --git a/ui/MainWindowController.h b/ui/cocoa/MainWindowController.h similarity index 100% rename from ui/MainWindowController.h rename to ui/cocoa/MainWindowController.h diff --git a/ui/MainWindowController.m b/ui/cocoa/MainWindowController.m similarity index 100% rename from ui/MainWindowController.m rename to ui/cocoa/MainWindowController.m diff --git a/ui/RequestHistory.h b/ui/cocoa/RequestHistory.h similarity index 89% rename from ui/RequestHistory.h rename to ui/cocoa/RequestHistory.h index f4f2cfa..ee53c30 100644 --- a/ui/RequestHistory.h +++ b/ui/cocoa/RequestHistory.h @@ -1,5 +1,5 @@ #import -#import "ui_darwin.h" +#import "ui.h" @interface RequestHistory : NSObject - (void) addRequestItem:(RequestMeta *)meta; diff --git a/ui/RequestHistory.m b/ui/cocoa/RequestHistory.m similarity index 100% rename from ui/RequestHistory.m rename to ui/cocoa/RequestHistory.m diff --git a/ui/RequestHistoryItem.h b/ui/cocoa/RequestHistoryItem.h similarity index 94% rename from ui/RequestHistoryItem.h rename to ui/cocoa/RequestHistoryItem.h index ce7fae6..ad3aa3f 100644 --- a/ui/RequestHistoryItem.h +++ b/ui/cocoa/RequestHistoryItem.h @@ -1,5 +1,5 @@ #import -#import "ui_darwin.h" +#import "ui.h" @interface RequestHistoryItem : NSObject diff --git a/ui/RequestHistoryItem.m b/ui/cocoa/RequestHistoryItem.m similarity index 100% rename from ui/RequestHistoryItem.m rename to ui/cocoa/RequestHistoryItem.m diff --git a/ui/cocoa/ui.go b/ui/cocoa/ui.go new file mode 100644 index 0000000..89e6ed7 --- /dev/null +++ b/ui/cocoa/ui.go @@ -0,0 +1,79 @@ +package cocoa + +import ( + "log" + + "github.com/jordanorelli/dws/events" +) + +/* +#cgo CFLAGS: -x objective-c +#cgo LDFLAGS: -framework Cocoa +#include +#include "ui.h" +*/ +import "C" + +var instance *ui + +type ui struct { + in chan events.BackgroundEvent + out chan events.UserEvent +} + +func Desktop() *ui { + if instance != nil { + return instance + } + log.Println("Creating new Cocoa UI") + C.ui_init() + instance = new(ui) + return instance +} + +func (ui *ui) Run(out chan events.UserEvent, in chan events.BackgroundEvent) error { + log.Println("Running Desktop UI") + ui.in = in + ui.out = out + go ui.forwardEvents() + C.ui_run() + return nil +} + +func (ui *ui) forwardEvent(e events.BackgroundEvent) { + switch v := e.(type) { + case events.SigIntEvent: + log.Println("Cocoa UI sees sig int, forwarding to NSApp") + C.bg_shutdown() + + case events.SetRootEvent: + cpath := C.CString(v.Path) + C.bg_set_root(cpath) + + case events.BeginRequestEvent: + cpath := C.CString(v.Path) + req := (*C.struct_RequestMeta)(C.malloc(C.sizeof_struct_RequestMeta)) + req.seq = C.int(v.Seq) + req.path = cpath + C.bg_received_request(req) + + case events.EndRequestEvent: + res := (*C.struct_ResponseMeta)(C.malloc(C.sizeof_struct_ResponseMeta)) + res.seq = C.int(v.Seq) + res.status = C.int(v.Status) + res.bytes = C.int(v.Bytes) + C.bg_sent_response(res) + } +} + +func (ui *ui) forwardEvents() { + for e := range ui.in { + ui.forwardEvent(e) + } +} + +//export selectDirectory +func selectDirectory(cpath *C.char) { + path := C.GoString(cpath) + instance.out <- events.UserSelectedDirectory{Path: path} +} diff --git a/ui/ui_darwin.h b/ui/cocoa/ui.h similarity index 100% rename from ui/ui_darwin.h rename to ui/cocoa/ui.h diff --git a/ui/ui_darwin.m b/ui/cocoa/ui.m similarity index 97% rename from ui/ui_darwin.m rename to ui/cocoa/ui.m index d744fbb..759a4ad 100644 --- a/ui/ui_darwin.m +++ b/ui/cocoa/ui.m @@ -1,7 +1,7 @@ #import #import "AppDelegate.h" #import "EventBridge.h" -#import "ui_darwin.h" +#import "ui.h" id defaultAutoreleasePool; id appDelegate; diff --git a/ui/ui_darwin.go b/ui/ui_darwin.go index 823a6f0..df45912 100644 --- a/ui/ui_darwin.go +++ b/ui/ui_darwin.go @@ -1,80 +1,9 @@ package ui import ( - "log" - - "github.com/jordanorelli/dws/events" + "github.com/jordanorelli/dws/ui/cocoa" ) -/* -#cgo CFLAGS: -x objective-c -#cgo LDFLAGS: -framework Cocoa -#include -#include "ui_darwin.h" -*/ -import "C" - -var desktopUI *cocoaUI - func Desktop() UI { - if desktopUI != nil { - return desktopUI - } - - log.Println("Creating new Cocoa UI") - C.ui_init() - desktopUI = new(cocoaUI) - return desktopUI -} - -type cocoaUI struct { - in chan events.BackgroundEvent - out chan events.UserEvent -} - -func (ui *cocoaUI) Run(out chan events.UserEvent, in chan events.BackgroundEvent) error { - log.Println("Running Desktop UI") - ui.in = in - ui.out = out - go ui.forwardEvents() - C.ui_run() - return nil -} - -func (ui *cocoaUI) forwardEvent(e events.BackgroundEvent) { - switch v := e.(type) { - case events.SigIntEvent: - log.Println("Cocoa UI sees sig int, forwarding to NSApp") - C.bg_shutdown() - - case events.SetRootEvent: - cpath := C.CString(v.Path) - C.bg_set_root(cpath) - - case events.BeginRequestEvent: - cpath := C.CString(v.Path) - req := (*C.struct_RequestMeta)(C.malloc(C.sizeof_struct_RequestMeta)) - req.seq = C.int(v.Seq) - req.path = cpath - C.bg_received_request(req) - - case events.EndRequestEvent: - res := (*C.struct_ResponseMeta)(C.malloc(C.sizeof_struct_ResponseMeta)) - res.seq = C.int(v.Seq) - res.status = C.int(v.Status) - res.bytes = C.int(v.Bytes) - C.bg_sent_response(res) - } -} - -func (ui *cocoaUI) forwardEvents() { - for e := range ui.in { - ui.forwardEvent(e) - } -} - -//export selectDirectory -func selectDirectory(cpath *C.char) { - path := C.GoString(cpath) - desktopUI.out <- events.UserSelectedDirectory{Path: path} + return cocoa.Desktop() }