mirror of https://github.com/gizak/termui.git
Add custom event handling
This commit is contained in:
parent
86950762b0
commit
2e6591a15e
|
@ -21,6 +21,14 @@ func main() {
|
||||||
p.TextFgColor = ui.ColorWhite
|
p.TextFgColor = ui.ColorWhite
|
||||||
p.BorderLabel = "Text Box"
|
p.BorderLabel = "Text Box"
|
||||||
p.BorderFg = ui.ColorCyan
|
p.BorderFg = ui.ColorCyan
|
||||||
|
p.Handle("/timer/1s", func(e ui.Event) {
|
||||||
|
cnt := e.Data.(ui.EvtTimer)
|
||||||
|
if cnt.Count%2 == 0 {
|
||||||
|
p.TextFgColor = ui.ColorRed
|
||||||
|
} else {
|
||||||
|
p.TextFgColor = ui.ColorWhite
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
strs := []string{"[0] gizak/termui", "[1] editbox.go", "[2] iterrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"}
|
strs := []string{"[0] gizak/termui", "[1] editbox.go", "[2] iterrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"}
|
||||||
list := ui.NewList()
|
list := ui.NewList()
|
||||||
|
|
10
events.go
10
events.go
|
@ -315,3 +315,13 @@ func NewTimerCh(du time.Duration) chan Event {
|
||||||
|
|
||||||
var DefualtHandler = func(e Event) {
|
var DefualtHandler = func(e Event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var usrEvtCh = make(chan Event)
|
||||||
|
|
||||||
|
func SendCustomEvt(path string, data interface{}) {
|
||||||
|
e := Event{}
|
||||||
|
e.Path = path
|
||||||
|
e.Data = data
|
||||||
|
e.Time = time.Now().Unix()
|
||||||
|
usrEvtCh <- e
|
||||||
|
}
|
||||||
|
|
|
@ -41,6 +41,8 @@ func Init() error {
|
||||||
DefaultEvtStream.Init()
|
DefaultEvtStream.Init()
|
||||||
DefaultEvtStream.Merge("termbox", NewSysEvtCh())
|
DefaultEvtStream.Merge("termbox", NewSysEvtCh())
|
||||||
DefaultEvtStream.Merge("timer", NewTimerCh(time.Second))
|
DefaultEvtStream.Merge("timer", NewTimerCh(time.Second))
|
||||||
|
DefaultEvtStream.Merge("custom", usrEvtCh)
|
||||||
|
|
||||||
DefaultEvtStream.Handle("/", DefualtHandler)
|
DefaultEvtStream.Handle("/", DefualtHandler)
|
||||||
DefaultEvtStream.Handle("/sys/wnd/resize", func(e Event) {
|
DefaultEvtStream.Handle("/sys/wnd/resize", func(e Event) {
|
||||||
w := e.Data.(EvtWnd)
|
w := e.Data.(EvtWnd)
|
||||||
|
|
|
@ -40,8 +40,13 @@ func main() {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
termui.Handle(("/usr"), func(e termui.Event) {
|
||||||
|
debug.Logf("->%v\n", e)
|
||||||
|
})
|
||||||
|
|
||||||
termui.Handle("/timer/1s", func(e termui.Event) {
|
termui.Handle("/timer/1s", func(e termui.Event) {
|
||||||
t := e.Data.(termui.EvtTimer)
|
t := e.Data.(termui.EvtTimer)
|
||||||
|
termui.SendCustomEvt("/usr/t", t.Count)
|
||||||
|
|
||||||
if t.Count%2 == 0 {
|
if t.Count%2 == 0 {
|
||||||
b.BorderLabel = "[HELLO](fg-red,bg-green) [WORLD](fg-blue,bg-white)"
|
b.BorderLabel = "[HELLO](fg-red,bg-green) [WORLD](fg-blue,bg-white)"
|
||||||
|
@ -52,5 +57,6 @@ func main() {
|
||||||
termui.Render(b)
|
termui.Render(b)
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
termui.Loop()
|
termui.Loop()
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,3 +80,11 @@ func (wm WgtMgr) WgtHandlersHook() func(Event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultWgtMgr WgtMgr
|
var DefaultWgtMgr WgtMgr
|
||||||
|
|
||||||
|
func (b *Block) Handle(path string, handler func(Event)) {
|
||||||
|
if _, ok := DefaultWgtMgr[b.Id()]; !ok {
|
||||||
|
DefaultWgtMgr.AddWgt(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
DefaultWgtMgr.AddWgtHandler(b.Id(), path, handler)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue