tui/views/view_margin.go

49 lines
1.1 KiB
Go
Raw Normal View History

2022-04-02 13:01:41 +02:00
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) {
2022-04-02 15:09:52 +02:00
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]
2022-04-02 13:01:41 +02:00
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()
2022-04-02 15:09:52 +02:00
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)
2022-04-02 13:01:41 +02:00
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{
2022-04-02 15:09:52 +02:00
tui.SideTop: top,
tui.SideRight: right,
tui.SideBottom: bottom,
tui.SideLeft: left,
2022-04-02 13:01:41 +02:00
}
}