124 lines
3.0 KiB
Go
124 lines
3.0 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 view == v {
|
|
g.views = append(g.views[:index], g.views[index:]...)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
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 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
|
|
}
|
|
} else if g.Orientation == tui.Vertical {
|
|
remainingSpacePerView := buf.Height() - layout.Sum.Height
|
|
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
|
|
}
|
|
}
|
|
|
|
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.Width, -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
|
|
}
|