forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layoutable.go
69 lines (57 loc) · 1.35 KB
/
layoutable.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package parts
import (
"fmt"
"github.com/google/gxui/math"
"github.com/google/gxui/mixins/outer"
)
type LayoutableOuter interface {
outer.Parenter
outer.Redrawer
}
type Layoutable struct {
outer LayoutableOuter
margin math.Spacing
rect math.Rect
relayoutRequested bool
}
func (l *Layoutable) Init(outer LayoutableOuter) {
l.outer = outer
}
func (l *Layoutable) SetMargin(m math.Spacing) {
l.margin = m
if p := l.outer.Parent(); p != nil {
p.Relayout()
}
}
func (l *Layoutable) Margin() math.Spacing {
return l.margin
}
func (l *Layoutable) Bounds() math.Rect {
return l.rect
}
func (l *Layoutable) Layout(rect math.Rect) {
if rect.W() < 0 {
panic(fmt.Errorf("Layout() called with a negative width. Rect: %v", rect))
}
if rect.H() < 0 {
panic(fmt.Errorf("Layout() called with a negative height. Rect: %v", rect))
}
boundsChanged := l.rect != rect
l.rect = rect
if l.relayoutRequested || boundsChanged {
callLayoutChildrenIfSupported(l.outer)
l.outer.Redraw()
l.relayoutRequested = false
}
}
func (l *Layoutable) Relayout() {
if !l.relayoutRequested {
if p := l.outer.Parent(); p != nil {
l.relayoutRequested = true
p.Relayout()
}
}
}