2023-04-24 11:41:38 +02:00
|
|
|
package layouts
|
2022-04-02 15:09:52 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"git.tordarus.net/Tordarus/tui"
|
|
|
|
)
|
|
|
|
|
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) {
|
2022-04-02 15:09:52 +02:00
|
|
|
for _, view := range g.Views() {
|
|
|
|
if view.OnKeyPressed(event) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2022-05-04 12:03:51 +02:00
|
|
|
|
2022-05-04 14:47:55 +02:00
|
|
|
// TODO OnMouseEvent
|