112 lines
2.5 KiB
Go
112 lines
2.5 KiB
Go
|
package views
|
||
|
|
||
|
import (
|
||
|
"git.tordarus.net/Tordarus/tui"
|
||
|
)
|
||
|
|
||
|
// SeperatorGroup ia a tui.Group which separates
|
||
|
type SeperatorGroup struct {
|
||
|
tui.ViewTmpl
|
||
|
views []tui.View
|
||
|
|
||
|
gravity map[tui.View]int
|
||
|
gravitySum int
|
||
|
|
||
|
Orientation tui.Orientation
|
||
|
}
|
||
|
|
||
|
var _ tui.Group = &SeperatorGroup{}
|
||
|
|
||
|
func NewSeparatorGroup(orientation tui.Orientation) *SeperatorGroup {
|
||
|
return &SeperatorGroup{
|
||
|
views: make([]tui.View, 0),
|
||
|
gravity: map[tui.View]int{},
|
||
|
Orientation: orientation,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (g *SeperatorGroup) Views() []tui.View {
|
||
|
return g.views[:]
|
||
|
}
|
||
|
|
||
|
func (g *SeperatorGroup) AppendView(v tui.View, gravity int) {
|
||
|
g.views = append(g.views, v)
|
||
|
g.gravitySum += gravity
|
||
|
g.gravity[v] = gravity
|
||
|
}
|
||
|
|
||
|
func (g *SeperatorGroup) PrependView(v tui.View, gravity int) {
|
||
|
g.views = append([]tui.View{v}, g.views...)
|
||
|
g.gravitySum += gravity
|
||
|
g.gravity[v] = gravity
|
||
|
}
|
||
|
|
||
|
func (g *SeperatorGroup) 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 *SeperatorGroup) 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 *SeperatorGroup) 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 *SeperatorGroup) View(slot Slot) tui.View {
|
||
|
return g.views[slot]
|
||
|
}
|
||
|
|
||
|
func (g *SeperatorGroup) 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 *SeperatorGroup) Layout() (prefWidth, prefHeight int) {
|
||
|
return -1, -1
|
||
|
}
|
||
|
|
||
|
func (g *SeperatorGroup) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
|
||
|
for _, view := range g.Views() {
|
||
|
if view.OnKeyPressed(event) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|