Refactor widget attributes to interface methods

This commit is contained in:
Kevin Cotugno 2017-12-27 16:03:14 -08:00
parent 3a5c620893
commit c5339a9ae4
4 changed files with 63 additions and 27 deletions

View File

@ -10,9 +10,9 @@ type ListEntry interface {
}
type ListWidget struct {
Constrs Constraints
Attrs Attributes
Size Size
constraints Constraints
attributes Attributes
size Size
cellLock sync.Mutex
cells [][]Cell
@ -28,8 +28,29 @@ type ListWidget struct {
lastSize Size
}
func (l *ListWidget) SetSize(size Size) {
l.Size = size
func (l ListWidget) Attributes() Attributes {
return l.attributes
}
func (l *ListWidget) SetAttributes(a Attributes) {
l.attributes = a
l.recalculateCells()
}
func (l ListWidget) Constraints() Constraints {
return l.constraints
}
func (l *ListWidget) SetConstraints(c Constraints) {
l.constraints = c
}
func (l ListWidget) Size() Size {
return l.size
}
func (l *ListWidget) SetSize(s Size) {
l.size = s
}
func (l *ListWidget) Render() [][]Cell {
@ -79,10 +100,6 @@ func (l *ListWidget) Render() [][]Cell {
return append([][]Cell(nil), l.cells...)
}
func (l ListWidget) Constraints() Constraints {
return l.Constrs
}
func (l *ListWidget) AddEntry(entry ListEntry) {
if l.listBuf == nil {
l.listBuf = make([][]Cell, 1)
@ -148,15 +165,15 @@ func (l *ListWidget) recalculateCells() {
border = 1
top := make([]Cell, longest+2)
top[0] = Cell{Value: '┏', Attrs: l.Attrs}
top[longest+1] = Cell{Value: '┓', Attrs: l.Attrs}
top[0] = Cell{Value: '┏', Attrs: l.Attributes()}
top[longest+1] = Cell{Value: '┓', Attrs: l.Attributes()}
for i := 1; i <= longest; i++ {
top[i] = Cell{Value: '━', Attrs: l.Attrs}
top[i] = Cell{Value: '━', Attrs: l.Attributes()}
}
bottom := append([]Cell(nil), top...)
bottom[0] = Cell{Value: '┗', Attrs: l.Attrs}
bottom[longest+1] = Cell{Value: '┛', Attrs: l.Attrs}
bottom[0] = Cell{Value: '┗', Attrs: l.Attributes()}
bottom[longest+1] = Cell{Value: '┛', Attrs: l.Attributes()}
cells = append([][]Cell{top}, cells...)
cells = append(cells, bottom)
@ -173,7 +190,7 @@ func (l *ListWidget) recalculateCells() {
}
if l.border {
c := Cell{Value: '┃', Attrs: l.Attrs}
c := Cell{Value: '┃', Attrs: l.Attributes()}
cells[i+border][0] = c
cells[i+border][longest+1] = c
}

View File

@ -21,5 +21,9 @@ type Size struct {
type Widget interface {
Render() [][]Cell
Constraints() Constraints
SetConstraints(Constraints)
Size() Size
SetSize(size Size)
Attributes() Attributes
SetAttributes(attrs Attributes)
}

View File

@ -1,11 +1,11 @@
package exhibit
type WindowWidget struct {
Constrs Constraints
Size Size
Border Border
widgets []Widget
attributes Attributes
constraints Constraints
size Size
border Border
widgets []Widget
}
func (w *WindowWidget) AddWidget(widget Widget) {
@ -16,12 +16,28 @@ func (w *WindowWidget) AddWidget(widget Widget) {
w.widgets = append(w.widgets, widget)
}
func (w *WindowWidget) SetSize(size Size) {
w.Size = size
func (w WindowWidget) Attributes() Attributes {
return w.attributes
}
func (w *WindowWidget) Constraints() Constraints {
return w.Constrs
func (w *WindowWidget) SetAttributes(a Attributes) {
w.attributes = a
}
func (w WindowWidget) Constraints() Constraints {
return w.constraints
}
func (w *WindowWidget) SetConstraints(c Constraints) {
w.constraints = c
}
func (w WindowWidget) Size() Size {
return w.size
}
func (w *WindowWidget) SetSize(s Size) {
w.size = s
}
func (w *WindowWidget) Render() [][]Cell {

View File

@ -78,15 +78,14 @@ func main() {
window = &exhibit.WindowWidget{}
topAsks = &exhibit.ListWidget{}
window.Constrs.Bottom = true
topAsks.SetBorder(true)
topAsks.SetRightAlign(true)
topAsks.Attrs.ForegroundColor = exhibit.FGYellow
topAsks.SetAttributes(exhibit.Attributes{ForegroundColor: exhibit.FGYellow})
topBids = &exhibit.ListWidget{}
topBids.SetBorder(true)
topBids.SetRightAlign(true)
topBids.Attrs.ForegroundColor = exhibit.FGGreen
topBids.SetAttributes(exhibit.Attributes{ForegroundColor: exhibit.FGGreen})
midPrice = &exhibit.ListWidget{}
midPrice.SetRightAlign(true)