WIP Wrap up Event

This commit is contained in:
Zack Guo 2015-08-08 19:07:32 -04:00
parent 5816873b74
commit 283c3a36f2

View File

@ -8,10 +8,10 @@
package termui package termui
import "github.com/nsf/termbox-go" //import "github.com/nsf/termbox-go"
/***********************************termbox-go**************************************/ /***********************************termbox-go**************************************/
/*
type ( type (
EventType uint8 EventType uint8
Modifier uint8 Modifier uint8
@ -127,9 +127,9 @@ const (
EventRaw EventRaw
EventNone EventNone
) )
*/
/**************************************end**************************************/ /**************************************end**************************************/
/*
// convert termbox.Event to termui.Event // convert termbox.Event to termui.Event
func uiEvt(e termbox.Event) Event { func uiEvt(e termbox.Event) Event {
event := Event{} event := Event{}
@ -171,49 +171,52 @@ func evtListen() {
} }
}() }()
} }
*/
/* type Event struct {
// EventHandlers is a handler sequence Type string
var EventHandlers []func(Event) Uri string
Data interface{}
var signalQuit = make(chan bool) Time int
Refer string
// Quit sends quit signal to terminate termui
func Quit() {
signalQuit <- true
} }
// Wait listening to signalQuit, block operation. type evtCtl struct {
func Wait() { in chan Event
<-signalQuit out chan Event
suspend chan int
recover chan int
close chan int
} }
// RegEvtHandler register function into TSEventHandler sequence. //
func RegEvtHandler(fn func(Event)) { type EvtStream struct {
EventHandlers = append(EventHandlers, fn) srcMap map[string]evtCtl
stream chan Event
} }
// EventLoop handles all events and func newEvtCtl() evtCtl {
// redirects every event to callbacks in EventHandlers ec := evtCtl{}
func EventLoop() { ec.in = make(chan Event)
evt := make(chan termbox.Event) ec.suspend = make(chan int)
ec.recover = make(chan int)
ec.close = make(chan int)
ec.out = make(chan Event)
return ec
}
go func() { func NewEvtStream() EvtStream {
for { return EvtStream{
evt <- termbox.PollEvent() srcMap: make(map[string]evtCtl),
} stream: make(chan Event),
}()
for {
select {
case c := <-signalQuit:
defer func() { signalQuit <- c }()
return
case e := <-evt:
for _, fn := range EventHandlers {
fn(uiEvt(e))
}
}
} }
} }
/*
func (es *EvtStream) hookup() {
}
func (es EvtStream) Subscribe(uri string) chan Event {
}
*/ */