Skip to content

Commit 11e80e3

Browse files
author
rj
committed
Updated widgets in main package to use the new behaviour of Mount in base to use new "nil" or nilish element.
1 parent 6cff4e9 commit 11e80e3

11 files changed

+46
-91
lines changed

align.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (*alignElement) Kind() *base.Kind {
8787
}
8888

8989
func (w *alignElement) Layout(bc base.Constraints) base.Size {
90-
size := base.Layout(w.child, bc.Loosen())
90+
size := w.child.Layout(bc.Loosen())
9191
w.childSize = size
9292
if w.widthFactor > 0 {
9393
size.Width = base.Length(float64(size.Width) * w.widthFactor)
@@ -99,10 +99,6 @@ func (w *alignElement) Layout(bc base.Constraints) base.Size {
9999
}
100100

101101
func (w *alignElement) MinIntrinsicHeight(width base.Length) base.Length {
102-
if w.child == nil {
103-
return 0
104-
}
105-
106102
height := w.child.MinIntrinsicHeight(width)
107103
if w.heightFactor > 0 {
108104
return base.Length(float64(height) * w.heightFactor)
@@ -111,10 +107,6 @@ func (w *alignElement) MinIntrinsicHeight(width base.Length) base.Length {
111107
}
112108

113109
func (w *alignElement) MinIntrinsicWidth(height base.Length) base.Length {
114-
if w.child == nil {
115-
return 0
116-
}
117-
118110
width := w.child.MinIntrinsicWidth(height)
119111
if w.widthFactor > 0 {
120112
return base.Length(float64(width) * w.widthFactor)
@@ -123,10 +115,6 @@ func (w *alignElement) MinIntrinsicWidth(height base.Length) base.Length {
123115
}
124116

125117
func (w *alignElement) SetBounds(bounds base.Rectangle) {
126-
if w.child == nil {
127-
return
128-
}
129-
130118
x := bounds.Min.X.Scale(int(w.hAlign)-int(AlignEnd), int(AlignStart)-int(AlignEnd)) +
131119
(bounds.Max.X-w.childSize.Width).Scale(int(w.hAlign)-int(AlignStart), int(AlignEnd)-int(AlignStart))
132120
y := bounds.Min.Y.Scale(int(w.vAlign)-int(AlignEnd), int(AlignStart)-int(AlignEnd)) +

align_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,10 @@ func TestAlignLayout(t *testing.T) {
100100

101101
for i, v := range cases {
102102
elem := alignElement{
103+
child: mock.NewIfNotZero(v.in),
103104
widthFactor: v.widthFactor,
104105
heightFactor: v.heightFactor,
105106
}
106-
if !v.in.IsZero() {
107-
elem.child = mock.New(v.in)
108-
}
109107

110108
if out := elem.Layout(v.bc); out != v.out {
111109
t.Errorf("Case %d: Returned size does not match, got %v, want %v", i, out, v.out)
@@ -135,12 +133,10 @@ func TestAlignMinIntrinsicSize(t *testing.T) {
135133

136134
for i, v := range cases {
137135
elem := alignElement{
136+
child: mock.NewIfNotZero(v.in),
138137
widthFactor: v.widthFactor,
139138
heightFactor: v.heightFactor,
140139
}
141-
if !v.in.IsZero() {
142-
elem.child = mock.New(v.in)
143-
}
144140

145141
if out := elem.MinIntrinsicWidth(base.Inf); out != v.out.Width {
146142
t.Errorf("Case %d: Returned min intrinsic width does not match, got %v, want %v", i, out, v.out.Width)

base/nil.go

+4
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ func (*nilElement) MinIntrinsicWidth(Length) Length {
4646
return 0
4747
}
4848

49+
func (*nilElement) Props() Widget {
50+
return nil
51+
}
52+
4953
func (*nilElement) SetBounds(Rectangle) {
5054
return
5155
}

decoration.go

+1-11
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (w *decorationElement) Layout(bc base.Constraints) base.Size {
4646
vinset := w.insets.Top + w.insets.Bottom
4747

4848
innerConstraints := bc.Inset(hinset, vinset)
49-
w.childSize = base.Layout(w.child, innerConstraints)
49+
w.childSize = w.child.Layout(innerConstraints)
5050
return base.Size{
5151
w.childSize.Width + hinset,
5252
w.childSize.Height + vinset,
@@ -55,21 +55,11 @@ func (w *decorationElement) Layout(bc base.Constraints) base.Size {
5555

5656
func (w *decorationElement) MinIntrinsicHeight(width base.Length) base.Length {
5757
vinset := w.insets.Top + w.insets.Bottom
58-
59-
if w.child == nil {
60-
return vinset
61-
}
62-
6358
return w.child.MinIntrinsicHeight(width) + vinset
6459
}
6560

6661
func (w *decorationElement) MinIntrinsicWidth(height base.Length) base.Length {
6762
hinset := w.insets.Left + w.insets.Right
68-
69-
if w.child == nil {
70-
return hinset
71-
}
72-
7363
return w.child.MinIntrinsicWidth(height) + hinset
7464
}
7565

decoration_linux.go

+6-8
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (w *Decoration) mount(parent base.Control) (base.Element, error) {
3030
control.Connect("draw", decorationOnDraw, retval)
3131
control.Show()
3232

33-
child, err := base.DiffChild(parent, nil, w.Child)
33+
child, err := base.Mount(parent, w.Child)
3434
if err != nil {
3535
control.Destroy()
3636
return nil, err
@@ -117,13 +117,11 @@ func (w *decorationElement) SetBounds(bounds base.Rectangle) {
117117
pixels := bounds.Pixels()
118118
syscall.SetBounds(&w.handle.Widget, pixels.Min.X, pixels.Min.Y, pixels.Dx(), pixels.Dy())
119119

120-
if w.child != nil {
121-
bounds.Min.X += w.insets.Left
122-
bounds.Min.Y += w.insets.Top
123-
bounds.Max.X -= w.insets.Right
124-
bounds.Max.Y -= w.insets.Bottom
125-
w.child.SetBounds(bounds)
126-
}
120+
bounds.Min.X += w.insets.Left
121+
bounds.Min.Y += w.insets.Top
122+
bounds.Max.X -= w.insets.Right
123+
bounds.Max.Y -= w.insets.Bottom
124+
w.child.SetBounds(bounds)
127125
}
128126

129127
func (w *decorationElement) updateProps(data *Decoration) error {

decoration_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,9 @@ func TestDecorationMinIntrinsicSize(t *testing.T) {
106106

107107
for i, v := range cases {
108108
elem := decorationElement{
109+
child: mock.NewIfNotZero(v.mockSize),
109110
insets: v.insets,
110111
}
111-
if !v.mockSize.IsZero() {
112-
elem.child = mock.New(v.mockSize)
113-
}
114112

115113
if out := elem.MinIntrinsicWidth(base.Inf); out != v.out.Width {
116114
t.Errorf("Case %d: Returned min intrinsic width does not match, got %v, want %v", i, out, v.out.Width)

decoration_windows.go

+13-20
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,10 @@ func (w *Decoration) mount(parent base.Control) (base.Element, error) {
5454
}
5555
win.SetWindowLongPtr(hwnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(retval)))
5656

57-
if w.Child != nil {
58-
child, err := w.Child.Mount(base.Control{hwnd})
59-
if err != nil {
60-
win.DestroyWindow(hwnd)
61-
return nil, err
62-
}
63-
retval.child = child
57+
retval.child, err = base.Mount(base.Control{hwnd}, w.Child)
58+
if err != nil {
59+
win.DestroyWindow(hwnd)
60+
return nil, err
6461
}
6562

6663
return retval, nil
@@ -162,23 +159,19 @@ func (w *decorationElement) SetBounds(bounds base.Rectangle) {
162159
// Update background control position
163160
w.Control.SetBounds(bounds)
164161

165-
if w.child != nil {
166-
px := base.FromPixelsX(1)
167-
py := base.FromPixelsY(1)
168-
position := bounds.Min
169-
bounds.Min.X += px + w.insets.Left - position.X
170-
bounds.Min.Y += py + w.insets.Top - position.Y
171-
bounds.Max.X -= px + w.insets.Right + position.X
172-
bounds.Max.Y -= py + w.insets.Bottom + position.Y
173-
w.child.SetBounds(bounds)
174-
}
162+
px := base.FromPixelsX(1)
163+
py := base.FromPixelsY(1)
164+
position := bounds.Min
165+
bounds.Min.X += px + w.insets.Left - position.X
166+
bounds.Min.Y += py + w.insets.Top - position.Y
167+
bounds.Max.X -= px + w.insets.Right + position.X
168+
bounds.Max.Y -= py + w.insets.Bottom + position.Y
169+
w.child.SetBounds(bounds)
175170
}
176171

177172
func (w *decorationElement) SetOrder(previous win.HWND) win.HWND {
178173
previous = w.Control.SetOrder(previous)
179-
if w.child != nil {
180-
previous = w.child.SetOrder(previous)
181-
}
174+
w.child.SetOrder(0)
182175
return previous
183176
}
184177

expand.go

+2-12
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,19 @@ func (*expandElement) Kind() *base.Kind {
6262
}
6363

6464
func (w *expandElement) Layout(bc base.Constraints) base.Size {
65-
return base.Layout(w.child, bc)
65+
return w.child.Layout(bc)
6666
}
6767

6868
func (w *expandElement) MinIntrinsicHeight(width base.Length) base.Length {
69-
if w.child == nil {
70-
return 0
71-
}
72-
7369
return w.child.MinIntrinsicHeight(width)
7470
}
7571

7672
func (w *expandElement) MinIntrinsicWidth(height base.Length) base.Length {
77-
if w.child == nil {
78-
return 0
79-
}
80-
8173
return w.child.MinIntrinsicWidth(height)
8274
}
8375

8476
func (w *expandElement) SetBounds(bounds base.Rectangle) {
85-
if w.child != nil {
86-
w.child.SetBounds(bounds)
87-
}
77+
w.child.SetBounds(bounds)
8878
}
8979

9080
func (w *expandElement) updateProps(data *Expand) (err error) {

mock/mock.go

+14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ func New(size base.Size) *Element {
1515
}
1616
}
1717

18+
// NewIfNotZero returns a new mock element if the size is not zero, otherwise
19+
// it returns the nil element.
20+
func NewIfNotZero(size base.Size) base.Element {
21+
if size.IsZero() {
22+
elem, err := base.Mount(base.Control{}, nil)
23+
if err != nil {
24+
panic("Mounting a memory only element should never fail")
25+
}
26+
return elem
27+
}
28+
29+
return New(size)
30+
}
31+
1832
// NewList returns a slice of new mock elements.
1933
func NewList(sizes ...base.Size) []base.Element {
2034
ret := make([]base.Element, 0, len(sizes))

padding.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (w *paddingElement) Layout(bc base.Constraints) base.Size {
8383
vinset := w.insets.Top + w.insets.Bottom
8484

8585
innerConstraints := bc.Inset(hinset, vinset)
86-
w.childSize = base.Layout(w.child, innerConstraints)
86+
w.childSize = w.child.Layout(innerConstraints)
8787
return base.Size{
8888
w.childSize.Width + hinset,
8989
w.childSize.Height + vinset,
@@ -92,29 +92,15 @@ func (w *paddingElement) Layout(bc base.Constraints) base.Size {
9292

9393
func (w *paddingElement) MinIntrinsicHeight(width base.Length) base.Length {
9494
vinset := w.insets.Top + w.insets.Bottom
95-
96-
if w.child == nil {
97-
return vinset
98-
}
99-
10095
return w.child.MinIntrinsicHeight(width) + vinset
10196
}
10297

10398
func (w *paddingElement) MinIntrinsicWidth(height base.Length) base.Length {
10499
hinset := w.insets.Left + w.insets.Right
105-
106-
if w.child == nil {
107-
return hinset
108-
}
109-
110100
return w.child.MinIntrinsicWidth(height) + hinset
111101
}
112102

113103
func (w *paddingElement) SetBounds(bounds base.Rectangle) {
114-
if w.child == nil {
115-
return
116-
}
117-
118104
bounds.Min.X += w.insets.Left
119105
bounds.Min.Y += w.insets.Top
120106
bounds.Max.X -= w.insets.Right

padding_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,9 @@ func TestPaddingMinIntrinsicSize(t *testing.T) {
8383

8484
for i, v := range cases {
8585
elem := paddingElement{
86+
child: mock.NewIfNotZero(v.mockSize),
8687
insets: v.insets,
8788
}
88-
if !v.mockSize.IsZero() {
89-
elem.child = mock.New(v.mockSize)
90-
}
9189

9290
if out := elem.MinIntrinsicWidth(base.Inf); out != v.out.Width {
9391
t.Errorf("Case %d: Returned min intrinsic width does not match, got %v, want %v", i, out, v.out.Width)

0 commit comments

Comments
 (0)