tui/views/layout_flow.go
2022-04-03 17:31:46 +02:00

148 lines
3.4 KiB
Go

package views
import (
"git.tordarus.net/Tordarus/tui"
)
// FlowLayout ia a tui.Layout which places its children in a linear layout
type FlowLayout struct {
tui.ViewTmpl
views []tui.View
lastLayoutPhase *LayoutResult
// Orientation defines in which direction the children will be placed
Orientation tui.Orientation
}
var _ tui.Layout = &FlowLayout{}
func NewFlowLayout(orientation tui.Orientation) *FlowLayout {
return &FlowLayout{
views: make([]tui.View, 0),
Orientation: orientation,
}
}
func (g *FlowLayout) Views() []tui.View {
return g.views
}
func (g *FlowLayout) AppendViews(v ...tui.View) {
g.views = append(g.views, v...)
}
func (g *FlowLayout) PrependViews(v ...tui.View) {
g.views = append(v, g.views...)
}
func (g *FlowLayout) InsertView(v tui.View, index int) {
g.views = append(g.views[:index], append([]tui.View{v}, g.views[index:]...)...)
}
func (g *FlowLayout) removeView(v tui.View) {
for index, view := range g.Views() {
if v == view {
g.views = append(g.views[:index], g.views[index+1:]...)
return
}
}
}
func (g *FlowLayout) RemoveViews(v ...tui.View) {
views := make([]tui.View, 0, len(v))
for _, view := range v {
views = append(views, view)
}
for _, view := range views {
g.removeView(view)
}
}
func (g *FlowLayout) Draw(buf *tui.ViewBuffer) {
g.ViewTmpl.Draw(buf)
if g.lastLayoutPhase == nil {
g.Layout()
}
layout := g.lastLayoutPhase
if g.Orientation == tui.Horizontal {
remainingSpacePerView := buf.Width() - layout.Sum.Width
if remainingSpacePerView < 0 {
remainingSpacePerView = 0
}
if layout.HorizontalNegativeCount > 0 {
remainingSpacePerView /= layout.HorizontalNegativeCount
}
x := 0
for _, view := range g.views {
size := layout.Sizes[view]
size.Height = iff(size.Height < 0, buf.Height(), size.Height)
if size.Width < 0 {
size.Width = iff(layout.Sum.Width > buf.Width(), 0, remainingSpacePerView)
}
view.Draw(buf.Sub(x, 0, size.Width, size.Height))
x += size.Width
if x >= buf.Width() {
break
}
}
} else if g.Orientation == tui.Vertical {
remainingSpacePerView := buf.Height() - layout.Sum.Height
if remainingSpacePerView < 0 {
remainingSpacePerView = 0
}
if layout.VerticalNegativeCount > 0 {
remainingSpacePerView /= layout.VerticalNegativeCount
}
y := 0
for _, view := range g.views {
size := layout.Sizes[view]
size.Width = iff(size.Width < 0, buf.Width(), size.Width)
if size.Height < 0 {
size.Height = iff(layout.Sum.Height > buf.Height(), 0, remainingSpacePerView)
}
view.Draw(buf.Sub(0, y, size.Width, size.Height))
y += size.Height
if y >= buf.Height() {
break
}
}
}
g.lastLayoutPhase = nil
}
func (g *FlowLayout) Layout() (prefWidth, prefHeight int) {
layout := CalculateLayoutResult(g.Views())
g.lastLayoutPhase = layout
if g.Orientation == tui.Horizontal {
prefWidth = iff(layout.HorizontalNegativeCount == 0, layout.Sum.Width, -1)
prefHeight = iff(layout.VerticalNegativeCount == 0, layout.Max.Height, -1)
} else if g.Orientation == tui.Vertical {
prefWidth = iff(layout.HorizontalNegativeCount == 0, layout.Max.Width, -1)
prefHeight = iff(layout.VerticalNegativeCount == 0, layout.Sum.Height, -1)
}
layout.Pref = tui.Size{Width: prefWidth, Height: prefHeight}
return
}
func (g *FlowLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
for _, view := range g.Views() {
if view.OnKeyPressed(event) {
return true
}
}
return false
}