48 lines
942 B
Go
48 lines
942 B
Go
|
package views
|
||
|
|
||
|
import (
|
||
|
"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, min(vw, v.MaxWidth), v.MaxWidth)
|
||
|
} else {
|
||
|
prefWidth = vw
|
||
|
}
|
||
|
|
||
|
if v.MaxHeight >= 0 {
|
||
|
prefHeight = iff(vh >= 0, min(vh, v.MaxHeight), v.MaxHeight)
|
||
|
} else {
|
||
|
prefHeight = vh
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|