tui/views/view_constrain.go
2022-05-04 11:13:18 +02:00

31 lines
677 B
Go

package views
import (
"git.tordarus.net/Tordarus/tui"
)
// ConstrainView is a tui.Wrapper which constrains the dimensions of its View
type ConstrainView struct {
tui.WrapperTmpl
MaxWidth int
MaxHeight int
}
var _ tui.Wrapper = &ConstrainView{}
func NewConstrainView(view tui.View, maxWidth, maxHeight int) *ConstrainView {
v := new(ConstrainView)
v.SetView(view)
v.Constrain(maxWidth, maxHeight)
return v
}
func (v *ConstrainView) Constrain(maxWidth, maxHeight int) {
v.MaxWidth, v.MaxHeight = maxWidth, maxHeight
}
func (v *ConstrainView) Layout() (prefWidth, prefHeight int) {
vw, vh := v.View().Layout()
return min(vw, v.MaxWidth), min(vh, v.MaxHeight)
}