49 lines
984 B
Go
49 lines
984 B
Go
package views
|
|
|
|
import (
|
|
"git.milar.in/milarin/gmath"
|
|
"git.milar.in/milarin/gui"
|
|
)
|
|
|
|
// ConstrainView is a gui.Wrapper which constrains the dimensions of its View
|
|
type ConstrainView struct {
|
|
gui.WrapperTmpl
|
|
MaxWidth int
|
|
MaxHeight int
|
|
}
|
|
|
|
var _ gui.Wrapper = &ConstrainView{}
|
|
|
|
func NewConstrainView(view gui.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(ctx gui.AppContext) (prefWidth, prefHeight int) {
|
|
if v.View() == nil {
|
|
return v.MaxWidth, v.MaxHeight
|
|
}
|
|
|
|
vw, vh := v.View().Layout(ctx)
|
|
|
|
if v.MaxWidth >= 0 {
|
|
prefWidth = iff(vw >= 0, gmath.Min(vw, v.MaxWidth), v.MaxWidth)
|
|
} else {
|
|
prefWidth = vw
|
|
}
|
|
|
|
if v.MaxHeight >= 0 {
|
|
prefHeight = iff(vh >= 0, gmath.Min(vh, v.MaxHeight), v.MaxHeight)
|
|
} else {
|
|
prefHeight = vh
|
|
}
|
|
|
|
return
|
|
}
|