tui/layouts/layout_separator.go

119 lines
2.6 KiB
Go
Raw Normal View History

2023-04-24 11:41:38 +02:00
package layouts
2022-04-02 15:09:52 +02:00
import (
2023-04-24 11:55:04 +02:00
"git.milar.in/milarin/tui"
2022-04-02 15:09:52 +02:00
)
2022-05-04 14:16:08 +02:00
// SeperatorLayout ia a tui.Layout which separates its view into gravity-based portions
2022-04-02 15:21:17 +02:00
type SeperatorLayout struct {
2022-04-02 15:09:52 +02:00
tui.ViewTmpl
views []tui.View
gravity map[tui.View]int
gravitySum int
Orientation tui.Orientation
}
2022-04-02 15:21:17 +02:00
var _ tui.Layout = &SeperatorLayout{}
2022-04-02 15:09:52 +02:00
2022-04-02 15:21:17 +02:00
func NewSeparatorLayout(orientation tui.Orientation) *SeperatorLayout {
return &SeperatorLayout{
2022-04-02 15:09:52 +02:00
views: make([]tui.View, 0),
gravity: map[tui.View]int{},
Orientation: orientation,
}
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) Views() []tui.View {
2022-04-02 15:09:52 +02:00
return g.views[:]
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) AppendView(v tui.View, gravity int) {
2022-04-02 15:09:52 +02:00
g.views = append(g.views, v)
g.gravitySum += gravity
g.gravity[v] = gravity
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) PrependView(v tui.View, gravity int) {
2022-04-02 15:09:52 +02:00
g.views = append([]tui.View{v}, g.views...)
g.gravitySum += gravity
g.gravity[v] = gravity
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) InsertView(v tui.View, index int, gravity int) {
2022-04-02 15:09:52 +02:00
g.views = append(g.views[:index], append([]tui.View{v}, g.views[index:]...)...)
g.gravitySum += gravity
g.gravity[v] = gravity
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) SetGravity(v tui.View, gravity int) {
2022-04-02 15:09:52 +02:00
for _, view := range g.Views() {
if view == v {
g.gravitySum += gravity - g.gravity[v]
g.gravity[v] = gravity
return
}
}
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) RemoveView(v tui.View) {
2022-04-02 15:09:52 +02:00
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
}
}
}
2022-05-04 14:16:08 +02:00
func (g *SeperatorLayout) View(index int) tui.View {
return g.views[index]
2022-04-02 15:09:52 +02:00
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) Draw(buf *tui.ViewBuffer) {
2022-04-02 15:09:52 +02:00
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
}
}
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) Layout() (prefWidth, prefHeight int) {
2022-04-02 15:09:52 +02:00
return -1, -1
}
2022-04-02 15:21:17 +02:00
func (g *SeperatorLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
if g.KeyPressed != nil {
return g.KeyPressed(event)
}
2022-04-02 15:09:52 +02:00
for _, view := range g.Views() {
if view.OnKeyPressed(event) {
return true
}
}
2022-04-02 15:09:52 +02:00
return false
}
// TODO OnMouseEvent