You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dws/ui/ui_darwin.go

87 lines
1.5 KiB
Go

8 years ago
package ui
import (
"log"
"unsafe"
"github.com/jordanorelli/dws/events"
8 years ago
)
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <stdlib.h>
8 years ago
#include "ui_darwin.h"
*/
import "C"
var desktopUI *cocoaUI
func Desktop() UI {
if desktopUI != nil {
return desktopUI
}
log.Println("Creating new Cocoa UI")
8 years ago
C.initialize()
desktopUI = new(cocoaUI)
return desktopUI
8 years ago
}
type cocoaUI struct {
in chan events.BackgroundEvent
out chan events.UserEvent
8 years ago
}
func (ui *cocoaUI) Run(out chan events.UserEvent, in chan events.BackgroundEvent) error {
log.Println("Running Desktop UI")
ui.in = in
ui.out = out
8 years ago
go ui.forwardEvents()
C.run()
8 years ago
return nil
}
8 years ago
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.shutdown()
case events.SetRootEvent:
cpath := C.CString(v.Path)
defer C.free(unsafe.Pointer(cpath))
C.set_root(cpath)
case events.BeginRequestEvent:
cpath := C.CString(v.Path)
defer C.free(unsafe.Pointer(cpath))
req := &C.struct_RequestMeta{
8 years ago
seq: C.int(v.Seq),
8 years ago
path: cpath,
}
8 years ago
C.received_request(req)
8 years ago
case events.EndRequestEvent:
8 years ago
C.sent_response(&C.struct_ResponseMeta{
seq: C.int(v.Seq),
status: C.int(v.Status),
bytes: C.int(v.Bytes),
})
8 years ago
}
}
8 years ago
func (ui *cocoaUI) forwardEvents() {
for e := range ui.in {
8 years ago
ui.forwardEvent(e)
8 years ago
}
}
//export selectDirectory
func selectDirectory(cpath *C.char) {
path := C.GoString(cpath)
desktopUI.out <- events.UserSelectedDirectory{Path: path}
8 years ago
}