tui/views/view_constrain.go

31 lines
677 B
Go
Raw Normal View History

2022-04-02 13:01:41 +02:00
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{}
2022-04-02 13:01:41 +02:00
2022-05-04 11:13:18 +02:00
func NewConstrainView(view tui.View, maxWidth, maxHeight int) *ConstrainView {
2022-04-02 13:01:41 +02:00
v := new(ConstrainView)
v.SetView(view)
2022-05-04 11:13:18 +02:00
v.Constrain(maxWidth, maxHeight)
2022-04-02 13:01:41 +02:00
return v
}
func (v *ConstrainView) Constrain(maxWidth, maxHeight int) {
v.MaxWidth, v.MaxHeight = maxWidth, maxHeight
}
func (v *ConstrainView) Layout() (prefWidth, prefHeight int) {
2022-05-04 11:13:18 +02:00
vw, vh := v.View().Layout()
return min(vw, v.MaxWidth), min(vh, v.MaxHeight)
2022-04-02 13:01:41 +02:00
}