2022-04-02 13:01:41 +02:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.tordarus.net/Tordarus/tui"
|
|
|
|
)
|
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
// BorderLayout ia a tui.Layout which places its children onto a given tui.Side
|
|
|
|
type BorderLayout struct {
|
2022-04-02 13:01:41 +02:00
|
|
|
tui.ViewTmpl
|
|
|
|
views map[Slot]tui.View
|
|
|
|
horizontalLayout *LayoutResult
|
|
|
|
verticalLayout *LayoutResult
|
2022-05-04 12:03:51 +02:00
|
|
|
|
|
|
|
viewDimensions map[Slot]tui.Dimension
|
2022-04-02 13:01:41 +02:00
|
|
|
}
|
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
var _ tui.Layout = &BorderLayout{}
|
2022-04-02 13:01:41 +02:00
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
func NewBorderLayout() *BorderLayout {
|
|
|
|
return &BorderLayout{
|
2022-05-04 12:03:51 +02:00
|
|
|
views: map[Slot]tui.View{},
|
|
|
|
viewDimensions: map[Slot]tui.Dimension{},
|
2022-04-02 13:01:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
func (g *BorderLayout) Views() []tui.View {
|
2022-04-02 13:01:41 +02:00
|
|
|
s := make([]tui.View, 0, len(g.views))
|
|
|
|
for _, view := range g.views {
|
|
|
|
s = append(s, view)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
func (g *BorderLayout) SetView(v tui.View, slot Slot) {
|
2022-04-02 13:01:41 +02:00
|
|
|
g.views[slot] = v
|
|
|
|
}
|
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
func (g *BorderLayout) View(slot Slot) tui.View {
|
2022-04-02 13:01:41 +02:00
|
|
|
return g.views[slot]
|
|
|
|
}
|
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
func (g *BorderLayout) Draw(buf *tui.ViewBuffer) {
|
2022-04-02 13:01:41 +02:00
|
|
|
g.ViewTmpl.Draw(buf)
|
|
|
|
|
|
|
|
if g.verticalLayout == nil {
|
|
|
|
g.Layout()
|
|
|
|
}
|
|
|
|
verticalLayout := g.verticalLayout
|
|
|
|
|
|
|
|
if g.horizontalLayout == nil {
|
|
|
|
g.Layout()
|
|
|
|
}
|
|
|
|
horizontalLayout := g.horizontalLayout
|
|
|
|
|
|
|
|
remainingVerticalSpacePerView := (buf.Height() - verticalLayout.Sum.Height)
|
|
|
|
if verticalLayout.VerticalNegativeCount > 0 {
|
|
|
|
remainingVerticalSpacePerView /= verticalLayout.VerticalNegativeCount
|
|
|
|
}
|
|
|
|
|
|
|
|
remainingHorizontalSpacePerView := (buf.Width() - horizontalLayout.Sum.Width)
|
|
|
|
if horizontalLayout.HorizontalNegativeCount > 0 {
|
|
|
|
remainingHorizontalSpacePerView /= horizontalLayout.HorizontalNegativeCount
|
|
|
|
}
|
|
|
|
|
|
|
|
fitsVertically := buf.Height() >= verticalLayout.Sum.Height
|
|
|
|
fitsHorizontally := buf.Width() >= horizontalLayout.Sum.Width
|
|
|
|
|
|
|
|
var topHeight int
|
|
|
|
var bottomHeight int
|
|
|
|
var leftWidth int
|
|
|
|
var rightWidth int
|
|
|
|
|
|
|
|
if view, ok := g.views[Top]; ok {
|
|
|
|
_, topHeight = view.Layout()
|
|
|
|
|
|
|
|
if fitsVertically {
|
|
|
|
topHeight = iff(topHeight < 0, remainingVerticalSpacePerView, topHeight)
|
|
|
|
} else {
|
|
|
|
topHeight = int(float64(buf.Height()) * float64(topHeight) / float64(verticalLayout.Sum.Height))
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:03:51 +02:00
|
|
|
g.viewDimensions[Top] = tui.D(0, 0, buf.Width(), topHeight)
|
2022-04-02 13:01:41 +02:00
|
|
|
view.Draw(buf.Sub(0, 0, buf.Width(), topHeight))
|
|
|
|
}
|
|
|
|
|
|
|
|
if view, ok := g.views[Bottom]; ok {
|
|
|
|
_, bottomHeight = view.Layout()
|
|
|
|
|
|
|
|
if fitsVertically {
|
|
|
|
bottomHeight = iff(bottomHeight < 0, remainingVerticalSpacePerView, bottomHeight)
|
|
|
|
} else {
|
|
|
|
bottomHeight = int(float64(buf.Height()) * float64(bottomHeight) / float64(verticalLayout.Sum.Height))
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:03:51 +02:00
|
|
|
g.viewDimensions[Bottom] = tui.D(0, buf.Height()-bottomHeight, buf.Width(), bottomHeight)
|
2022-04-02 13:01:41 +02:00
|
|
|
view.Draw(buf.Sub(0, buf.Height()-bottomHeight, buf.Width(), bottomHeight))
|
|
|
|
}
|
|
|
|
|
|
|
|
if view, ok := g.views[Left]; ok {
|
|
|
|
leftWidth, _ = view.Layout()
|
|
|
|
|
|
|
|
if fitsHorizontally {
|
|
|
|
leftWidth = iff(leftWidth < 0, remainingHorizontalSpacePerView, leftWidth)
|
|
|
|
} else {
|
|
|
|
leftWidth = int(float64(buf.Width()) * float64(leftWidth) / float64(horizontalLayout.Sum.Width))
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:03:51 +02:00
|
|
|
g.viewDimensions[Left] = tui.D(0, topHeight, leftWidth, buf.Height()-topHeight-bottomHeight)
|
2022-04-02 13:01:41 +02:00
|
|
|
view.Draw(buf.Sub(0, topHeight, leftWidth, buf.Height()-topHeight-bottomHeight))
|
|
|
|
}
|
|
|
|
|
|
|
|
if view, ok := g.views[Right]; ok {
|
|
|
|
rightWidth, _ = view.Layout()
|
|
|
|
|
|
|
|
if fitsHorizontally {
|
|
|
|
rightWidth = iff(rightWidth < 0, remainingHorizontalSpacePerView, rightWidth)
|
|
|
|
} else {
|
|
|
|
rightWidth = int(float64(buf.Width()) * float64(rightWidth) / float64(horizontalLayout.Sum.Width))
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:03:51 +02:00
|
|
|
g.viewDimensions[Right] = tui.D(buf.Width()-rightWidth, topHeight, rightWidth, buf.Height()-topHeight-bottomHeight)
|
2022-04-02 13:01:41 +02:00
|
|
|
view.Draw(buf.Sub(buf.Width()-rightWidth, topHeight, rightWidth, buf.Height()-topHeight-bottomHeight))
|
|
|
|
}
|
|
|
|
|
|
|
|
if view, ok := g.views[Center]; ok {
|
2022-05-04 12:03:51 +02:00
|
|
|
g.viewDimensions[Center] = tui.D(leftWidth, topHeight, buf.Width()-leftWidth-rightWidth, buf.Height()-topHeight-bottomHeight)
|
2022-04-02 13:01:41 +02:00
|
|
|
view.Draw(buf.Sub(leftWidth, topHeight, buf.Width()-leftWidth-rightWidth, buf.Height()-topHeight-bottomHeight))
|
|
|
|
}
|
|
|
|
|
|
|
|
g.verticalLayout = nil
|
|
|
|
g.horizontalLayout = nil
|
|
|
|
}
|
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
func (g *BorderLayout) Layout() (prefWidth, prefHeight int) {
|
2022-04-02 13:01:41 +02:00
|
|
|
g.verticalLayout = CalculateLayoutResult([]tui.View{g.View(Top), g.View(Center), g.View(Bottom)})
|
|
|
|
g.horizontalLayout = CalculateLayoutResult([]tui.View{g.View(Left), g.View(Center), g.View(Right)})
|
|
|
|
return -1, -1
|
|
|
|
}
|
|
|
|
|
2022-04-02 15:21:17 +02:00
|
|
|
func (g *BorderLayout) OnKeyPressed(event *tui.KeyEvent) (consumed bool) {
|
2022-04-02 13:01:41 +02:00
|
|
|
for _, view := range g.Views() {
|
|
|
|
if view.OnKeyPressed(event) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-04 12:03:51 +02:00
|
|
|
func (g *BorderLayout) OnMouseClicked(event *tui.MouseEvent) (consumed bool) {
|
|
|
|
for slot, dim := range g.viewDimensions {
|
|
|
|
if event.Position.In(dim) {
|
|
|
|
g.views[slot].OnMouseClicked(event)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-04-02 13:01:41 +02:00
|
|
|
type Slot uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
Top Slot = iota
|
|
|
|
Bottom
|
|
|
|
Left
|
|
|
|
Right
|
|
|
|
Center
|
|
|
|
)
|