renamed group in layout

This commit is contained in:
Timon Ringwald 2022-04-02 15:21:17 +02:00
parent b797dc0b2c
commit 841e22e8de
6 changed files with 72 additions and 72 deletions

View File

@ -10,7 +10,7 @@ import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
) )
func TestFlowGroup(t *testing.T) { func TestFlowLayout(t *testing.T) {
textView := views.NewTextView("hello world!") textView := views.NewTextView("hello world!")
textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack)) textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack))
@ -28,10 +28,10 @@ func TestFlowGroup(t *testing.T) {
growView2 := views.NewGrowView() growView2 := views.NewGrowView()
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow)) growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
flowGroup := views.NewFlowGroup(tui.Vertical) flowLayout := views.NewFlowLayout(tui.Vertical)
flowGroup.AppendViews(marginView, growView, textView2) flowLayout.AppendViews(marginView, growView, textView2)
constrainView := views.NewConstrainView(flowGroup) constrainView := views.NewConstrainView(flowLayout)
constrainView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple)) constrainView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
constrainView.Constrain(-1, -1) constrainView.Constrain(-1, -1)
@ -55,7 +55,7 @@ func TestFlowGroup(t *testing.T) {
fmt.Println(err) fmt.Println(err)
} }
func TestSeparatorGroup(t *testing.T) { func TestSeparatorLayout(t *testing.T) {
textView := views.NewTextView("hello world!") textView := views.NewTextView("hello world!")
textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack)) textView.SetStyle(tui.StyleDefault.Background(tcell.ColorRed).Foreground(tcell.ColorBlack))
@ -70,12 +70,12 @@ func TestSeparatorGroup(t *testing.T) {
growView2 := views.NewGrowView() growView2 := views.NewGrowView()
growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow)) growView2.SetStyle(tui.StyleDefault.Background(tcell.ColorYellow))
separatorGroup := views.NewSeparatorGroup(tui.Vertical) separatorLayout := views.NewSeparatorLayout(tui.Vertical)
separatorGroup.AppendView(frameView, 1) separatorLayout.AppendView(frameView, 1)
separatorGroup.AppendView(growView, 1) separatorLayout.AppendView(growView, 1)
separatorGroup.AppendView(textView2, 1) separatorLayout.AppendView(textView2, 1)
screen, err := tui.NewScreen(separatorGroup) screen, err := tui.NewScreen(separatorLayout)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return
@ -95,7 +95,7 @@ func TestSeparatorGroup(t *testing.T) {
fmt.Println(err) fmt.Println(err)
} }
func TestBorderGroup(t *testing.T) { func TestBorderLayout(t *testing.T) {
topView := views.NewConstrainView(nil) topView := views.NewConstrainView(nil)
topView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue)) topView.SetStyle(tui.StyleDefault.Background(tcell.ColorBlue))
topView.Constrain(10, 10) topView.Constrain(10, 10)
@ -116,15 +116,15 @@ func TestBorderGroup(t *testing.T) {
centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple)) centerView.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
centerView.Constrain(10, 10) centerView.Constrain(10, 10)
borderGroup := views.NewBorderGroup() borderLayout := views.NewBorderLayout()
borderGroup.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple)) borderLayout.SetStyle(tui.StyleDefault.Background(tcell.ColorPurple))
borderGroup.SetView(topView, views.Top) borderLayout.SetView(topView, views.Top)
borderGroup.SetView(bottomView, views.Bottom) borderLayout.SetView(bottomView, views.Bottom)
borderGroup.SetView(leftView, views.Left) borderLayout.SetView(leftView, views.Left)
borderGroup.SetView(rightView, views.Right) borderLayout.SetView(rightView, views.Right)
borderGroup.SetView(centerView, views.Center) borderLayout.SetView(centerView, views.Center)
screen, err := tui.NewScreen(borderGroup) screen, err := tui.NewScreen(borderLayout)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
return return

View File

