1// Copyright 2016 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package widget
6
7import (
8	"golang.org/x/exp/shiny/unit"
9	"golang.org/x/exp/shiny/widget/node"
10	"golang.org/x/exp/shiny/widget/theme"
11)
12
13// Sizer is a shell widget that overrides its child's measured size.
14type Sizer struct {
15	node.ShellEmbed
16	NaturalWidth  unit.Value
17	NaturalHeight unit.Value
18}
19
20// NewSizer returns a new Sizer widget of the given natural size. Its parent
21// widget may lay it out at a different size than its natural size, such as
22// expanding to fill a panel's width.
23func NewSizer(naturalWidth, naturalHeight unit.Value, inner node.Node) *Sizer {
24	w := &Sizer{
25		NaturalWidth:  naturalWidth,
26		NaturalHeight: naturalHeight,
27	}
28	w.Wrapper = w
29	if inner != nil {
30		w.Insert(inner, nil)
31	}
32	return w
33}
34
35func (w *Sizer) Measure(t *theme.Theme, widthHint, heightHint int) {
36	w.MeasuredSize.X = t.Pixels(w.NaturalWidth).Round()
37	w.MeasuredSize.Y = t.Pixels(w.NaturalHeight).Round()
38	if c := w.FirstChild; c != nil {
39		c.Wrapper.Measure(t, w.MeasuredSize.X, w.MeasuredSize.Y)
40	}
41}
42