aerc/ui/ui.go

102 lines
2.0 KiB
Go
Raw Normal View History

2018-01-11 03:03:56 +00:00
package ui
import (
tb "github.com/nsf/termbox-go"
"git.sr.ht/~sircmpwn/aerc2/config"
)
func Initialize(conf *config.AercConfig) (*UIState, error) {
state := UIState{
2018-01-11 03:41:15 +00:00
Config: conf,
2018-01-11 03:03:56 +00:00
InvalidPanes: InvalidateAll,
2018-01-11 03:41:15 +00:00
2018-01-11 14:04:18 +00:00
tbEvents: make(chan tb.Event, 10),
workerEvents: make(chan wrappedMessage),
2018-01-11 03:03:56 +00:00
}
if err := tb.Init(); err != nil {
return nil, err
}
tb.SetInputMode(tb.InputEsc | tb.InputMouse)
tb.SetOutputMode(tb.Output256)
2018-01-11 03:41:15 +00:00
go (func() {
for !state.Exit {
state.tbEvents <- tb.PollEvent()
}
})()
2018-01-11 03:03:56 +00:00
return &state, nil
}
func (state *UIState) Close() {
tb.Close()
}
2018-01-11 03:41:15 +00:00
func (state *UIState) AddTab(tab AercTab) {
2018-01-11 03:54:55 +00:00
tab.SetParent(state)
2018-01-11 03:41:15 +00:00
state.Tabs = append(state.Tabs, tab)
2018-01-11 14:04:18 +00:00
if listener, ok := tab.(WorkerListener); ok {
go (func() {
for msg := range listener.GetChannel() {
state.workerEvents <- wrappedMessage{
msg: msg,
listener: listener,
}
}
})()
}
2018-01-11 03:41:15 +00:00
}
2018-01-11 03:03:56 +00:00
func (state *UIState) Invalidate(what uint) {
state.InvalidPanes |= what
}
2018-01-11 03:54:55 +00:00
func (state *UIState) InvalidateFrom(tab AercTab) {
if state.Tabs[state.SelectedTab] == tab {
state.Invalidate(InvalidateTabView)
}
}
2018-01-11 03:41:15 +00:00
func (state *UIState) calcGeometries() {
width, height := tb.Size()
// TODO: more
state.Panes.TabView = Geometry{
Row: 0,
Col: 0,
Width: width,
Height: height,
}
}
2018-01-11 03:03:56 +00:00
func (state *UIState) Tick() bool {
2018-01-11 03:41:15 +00:00
select {
case event := <-state.tbEvents:
switch event.Type {
case tb.EventKey:
if event.Key == tb.KeyEsc {
state.Exit = true
}
case tb.EventResize:
state.Invalidate(InvalidateAll)
2018-01-11 03:03:56 +00:00
}
2018-01-11 14:04:18 +00:00
case msg := <-state.workerEvents:
msg.listener.HandleMessage(msg.msg)
2018-01-11 03:41:15 +00:00
default:
// no-op
break
2018-01-11 03:03:56 +00:00
}
if state.InvalidPanes != 0 {
2018-01-11 03:54:55 +00:00
invalid := state.InvalidPanes
state.InvalidPanes = 0
if invalid&InvalidateAll == InvalidateAll {
2018-01-11 03:41:15 +00:00
tb.Clear(tb.ColorDefault, tb.ColorDefault)
state.calcGeometries()
}
2018-01-11 03:54:55 +00:00
if invalid&InvalidateTabView != 0 {
2018-01-11 03:41:15 +00:00
tab := state.Tabs[state.SelectedTab]
tab.Render(state.Panes.TabView)
}
tb.Flush()
2018-01-11 03:03:56 +00:00
}
return true
}