tui/views/view_margin.go
2022-04-02 17:16:51 +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.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) Style() tui.Style {
return v.ViewTmpl.Style()
}
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,
}
}