tui/views/view_margin.go
2023-04-24 11:55:04 +02:00

45 lines
1.1 KiB
Go

package views
import "git.milar.in/milarin/tui"
// MarginView is a tui.Wrapper which applies margin around its view
type MarginView struct {
tui.WrapperTmpl
Margin map[tui.Side]int
}
var _ tui.Wrapper = &MarginView{}
func NewMarginView(view tui.View, top, right, bottom, left int) *MarginView {
v := new(MarginView)
v.SetView(view)
v.SetMargin(top, right, bottom, left)
return v
}
func (g *MarginView) Draw(buf *tui.ViewBuffer) {
x := g.Margin[tui.SideLeft]
y := g.Margin[tui.SideTop]
w := buf.Width() - x - g.Margin[tui.SideRight]
h := buf.Height() - y - g.Margin[tui.SideBottom]
g.ViewTmpl.Draw(buf)
g.View().Draw(buf.Sub(x, y, w, h))
}
func (v *MarginView) Layout() (prefWidth, prefHeight int) {
w, h := v.View().Layout()
w = iff(w > 0, w+v.Margin[tui.SideLeft]+v.Margin[tui.SideRight], w)
h = iff(h > 0, h+v.Margin[tui.SideTop]+v.Margin[tui.SideBottom], h)
return w, h
}
func (v *MarginView) SetMargin(top, right, bottom, left int) {
v.Margin = map[tui.Side]int{
tui.SideTop: top,
tui.SideRight: right,
tui.SideBottom: bottom,
tui.SideLeft: left,
}
}