ConstrainView and GrowView improved

This commit is contained in:
Timon Ringwald 2022-05-04 11:13:18 +02:00
parent c3ba6e36f2
commit e9f5b6687e
4 changed files with 39 additions and 23 deletions

View File

@ -98,6 +98,22 @@ func TestBorderView(t *testing.T) {
}
}
func TestGrowView(t *testing.T) {
textView := views.NewTextView("hello world")
textView.SetStyle(textView.Style().Background(tcell.ColorYellow).Foreground(tcell.ColorBlack))
growView := views.NewGrowView(textView)
screen, err := tui.NewScreen(growView)
if err != nil {
t.Error(err)
return
}
if err := screen.Start(); err != nil {
fmt.Println(err)
}
}
func TestFlowLayout(t *testing.T) {
textView := views.NewTextView("hello world!")
textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack))
@ -109,18 +125,17 @@ func TestFlowLayout(t *testing.T) {
textView2 := views.NewTextView("Hi!")
textView2.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow))
growView := views.NewGrowView()
growView := views.NewGrowView(nil)
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
growView2 := views.NewGrowView()
growView2 := views.NewGrowView(nil)
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
flowLayout := views.NewFlowLayout(tui.Vertical)
flowLayout.AppendViews(marginView, growView, textView2)
constrainView := views.NewConstrainView(flowLayout)
constrainView := views.NewConstrainView(flowLayout, -1, -1)
constrainView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
constrainView.Constrain(-1, -1)
screen, err := tui.NewScreen(constrainView)
if err != nil {
@ -151,10 +166,10 @@ func TestSeparatorLayout(t *testing.T) {
textView2 := views.NewTextView("Hi!")
textView2.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue).Foreground(tcell.ColorYellow))
growView := views.NewGrowView()
growView := views.NewGrowView(nil)
growView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
growView2 := views.NewGrowView()
growView2 := views.NewGrowView(nil)
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
separatorLayout := views.NewSeparatorLayout(tui.Vertical)
@ -183,25 +198,20 @@ func TestSeparatorLayout(t *testing.T) {
}
func TestBorderLayout(t *testing.T) {
topView := views.NewConstrainView(nil)
topView := views.NewConstrainView(nil, 10, 10)
topView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue))
topView.Constrain(10, 10)
bottomView := views.NewConstrainView(nil)
bottomView := views.NewConstrainView(nil, 10, 10)
bottomView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed))
bottomView.Constrain(10, 10)
leftView := views.NewConstrainView(nil)
leftView := views.NewConstrainView(nil, 10, 10)
leftView.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
leftView.Constrain(10, 10)
rightView := views.NewConstrainView(nil)
rightView := views.NewConstrainView(nil, 10, 10)
rightView.SetStyle(tui.StyleDefault.Background(tcell.ColorGreen))
rightView.Constrain(10, 10)
centerView := views.NewConstrainView(nil)
centerView := views.NewConstrainView(nil, 10, 10)
centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
centerView.Constrain(10, 10)
borderLayout := views.NewBorderLayout()
borderLayout.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))

3
views/layout_grid.go Normal file
View File

@ -0,0 +1,3 @@
package views
// TODO

View File

@ -13,10 +13,10 @@ type ConstrainView struct {
var _ tui.Wrapper = &ConstrainView{}
func NewConstrainView(view tui.View) *ConstrainView {
func NewConstrainView(view tui.View, maxWidth, maxHeight int) *ConstrainView {
v := new(ConstrainView)
v.SetView(view)
v.Constrain(-1, -1)
v.Constrain(maxWidth, maxHeight)
return v
}
@ -25,5 +25,6 @@ func (v *ConstrainView) Constrain(maxWidth, maxHeight int) {
}
func (v *ConstrainView) Layout() (prefWidth, prefHeight int) {
return v.MaxWidth, v.MaxHeight
vw, vh := v.View().Layout()
return min(vw, v.MaxWidth), min(vh, v.MaxHeight)
}

View File

@ -4,15 +4,17 @@ import (
"git.tordarus.net/Tordarus/tui"
)
// GrowView is a tui.View which always demands all available space
// GrowView is a tui.Wrapper which always demands all available space
type GrowView struct {
tui.ViewTmpl
tui.WrapperTmpl
}
var _ tui.View = &GrowView{}
func NewGrowView() *GrowView {
return &GrowView{}
func NewGrowView(view tui.View) *GrowView {
g := &GrowView{}
g.SetView(view)
return g
}
func (v *GrowView) Layout() (prefWidth, prefHeight int) {