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

81 lines
1.6 KiB
Go

7 years ago
package ui
import (
"log"
"github.com/jordanorelli/dws/events"
7 years ago
)
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#include <stdlib.h>
7 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")
C.ui_init()
desktopUI = new(cocoaUI)
return desktopUI
7 years ago
}
type cocoaUI struct {
in chan events.BackgroundEvent
out chan events.UserEvent
7 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
7 years ago
go ui.forwardEvents()
C.ui_run()
7 years ago
return nil
}
7 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.bg_shutdown()
7 years ago
case events.SetRootEvent:
cpath := C.CString(v.Path)
C.bg_set_root(cpath)
7 years ago
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)
7 years ago
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)
7 years ago
}
}
7 years ago
func (ui *cocoaUI) forwardEvents() {
for e := range ui.in {
7 years ago
ui.forwardEvent(e)
7 years ago
}
}
//export selectDirectory
func selectDirectory(cpath *C.char) {
path := C.GoString(cpath)
desktopUI.out <- events.UserSelectedDirectory{Path: path}
7 years ago
}