165 lines
3.9 KiB
Go
165 lines
3.9 KiB
Go
package layouts
|
|
|
|
import (
|
|
"git.milar.in/milarin/tui"
|
|
)
|
|
|
|
// FlowLayout ia a tui.Layout which places its children in a linear layout
|
|
type FlowLayout struct {
|
|
tui.ViewTmpl
|
|
views []tui.View
|
|
lastLayoutPhase *LayoutResult
|
|
|
|
viewDims map[tui.View]tui.Dimension
|
|
|
|
// 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),
|
|
viewDims: map[tui.View]tui.Dimension{},
|
|
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 {
|
|
delete(g.viewDims, view)
|
|
g.views = append(g.views[:index], g.views[index+1:]...)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (g *FlowLayout) RemoveViews(v ...tui.View) {
|
|
views := append(make([]tui.View, 0, len(v)), v...)
|
|
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)
|
|
}
|
|
|
|
g.viewDims[view] = tui.D(x, 0, size.Width, size.Height)
|
|
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)
|
|
}
|
|
|
|
g.viewDims[view] = tui.D(0, y, size.Width, size.Height)
|
|
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) {
|
|
if g.KeyPressed != nil {
|
|
return g.KeyPressed(event)
|
|
}
|
|
|
|
for _, view := range g.Views() {
|
|
if view.OnKeyPressed(event) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (g *FlowLayout) OnMouseEvent(event *tui.MouseEvent) (consumed bool) {
|
|
for view, dim := range g.viewDims {
|
|
if event.Position.In(dim) {
|
|
view.OnMouseEvent(event)
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|