2018-01-11 04:03:56 +01:00
|
|
|
package ui
|
|
|
|
|
|
|
|
import (
|
|
|
|
tb "github.com/nsf/termbox-go"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/config"
|
|
|
|
)
|
|
|
|
|
2018-02-17 22:35:36 +01:00
|
|
|
type UI struct {
|
|
|
|
Exit bool
|
2018-02-28 03:17:26 +01:00
|
|
|
Content DrawableInteractive
|
2018-02-17 22:35:36 +01:00
|
|
|
ctx *Context
|
2018-01-11 04:41:15 +01:00
|
|
|
|
2018-02-17 22:35:36 +01:00
|
|
|
tbEvents chan tb.Event
|
|
|
|
invalidations chan interface{}
|
|
|
|
}
|
|
|
|
|
2018-02-28 03:17:26 +01:00
|
|
|
func Initialize(conf *config.AercConfig,
|
|
|
|
content DrawableInteractive) (*UI, error) {
|
|
|
|
|
2018-01-11 04:03:56 +01:00
|
|
|
if err := tb.Init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-02-17 22:35:36 +01:00
|
|
|
width, height := tb.Size()
|
|
|
|
state := UI{
|
|
|
|
Content: content,
|
|
|
|
ctx: NewContext(width, height),
|
|
|
|
|
|
|
|
tbEvents: make(chan tb.Event, 10),
|
|
|
|
invalidations: make(chan interface{}),
|
|
|
|
}
|
2018-01-11 04:03:56 +01:00
|
|
|
tb.SetInputMode(tb.InputEsc | tb.InputMouse)
|
|
|
|
tb.SetOutputMode(tb.Output256)
|
2018-01-11 04:41:15 +01:00
|
|
|
go (func() {
|
|
|
|
for !state.Exit {
|
|
|
|
state.tbEvents <- tb.PollEvent()
|
|
|
|
}
|
|
|
|
})()
|
2018-02-17 22:35:36 +01:00
|
|
|
go (func() { state.invalidations <- nil })()
|
|
|
|
content.OnInvalidate(func(_ Drawable) {
|
|
|
|
go (func() { state.invalidations <- nil })()
|
|
|
|
})
|
2018-01-11 04:03:56 +01:00
|
|
|
return &state, nil
|
|
|
|
}
|
|
|
|
|
2018-02-17 22:35:36 +01:00
|
|
|
func (state *UI) Close() {
|
2018-01-11 04:03:56 +01:00
|
|
|
tb.Close()
|
|
|
|
}
|
|
|
|
|
2018-02-17 22:35:36 +01:00
|
|
|
func (state *UI) Tick() bool {
|
2018-01-11 04:41:15 +01:00
|
|
|
select {
|
|
|
|
case event := <-state.tbEvents:
|
|
|
|
switch event.Type {
|
|
|
|
case tb.EventKey:
|
2018-02-28 03:17:26 +01:00
|
|
|
// TODO: temporary
|
2018-01-11 04:41:15 +01:00
|
|
|
if event.Key == tb.KeyEsc {
|
|
|
|
state.Exit = true
|
|
|
|
}
|
|
|
|
case tb.EventResize:
|
|
|
|
tb.Clear(tb.ColorDefault, tb.ColorDefault)
|
2018-02-17 22:35:36 +01:00
|
|
|
state.ctx = NewContext(event.Width, event.Height)
|
|
|
|
state.Content.Invalidate()
|
2018-01-11 04:41:15 +01:00
|
|
|
}
|
2018-02-28 03:17:26 +01:00
|
|
|
state.Content.Event(event)
|
2018-02-17 22:35:36 +01:00
|
|
|
case <-state.invalidations:
|
|
|
|
state.Content.Draw(state.ctx)
|
2018-01-11 04:41:15 +01:00
|
|
|
tb.Flush()
|
2018-02-17 22:35:36 +01:00
|
|
|
default:
|
|
|
|
return false
|
2018-01-11 04:03:56 +01:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|