forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
121 lines (109 loc) · 2.75 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
// 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
CreateExpandButton(theme gxui.Theme, node *TreeInternalNode) gxui.Button
PaintUnexpandedSelection(c gxui.Canvas, r math.Rect)
}
type Tree struct {
List
parts.Focusable
outer TreeOuter
adapterInner gxui.TreeAdapter
adapterOuter *TreeToListAdapter
}
func (t *Tree) Init(outer TreeOuter, theme gxui.Theme) {
t.List.Init(outer, theme)
t.Focusable.Init(outer)
t.outer = outer
// Interface compliance test
_ = gxui.Tree(t)
}
// gxui.Tree complaince
func (t *Tree) SetAdapter(adapter gxui.TreeAdapter) {
if t.adapterInner == adapter {
return
}
if adapter != nil {
t.adapterInner = adapter
t.adapterOuter = CreateTreeToListAdapter(adapter, t.outer.CreateExpandButton)
t.List.SetAdapter(t.adapterOuter)
} else {
t.adapterOuter = nil
t.adapterInner = nil
t.List.SetAdapter(nil)
}
}
func (t *Tree) Adapter() gxui.TreeAdapter {
return t.adapterInner
}
func (t *Tree) CreateExpandButton(theme gxui.Theme, node *TreeInternalNode) gxui.Button {
btn := theme.CreateButton()
btn.SetMargin(math.Spacing{L: 2, R: 2, T: 1, B: 1})
btn.OnClick(func(ev gxui.MouseEvent) {
if ev.Button == gxui.MouseButtonLeft {
if node.IsExpanded() {
node.Collapse()
} else {
node.Expand()
}
}
})
node.OnExpandedChanged(func(e bool) {
if e {
btn.SetText("-")
} else {
btn.SetText("+")
}
})
if node.IsExpanded() {
btn.SetText("-")
} else {
btn.SetText("+")
}
return btn
}
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.Control, idx int) {
t.List.PaintChild(c, child, idx)
if t.selectedId != gxui.InvalidAdapterItemId {
id := t.adapterOuter.DeepestVisibleAncestor(t.selectedId)
if id != t.selectedId {
// The selected item is hidden by an unexpanded node.
// Highlight the deepest visible node instead.
if item, found := t.items[id]; found {
if child == item.Control {
b := child.Bounds().Expand(child.Margin())
t.outer.PaintUnexpandedSelection(c, b)
}
}
}
}
}
// InputEventHandler override
func (t *Tree) KeyPress(ev gxui.KeyboardEvent) (consume bool) {
id := t.Selected()
switch ev.Key {
case gxui.KeyLeft:
newId := t.adapterOuter.Collapse(id)
if newId != id {
t.Select(newId)
return true
}
case gxui.KeyRight:
if t.adapterOuter.Expand(id) {
return true
}
}
return t.List.KeyPress(ev)
}