diff --git a/tests/screen_test.go b/tests/screen_test.go index a67726a..9f0adc7 100644 --- a/tests/screen_test.go +++ b/tests/screen_test.go @@ -10,7 +10,7 @@ import ( "github.com/gdamore/tcell" ) -func TestFlowGroup(t *testing.T) { +func TestFlowLayout(t *testing.T) { textView := views.NewTextView("hello world!") textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack)) @@ -28,10 +28,10 @@ func TestFlowGroup(t *testing.T) { growView2 := views.NewGrowView() growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow)) - flowGroup := views.NewFlowGroup(tui.Vertical) - flowGroup.AppendViews(marginView, growView, textView2) + flowLayout := views.NewFlowLayout(tui.Vertical) + flowLayout.AppendViews(marginView, growView, textView2) - constrainView := views.NewConstrainView(flowGroup) + constrainView := views.NewConstrainView(flowLayout) constrainView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple)) constrainView.Constrain(-1, -1) @@ -55,7 +55,7 @@ func TestFlowGroup(t *testing.T) { fmt.Println(err) } -func TestSeparatorGroup(t *testing.T) { +func TestSeparatorLayout(t *testing.T) { textView := views.NewTextView("hello world!") textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack)) @@ -70,12 +70,12 @@ func TestSeparatorGroup(t *testing.T) { growView2 := views.NewGrowView() growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow)) - separatorGroup := views.NewSeparatorGroup(tui.Vertical) - separatorGroup.AppendView(frameView, 1) - separatorGroup.AppendView(growView, 1) - separatorGroup.AppendView(textView2, 1) + separatorLayout := views.NewSeparatorLayout(tui.Vertical) + separatorLayout.AppendView(frameView, 1) + separatorLayout.AppendView(growView, 1) + separatorLayout.AppendView(textView2, 1) - screen, err := tui.NewScreen(separatorGroup) + screen, err := tui.NewScreen(separatorLayout) if err != nil { t.Error(err) return @@ -95,7 +95,7 @@ func TestSeparatorGroup(t *testing.T) { fmt.Println(err) } -func TestBorderGroup(t *testing.T) { +func TestBorderLayout(t *testing.T) { topView := views.NewConstrainView(nil) topView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue)) topView.Constrain(10, 10) @@ -116,15 +116,15 @@ func TestBorderGroup(t *testing.T) { centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple)) centerView.Constrain(10, 10) - borderGroup := views.NewBorderGroup() - borderGroup.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple)) - borderGroup.SetView(topView, views.Top) - borderGroup.SetView(bottomView, views.Bottom) - borderGroup.SetView(leftView, views.Left) - borderGroup.SetView(rightView, views.Right) - borderGroup.SetView(centerView, views.Center) + borderLayout := views.NewBorderLayout() + borderLayout.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple)) + borderLayout.SetView(topView, views.Top) + borderLayout.SetView(bottomView, views.Bottom) + borderLayout.SetView(leftView, views.Left) + borderLayout.SetView(rightView, views.Right) + borderLayout.SetView(centerView, views.Center) - screen, err := tui.NewScreen(borderGroup) + screen, err := tui.NewScreen(borderLayout) if err != nil { t.Error(err) return diff --git a/view.go b/view.go index 1f2ad6b..7305d65 100644 --- a/view.go +++ b/view.go @@ -21,19 +21,19 @@ type View interface { Draw(buf *ViewBuffer) } -// Group defines the behavior of a View which can hold multiple sub views -type Group interface { +// Layout defines the behavior of a View which can hold multiple sub views +type Layout interface { View Views() []View } -// Wrapper defines the behavior of a GroupView which can hold exactly one sub view. +// Wrapper defines the behavior of a Layout which can hold exactly one sub view. // To define custom Wrappers, it is recommended to add WrapperTmpl // as the promoted anonymous field for your custom Wrapper struct. // It implements the Wrapper interface with useful default behavior type Wrapper interface { - Group + Layout SetView(View) View() View diff --git a/views/bordergroup.go b/views/layout_border.go similarity index 85% rename from views/bordergroup.go rename to views/layout_border.go index 0150a33..b99e85d 100644 --- a/views/bordergroup.go +++ b/views/layout_border.go @@ -4,23 +4,23 @@ import ( "git.tordarus.net/Tordarus/tui" ) -// BorderGroup ia a tui.Group which places its children onto a given tui.Side -type BorderGroup struct { +// BorderLayout ia a tui.Layout which places its children onto a given tui.Side +type BorderLayout struct { tui.ViewTmpl views map[Slot]tui.View horizontalLayout *LayoutResult verticalLayout *LayoutResult } -var _ tui.Group = &BorderGroup{} +var _ tui.Layout = &BorderLayout{} -func NewBorderGroup() *BorderGroup { - return &BorderGroup{ +func NewBorderLayout() *BorderLayout { + return &BorderLayout{ views: map[Slot]tui.View{}, } } -func (g *BorderGroup) Views() []tui.View { +func (g *BorderLayout) Views() []tui.View { s := make([]tui.View, 0, len(g.views)) for _, view := range g.views { s = append(s, view) @@ -28,15 +28,15 @@ func (g *BorderGroup) Views() []tui.View { return s } -func (g *BorderGroup) SetView(v tui.View, slot Slot) { +func (g *BorderLayout) SetView(v tui.View, slot Slot) { g.views[slot] = v } -func (g *BorderGroup) View(slot Slot) tui.View { +func (g *BorderLayout) View(slot Slot) tui.View { return g.views[slot] } -func (g *BorderGroup) Draw(buf *tui.ViewBuffer) { +func (g *BorderLayout) Draw(buf *tui.ViewBuffer) { g.ViewTmpl.Draw(buf) if g.verticalLayout == nil { @@ -123,13 +123,13 @@ func (g *BorderGroup) Draw(buf *tui.ViewBuffer) { g.horizontalLayout = nil } -func (g *BorderGroup) Layout() (prefWidth, prefHeight int) { +func (g *BorderLayout) Layout() (prefWidth, prefHeight int) { g.verticalLayout = CalculateLayoutResult([]tui.View{g.View(Top), g.View(Center), g.View(Bottom)}) g.horizontalLayout = CalculateLayoutResult([]tui.View{g.View(Left), g.View(Center), g.View(Right)}) return -1, -1 } -func (g *BorderGroup) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { +func (g *BorderLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { for _, view := range g.Views() { if view.OnKeyPressed(event) { return true diff --git a/views/coordgroup.go b/views/layout_coord.go similarity index 58% rename from views/coordgroup.go rename to views/layout_coord.go index 2fdc5dd..181654e 100644 --- a/views/coordgroup.go +++ b/views/layout_coord.go @@ -2,21 +2,21 @@ package views import "git.tordarus.net/Tordarus/tui" -// CoordGroup is a tui.Group which places its children on predefined coordinates -type CoordGroup struct { +// CoordLayout is a tui.Layout which places its children on predefined coordinates +type CoordLayout struct { tui.ViewTmpl views map[tui.View]tui.Dimension } -var _ tui.Group = &CoordGroup{} +var _ tui.Layout = &CoordLayout{} -func NewCoordGroup() *CoordGroup { - return &CoordGroup{ +func NewCoordLayout() *CoordLayout { + return &CoordLayout{ views: map[tui.View]tui.Dimension{}, } } -func (g *CoordGroup) Views() []tui.View { +func (g *CoordLayout) Views() []tui.View { s := make([]tui.View, 0, len(g.views)) for v := range g.views { s = append(s, v) @@ -26,20 +26,20 @@ func (g *CoordGroup) Views() []tui.View { // SetView places v at the given coordinates with the given dimensions. // v will be added to g's children if not already added before -func (g *CoordGroup) SetView(v tui.View, x, y, width, height int) { +func (g *CoordLayout) SetView(v tui.View, x, y, width, height int) { g.views[v] = tui.Dimension{Point: tui.Point{X: x, Y: y}, Size: tui.Size{Width: width, Height: height}} } -func (g *CoordGroup) Draw(buf *tui.ViewBuffer) { +func (g *CoordLayout) Draw(buf *tui.ViewBuffer) { for v, d := range g.views { v.Draw(buf.Sub(d.X, d.Y, d.Width, d.Height)) } } -func (v *CoordGroup) Layout() (prefWidth, prefHeight int) { +func (v *CoordLayout) Layout() (prefWidth, prefHeight int) { return -1, -1 } -func (g *CoordGroup) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { +func (g *CoordLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { for _, view := range g.Views() { if view.OnKeyPressed(event) { return true diff --git a/views/flowgroup.go b/views/layout_flow.go similarity index 79% rename from views/flowgroup.go rename to views/layout_flow.go index d255499..a3c9557 100644 --- a/views/flowgroup.go +++ b/views/layout_flow.go @@ -4,8 +4,8 @@ import ( "git.tordarus.net/Tordarus/tui" ) -// FlowGroup ia a tui.Group which places its children in a linear layout -type FlowGroup struct { +// FlowLayout ia a tui.Layout which places its children in a linear layout +type FlowLayout struct { tui.ViewTmpl views []tui.View lastLayoutPhase *LayoutResult @@ -14,32 +14,32 @@ type FlowGroup struct { Orientation tui.Orientation } -var _ tui.Group = &FlowGroup{} +var _ tui.Layout = &FlowLayout{} -func NewFlowGroup(orientation tui.Orientation) *FlowGroup { - return &FlowGroup{ +func NewFlowLayout(orientation tui.Orientation) *FlowLayout { + return &FlowLayout{ views: make([]tui.View, 0), Orientation: orientation, } } -func (g *FlowGroup) Views() []tui.View { +func (g *FlowLayout) Views() []tui.View { return g.views[:] } -func (g *FlowGroup) AppendViews(v ...tui.View) { +func (g *FlowLayout) AppendViews(v ...tui.View) { g.views = append(g.views, v...) } -func (g *FlowGroup) PrependViews(v ...tui.View) { +func (g *FlowLayout) PrependViews(v ...tui.View) { g.views = append(v, g.views...) } -func (g *FlowGroup) InsertView(v tui.View, index int) { +func (g *FlowLayout) InsertView(v tui.View, index int) { g.views = append(g.views[:index], append([]tui.View{v}, g.views[index:]...)...) } -func (g *FlowGroup) RemoveView(v tui.View) { +func (g *FlowLayout) RemoveView(v tui.View) { for index, view := range g.Views() { if view == v { g.views = append(g.views[:index], g.views[index:]...) @@ -48,7 +48,7 @@ func (g *FlowGroup) RemoveView(v tui.View) { } } -func (g *FlowGroup) Draw(buf *tui.ViewBuffer) { +func (g *FlowLayout) Draw(buf *tui.ViewBuffer) { g.ViewTmpl.Draw(buf) if g.lastLayoutPhase == nil { @@ -97,7 +97,7 @@ func (g *FlowGroup) Draw(buf *tui.ViewBuffer) { g.lastLayoutPhase = nil } -func (g *FlowGroup) Layout() (prefWidth, prefHeight int) { +func (g *FlowLayout) Layout() (prefWidth, prefHeight int) { layout := CalculateLayoutResult(g.Views()) g.lastLayoutPhase = layout @@ -113,7 +113,7 @@ func (g *FlowGroup) Layout() (prefWidth, prefHeight int) { return } -func (g *FlowGroup) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { +func (g *FlowLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { for _, view := range g.Views() { if view.OnKeyPressed(event) { return true diff --git a/views/separatorgroup.go b/views/layout_separator.go similarity index 67% rename from views/separatorgroup.go rename to views/layout_separator.go index 0ad0374..2110d25 100644 --- a/views/separatorgroup.go +++ b/views/layout_separator.go @@ -4,8 +4,8 @@ import ( "git.tordarus.net/Tordarus/tui" ) -// SeperatorGroup ia a tui.Group which separates -type SeperatorGroup struct { +// SeperatorLayout ia a tui.Layout which separates +type SeperatorLayout struct { tui.ViewTmpl views []tui.View @@ -15,39 +15,39 @@ type SeperatorGroup struct { Orientation tui.Orientation } -var _ tui.Group = &SeperatorGroup{} +var _ tui.Layout = &SeperatorLayout{} -func NewSeparatorGroup(orientation tui.Orientation) *SeperatorGroup { - return &SeperatorGroup{ +func NewSeparatorLayout(orientation tui.Orientation) *SeperatorLayout { + return &SeperatorLayout{ views: make([]tui.View, 0), gravity: map[tui.View]int{}, Orientation: orientation, } } -func (g *SeperatorGroup) Views() []tui.View { +func (g *SeperatorLayout) Views() []tui.View { return g.views[:] } -func (g *SeperatorGroup) AppendView(v tui.View, gravity int) { +func (g *SeperatorLayout) AppendView(v tui.View, gravity int) { g.views = append(g.views, v) g.gravitySum += gravity g.gravity[v] = gravity } -func (g *SeperatorGroup) PrependView(v tui.View, gravity int) { +func (g *SeperatorLayout) PrependView(v tui.View, gravity int) { g.views = append([]tui.View{v}, g.views...) g.gravitySum += gravity g.gravity[v] = gravity } -func (g *SeperatorGroup) InsertView(v tui.View, index int, gravity int) { +func (g *SeperatorLayout) InsertView(v tui.View, index int, gravity int) { g.views = append(g.views[:index], append([]tui.View{v}, g.views[index:]...)...) g.gravitySum += gravity g.gravity[v] = gravity } -func (g *SeperatorGroup) SetGravity(v tui.View, gravity int) { +func (g *SeperatorLayout) SetGravity(v tui.View, gravity int) { for _, view := range g.Views() { if view == v { g.gravitySum += gravity - g.gravity[v] @@ -57,7 +57,7 @@ func (g *SeperatorGroup) SetGravity(v tui.View, gravity int) { } } -func (g *SeperatorGroup) RemoveView(v tui.View) { +func (g *SeperatorLayout) RemoveView(v tui.View) { for index, view := range g.Views() { if view == v { g.views = append(g.views[:index], g.views[index:]...) @@ -68,11 +68,11 @@ func (g *SeperatorGroup) RemoveView(v tui.View) { } } -func (g *SeperatorGroup) View(slot Slot) tui.View { +func (g *SeperatorLayout) View(slot Slot) tui.View { return g.views[slot] } -func (g *SeperatorGroup) Draw(buf *tui.ViewBuffer) { +func (g *SeperatorLayout) Draw(buf *tui.ViewBuffer) { g.ViewTmpl.Draw(buf) if g.Orientation == tui.Horizontal { @@ -97,11 +97,11 @@ func (g *SeperatorGroup) Draw(buf *tui.ViewBuffer) { } -func (g *SeperatorGroup) Layout() (prefWidth, prefHeight int) { +func (g *SeperatorLayout) Layout() (prefWidth, prefHeight int) { return -1, -1 } -func (g *SeperatorGroup) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { +func (g *SeperatorLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { for _, view := range g.Views() { if view.OnKeyPressed(event) { return true