diff options
author | Tim Culverhouse <tim@timculverhouse.com> | 2022-10-07 11:00:31 -0500 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2022-10-12 22:16:40 +0200 |
commit | ba24e92062f1a9b38065d503fd1dde749c27a56c (patch) | |
tree | d1a2981f8f9bda50a070b50c688388fe2130f770 /lib/ui | |
parent | 34014d3ceeebe8a9c131213fa56d1977fbc26b4a (diff) | |
download | aerc-ba24e92062f1a9b38065d503fd1dde749c27a56c.tar.gz |
invalidatable: cleanup dead code
Remove invalidatable type and all associated calls. All items can
directly invalidate the UI.
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/ui')
-rw-r--r-- | lib/ui/borders.go | 8 | ||||
-rw-r--r-- | lib/ui/fill.go | 4 | ||||
-rw-r--r-- | lib/ui/grid.go | 59 | ||||
-rw-r--r-- | lib/ui/interfaces.go | 5 | ||||
-rw-r--r-- | lib/ui/invalidatable.go | 17 | ||||
-rw-r--r-- | lib/ui/popover.go | 8 | ||||
-rw-r--r-- | lib/ui/stack.go | 25 | ||||
-rw-r--r-- | lib/ui/tab.go | 42 | ||||
-rw-r--r-- | lib/ui/text.go | 3 | ||||
-rw-r--r-- | lib/ui/textinput.go | 5 | ||||
-rw-r--r-- | lib/ui/ui.go | 2 |
11 files changed, 19 insertions, 159 deletions
diff --git a/lib/ui/borders.go b/lib/ui/borders.go index 92e29b06..81d5db61 100644 --- a/lib/ui/borders.go +++ b/lib/ui/borders.go @@ -14,7 +14,6 @@ const ( ) type Bordered struct { - Invalidatable borders uint content Drawable uiConfig *config.UIConfig @@ -28,20 +27,15 @@ func NewBordered( content: content, uiConfig: uiConfig, } - content.OnInvalidate(b.contentInvalidated) return b } -func (bordered *Bordered) contentInvalidated(d Drawable) { - bordered.Invalidate() -} - func (bordered *Bordered) Children() []Drawable { return []Drawable{bordered.content} } func (bordered *Bordered) Invalidate() { - bordered.DoInvalidate(bordered) + Invalidate() } func (bordered *Bordered) Draw(ctx *Context) { diff --git a/lib/ui/fill.go b/lib/ui/fill.go index 13ad286d..ff248905 100644 --- a/lib/ui/fill.go +++ b/lib/ui/fill.go @@ -21,10 +21,6 @@ func (f Fill) Draw(ctx *Context) { } } -func (f Fill) OnInvalidate(callback func(d Drawable)) { - // no-op -} - func (f Fill) Invalidate() { // no-op } diff --git a/lib/ui/grid.go b/lib/ui/grid.go index fe1d03e8..76eba000 100644 --- a/lib/ui/grid.go +++ b/lib/ui/grid.go @@ -1,21 +1,17 @@ package ui import ( - "fmt" "math" "sync" - "sync/atomic" "github.com/gdamore/tcell/v2" ) type Grid struct { - Invalidatable rows []GridSpec rowLayout []gridLayout columns []GridSpec columnLayout []gridLayout - invalid bool // Protected by mutex cells []*GridCell @@ -51,11 +47,10 @@ type GridCell struct { RowSpan int ColSpan int Content Drawable - invalid atomic.Value // bool } func NewGrid() *Grid { - return &Grid{invalid: true} + return &Grid{} } // MakeGrid creates a grid with the specified number of columns and rows. Each @@ -95,19 +90,12 @@ func (grid *Grid) Columns(spec []GridSpec) *Grid { } func (grid *Grid) Draw(ctx *Context) { - invalid := grid.invalid - if invalid { - grid.reflow(ctx) - } + grid.reflow(ctx) grid.mutex.RLock() defer grid.mutex.RUnlock() for _, cell := range grid.cells { - cellInvalid := cell.invalid.Load().(bool) - if !cellInvalid && !invalid { - continue - } rows := grid.rowLayout[cell.Row : cell.Row+cell.RowSpan] cols := grid.columnLayout[cell.Column : cell.Column+cell.ColSpan] x := cols[0].Offset @@ -142,16 +130,11 @@ func (grid *Grid) Draw(ctx *Context) { func (grid *Grid) MouseEvent(localX int, localY int, event tcell.Event) { if event, ok := event.(*tcell.EventMouse); ok { - invalid := grid.invalid grid.mutex.RLock() defer grid.mutex.RUnlock() for _, cell := range grid.cells { - cellInvalid := cell.invalid.Load().(bool) - if !cellInvalid && !invalid { - continue - } rows := grid.rowLayout[cell.Row : cell.Row+cell.RowSpan] cols := grid.columnLayout[cell.Column : cell.Column+cell.ColSpan] x := cols[0].Offset @@ -220,23 +203,10 @@ func (grid *Grid) reflow(ctx *Context) { } flow(&grid.rows, &grid.rowLayout, ctx.Height()) flow(&grid.columns, &grid.columnLayout, ctx.Width()) - grid.invalid = false -} - -func (grid *Grid) invalidateLayout() { - grid.invalid = true - grid.DoInvalidate(grid) } func (grid *Grid) Invalidate() { - grid.invalidateLayout() - grid.mutex.RLock() - for _, cell := range grid.cells { - if cell.Content != nil { - cell.Content.Invalidate() - } - } - grid.mutex.RUnlock() + Invalidate() } func (grid *Grid) AddChild(content Drawable) *GridCell { @@ -248,9 +218,7 @@ func (grid *Grid) AddChild(content Drawable) *GridCell { grid.mutex.Lock() grid.cells = append(grid.cells, cell) grid.mutex.Unlock() - cell.Content.OnInvalidate(grid.cellInvalidated) - cell.invalid.Store(true) - grid.invalidateLayout() + grid.Invalidate() return cell } @@ -263,24 +231,7 @@ func (grid *Grid) RemoveChild(content Drawable) { } } grid.mutex.Unlock() - grid.invalidateLayout() -} - -func (grid *Grid) cellInvalidated(drawable Drawable) { - var cell *GridCell - grid.mutex.RLock() - for _, cell = range grid.cells { - if cell.Content == drawable { - break - } - cell = nil - } - grid.mutex.RUnlock() - if cell == nil { - panic(fmt.Errorf("Attempted to invalidate unknown cell")) - } - cell.invalid.Store(true) - grid.DoInvalidate(grid) + grid.Invalidate() } func Const(i int) func() int { diff --git a/lib/ui/interfaces.go b/lib/ui/interfaces.go index 8a62ecf0..757f81c9 100644 --- a/lib/ui/interfaces.go +++ b/lib/ui/interfaces.go @@ -12,10 +12,7 @@ type AercMsg interface{} type Drawable interface { // Called when this renderable should draw itself. Draw(ctx *Context) - // Specifies a function to call when this cell needs to be redrawn. The - // callback may be called in any goroutine. - OnInvalidate(callback func(d Drawable)) - // Invalidates the drawable. This can be called from any goroutine. + // Invalidates the UI. This can be called from any goroutine. Invalidate() } diff --git a/lib/ui/invalidatable.go b/lib/ui/invalidatable.go deleted file mode 100644 index fc354bde..00000000 --- a/lib/ui/invalidatable.go +++ /dev/null @@ -1,17 +0,0 @@ -package ui - -import ( - "sync/atomic" -) - -type Invalidatable struct { - onInvalidate atomic.Value -} - -func (i *Invalidatable) OnInvalidate(f func(d Drawable)) { - i.onInvalidate.Store(f) -} - -func (i *Invalidatable) DoInvalidate(d Drawable) { - atomic.StoreInt32(&dirty, DIRTY) -} diff --git a/lib/ui/popover.go b/lib/ui/popover.go index 8c8c4cda..9cc491da 100644 --- a/lib/ui/popover.go +++ b/lib/ui/popover.go @@ -53,11 +53,5 @@ func (p *Popover) Focus(f bool) { } func (p *Popover) Invalidate() { - p.content.Invalidate() -} - -func (p *Popover) OnInvalidate(f func(Drawable)) { - p.content.OnInvalidate(func(_ Drawable) { - f(p) - }) + Invalidate() } diff --git a/lib/ui/stack.go b/lib/ui/stack.go index 5ccf13bc..c0aca4e4 100644 --- a/lib/ui/stack.go +++ b/lib/ui/stack.go @@ -9,9 +9,8 @@ import ( ) type Stack struct { - children []Drawable - onInvalidate []func(d Drawable) - uiConfig config.UIConfig + children []Drawable + uiConfig config.UIConfig } func NewStack(uiConfig config.UIConfig) *Stack { @@ -22,14 +21,8 @@ func (stack *Stack) Children() []Drawable { return stack.children } -func (stack *Stack) OnInvalidate(onInvalidate func(d Drawable)) { - stack.onInvalidate = append(stack.onInvalidate, onInvalidate) -} - func (stack *Stack) Invalidate() { - for _, fn := range stack.onInvalidate { - fn(stack) - } + Invalidate() } func (stack *Stack) Draw(ctx *Context) { @@ -50,11 +43,7 @@ func (stack *Stack) MouseEvent(localX int, localY int, event tcell.Event) { } func (stack *Stack) Push(d Drawable) { - if len(stack.children) != 0 { - stack.Peek().OnInvalidate(nil) - } stack.children = append(stack.children, d) - d.OnInvalidate(stack.invalidateFromChild) stack.Invalidate() } @@ -65,10 +54,6 @@ func (stack *Stack) Pop() Drawable { d := stack.children[len(stack.children)-1] stack.children = stack.children[:len(stack.children)-1] stack.Invalidate() - d.OnInvalidate(nil) - if len(stack.children) != 0 { - stack.Peek().OnInvalidate(stack.invalidateFromChild) - } return d } @@ -78,7 +63,3 @@ func (stack *Stack) Peek() Drawable { } return stack.children[len(stack.children)-1] } - -func (stack *Stack) invalidateFromChild(d Drawable) { - stack.Invalidate() -} diff --git a/lib/ui/tab.go b/lib/ui/tab.go index df76ccca..d4841efc 100644 --- a/lib/ui/tab.go +++ b/lib/ui/tab.go @@ -20,9 +20,6 @@ type Tabs struct { uiConfig *config.UIConfig - onInvalidateStrip func(d Drawable) //nolint:structcheck // used within this file - onInvalidateContent func(d Drawable) - parent *Tabs //nolint:structcheck // used within this file CloseTab func(index int) } @@ -59,7 +56,6 @@ func (tabs *Tabs) Add(content Drawable, name string, uiConf *config.UIConfig) *T } tabs.tabs = append(tabs.tabs, tab) tabs.selectPriv(len(tabs.tabs) - 1) - content.OnInvalidate(tabs.invalidateChild) return tab } @@ -73,18 +69,6 @@ func (tabs *Tabs) Names() []string { return names } -func (tabs *Tabs) invalidateChild(d Drawable) { - if tabs.curIndex >= len(tabs.tabs) { - return - } - - if tabs.tabs[tabs.curIndex].Content == d { - if tabs.onInvalidateContent != nil { - tabs.onInvalidateContent(tabs.TabContent) - } - } -} - func (tabs *Tabs) Remove(content Drawable) { tabs.m.Lock() defer tabs.m.Unlock() @@ -133,8 +117,7 @@ func (tabs *Tabs) Replace(contentSrc Drawable, contentTarget Drawable, name stri break } } - tabs.TabStrip.Invalidate() - contentTarget.OnInvalidate(tabs.invalidateChild) + Invalidate() } func (tabs *Tabs) Get(index int) *Tab { @@ -172,8 +155,7 @@ func (tabs *Tabs) selectPriv(index int) bool { tabs.pushHistory(tabs.curIndex) } tabs.curIndex = index - tabs.TabStrip.Invalidate() - tabs.TabContent.Invalidate() + Invalidate() } return true } @@ -246,7 +228,7 @@ func (tabs *Tabs) moveTabPriv(to int, relative bool) { tabs.tabs[to] = tab tabs.curIndex = to - tabs.TabStrip.Invalidate() + Invalidate() } func (tabs *Tabs) PinTab() { @@ -384,9 +366,7 @@ func (strip *TabStrip) Draw(ctx *Context) { } func (strip *TabStrip) Invalidate() { - if strip.onInvalidateStrip != nil { - strip.onInvalidateStrip(strip) - } + Invalidate() } func (strip *TabStrip) MouseEvent(localX int, localY int, event tcell.Event) { @@ -440,10 +420,6 @@ func (strip *TabStrip) MouseEvent(localX int, localY int, event tcell.Event) { } } -func (strip *TabStrip) OnInvalidate(onInvalidate func(d Drawable)) { - strip.onInvalidateStrip = onInvalidate -} - func (strip *TabStrip) clicked(mouseX int, mouseY int) (int, bool) { x := 0 for i, tab := range strip.tabs { @@ -490,13 +466,5 @@ func (content *TabContent) MouseEvent(localX int, localY int, event tcell.Event) } func (content *TabContent) Invalidate() { - if content.onInvalidateContent != nil { - content.onInvalidateContent(content) - } - tab := content.tabs[content.curIndex] - tab.Content.Invalidate() -} - -func (content *TabContent) OnInvalidate(onInvalidate func(d Drawable)) { - content.onInvalidateContent = onInvalidate + Invalidate() } diff --git a/lib/ui/text.go b/lib/ui/text.go index ffae1938..def61a0a 100644 --- a/lib/ui/text.go +++ b/lib/ui/text.go @@ -12,7 +12,6 @@ const ( ) type Text struct { - Invalidatable text string strategy uint style tcell.Style @@ -51,5 +50,5 @@ func (t *Text) Draw(ctx *Context) { } func (t *Text) Invalidate() { - t.DoInvalidate(t) + Invalidate() } diff --git a/lib/ui/textinput.go b/lib/ui/textinput.go index d99871ce..2a8b7c73 100644 --- a/lib/ui/textinput.go +++ b/lib/ui/textinput.go @@ -17,7 +17,6 @@ import ( // TODO: scrolling type TextInput struct { - Invalidatable sync.Mutex cells int ctx *Context @@ -90,7 +89,7 @@ func (ti *TextInput) Set(value string) *TextInput { } func (ti *TextInput) Invalidate() { - ti.DoInvalidate(ti) + Invalidate() } func (ti *TextInput) Draw(ctx *Context) { @@ -530,5 +529,3 @@ func findStem(words []string) string { func (c *completions) Focus(_ bool) {} func (c *completions) Invalidate() {} - -func (c *completions) OnInvalidate(_ func(Drawable)) {} diff --git a/lib/ui/ui.go b/lib/ui/ui.go index 073684cc..313f90d8 100644 --- a/lib/ui/ui.go +++ b/lib/ui/ui.go @@ -122,7 +122,7 @@ func (state *UI) HandleEvent(event tcell.Event) { state.screen.Clear() width, height := event.Size() state.ctx = NewContext(width, height, state.screen, state.onPopover) - state.Content.Invalidate() + Invalidate() } // if we have a popover, and it can handle the event, it does so if state.popover == nil || !state.popover.Event(event) { |