forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
143 lines (123 loc) · 3.21 KB
/
container.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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"
"github.com/google/gxui/math"
"github.com/google/gxui/mixins/outer"
)
type ContainerOuter interface {
gxui.Container
outer.Attachable
outer.IsVisibler
outer.LayoutChildren
outer.Parenter
outer.Sized
}
type Container struct {
outer ContainerOuter
children gxui.Children
isMouseEventTarget bool
relayoutSuspended bool
}
func (c *Container) Init(outer ContainerOuter) {
c.outer = outer
c.children = gxui.Children{}
outer.OnAttach(func() {
for _, v := range c.children {
v.Control.Attach()
}
})
outer.OnDetach(func() {
for _, v := range c.children {
v.Control.Detach()
}
})
}
func (c *Container) SetMouseEventTarget(mouseEventTarget bool) {
c.isMouseEventTarget = mouseEventTarget
}
func (c *Container) IsMouseEventTarget() bool {
return c.isMouseEventTarget
}
// RelayoutSuspended returns true if adding or removing a child Control to this
// Container will not trigger a relayout of this Container. The default is false
// where any mutation will trigger a relayout.
func (c *Container) RelayoutSuspended() bool {
return c.relayoutSuspended
}
// SetRelayoutSuspended enables or disables relayout of the Container on
// adding or removing a child Control to this Container.
func (c *Container) SetRelayoutSuspended(enable bool) {
c.relayoutSuspended = true
}
// gxui.Parent compliance
func (c *Container) Children() gxui.Children {
return c.children
}
// gxui.Container compliance
func (c *Container) AddChild(control gxui.Control) *gxui.Child {
return c.outer.AddChildAt(len(c.children), control)
}
func (c *Container) AddChildAt(index int, control gxui.Control) *gxui.Child {
if control.Parent() != nil {
panic("Child already has a parent")
}
if index < 0 || index > len(c.children) {
panic(fmt.Errorf("Index %d is out of bounds. Acceptable range: [%d - %d]",
index, 0, len(c.children)))
}
child := &gxui.Child{Control: control}
c.children = append(c.children, nil)
copy(c.children[index+1:], c.children[index:])
c.children[index] = child
control.SetParent(c.outer)
if c.outer.Attached() {
control.Attach()
}
if !c.relayoutSuspended {
c.outer.Relayout()
}
return child
}
func (c *Container) RemoveChild(control gxui.Control) {
for i := range c.children {
if c.children[i].Control == control {
c.outer.RemoveChildAt(i)
return
}
}
panic("Child not part of container")
}
func (c *Container) RemoveChildAt(index int) {
child := c.children[index]
c.children = append(c.children[:index], c.children[index+1:]...)
child.Control.SetParent(nil)
if c.outer.Attached() {
child.Control.Detach()
}
if !c.relayoutSuspended {
c.outer.Relayout()
}
}
func (c *Container) RemoveAll() {
for i := len(c.children) - 1; i >= 0; i-- {
c.outer.RemoveChildAt(i)
}
}
func (c *Container) ContainsPoint(p math.Point) bool {
if !c.outer.IsVisible() || !c.outer.Size().Rect().Contains(p) {
return false
}
for _, v := range c.children {
if v.Control.ContainsPoint(p.Sub(v.Offset)) {
return true
}
}
if c.IsMouseEventTarget() {
return true
}
return false
}