forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter_layout.go
154 lines (130 loc) · 3.78 KB
/
splitter_layout.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
// 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/base"
)
type SplitterLayoutOuter interface {
base.ContainerOuter
CreateSplitterBar() gxui.Control
}
type SplitterLayout struct {
base.Container
outer SplitterLayoutOuter
theme gxui.Theme
orientation gxui.Orientation
splitterWidth int
weights map[gxui.Control]float32
}
func (l *SplitterLayout) Init(outer SplitterLayoutOuter, theme gxui.Theme) {
l.Container.Init(outer, theme)
l.outer = outer
l.theme = theme
l.weights = make(map[gxui.Control]float32)
l.splitterWidth = 4
l.SetMouseEventTarget(true)
// Interface compliance test
_ = gxui.SplitterLayout(l)
}
func (l *SplitterLayout) LayoutChildren() {
s := l.outer.Bounds().Size().Contract(l.Padding())
o := l.Padding().LT()
children := l.outer.Children()
splitterCount := len(children) / 2
splitterWidth := l.splitterWidth
if l.orientation.Horizontal() {
s.W -= splitterWidth * splitterCount
} else {
s.H -= splitterWidth * splitterCount
}
netWeight := float32(0.0)
for i, c := range children {
if isSplitter := (i & 1) == 1; !isSplitter {
netWeight += l.weights[c]
}
}
d := 0
for i, c := range children {
var cr math.Rect
if isSplitter := (i & 1) == 1; !isSplitter {
cm := c.Margin()
frac := l.weights[c] / netWeight
if l.orientation.Horizontal() {
cw := int(float32(s.W) * frac)
cr = math.CreateRect(d+cm.L, cm.T, d+cw-cm.R, s.H-cm.B)
d += cw
} else {
ch := int(float32(s.H) * frac)
cr = math.CreateRect(cm.L, d+cm.T, s.W-cm.R, d+ch-cm.B)
d += ch
}
} else {
if l.orientation.Horizontal() {
cr = math.CreateRect(d, 0, d+splitterWidth, s.H)
} else {
cr = math.CreateRect(0, d, s.W, d+splitterWidth)
}
d += splitterWidth
}
c.Layout(cr.Offset(o).Canon())
}
}
func (l *SplitterLayout) ChildWeight(child gxui.Control) float32 {
return l.weights[child]
}
func (l *SplitterLayout) SetChildWeight(child gxui.Control, weight float32) {
if l.weights[child] != weight {
l.weights[child] = weight
l.LayoutChildren()
}
}
func (l *SplitterLayout) DesiredSize(min, max math.Size) math.Size {
return max
}
func (l *SplitterLayout) Orientation() gxui.Orientation {
return l.orientation
}
func (l *SplitterLayout) SetOrientation(o gxui.Orientation) {
if l.orientation != o {
l.orientation = o
l.LayoutChildren()
}
}
func (l *SplitterLayout) CreateSplitterBar() gxui.Control {
b := &SplitterBar{}
b.Init(b, l.theme)
b.OnSplitterDragged(func(wndPnt math.Point) { l.SplitterDragged(b, wndPnt) })
return b
}
func (l *SplitterLayout) SplitterDragged(splitter gxui.Control, wndPnt math.Point) {
o := l.orientation
p := gxui.WindowToChild(wndPnt, l)
splitterIndex := l.ChildIndex(splitter)
childA, childB := l.ChildAt(splitterIndex-1), l.ChildAt(splitterIndex+1)
boundsA, boundsB := childA.Bounds(), childB.Bounds()
min, max := o.Major(boundsA.Min.XY()), o.Major(boundsB.Max.XY())
frac := math.RampSat(float32(o.Major(p.XY())), float32(min), float32(max))
netWeight := l.weights[childA] + l.weights[childB]
l.weights[childA] = netWeight * frac
l.weights[childB] = netWeight * (1.0 - frac)
l.LayoutChildren()
}
// parts.Container overrides
func (l *SplitterLayout) AddChildAt(index int, child gxui.Control) {
l.weights[child] = 1.0
if l.Container.ChildCount() > 0 {
l.Container.AddChildAt(index, l.outer.CreateSplitterBar())
index++
}
l.Container.AddChildAt(index, child)
}
func (l *SplitterLayout) RemoveChildAt(index int) {
if l.Container.ChildCount() > 1 {
l.Container.RemoveChildAt(index + 1)
}
delete(l.weights, l.ChildAt(index))
l.Container.RemoveChildAt(index)
}