diff options
author | Drew DeVault <sir@cmpwn.com> | 2018-01-10 22:41:15 -0500 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2018-01-10 22:41:15 -0500 |
commit | 77a0f68758905faa74407499ff92c90929e27989 (patch) | |
tree | 44edc8d2de572deff60330f609aecab56aa4dea7 /ui/render.go | |
parent | db1b2cd53f5dc7bfbfb6ee54ad0bb0882ea2cc03 (diff) | |
download | aerc-77a0f68758905faa74407499ff92c90929e27989.tar.gz |
Make termbox event loop async
Diffstat (limited to 'ui/render.go')
-rw-r--r-- | ui/render.go | 53 |
1 files changed, 44 insertions, 9 deletions
diff --git a/ui/render.go b/ui/render.go index bca0cf60..4fedc2cc 100644 --- a/ui/render.go +++ b/ui/render.go @@ -8,15 +8,21 @@ import ( func Initialize(conf *config.AercConfig) (*UIState, error) { state := UIState{ + Config: conf, InvalidPanes: InvalidateAll, - Tabs: make([]AercTab, len(conf.Accounts)), + + tbEvents: make(chan tb.Event, 10), } - // TODO: Initialize each tab to a mailbox tab if err := tb.Init(); err != nil { return nil, err } tb.SetInputMode(tb.InputEsc | tb.InputMouse) tb.SetOutputMode(tb.Output256) + go (func() { + for !state.Exit { + state.tbEvents <- tb.PollEvent() + } + })() return &state, nil } @@ -24,21 +30,50 @@ func (state *UIState) Close() { tb.Close() } +func (state *UIState) AddTab(tab AercTab) { + state.Tabs = append(state.Tabs, tab) +} + func (state *UIState) Invalidate(what uint) { state.InvalidPanes |= what } +func (state *UIState) calcGeometries() { + width, height := tb.Size() + // TODO: more + state.Panes.TabView = Geometry{ + Row: 0, + Col: 0, + Width: width, + Height: height, + } +} + func (state *UIState) Tick() bool { - switch e := tb.PollEvent(); e.Type { - case tb.EventKey: - if e.Key == tb.KeyEsc { - state.Exit = true + 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) } - case tb.EventResize: - state.Invalidate(InvalidateAll) + default: + // no-op + break } if state.InvalidPanes != 0 { - // TODO: re-render + if state.InvalidPanes&InvalidateAll == InvalidateAll { + tb.Clear(tb.ColorDefault, tb.ColorDefault) + state.calcGeometries() + } + if state.InvalidPanes&InvalidateTabs != 0 { + tab := state.Tabs[state.SelectedTab] + tab.Render(state.Panes.TabView) + } + tb.Flush() state.InvalidPanes = 0 } return true |