tui/views/marginview.go
Timon Ringwald dfa00f5fe3 more views
2022-04-02 13:01:41 +02:00

49 lines
1.1 KiB
Go

package views
import "git.tordarus.net/Tordarus/tui"
// MarginView is a tui.Wrapper which applies margin around its view
type MarginView struct {
tui.WrapperTmpl
Margin map[tui.Side]int
}
var _ tui.View = &MarginView{}
func NewMarginView(view tui.View) *MarginView {
v := new(MarginView)
v.SetView(view)
v.SetMargin(0, 0, 0, 0)
return v
}
func (g *MarginView) Draw(buf *tui.ViewBuffer) {
x := g.Margin[tui.Left]
y := g.Margin[tui.Top]
w := buf.Width() - x - g.Margin[tui.Right]
h := buf.Height() - y - g.Margin[tui.Bottom]
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.Left]+v.Margin[tui.Right], w)
h = iff(h > 0, h+v.Margin[tui.Top]+v.Margin[tui.Bottom], h)
return w, h
}
func (v *MarginView) Style() tui.Style {
return v.ViewTmpl.Style()
}
func (v *MarginView) SetMargin(top, right, bottom, left int) {
v.Margin = map[tui.Side]int{
tui.Top: top,
tui.Right: right,
tui.Bottom: bottom,
tui.Left: left,
}
}