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>
This commit is contained in:
Tim Culverhouse 2022-10-07 11:00:31 -05:00 committed by Robin Jarry
parent 34014d3cee
commit ba24e92062
41 changed files with 76 additions and 303 deletions

View file

@ -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) {

View file

@ -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
}

View file

@ -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 {

View file

@ -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()
}

View file

@ -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)
}

View file

@ -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()
}

View file

@ -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()
}

View file

@ -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()
}

View file

@ -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()
}

View file

@ -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)) {}

View file

@ -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) {