Debian Codegraph @

shiny/widget/padder.go

raw symbols SCIP
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
5 package widget
6
7 import (
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 // Padder is a shell widget that adds a margin to the inner widget's measured
14 // size. That margin may be added horizontally (left and right), vertically
15 // (top and bottom) or both, determined by the Padder's axis.
16 //
17 // That marginal space is not considered part of the inner widget's geometry.
18 // For example, to make that space 'clickable', construct the Padder inside of
19 // an event handling widget instead of vice versa.
20 type Padder struct {
21 node.ShellEmbed
22 Axis Axis
23 Margin unit.Value
24 }
25
26 // NewPadder returns a new Padder widget.
27 func NewPadder(a Axis, margin unit.Value, inner node.Node) *Padder {
28 w := &Padder{
29 Axis: a,
30 Margin: margin,
31 }
32 w.Wrapper = w
33 if inner != nil {
34 w.Insert(inner, nil)
35 }
36 return w
37 }
38
39 func (w *Padder) Measure(t *theme.Theme, widthHint, heightHint int) {
40 margin2 := t.Pixels(w.Margin).Round() * 2
41 if w.Axis.Horizontal() && widthHint >= 0 {
42 widthHint -= margin2
43 if widthHint < 0 {
44 widthHint = 0
45 }
46 }
47 if w.Axis.Vertical() && heightHint >= 0 {
48 heightHint -= margin2
49 if heightHint < 0 {
50 heightHint = 0
51 }
52 }
53 w.ShellEmbed.Measure(t, widthHint, heightHint)
54 if w.Axis.Horizontal() {
55 w.MeasuredSize.X += margin2
56 }
57 if w.Axis.Vertical() {
58 w.MeasuredSize.Y += margin2
59 }
60 }
61
62 func (w *Padder) Layout(t *theme.Theme) {
63 if c := w.FirstChild; c != nil {
64 r := w.Rect.Sub(w.Rect.Min)
65 inset := r.Inset(t.Pixels(w.Margin).Round())
66 if w.Axis.Horizontal() {
67 r.Min.X = inset.Min.X
68 r.Max.X = inset.Max.X
69 }
70 if w.Axis.Vertical() {
71 r.Min.Y = inset.Min.Y
72 r.Max.Y = inset.Max.Y
73 }
74 c.Rect = r
75 c.Wrapper.Layout(t)
76 }
77 }