/
splits.go
356 lines (326 loc) · 8.28 KB
/
splits.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) 2018, Cogent Core. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package core
import (
"strconv"
"strings"
"cogentcore.org/core/events"
"cogentcore.org/core/math32"
"cogentcore.org/core/states"
"cogentcore.org/core/styles"
"cogentcore.org/core/system"
"cogentcore.org/core/tree"
)
// Config notes: only needs config when number of kids changes
// otherwise just needs new layout
// Splits allocates a certain proportion of its space to each of its children
// along [styles.Style.Direction]. It adds [Handle] widgets to its parts that
// allow the user to customize the amount of space allocated to each child.
type Splits struct { //core:embedder
Layout
// Splits is the proportion (0-1 normalized, enforced) of space
// allocated to each element. 0 indicates that an element should
// be completely collapsed. By default, each element gets the
// same amount of space.
Splits []float32 `set:"-"`
// SavedSplits is a saved version of the splits that can be restored
// for dynamic collapse/expand operations.
SavedSplits []float32 `set:"-"`
}
func (sl *Splits) OnInit() {
sl.WidgetBase.OnInit()
sl.HandleEvents()
sl.SetStyles()
}
func (sl *Splits) SetStyles() {
sl.Style(func(s *styles.Style) {
s.Grow.Set(1, 1)
s.Margin.Zero()
s.Padding.Zero()
s.Min.Y.Em(10)
if sl.SizeClass() == SizeCompact {
s.Direction = styles.Column
} else {
s.Direction = styles.Row
}
})
sl.OnWidgetAdded(func(w Widget) {
if hl, ok := w.(*Handle); ok && w.Parent() == sl.Parts {
hl.OnChange(func(e events.Event) {
sl.SetSplitAction(hl.IndexInParent(), hl.Value())
})
w.Style(func(s *styles.Style) {
s.Direction = sl.Styles.Direction
})
} else if w.Parent() == sl.This() {
// splits elements must scroll independently and grow
w.Style(func(s *styles.Style) {
s.Overflow.Set(styles.OverflowAuto)
s.Grow.Set(1, 1)
})
}
})
}
// SetSplits sets the [Splits.Splits]:
// Splits is the proportion (0-1 normalized, enforced) of space
// allocated to each element. 0 indicates that an element should
// be completely collapsed. By default, each element gets the
// same amount of space.
func (t *Splits) SetSplits(v ...float32) *Splits {
// NOTE: this must be defined manually to avoid naming conflicts
// with embedders of splits downstream
t.Splits = v
return t
}
// UpdateSplits normalizes the splits and ensures that there are as
// many split proportions as children.
func (sl *Splits) UpdateSplits() *Splits {
sz := len(sl.Kids)
if sz == 0 {
return sl
}
if sl.Splits == nil || len(sl.Splits) != sz {
sl.Splits = make([]float32, sz)
}
sum := float32(0)
for _, sp := range sl.Splits {
sum += sp
}
if sum == 0 { // set default even splits
sl.EvenSplits()
sum = 1
} else {
norm := 1 / sum
for i := range sl.Splits {
sl.Splits[i] *= norm
}
}
return sl
}
// EvenSplits splits space evenly across all panels
func (sl *Splits) EvenSplits() {
sz := len(sl.Kids)
if sz == 0 {
return
}
even := 1.0 / float32(sz)
for i := range sl.Splits {
sl.Splits[i] = even
}
sl.NeedsLayout()
}
// SaveSplits saves the current set of splits in SavedSplits, for a later RestoreSplits
func (sl *Splits) SaveSplits() {
sz := len(sl.Splits)
if sz == 0 {
return
}
if sl.SavedSplits == nil || len(sl.SavedSplits) != sz {
sl.SavedSplits = make([]float32, sz)
}
copy(sl.SavedSplits, sl.Splits)
}
// RestoreSplits restores a previously-saved set of splits (if it exists), does an update
func (sl *Splits) RestoreSplits() {
if sl.SavedSplits == nil {
return
}
sl.SetSplits(sl.SavedSplits...).NeedsLayout()
}
// CollapseChild collapses given child(ren) (sets split proportion to 0),
// optionally saving the prior splits for later Restore function -- does an
// Update -- triggered by double-click of splitter
func (sl *Splits) CollapseChild(save bool, idxs ...int) {
if save {
sl.SaveSplits()
}
sz := len(sl.Kids)
for _, idx := range idxs {
if idx >= 0 && idx < sz {
sl.Splits[idx] = 0
}
}
sl.UpdateSplits()
sl.NeedsLayout()
}
// RestoreChild restores given child(ren) -- does an Update
func (sl *Splits) RestoreChild(idxs ...int) {
sz := len(sl.Kids)
for _, idx := range idxs {
if idx >= 0 && idx < sz {
sl.Splits[idx] = 1.0 / float32(sz)
}
}
sl.UpdateSplits()
sl.NeedsLayout()
}
// IsCollapsed returns true if given split number is collapsed
func (sl *Splits) IsCollapsed(idx int) bool {
sz := len(sl.Kids)
if idx >= 0 && idx < sz {
return sl.Splits[idx] < 0.01
}
return false
}
// SetSplitAction sets the new splitter value, for given splitter.
// New value is 0..1 value of position of that splitter.
// It is a sum of all the positions up to that point.
// Splitters are updated to ensure that selected position is achieved,
// while dividing remainder appropriately.
func (sl *Splits) SetSplitAction(idx int, nwval float32) {
sz := len(sl.Splits)
oldsum := float32(0)
for i := 0; i <= idx; i++ {
oldsum += sl.Splits[i]
}
delta := nwval - oldsum
oldval := sl.Splits[idx]
uval := oldval + delta
if uval < 0 {
uval = 0
delta = -oldval
nwval = oldsum + delta
}
rmdr := 1 - nwval
if idx < sz-1 {
oldrmdr := 1 - oldsum
if oldrmdr <= 0 {
if rmdr > 0 {
dper := rmdr / float32((sz-1)-idx)
for i := idx + 1; i < sz; i++ {
sl.Splits[i] = dper
}
}
} else {
for i := idx + 1; i < sz; i++ {
curval := sl.Splits[i]
sl.Splits[i] = rmdr * (curval / oldrmdr) // proportional
}
}
}
sl.Splits[idx] = uval
sl.UpdateSplits()
sl.NeedsLayout()
}
func (sl *Splits) Config() {
sl.UpdateSplits()
sl.ConfigSplitters()
}
func (sl *Splits) ConfigSplitters() {
parts := sl.NewParts()
sz := len(sl.Kids)
if parts.SetNChildren(sz-1, HandleType, "handle-") {
parts.Update()
}
}
func (sl *Splits) HandleEvents() {
sl.OnKeyChord(func(e events.Event) {
kc := string(e.KeyChord())
mod := "Control+"
if TheApp.Platform() == system.MacOS {
mod = "Meta+"
}
if !strings.HasPrefix(kc, mod) {
return
}
kns := kc[len(mod):]
knc, err := strconv.Atoi(kns)
if err != nil {
return
}
kn := int(knc)
if kn == 0 {
e.SetHandled()
sl.EvenSplits()
} else if kn <= len(sl.Kids) {
e.SetHandled()
if sl.Splits[kn-1] <= 0.01 {
sl.RestoreChild(kn - 1)
} else {
sl.CollapseChild(true, kn-1)
}
}
})
}
func (sl *Splits) ApplyStyle() {
sl.UpdateSplits()
sl.ApplyStyleWidget()
sl.ConfigSplitters()
}
func (sl *Splits) SizeDownSetAllocs(iter int) {
sz := &sl.Geom.Size
csz := sz.Alloc.Content
dim := sl.Styles.Direction.Dim()
od := dim.Other()
cszd := csz.Dim(dim)
cszod := csz.Dim(od)
sl.WidgetKidsIter(func(i int, kwi Widget, kwb *WidgetBase) bool {
sw := math32.Round(sl.Splits[i] * cszd)
ksz := &kwb.Geom.Size
ksz.Alloc.Total.SetDim(dim, sw)
ksz.Alloc.Total.SetDim(od, cszod)
ksz.SetContentFromTotal(&ksz.Alloc)
// ksz.Actual = ksz.Alloc
return tree.Continue
})
}
func (sl *Splits) Position() {
if !sl.HasChildren() {
sl.Layout.Position()
return
}
sl.UpdateSplits()
sl.ConfigScrolls()
sl.PositionSplits()
sl.PositionChildren()
}
func (sl *Splits) PositionSplits() {
if sl.NumChildren() <= 1 {
return
}
if sl.Parts != nil {
sl.Parts.Geom.Size = sl.Geom.Size // inherit: allows bbox to include handle
}
dim := sl.Styles.Direction.Dim()
od := dim.Other()
csz := sl.Geom.Size.Alloc.Content // key to use Alloc here! excludes gaps
cszd := csz.Dim(dim)
pos := float32(0)
hand := sl.Parts.Child(0).(*Handle)
hwd := hand.Geom.Size.Actual.Total.Dim(dim)
hht := hand.Geom.Size.Actual.Total.Dim(od)
mid := (csz.Dim(od) - hht) / 2
sl.WidgetKidsIter(func(i int, kwi Widget, kwb *WidgetBase) bool {
kwb.Geom.RelPos.SetZero()
if i == 0 {
return tree.Continue
}
sw := math32.Round(sl.Splits[i-1] * cszd)
pos += sw + hwd
kwb.Geom.RelPos.SetDim(dim, pos)
hl := sl.Parts.Child(i - 1).(*Handle)
hl.Geom.RelPos.SetDim(dim, pos-hwd)
hl.Geom.RelPos.SetDim(od, mid)
hl.Min = 0
hl.Max = cszd
hl.Pos = pos
return tree.Continue
})
}
func (sl *Splits) RenderWidget() {
if sl.PushBounds() {
sl.WidgetKidsIter(func(i int, kwi Widget, kwb *WidgetBase) bool {
sp := sl.Splits[i]
if sp <= 0.01 {
kwb.SetState(true, states.Invisible)
} else {
kwb.SetState(false, states.Invisible)
}
kwi.RenderWidget()
return tree.Continue
})
sl.RenderParts()
sl.PopBounds()
}
}