From 55e84533022c1f9bf9fd9e2cd2db930394b590b8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 10 Jan 2018 22:54:55 -0500 Subject: Improve invalidation logic --- ui/account.go | 13 +++++---- ui/render.go | 80 ----------------------------------------------------- ui/types.go | 11 +++++--- ui/ui.go | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 89 deletions(-) delete mode 100644 ui/render.go create mode 100644 ui/ui.go diff --git a/ui/account.go b/ui/account.go index 0949e52..9c16cc5 100644 --- a/ui/account.go +++ b/ui/account.go @@ -11,6 +11,8 @@ type AccountTab struct { Config *config.AccountConfig Worker *worker.Worker Parent *UIState + + counter int } func NewAccountTab(conf *config.AccountConfig, work *worker.Worker) *AccountTab { @@ -24,10 +26,6 @@ func (acc *AccountTab) Name() string { return acc.Config.Name } -func (acc *AccountTab) Invalid() bool { - return false -} - func (acc *AccountTab) SetParent(parent *UIState) { acc.Parent = parent } @@ -37,5 +35,10 @@ func (acc *AccountTab) Render(at Geometry) { Fg: tb.ColorDefault, Bg: tb.ColorDefault, } - TPrintf(&at, cell, "%s", acc.Name()) + TPrintf(&at, cell, "%s %d", acc.Name(), acc.counter) + acc.counter++ + if acc.counter%10000 == 0 { + acc.counter = 0 + } + acc.Parent.InvalidateFrom(acc) } diff --git a/ui/render.go b/ui/render.go deleted file mode 100644 index 4fedc2c..0000000 --- a/ui/render.go +++ /dev/null @@ -1,80 +0,0 @@ -package ui - -import ( - tb "github.com/nsf/termbox-go" - - "git.sr.ht/~sircmpwn/aerc2/config" -) - -func Initialize(conf *config.AercConfig) (*UIState, error) { - state := UIState{ - Config: conf, - InvalidPanes: InvalidateAll, - - tbEvents: make(chan tb.Event, 10), - } - 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 -} - -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 { - 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) - } - default: - // no-op - break - } - if state.InvalidPanes != 0 { - 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 -} diff --git a/ui/types.go b/ui/types.go index 588c3b3..14a91c3 100644 --- a/ui/types.go +++ b/ui/types.go @@ -7,14 +7,18 @@ import ( ) const ( - Valid = 0 - InvalidateTabs = 1 << iota + Valid = 0 + InvalidateTabList = 1 << iota + InvalidateTabView InvalidateSidebar InvalidateStatusBar ) const ( - InvalidateAll = InvalidateTabs | InvalidateSidebar | InvalidateStatusBar + InvalidateAll = InvalidateTabList | + InvalidateTabView | + InvalidateSidebar | + InvalidateStatusBar ) type Geometry struct { @@ -26,7 +30,6 @@ type Geometry struct { type AercTab interface { Name() string - Invalid() bool Render(at Geometry) SetParent(parent *UIState) } diff --git a/ui/ui.go b/ui/ui.go new file mode 100644 index 0000000..f01af08 --- /dev/null +++ b/ui/ui.go @@ -0,0 +1,88 @@ +package ui + +import ( + tb "github.com/nsf/termbox-go" + + "git.sr.ht/~sircmpwn/aerc2/config" +) + +func Initialize(conf *config.AercConfig) (*UIState, error) { + state := UIState{ + Config: conf, + InvalidPanes: InvalidateAll, + + tbEvents: make(chan tb.Event, 10), + } + 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 +} + +func (state *UIState) Close() { + tb.Close() +} + +func (state *UIState) AddTab(tab AercTab) { + tab.SetParent(state) + state.Tabs = append(state.Tabs, tab) +} + +func (state *UIState) Invalidate(what uint) { + state.InvalidPanes |= what +} + +func (state *UIState) InvalidateFrom(tab AercTab) { + if state.Tabs[state.SelectedTab] == tab { + state.Invalidate(InvalidateTabView) + } +} + +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 { + 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) + } + default: + // no-op + break + } + if state.InvalidPanes != 0 { + invalid := state.InvalidPanes + state.InvalidPanes = 0 + if invalid&InvalidateAll == InvalidateAll { + tb.Clear(tb.ColorDefault, tb.ColorDefault) + state.calcGeometries() + } + if invalid&InvalidateTabView != 0 { + tab := state.Tabs[state.SelectedTab] + tab.Render(state.Panes.TabView) + } + tb.Flush() + } + return true +} -- cgit v1.2.3