tui/views/view_margin.go

45 lines
1.1 KiB
Go
Raw Normal View History

2022-04-02 13:01:41 +02:00
package views
2023-04-24 11:55:04 +02:00
import "git.milar.in/milarin/tui"
2022-04-02 13:01:41 +02:00
// 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{}
2022-04-02 13:01:41 +02:00
func NewMarginView(view tui.View, top, right, bottom, left int) *MarginView {
2022-04-02 13:01:41 +02:00
v := new(MarginView)
v.SetView(view)
v.SetMargin(top, right, bottom, left)
2022-04-02 13:01:41 +02:00
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) 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
}
}