@ -21,19 +21,19 @@ type View interface {
Draw(buf *ViewBuffer) Draw(buf *ViewBuffer)
} }
// Group defines the behavior of a View which can hold multiple sub views // Layout defines the behavior of a View which can hold multiple sub views
type Group interface { type Layout interface {
View View
Views() []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 // To define custom Wrappers, it is recommended to add WrapperTmpl
// as the promoted anonymous field for your custom Wrapper struct. // as the promoted anonymous field for your custom Wrapper struct.
// It implements the Wrapper interface with useful default behavior // It implements the Wrapper interface with useful default behavior
type Wrapper interface { type Wrapper interface {
Group Layout
SetView(View) SetView(View)
View() View View() View

View File

@ -4,23 +4,23 @@ import (
"git.tordarus.net/Tordarus/tui" "git.tordarus.net/Tordarus/tui"
) )
// BorderGroup ia a tui.Group which places its children onto a given tui.Side // BorderLayout ia a tui.Layout which places its children onto a given tui.Side
type BorderGroup struct { type BorderLayout struct {
tui.ViewTmpl tui.ViewTmpl
views map[Slot]tui.View views map[Slot]tui.View
horizontalLayout *LayoutResult horizontalLayout *LayoutResult
verticalLayout *LayoutResult verticalLayout *LayoutResult
} }
var _ tui.Group = &BorderGroup{} var _ tui.Layout = &BorderLayout{}
func NewBorderGroup() *BorderGroup { func NewBorderLayout() *BorderLayout {
return &BorderGroup{ return &BorderLayout{
views: map[Slot]tui.View{}, 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)) s := make([]tui.View, 0, len(g.views))
for _, view := range g.views { for _, view := range g.views {
s = append(s, view) s = append(s, view)
@ -28,15 +28,15 @@ func (g *BorderGroup) Views() []tui.View {
return s return s
} }
func (g *BorderGroup) SetView(v tui.View, slot Slot) { func (g *BorderLayout) SetView(v tui.View, slot Slot) {
g.views[slot] = v g.views[slot] = v
} }
func (g *BorderGroup) View(slot Slot) tui.View { func (g *BorderLayout) View(slot Slot) tui.View {
return g.views[slot] return g.views[slot]
} }
func (g *BorderGroup) Draw(buf *tui.ViewBuffer) { func (g *BorderLayout) Draw(buf *tui.ViewBuffer) {
g.ViewTmpl.Draw(buf) g.ViewTmpl.Draw(buf)
if g.verticalLayout == nil { if g.verticalLayout == nil {
@ -123,13 +123,13 @@ func (g *BorderGroup) Draw(buf *tui.ViewBuffer) {
g.horizontalLayout = nil 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.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)}) g.horizontalLayout = CalculateLayoutResult([]tui.View{g.View(Left), g.View(Center), g.View(Right)})
return -1, -1 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() { for _, view := range g.Views() {
if view.OnKeyPressed(event) { if view.OnKeyPressed(event) {
return true return true

View File

@ -2,21 +2,21 @@ package views
import "git.tordarus.net/Tordarus/tui" import "git.tordarus.net/Tordarus/tui"
// CoordGroup is a tui.Group which places its children on predefined coordinates // CoordLayout is a tui.Layout which places its children on predefined coordinates
type CoordGroup struct { type CoordLayout struct {
tui.ViewTmpl tui.ViewTmpl
views map[tui.View]tui.Dimension views map[tui.View]tui.Dimension
} }
var _ tui.Group = &CoordGroup{} var _ tui.Layout = &CoordLayout{}
func NewCoordGroup() *CoordGroup { func NewCoordLayout() *CoordLayout {
return &CoordGroup{ return &CoordLayout{
views: map[tui.View]tui.Dimension{}, 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)) s := make([]tui.View, 0, len(g.views))
for v := range g.views { for v := range g.views {
s = append(s, v) 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. // SetView places v at the given coordinates with the given dimensions.
// v will be added to g's children if not already added before // 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}} 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 { for v, d := range g.views {
v.Draw(buf.Sub(d.X, d.Y, d.Width, d.Height)) 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 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() { for _, view := range g.Views() {
if view.OnKeyPressed(event) { if view.OnKeyPressed(event) {
return true return true

View File

@ -4,8 +4,8 @@ import (
"git.tordarus.net/Tordarus/tui" "git.tordarus.net/Tordarus/tui"
) )
// FlowGroup ia a tui.Group which places its children in a linear layout // FlowLayout ia a tui.Layout which places its children in a linear layout
type FlowGroup struct { type FlowLayout struct {
tui.ViewTmpl tui.ViewTmpl
views []tui.View views []tui.View
lastLayoutPhase *LayoutResult lastLayoutPhase *LayoutResult
@ -14,32 +14,32 @@ type FlowGroup struct {
Orientation tui.Orientation Orientation tui.Orientation
} }
var _ tui.Group = &FlowGroup{} var _ tui.Layout = &FlowLayout{}
func NewFlowGroup(orientation tui.Orientation) *FlowGroup { func NewFlowLayout(orientation tui.Orientation) *FlowLayout {
return &FlowGroup{ return &FlowLayout{
views: make([]tui.View, 0), views: make([]tui.View, 0),
Orientation: orientation, Orientation: orientation,
} }
} }
func (g *FlowGroup) Views() []tui.View { func (g *FlowLayout) Views() []tui.View {
return g.views[:] return g.views[:]
} }
func (g *FlowGroup) AppendViews(v ...tui.View) { func (g *FlowLayout) AppendViews(v ...tui.View) {
g.views = append(g.views, v...) 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...) 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:]...)...) 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() { for index, view := range g.Views() {
if view == v { if view == v {
g.views = append(g.views[:index], g.views[index:]...) 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) g.ViewTmpl.Draw(buf)
if g.lastLayoutPhase == nil { if g.lastLayoutPhase == nil {
@ -97,7 +97,7 @@ func (g *FlowGroup) Draw(buf *tui.ViewBuffer) {
g.lastLayoutPhase = nil g.lastLayoutPhase = nil
} }
func (g *FlowGroup) Layout() (prefWidth, prefHeight int) { func (g *FlowLayout) Layout() (prefWidth, prefHeight int) {
layout := CalculateLayoutResult(g.Views()) layout := CalculateLayoutResult(g.Views())
g.lastLayoutPhase = layout g.lastLayoutPhase = layout
@ -113,7 +113,7 @@ func (g *FlowGroup) Layout() (prefWidth, prefHeight int) {
return return
} }
func (g *FlowGroup) OnKeyPressed(event *tui.KeyEvent) (consumed bool) { func (g *FlowLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
for _, view := range g.Views() { for _, view := range g.Views() {
if view.OnKeyPressed(event) { if view.OnKeyPressed(event) {
return true return true

View File

@ -4,8 +4,8 @@ import (
"git.tordarus.net/Tordarus/tui" "git.tordarus.net/Tordarus/tui"
) )
// SeperatorGroup ia a tui.Group which separates // SeperatorLayout ia a tui.Layout which separates
type SeperatorGroup struct { type SeperatorLayout struct {
tui.ViewTmpl tui.ViewTmpl
views []tui.View views []tui.View
@ -15,39 +15,39 @@ type SeperatorGroup struct {
Orientation tui.Orientation Orientation tui.Orientation
} }
var _ tui.Group = &SeperatorGroup{} var _ tui.Layout = &SeperatorLayout{}
func NewSeparatorGroup(orientation tui.Orientation) *SeperatorGroup { func NewSeparatorLayout(orientation tui.Orientation) *SeperatorLayout {
return &SeperatorGroup{ return &SeperatorLayout{
views: make([]tui.View, 0), views: make([]tui.View, 0),
gravity: map[tui.View]int{}, gravity: map[tui.View]int{},
Orientation: orientation, Orientation: orientation,
} }
} }
func (g *SeperatorGroup) Views() []tui.View { func (g *SeperatorLayout) Views() []tui.View {
return g.views[:] 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.views = append(g.views, v)
g.gravitySum += gravity g.gravitySum += gravity
g.gravity[v] = 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.views = append([]tui.View{v}, g.views...)
g.gravitySum += gravity g.gravitySum += gravity
g.gravity[v] = 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.views = append(g.views[:index], append([]tui.View{v}, g.views[index:]...)...)
g.gravitySum += gravity g.gravitySum += gravity
g.gravity[v] = 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() { for _, view := range g.Views() {
if view == v { if view == v {
g.gravitySum += gravity - g.gravity[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() { for index, view := range g.Views() {
if view == v { if view == v {
g.views = append(g.views[:index], g.views[index:]...) 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] return g.views[slot]
} }
func (g *SeperatorGroup) Draw(buf *tui.ViewBuffer) { func (g *SeperatorLayout) Draw(buf *tui.ViewBuffer) {
g.ViewTmpl.Draw(buf) g.ViewTmpl.Draw(buf)
if g.Orientation == tui.Horizontal { 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 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() { for _, view := range g.Views() {
if view.OnKeyPressed(event) { if view.OnKeyPressed(event) {
return true return true