119 lines
2.6 KiB
Go
119 lines
2.6 KiB
Go
package layouts
|
|
|
|
import (
|
|
"git.milar.in/milarin/tui"
|
|
)
|
|
|
|
// SeperatorLayout ia a tui.Layout which separates its view into gravity-based portions
|
|
type SeperatorLayout struct {
|
|
tui.ViewTmpl
|
|
views []tui.View
|
|
|
|
gravity map[tui.View]int
|
|
gravitySum int
|
|
|
|
Orientation tui.Orientation
|
|
}
|
|
|
|
var _ tui.Layout = &SeperatorLayout{}
|
|
|
|
func NewSeparatorLayout(orientation tui.Orientation) *SeperatorLayout {
|
|
return &SeperatorLayout{
|
|
views: make([]tui.View, 0),
|
|
gravity: map[tui.View]int{},
|
|
Orientation: orientation,
|
|
}
|
|
}
|
|
|
|
func (g *SeperatorLayout) Views() []tui.View {
|
|
return g.views[:]
|
|
}
|
|
|
|
func (g *SeperatorLayout) AppendView(v tui.View, gravity int) {
|
|
g.views = append(g.views, v)
|
|
g.gravitySum += gravity
|
|
g.gravity[v] = gravity
|
|
}
|
|
|
|
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 *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 *SeperatorLayout) SetGravity(v tui.View, gravity int) {
|
|
for _, view := range g.Views() {
|
|
if view == v {
|
|
g.gravitySum += gravity - g.gravity[v]
|
|
g.gravity[v] = gravity
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
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:]...)
|
|
g.gravitySum -= g.gravity[v]
|
|
delete(g.gravity, v)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func (g *SeperatorLayout) View(index int) tui.View {
|
|
return g.views[index]
|
|
}
|
|
|
|
func (g *SeperatorLayout) Draw(buf *tui.ViewBuffer) {
|
|
g.ViewTmpl.Draw(buf)
|
|
|
|
if g.Orientation == tui.Horizontal {
|
|
x := 0
|
|
for _, v := range g.Views() {
|
|
viewGravity := g.gravity[v]
|
|
percentage := float64(viewGravity) / float64(g.gravitySum)
|
|
width := int(percentage * float64(buf.Width()))
|
|
v.Draw(buf.Sub(x, 0, width, buf.Height()))
|
|
x += width
|
|
}
|
|
} else if g.Orientation == tui.Vertical {
|
|
y := 0
|
|
for _, v := range g.Views() {
|
|
viewGravity := g.gravity[v]
|
|
percentage := float64(viewGravity) / float64(g.gravitySum)
|
|
height := int(percentage * float64(buf.Height()))
|
|
v.Draw(buf.Sub(0, y, buf.Width(), height))
|
|
y += height
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func (g *SeperatorLayout) Layout() (prefWidth, prefHeight int) {
|
|
return -1, -1
|
|
}
|
|
|
|
func (g *SeperatorLayout) 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
|
|
}
|
|
|
|
// TODO OnMouseEvent
|