forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
165 lines (142 loc) · 3.86 KB
/
tree.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 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 mixins
import (
"github.com/google/gxui"
"github.com/google/gxui/math"
"github.com/google/gxui/mixins/parts"
)
type TreeOuter interface {
ListOuter
PaintUnexpandedSelection(c gxui.Canvas, r math.Rect)
}
type Tree struct {
List
parts.Focusable
outer TreeOuter
treeAdapter gxui.TreeAdapter
listAdapter *TreeToListAdapter
creator TreeControlCreator
}
func (t *Tree) Init(outer TreeOuter, theme gxui.Theme) {
t.List.Init(outer, theme)
t.Focusable.Init(outer)
t.outer = outer
t.creator = defaultTreeControlCreator{}
// Interface compliance test
_ = gxui.Tree(t)
}
func (t *Tree) SetControlCreator(c TreeControlCreator) {
t.creator = c
if t.treeAdapter != nil {
t.listAdapter = CreateTreeToListAdapter(t.treeAdapter, t.creator)
t.DataReplaced()
}
}
// gxui.Tree complaince
func (t *Tree) SetAdapter(adapter gxui.TreeAdapter) {
if t.treeAdapter == adapter {
return
}
if adapter != nil {
t.treeAdapter = adapter
t.listAdapter = CreateTreeToListAdapter(adapter, t.creator)
t.List.SetAdapter(t.listAdapter)
} else {
t.listAdapter = nil
t.treeAdapter = nil
t.List.SetAdapter(nil)
}
}
func (t *Tree) Adapter() gxui.TreeAdapter {
return t.treeAdapter
}
func (t *Tree) Show(item gxui.AdapterItem) {
t.listAdapter.ExpandItem(item)
t.List.ScrollTo(item)
}
func (t *Tree) ContainsItem(item gxui.AdapterItem) bool {
return t.listAdapter != nil && t.listAdapter.Contains(item)
}
func (t *Tree) ExpandAll() {
t.listAdapter.ExpandAll()
}
func (t *Tree) CollapseAll() {
t.listAdapter.CollapseAll()
}
func (t *Tree) PaintUnexpandedSelection(c gxui.Canvas, r math.Rect) {
c.DrawRoundedRect(r, 2.0, 2.0, 2.0, 2.0, gxui.CreatePen(1, gxui.Gray50), gxui.TransparentBrush)
}
// List override
func (t *Tree) PaintChild(c gxui.Canvas, child *gxui.Child, idx int) {
t.List.PaintChild(c, child, idx)
if t.selectedItem != nil {
if deepest := t.listAdapter.DeepestNode(t.selectedItem); deepest != nil {
if item := deepest.Item(); item != t.selectedItem {
// The selected item is hidden by an unexpanded node.
// Highlight the deepest visible node instead.
if details, found := t.details[item]; found {
if child == details.child {
b := child.Bounds().Expand(child.Control.Margin())
t.outer.PaintUnexpandedSelection(c, b)
}
}
}
}
}
}
// InputEventHandler override
func (t *Tree) KeyPress(ev gxui.KeyboardEvent) (consume bool) {
switch ev.Key {
case gxui.KeyLeft:
if item := t.Selected(); item != nil {
node := t.listAdapter.DeepestNode(item)
if node.Collapse() {
return true
}
if p := node.Parent(); p != nil {
return t.Select(p.Item())
}
}
case gxui.KeyRight:
if item := t.Selected(); item != nil {
node := t.listAdapter.DeepestNode(item)
if node.Expand() {
return true
}
}
}
return t.List.KeyPress(ev)
}
type defaultTreeControlCreator struct{}
func (defaultTreeControlCreator) Create(theme gxui.Theme, control gxui.Control, node *TreeToListNode) gxui.Control {
ll := theme.CreateLinearLayout()
ll.SetDirection(gxui.LeftToRight)
btn := theme.CreateButton()
btn.SetBackgroundBrush(gxui.TransparentBrush)
btn.SetBorderPen(gxui.CreatePen(1, gxui.Gray30))
btn.SetMargin(math.Spacing{L: 2, R: 2, T: 1, B: 1})
btn.OnClick(func(ev gxui.MouseEvent) {
if ev.Button == gxui.MouseButtonLeft {
node.ToggleExpanded()
}
})
update := func() {
btn.SetVisible(!node.IsLeaf())
if node.IsExpanded() {
btn.SetText("-")
} else {
btn.SetText("+")
}
}
update()
gxui.WhileAttached(btn, node.OnChange, update)
ll.AddChild(btn)
ll.AddChild(control)
ll.SetPadding(math.Spacing{L: 16 * node.Depth()})
return ll
}
func (defaultTreeControlCreator) Size(theme gxui.Theme, treeControlSize math.Size) math.Size {
return treeControlSize
}