/
layout.go
482 lines (443 loc) · 12.5 KB
/
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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
// 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 (
"fmt"
"log/slog"
"time"
"unicode"
"cogentcore.org/core/abilities"
"cogentcore.org/core/enums"
"cogentcore.org/core/events"
"cogentcore.org/core/keymap"
"cogentcore.org/core/math32"
"cogentcore.org/core/parse/complete"
"cogentcore.org/core/styles"
"cogentcore.org/core/tree"
)
var (
// LayoutPrefMaxRows is maximum number of rows to use in a grid layout
// when computing the preferred size (ScPrefSizing)
LayoutPrefMaxRows = 20
// LayoutPrefMaxCols is maximum number of columns to use in a grid layout
// when computing the preferred size (ScPrefSizing)
LayoutPrefMaxCols = 20
// AutoScrollRate determines the rate of auto-scrolling of layouts
AutoScrollRate = float32(1.0)
)
// Layoutlags has bool flags for Layout
type LayoutFlags WidgetFlags //enums:bitflag -trim-prefix Layout
const (
// for stacked layout, only layout the top widget.
// this is appropriate for e.g., tab layout, which does a full
// redraw on stack changes, but not for e.g., check boxes which don't
LayoutStackTopOnly LayoutFlags = LayoutFlags(WidgetFlagsN) + iota
// true if this layout got a redo = true on previous iteration -- otherwise it just skips any re-layout on subsequent iteration
LayoutNeedsRedo
// LayoutNoKeys prevents processing of keyboard events for this layout.
// By default, Layout handles focus navigation events, but if an
// outer Widget handles these instead, then this should be set.
LayoutNoKeys
)
///////////////////////////////////////////////////////////////////
// Layout
// Layout is the primary node type responsible for organizing the sizes
// and positions of child widgets. It does not render, only organize,
// so properties like background and border will have no effect.
// All arbitrary collections of widgets should generally be contained
// within a layout or a [Frame]; otherwise, the parent widget must take over
// responsibility for positioning. Layouts automatically can add scrollbars
// depending on the [styles.Style.Overflow].
//
// For a [styles.Grid] layout, the [styles.Style.Columns] property should
// generally be set to the desired number of columns, from which the number of rows
// is computed; otherwise, it uses the square root of number of
// elements.
type Layout struct {
WidgetBase
// StackTop, for a [styles.Stacked] layout, is the index of the node to use as the top of the stack.
// Only the node at this index is rendered; if not a valid index, nothing is rendered.
StackTop int `set:"-"`
// LayImpl contains implementation state info for doing layout
LayImpl LayImplState `edit:"-" copier:"-" json:"-" xml:"-" set:"-"`
// whether scrollbar is used for given dim
HasScroll [2]bool `edit:"-" copier:"-" json:"-" xml:"-" set:"-"`
// scroll bars -- we fully manage them as needed
Scrolls [2]*Slider `edit:"-" copier:"-" json:"-" xml:"-" set:"-"`
// accumulated name to search for when keys are typed
FocusName string `edit:"-" copier:"-" json:"-" xml:"-" set:"-"`
// time of last focus name event -- for timeout
FocusNameTime time.Time `edit:"-" copier:"-" json:"-" xml:"-" set:"-"`
// last element focused on -- used as a starting point if name is the same
FocusNameLast tree.Node `edit:"-" copier:"-" json:"-" xml:"-" set:"-"`
}
func (ly *Layout) FlagType() enums.BitFlagSetter {
return (*LayoutFlags)(&ly.Flags)
}
func (ly *Layout) OnInit() {
ly.WidgetBase.OnInit()
ly.SetStyles()
ly.HandleEvents()
}
func (ly *Layout) Config() {
for d := math32.X; d <= math32.Y; d++ {
if ly.HasScroll[d] && ly.Scrolls[d] != nil {
ly.Scrolls[d].ApplyStyle()
}
}
}
func (ly *Layout) SetStyles() {
ly.Style(func(s *styles.Style) {
// we never want borders on layouts
s.MaxBorder = styles.Border{}
switch {
case s.Display == styles.Flex:
if s.Wrap {
s.Grow.Set(1, 0)
} else {
s.Grow.SetDim(s.Direction.Dim(), 1)
s.Grow.SetDim(s.Direction.Dim().Other(), 0)
}
case s.Display == styles.Stacked:
s.Grow.Set(1, 1)
case s.Display == styles.Grid:
s.Grow.Set(1, 1)
}
})
ly.StyleFinal(func(s *styles.Style) {
s.SetAbilities(s.Overflow.X == styles.OverflowAuto || s.Overflow.Y == styles.OverflowAuto, abilities.Scrollable, abilities.Slideable)
})
}
func (ly *Layout) Destroy() {
for d := math32.X; d <= math32.Y; d++ {
ly.DeleteScroll(d)
}
ly.WidgetBase.Destroy()
}
// DeleteScroll deletes scrollbar along given dimesion.
func (ly *Layout) DeleteScroll(d math32.Dims) {
if ly.Scrolls[d] == nil {
return
}
sb := ly.Scrolls[d]
sb.This().Destroy()
ly.Scrolls[d] = nil
}
func (ly *Layout) RenderChildren() {
if ly.Styles.Display == styles.Stacked {
kwi, _ := ly.StackTopWidget()
if kwi != nil {
kwi.RenderWidget()
}
return
}
ly.WidgetKidsIter(func(i int, kwi Widget, kwb *WidgetBase) bool {
kwi.RenderWidget()
return tree.Continue
})
}
func (ly *Layout) Render() {
if ly.PushBounds() {
ly.RenderChildren()
ly.RenderParts()
ly.RenderScrolls()
ly.PopBounds()
}
}
// ChildWithFocus returns a direct child of this layout that either is the
// current window focus item, or contains that focus item (along with its
// index) -- nil, -1 if none.
func (ly *Layout) ChildWithFocus() (Widget, int) {
em := ly.EventMgr()
if em == nil {
return nil, -1
}
var foc Widget
focIndex := -1
ly.WidgetKidsIter(func(i int, kwi Widget, kwb *WidgetBase) bool {
if kwb.ContainsFocus() {
foc = kwi
focIndex = i
return tree.Break
}
return tree.Continue
})
return foc, focIndex
}
// FocusNextChild attempts to move the focus into the next layout child
// (with wraparound to start) -- returns true if successful.
// if updn is true, then for Grid layouts, it moves down to next row
// instead of just the sequentially next item.
func (ly *Layout) FocusNextChild(updn bool) bool {
sz := len(ly.Kids)
if sz <= 1 {
return false
}
foc, idx := ly.ChildWithFocus()
if foc == nil {
fmt.Println("no child foc")
return false
}
em := ly.EventMgr()
if em == nil {
return false
}
cur := em.Focus
nxti := idx + 1
if ly.Styles.Display == styles.Grid && updn {
nxti = idx + ly.Styles.Columns
}
did := false
if nxti < sz {
nx := ly.Child(nxti).(Widget)
did = em.FocusOnOrNext(nx)
} else {
nx := ly.Child(0).(Widget)
did = em.FocusOnOrNext(nx)
}
if !did || em.Focus == cur {
return false
}
return true
}
// FocusPrevChild attempts to move the focus into the previous layout child
// (with wraparound to end) -- returns true if successful.
// If updn is true, then for Grid layouts, it moves up to next row
// instead of just the sequentially next item.
func (ly *Layout) FocusPrevChild(updn bool) bool {
sz := len(ly.Kids)
if sz <= 1 {
return false
}
foc, idx := ly.ChildWithFocus()
if foc == nil {
return false
}
em := ly.EventMgr()
if em == nil {
return false
}
cur := em.Focus
nxti := idx - 1
if ly.Styles.Display == styles.Grid && updn {
nxti = idx - ly.Styles.Columns
}
did := false
if nxti >= 0 {
did = em.FocusOnOrPrev(ly.Child(nxti).(Widget))
} else {
did = em.FocusOnOrPrev(ly.Child(sz - 1).(Widget))
}
if !did || em.Focus == cur {
return false
}
return true
}
func (ly *Layout) HandleEvents() {
ly.WidgetBase.HandleEvents()
ly.HandleKeys()
ly.On(events.Scroll, func(e events.Event) {
ly.ScrollDelta(e)
})
// we treat slide events on layouts as scroll events
// we must reverse the delta for "natural" scrolling behavior
ly.On(events.SlideMove, func(e events.Event) {
del := math32.Vector2FromPoint(e.PrevDelta()).MulScalar(-0.1)
ly.ScrollDelta(events.NewScroll(e.WindowPos(), del, e.Modifiers()))
})
}
// HandleKeys handles all key events for navigating focus within a Layout.
// Typically this is done by the parent Scene level layout, but can be
// done by default if FocusWithinable Ability is set.
func (ly *Layout) HandleKeys() {
ly.OnFinal(events.KeyChord, func(e events.Event) {
if ly.Is(LayoutNoKeys) {
return
}
kf := keymap.Of(e.KeyChord())
if DebugSettings.KeyEventTrace {
slog.Info("Layout KeyInput", "widget", ly, "keyFunction", kf)
}
if kf == keymap.Abort {
if ly.Scene.Stage.ClosePopupAndBelow() {
e.SetHandled()
}
return
}
em := ly.EventMgr()
if em == nil {
return
}
grid := ly.Styles.Display == styles.Grid
if ly.Styles.Direction == styles.Row || grid {
switch kf {
case keymap.MoveRight:
if ly.FocusNextChild(false) {
e.SetHandled()
}
return
case keymap.MoveLeft:
if ly.FocusPrevChild(false) {
e.SetHandled()
}
return
}
}
if ly.Styles.Direction == styles.Column || grid {
switch kf {
case keymap.MoveDown:
if ly.FocusNextChild(true) {
e.SetHandled()
}
return
case keymap.MoveUp:
if ly.FocusPrevChild(true) {
e.SetHandled()
}
return
case keymap.PageDown:
proc := false
for st := 0; st < SystemSettings.LayoutPageSteps; st++ {
if !ly.FocusNextChild(true) {
break
}
proc = true
}
if proc {
e.SetHandled()
}
return
case keymap.PageUp:
proc := false
for st := 0; st < SystemSettings.LayoutPageSteps; st++ {
if !ly.FocusPrevChild(true) {
break
}
proc = true
}
if proc {
e.SetHandled()
}
return
}
}
ly.FocusOnName(e)
})
}
// FocusOnName processes key events to look for an element starting with given name
func (ly *Layout) FocusOnName(e events.Event) bool {
kf := keymap.Of(e.KeyChord())
if DebugSettings.KeyEventTrace {
slog.Info("Layout FocusOnName", "widget", ly, "keyFunction", kf)
}
delay := e.Time().Sub(ly.FocusNameTime)
ly.FocusNameTime = e.Time()
if kf == keymap.FocusNext { // tab means go to next match -- don't worry about time
if ly.FocusName == "" || delay > SystemSettings.LayoutFocusNameTabTime {
ly.FocusName = ""
ly.FocusNameLast = nil
return false
}
} else {
if delay > SystemSettings.LayoutFocusNameTimeout {
ly.FocusName = ""
}
if !unicode.IsPrint(e.KeyRune()) || e.Modifiers() != 0 {
return false
}
sr := string(e.KeyRune())
if ly.FocusName == sr {
// re-search same letter
} else {
ly.FocusName += sr
ly.FocusNameLast = nil // only use last if tabbing
}
}
// e.SetHandled()
// fmt.Printf("searching for: %v last: %v\n", ly.FocusName, ly.FocusNameLast)
focel := ChildByLabelCanFocus(ly, ly.FocusName, ly.FocusNameLast)
if focel != nil {
focel = focel.This()
em := ly.EventMgr()
if em != nil {
em.SetFocusEvent(focel.(Widget)) // this will also scroll by default!
}
ly.FocusNameLast = focel
return true
} else {
if ly.FocusNameLast == nil {
ly.FocusName = "" // nothing being found
}
ly.FocusNameLast = nil // start over
}
return false
}
// ChildByLabelCanFocus uses breadth-first search to find
// the first focusable element within the layout whose Label (using
// [ToLabel]) matches the given name using [complete.IsSeedMatching].
// If after is non-nil, it only finds after that element.
func ChildByLabelCanFocus(ly *Layout, name string, after tree.Node) tree.Node {
gotAfter := false
completions := []complete.Completion{}
ly.WalkDownBreadth(func(k tree.Node) bool {
if k == ly.This() { // skip us
return tree.Continue
}
_, ni := AsWidget(k)
if ni == nil || !ni.CanFocus() { // don't go any further
return tree.Continue
}
if after != nil && !gotAfter {
if k == after {
gotAfter = true
}
return tree.Continue // skip to next
}
completions = append(completions, complete.Completion{
Text: ToLabel(k),
Desc: k.PathFrom(ly),
})
return tree.Continue
})
matches := complete.MatchSeedCompletion(completions, name)
if len(matches) > 0 {
return ly.FindPath(matches[0].Desc)
}
return nil
}
///////////////////////////////////////////////////////////
// Stretch and Space: spacing elements for layouts
// Stretch adds a stretchy element that grows to fill all
// available space. You can set [styles.Style.Grow] to change
// how much it grows relative to other growing elements.
type Stretch struct {
WidgetBase
}
func (st *Stretch) OnInit() {
st.WidgetBase.SetStyles()
// note: not getting base events
st.Style(func(s *styles.Style) {
s.Min.X.Ch(1)
s.Min.Y.Em(1)
s.Grow.Set(1, 1)
})
}
// Space is a fixed size blank space, with
// a default width of 1ch and a height of 1em.
// You can set [styles.Style.Min] to change its size.
type Space struct {
WidgetBase
}
func (sp *Space) OnInit() {
sp.WidgetBase.SetStyles()
// note: not getting base events
sp.Style(func(s *styles.Style) {
s.Min.X.Ch(1)
s.Min.Y.Em(1)
s.Padding.Zero()
s.Margin.Zero()
s.MaxBorder.Width.Zero()
s.Border.Width.Zero()
})
}