From e9f5b6687e0a3ed53c692b4e3ea30cc7dcbe36c9 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Wed, 4 May 2022 11:13:18 +0200 Subject: [PATCH] ConstrainView and GrowView improved --- tests/screen_test.go | 42 +++++++++++++++++++++++++---------------- views/layout_grid.go | 3 +++ views/view_constrain.go | 7 ++++--- views/view_grow.go | 10 ++++++---- 4 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 views/layout_grid.go diff --git a/tests/screen_test.go b/tests/screen_test.go index 302932a..8bcb738 100644 --- a/tests/screen_test.go +++ b/tests/screen_test.go @@ -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)) diff --git a/views/layout_grid.go b/views/layout_grid.go new file mode 100644 index 0000000..45abf24 --- /dev/null +++ b/views/layout_grid.go @@ -0,0 +1,3 @@ +package views + +// TODO diff --git a/views/view_constrain.go b/views/view_constrain.go index 3bb8e6d..a647869 100644 --- a/views/view_constrain.go +++ b/views/view_constrain.go @@ -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) } diff --git a/views/view_grow.go b/views/view_grow.go index 27bdeda..1ed9d3d 100644 --- a/views/view_grow.go +++ b/views/view_grow.go @@ -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) {