put cocoa ui in a package

master
Jordan Orelli 8 years ago
parent fd612c80d3
commit 1f57a1aa5d

@ -1,5 +1,5 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "ui_darwin.h" #import "ui.h"
@protocol EventListener @protocol EventListener
- (void) serverDidSetRoot:(NSString *)path; - (void) serverDidSetRoot:(NSString *)path;

@ -1,5 +1,5 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "ui_darwin.h" #import "ui.h"
@interface RequestHistory : NSObject <NSTableViewDataSource> @interface RequestHistory : NSObject <NSTableViewDataSource>
- (void) addRequestItem:(RequestMeta *)meta; - (void) addRequestItem:(RequestMeta *)meta;

@ -1,5 +1,5 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "ui_darwin.h" #import "ui.h"
@interface RequestHistoryItem : NSObject @interface RequestHistoryItem : NSObject

@ -0,0 +1,79 @@
package cocoa
import (
"log"
"github.com/jordanorelli/dws/events"
)
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <stdlib.h>
#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}
}

@ -1,7 +1,7 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import "AppDelegate.h" #import "AppDelegate.h"
#import "EventBridge.h" #import "EventBridge.h"
#import "ui_darwin.h" #import "ui.h"
id defaultAutoreleasePool; id defaultAutoreleasePool;
id appDelegate; id appDelegate;

@ -1,80 +1,9 @@
package ui package ui
import ( import (
"log" "github.com/jordanorelli/dws/ui/cocoa"
"github.com/jordanorelli/dws/events"
) )
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <stdlib.h>
#include "ui_darwin.h"
*/
import "C"
var desktopUI *cocoaUI
func Desktop() UI { func Desktop() UI {
if desktopUI != nil { return cocoa.Desktop()
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}
} }

Loading…
Cancel
Save