File: columns.go

package info (click to toggle)
golang-github-gcla-gowid 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,456 kB
  • sloc: makefile: 4
file content (749 lines) | stat: -rw-r--r-- 21,139 bytes parent folder | download
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// Copyright 2019-2022 Graham Clark. All rights reserved.  Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.

// Package columns provides a widget for organizing other widgets in columns.
package columns

import (
	"fmt"
	"strings"

	"github.com/gcla/gowid"
	"github.com/gcla/gowid/gwutil"
	"github.com/gcla/gowid/vim"
	"github.com/gcla/gowid/widgets/fill"
	tcell "github.com/gdamore/tcell/v2"
)

//======================================================================

type IWidget interface {
	gowid.ICompositeMultipleWidget
	gowid.ISettableDimensions
	gowid.ISettableSubWidgets
	gowid.IFindNextSelectable
	gowid.IPreferedPosition
	gowid.ISelectChild
	gowid.IIdentity
	WidgetWidths(size gowid.IRenderSize, focus gowid.Selector, focusIdx int, app gowid.IApp) []int
	Wrap() bool
	KeyIsLeft(*tcell.EventKey) bool
	KeyIsRight(*tcell.EventKey) bool
}

type Widget struct {
	widgets      []gowid.IContainerWidget
	focus        int    // -1 means nothing selectable
	prefCol      int    // caches the last set prefered col. Passes it on if widget hasn't changed focus
	widthHelper  []bool // optimizations to save frequent array allocations during use
	widthHelper2 []bool
	opt          Options
	*gowid.Callbacks
	gowid.AddressProvidesID
	gowid.SubWidgetsCallbacks
	gowid.FocusCallbacks
}

type Options struct {
	StartColumn      int  // column that gets initial focus
	Wrap             bool // whether or not to wrap from last column to first with movement operations
	DoNotSetSelected bool // Whether or not to set the focus.Selected field for the selected child
	LeftKeys         []vim.KeyPress
	RightKeys        []vim.KeyPress
}

func New(widgets []gowid.IContainerWidget, opts ...Options) *Widget {
	var opt Options
	if len(opts) > 0 {
		opt = opts[0]
	} else {
		opt = Options{
			StartColumn: -1,
		}
	}
	if opt.LeftKeys == nil {
		opt.LeftKeys = vim.AllLeftKeys
	}
	if opt.RightKeys == nil {
		opt.RightKeys = vim.AllRightKeys
	}
	res := &Widget{
		widgets:      widgets,
		focus:        -1,
		prefCol:      -1,
		widthHelper:  make([]bool, len(widgets)),
		widthHelper2: make([]bool, len(widgets)),
		opt:          opt,
	}
	res.SubWidgetsCallbacks = gowid.SubWidgetsCallbacks{CB: &res.Callbacks}
	res.FocusCallbacks = gowid.FocusCallbacks{CB: &res.Callbacks}

	if opt.StartColumn >= 0 {
		res.focus = gwutil.Min(opt.StartColumn, len(widgets)-1)
	} else {
		res.focus, _ = res.FindNextSelectable(1, res.Wrap())
	}

	var _ gowid.IWidget = res
	var _ IWidget = res
	var _ gowid.ICompositeMultipleDimensions = res
	var _ gowid.ICompositeMultipleWidget = res

	return res
}

//func Simple(ws ...gowid.IWidget) *Widget {
func NewFlow(ws ...interface{}) *Widget {
	return NewWithDim(gowid.RenderFlow{}, ws...)
}

func NewFixed(ws ...interface{}) *Widget {
	return NewWithDim(gowid.RenderFixed{}, ws...)
}

func NewWithDim(method gowid.IWidgetDimension, ws ...interface{}) *Widget {
	cws := make([]gowid.IContainerWidget, len(ws))
	for i := 0; i < len(ws); i++ {
		if cw, ok := ws[i].(gowid.IContainerWidget); ok {
			cws[i] = cw
		} else {
			cws[i] = &gowid.ContainerWidget{
				IWidget: ws[i].(gowid.IWidget),
				D:       method,
			}
		}
	}
	return New(cws)
}

func (w *Widget) SelectChild(f gowid.Selector) bool {
	return !w.opt.DoNotSetSelected && f.Selected
}

func (w *Widget) String() string {
	cols := make([]string, len(w.widgets))
	for i := 0; i < len(cols); i++ {
		cols[i] = fmt.Sprintf("%v", w.widgets[i])
	}
	return fmt.Sprintf("columns[%s]", strings.Join(cols, ","))
}

func (w *Widget) SetFocus(app gowid.IApp, i int) {
	old := w.focus
	w.focus = gwutil.Min(gwutil.Max(i, 0), len(w.widgets)-1)
	w.prefCol = -1 // moved, so pass on real focus from now on
	if old != w.focus {
		gowid.RunWidgetCallbacks(w.Callbacks, gowid.FocusCB{}, app, w)
	}
}

func (w *Widget) Wrap() bool {
	return w.opt.Wrap
}

func (w *Widget) Focus() int {
	return w.focus
}

func (w *Widget) SubWidgets() []gowid.IWidget {
	res := make([]gowid.IWidget, len(w.widgets))
	for i, iw := range w.widgets {
		res[i] = iw
	}
	return res
}

func (w *Widget) SetSubWidgets(widgets []gowid.IWidget, app gowid.IApp) {
	ws := make([]gowid.IContainerWidget, len(widgets))
	for i, iw := range widgets {
		if iwc, ok := iw.(gowid.IContainerWidget); ok {
			ws[i] = iwc
		} else {
			ws[i] = &gowid.ContainerWidget{IWidget: iw, D: gowid.RenderFlow{}}
		}
	}
	w.widthHelper = make([]bool, len(widgets))
	w.widthHelper2 = make([]bool, len(widgets))
	oldFocus := w.Focus()
	w.widgets = ws
	w.SetFocus(app, oldFocus)
	gowid.RunWidgetCallbacks(w.Callbacks, gowid.SubWidgetsCB{}, app, w)
}

func (w *Widget) Dimensions() []gowid.IWidgetDimension {
	res := make([]gowid.IWidgetDimension, len(w.widgets))
	for i, iw := range w.widgets {
		res[i] = iw.Dimension()
	}
	return res
}

func (w *Widget) SetDimensions(dimensions []gowid.IWidgetDimension, app gowid.IApp) {
	for i, id := range dimensions {
		w.widgets[i].SetDimension(id)
	}
	gowid.RunWidgetCallbacks(w.Callbacks, gowid.DimensionsCB{}, app, w)
}

func (w *Widget) Selectable() bool {
	return gowid.SelectableIfAnySubWidgetsAre(w)
}

func (w *Widget) FindNextSelectable(dir gowid.Direction, wrap bool) (int, bool) {
	return gowid.FindNextSelectableFrom(w, w.Focus(), dir, wrap)
}

func (w *Widget) UserInput(ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	return UserInput(w, ev, size, focus, app)
}

// RenderSize computes the size of this widget when it renders. This is
// done by computing the sizes of each subwidget, then arranging them the
// same way that Render() does.
//
func (w *Widget) RenderSize(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	return RenderSize(w, size, focus, app)
}

func (w *Widget) Render(size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	return Render(w, size, focus, app)
}

func (w *Widget) RenderSubWidgets(size gowid.IRenderSize, focus gowid.Selector, focusIdx int, app gowid.IApp) []gowid.ICanvas {
	return RenderSubWidgets(w, size, focus, focusIdx, app)
}

func (w *Widget) RenderedSubWidgetsSizes(size gowid.IRenderSize, focus gowid.Selector, focusIdx int, app gowid.IApp) []gowid.IRenderBox {
	return RenderedSubWidgetsSizes(w, size, focus, focusIdx, app)
}

// Return a slice of ints representing the width in columns for each of the subwidgets to be rendered
// in this context given the size argument.
//
func (w *Widget) WidgetWidths(size gowid.IRenderSize, focus gowid.Selector, focusIdx int, app gowid.IApp) []int {
	return WidgetWidths(w, size, focus, focusIdx, app)
}

// Construct the context in which each subwidget will be rendered. It's important to
// preserve the type of context e.g. a subwidget may only support being rendered in a
// fixed context. The newX parameter is the width the subwidget will have within the
// context of the Columns widget.
//
func (w *Widget) SubWidgetSize(size gowid.IRenderSize, newX int, sub gowid.IWidget, dim gowid.IWidgetDimension) gowid.IRenderSize {
	return SubWidgetSize(size, newX, dim)
}

func (w *Widget) GetPreferedPosition() gwutil.IntOption {
	f := w.prefCol
	if f == -1 {
		f = w.Focus()
	}
	if f == -1 {
		return gwutil.NoneInt()
	} else {
		return gwutil.SomeInt(f)
	}
}

func (w *Widget) SetPreferedPosition(cols int, app gowid.IApp) {
	col := gwutil.Min(gwutil.Max(cols, 0), len(w.widgets)-1)
	pref := col
	colLeft := col - 1
	colRight := col
	for colLeft >= 0 || colRight < len(w.widgets) {
		if colRight < len(w.widgets) && w.widgets[colRight].Selectable() {
			w.SetFocus(app, colRight)
			break
		} else {
			colRight++
		}
		if colLeft >= 0 && w.widgets[colLeft].Selectable() {
			w.SetFocus(app, colLeft)
			break
		} else {
			colLeft--
		}
	}
	w.prefCol = pref // Save it. Pass it on if widget doesn't change col before losing focus.
}

func (w *Widget) KeyIsLeft(evk *tcell.EventKey) bool {
	return vim.KeyIn(evk, w.opt.LeftKeys)
}

func (w *Widget) KeyIsRight(evk *tcell.EventKey) bool {
	return vim.KeyIn(evk, w.opt.RightKeys)
}

type IWidthHelper interface {
	WidthHelpers() ([]bool, []bool)
}

var _ IWidthHelper = (*Widget)(nil)

func (w *Widget) WidthHelpers() ([]bool, []bool) {
	return w.widthHelper, w.widthHelper2
}

//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

func SubWidgetSize(size gowid.IRenderSize, newX int, dim gowid.IWidgetDimension) gowid.IRenderSize {
	var subSize gowid.IRenderSize

	switch sz := size.(type) {
	case gowid.IRenderFixed:
		switch dim.(type) {
		case gowid.IRenderBox:
			subSize = dim
		default:
			subSize = gowid.RenderFixed{}
		}
	case gowid.IRenderBox:
		switch dim.(type) {
		case gowid.IRenderFixed:
			subSize = gowid.RenderFixed{}
		case gowid.IRenderFlow:
			subSize = gowid.RenderFlowWith{C: newX}
		case gowid.IRenderWithUnits, gowid.IRenderWithWeight:
			subSize = gowid.RenderBox{C: newX, R: sz.BoxRows()}
		default:
			subSize = gowid.RenderBox{C: newX, R: sz.BoxRows()}
		}
	case gowid.IRenderFlowWith:
		switch dim.(type) {
		case gowid.IRenderFixed:
			subSize = gowid.RenderFixed{}
		case gowid.IRenderFlow, gowid.IRenderWithUnits, gowid.IRenderWithWeight, gowid.IRenderRelative:
			// The newX argument is already computed to be the right number of cols for the subwidget
			subSize = gowid.RenderFlowWith{C: newX}
		default:
			panic(gowid.DimensionError{Size: size, Dim: dim})
		}
	default:
		panic(gowid.DimensionError{Size: size, Dim: dim})
	}
	return subSize
}

func UserInput(w IWidget, ev interface{}, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) bool {
	res := false
	subfocus := w.Focus()

	subSizes := w.WidgetWidths(size, focus, subfocus, app)

	dims := w.Dimensions()
	subs := w.SubWidgets()

	forChild := false

	if subfocus != -1 {
		res = true
		if evm, ok := ev.(*tcell.EventMouse); ok {
			curX := 0
			mx, _ := evm.Position()
		Loop:
			for i, c := range subSizes {
				if mx < curX+c && mx >= curX {
					subSize := w.SubWidgetSize(size, c, subs[i], dims[i])
					forChild = subs[i].UserInput(gowid.TranslatedMouseEvent(ev, -curX, 0), subSize, focus.SelectIf(w.SelectChild(focus) && i == subfocus), app)

					// Give the child focus if (a) it's selectable, and (b) if this is the click up corresponding
					// to a previous click down on this columns widget.
					switch evm.Buttons() {
					case tcell.Button1, tcell.Button2, tcell.Button3:
						app.SetClickTarget(evm.Buttons(), w)
					case tcell.ButtonNone:
						if !app.GetLastMouseState().NoButtonClicked() {
							if subs[i].Selectable() {
								clickit := false
								app.ClickTarget(func(k tcell.ButtonMask, v gowid.IIdentityWidget) {
									if v != nil && v.ID() == w.ID() {
										clickit = true
									}
								})
								if clickit {
									w.SetFocus(app, i)
								}
							}
						}
						break Loop
					}
					break
				}
				curX += c
			}
		} else {
			subC := subSizes[subfocus] // guaranteed to be a box
			subSize := w.SubWidgetSize(size, subC, subs[subfocus], dims[subfocus])
			forChild = gowid.UserInputIfSelectable(subs[w.Focus()], ev, subSize, focus, app)
		}
	}

	if !forChild && w.Focus() != -1 {
		res = false
		if evk, ok := ev.(*tcell.EventKey); ok {

			curw := subs[w.Focus()]
			prefPos := gowid.PrefPosition(curw)

			switch {
			case w.KeyIsRight(evk):
				res = Scroll(w, 1, w.Wrap(), app)
			case w.KeyIsLeft(evk):
				res = Scroll(w, -1, w.Wrap(), app)
			}

			if !prefPos.IsNone() {
				// New focus widget
				curw = subs[w.Focus()]
				gowid.SetPrefPosition(curw, prefPos.Val(), app)
			}

		}
	}

	return res
}

type IFocusSelectable interface {
	gowid.IFocus
	gowid.IFindNextSelectable
}

func Scroll(w IFocusSelectable, dir gowid.Direction, wrap bool, app gowid.IApp) bool {
	res := false
	next, ok := w.FindNextSelectable(dir, wrap)
	if ok {
		w.SetFocus(app, next)
		res = true
	}
	return res
}

type ICompositeMultipleDimensionsExt interface {
	gowid.ICompositeMultipleDimensions
	gowid.ISelectChild
}

func WidgetWidths(w ICompositeMultipleDimensionsExt, size gowid.IRenderSize, focus gowid.Selector, focusIdx int, app gowid.IApp) []int {
	return widgetWidthsExt(w, w.SubWidgets(), w.Dimensions(), size, focus, focusIdx, app)
}

// Precompute dims and subs
func widgetWidthsExt(w gowid.ISelectChild, subs []gowid.IWidget, dims []gowid.IWidgetDimension, size gowid.IRenderSize, focus gowid.Selector, focusIdx int, app gowid.IApp) []int {
	lenw := len(subs)

	res := make([]int, lenw)
	var widthHelper []bool
	var widthHelper2 []bool
	if w, ok := w.(IWidthHelper); ok {
		// Save some allocations
		widthHelper, widthHelper2 = w.WidthHelpers()
		defer func() {
			for i := 0; i < len(widthHelper); i++ {
				widthHelper[i] = false
				widthHelper2[i] = false
			}
		}()
	} else {
		widthHelper = make([]bool, lenw)
		widthHelper2 = make([]bool, lenw)
	}

	haveColsTotal := false
	var colsTotal int
	if _, ok := size.(gowid.IRenderFixed); !ok {
		cols, ok := size.(gowid.IColumns)
		if !ok {
			panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IColumns"})
		}
		colsTotal = cols.Columns()
		haveColsTotal = true
	}

	colsUsed := 0
	totalWeight := 0

	trunc := func(x *int) {
		if haveColsTotal && colsUsed+*x > colsTotal {
			*x = colsTotal - colsUsed
		}
	}

	// First, render the widgets whose width is known
	for i := 0; i < lenw; i++ {
		// This doesn't support IRenderFlow. That type comes with an associated width e.g.
		// "Flow with 25 columns". We don't have any way to apportion those columns amongst
		// the overall width for the widget.
		switch w2 := dims[i].(type) {
		case gowid.IRenderFixed:
			c := gowid.RenderSize(subs[i], gowid.RenderFixed{}, focus.SelectIf(w.SelectChild(focus) && i == focusIdx), app)
			res[i] = c.BoxColumns()
			trunc(&res[i])
			colsUsed += res[i]
			widthHelper[i] = true
			widthHelper2[i] = true
		case gowid.IRenderBox:
			res[i] = w2.BoxColumns()
			trunc(&res[i])
			colsUsed += res[i]
			widthHelper[i] = true
			widthHelper2[i] = true
		case gowid.IRenderFlowWith:
			res[i] = w2.FlowColumns()
			trunc(&res[i])
			colsUsed += res[i]
			widthHelper[i] = true
			widthHelper2[i] = true
		case gowid.IRenderWithUnits:
			res[i] = w2.Units()
			trunc(&res[i])
			colsUsed += res[i]
			widthHelper[i] = true
			widthHelper2[i] = true
		case gowid.IRenderRelative:
			cols, ok := size.(gowid.IColumns)
			if !ok {
				panic(gowid.WidgetSizeError{Widget: w, Size: size, Required: "gowid.IColumns"})
			}
			res[i] = int((w2.Relative() * float64(cols.Columns())) + 0.5)
			trunc(&res[i])
			colsUsed += res[i]
			widthHelper[i] = true
			widthHelper2[i] = true
		case gowid.IRenderWithWeight:
			// widget must be weighted
			totalWeight += w2.Weight()
			widthHelper[i] = false
			widthHelper2[i] = false
		default:
			panic(gowid.DimensionError{Size: size, Dim: w2})
		}
	}

	var colsLeft int
	var colsToDivideUp int
	if haveColsTotal {
		colsToDivideUp = colsTotal - colsUsed
		colsLeft = colsToDivideUp
	}

	// Now, divide up the remaining space among the weight columns
	lasti := -1
	maxedOut := false
	for {
		if colsLeft == 0 {
			break
		}
		doneone := false
		totalWeight = 0
		for i := 0; i < lenw; i++ {
			if w2, ok := dims[i].(gowid.IRenderWithWeight); ok && !widthHelper[i] {
				totalWeight += w2.Weight()
			}
		}
		colsToDivideUp = colsLeft
		for i := 0; i < lenw; i++ {
			// Can only be weight here if !helper[i] ; but not sufficient for it to be eligible
			if !widthHelper[i] {
				cols := int(((float32(dims[i].(gowid.IRenderWithWeight).Weight()) / float32(totalWeight)) * float32(colsToDivideUp)) + 0.5)
				if !maxedOut {
					if max, ok := dims[i].(gowid.IRenderMaxUnits); ok {
						if cols >= max.MaxUnits() {
							cols = max.MaxUnits()
							widthHelper[i] = true // this one is done
						}
					}
				}
				if cols > colsLeft {
					cols = colsLeft
				}
				if cols > 0 {
					if res[i] == -1 {
						res[i] = 0
					}
					res[i] += cols
					colsLeft -= cols
					lasti = gwutil.Max(i, lasti)
					doneone = true
				}
			}
		}
		if !doneone {
			// We used up all our extra space, after all weighted columns were maxed out. So
			// we're done. Any extra space (should be just 1 unit at most) goes on the last columns.
			if maxedOut {
				break
			}
			// All the weighted columns have been assigned, and all were maxed out. We still
			// have space to assign. So now grow the weighted columns, even though they don't need
			// any more space for a full render - what else to do with the space!
			maxedOut = true
			// Reset; all false indices will be indices of weighted columns again
			for i := 0; i < len(widthHelper); i++ {
				widthHelper[i] = widthHelper2[i]
			}
		}
	}
	if lasti != -1 && colsLeft > 0 {
		res[lasti] += colsLeft
	}

	return res
}

func RenderSize(w gowid.ICompositeMultipleWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.IRenderBox {
	subfocus := w.Focus()
	sizes := w.RenderedSubWidgetsSizes(size, focus, subfocus, app)

	maxcol, maxrow := 0, 0

	for _, sz := range sizes {
		maxcol += sz.BoxColumns()
		maxrow = gwutil.Max(maxrow, sz.BoxRows())
	}

	if cols, ok := size.(gowid.IColumns); ok {
		maxcol = cols.Columns()
		if rows, ok2 := size.(gowid.IRows); ok2 {
			maxrow = rows.Rows()
		}
	}

	return gowid.RenderBox{maxcol, maxrow}
}

func Render(w IWidget, size gowid.IRenderSize, focus gowid.Selector, app gowid.IApp) gowid.ICanvas {
	res := gowid.NewCanvas()
	subfocus := w.Focus()
	canvases := w.RenderSubWidgets(size, focus, subfocus, app)

	subs := w.SubWidgets()

	// Assemble subcanvases into final canvas
	for i := 0; i < len(subs); i++ {
		diff := res.BoxRows() - canvases[i].BoxRows()
		if diff > 0 {
			fill := fill.NewEmpty()
			fc := fill.Render(gowid.RenderBox{canvases[i].BoxColumns(), diff}, gowid.NotSelected, app)
			canvases[i].AppendBelow(fc, false, false)
		} else if diff < 0 {
			fill := fill.NewEmpty()
			fc := fill.Render(gowid.RenderBox{res.BoxColumns(), -diff}, gowid.NotSelected, app)
			res.AppendBelow(fc, false, false)
		}
		res.AppendRight(canvases[i], i == subfocus)
	}

	if cols, ok := size.(gowid.IColumns); ok {
		res.ExtendRight(gowid.EmptyLine(cols.Columns() - res.BoxColumns()))
		if rows, ok2 := size.(gowid.IRenderBox); ok2 && res.BoxRows() < rows.BoxRows() {
			gowid.AppendBlankLines(res, rows.BoxRows()-res.BoxRows())
		}
	}

	gowid.MakeCanvasRightSize(res, size)

	return res
}

var AllChildrenMaxDimension = fmt.Errorf("All columns widgets were rendered Max, so there is no max height to use.")

// RenderSubWidgets returns an array of canvases for each of the subwidgets, rendering them
// with in the context of a column with the provided size and focus.
func RenderSubWidgets(w IWidget, size gowid.IRenderSize, focus gowid.Selector, focusIdx int, app gowid.IApp) []gowid.ICanvas {
	subs := w.SubWidgets()
	dims := w.Dimensions()
	l := len(subs)
	canvases := make([]gowid.ICanvas, l)

	if l == 0 {
		return canvases
	}

	weights := w.WidgetWidths(size, focus, focusIdx, app)

	maxes := make([]int, 0, l)
	ssizes := make([]gowid.IRenderSize, 0, l)
	curMax := -1

	for i := 0; i < l; i++ {
		subSize := w.SubWidgetSize(size, weights[i], subs[i], dims[i])
		if _, ok := dims[i].(gowid.IRenderMax); ok {
			maxes = append(maxes, i)
			ssizes = append(ssizes, subSize)
		} else {
			canvases[i] = subs[i].Render(subSize, focus.SelectIf(w.SelectChild(focus) && i == focusIdx), app)
			if canvases[i].BoxRows() > curMax {
				curMax = canvases[i].BoxRows()
			}
		}
	}

	if curMax == -1 {
		panic(AllChildrenMaxDimension)
	}

	for j := 0; j < len(maxes); j++ {
		i := maxes[j]
		var mss gowid.IRenderSize = ssizes[j]
		switch css := mss.(type) {
		case gowid.IRenderFlowWith:
			mss = gowid.MakeRenderBox(css.FlowColumns(), curMax)
		case gowid.IRenderBox:
			mss = gowid.MakeRenderBox(css.BoxColumns(), curMax)
		default:
		}
		canvases[i] = subs[i].Render(mss, focus.SelectIf(w.SelectChild(focus) && i == focusIdx), app)
	}

	return canvases
}

// RenderedSubWidgetsSizes returns an array of boxes that bound each of the subwidgets as they
// would be rendered with the given size and focus.
func RenderedSubWidgetsSizes(w IWidget, size gowid.IRenderSize, focus gowid.Selector, focusIdx int, app gowid.IApp) []gowid.IRenderBox {
	subs := w.SubWidgets()
	dims := w.Dimensions()
	l := len(subs)

	res := make([]gowid.IRenderBox, l)
	weights := w.WidgetWidths(size, focus, focusIdx, app)

	maxes := make([]int, 0, l)
	ssizes := make([]int, 0, l)
	curMax := -1

	for i := 0; i < l; i++ {
		subSize := w.SubWidgetSize(size, weights[i], subs[i], dims[i])

		if _, ok := dims[i].(gowid.IRenderMax); ok {
			maxes = append(maxes, i)
			ssizes = append(ssizes, weights[i])
		} else {
			c := subs[i].RenderSize(subSize, focus.SelectIf(w.SelectChild(focus) && i == focusIdx), app)
			res[i] = gowid.RenderBox{weights[i], c.BoxRows()}

			if res[i].BoxRows() > curMax {
				curMax = res[i].BoxRows()
			}
		}
	}

	if curMax == -1 {
		panic(AllChildrenMaxDimension)
	}

	for j := 0; j < len(maxes); j++ {
		res[maxes[j]] = gowid.RenderBox{ssizes[j], curMax}
	}

	return res
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 110
// End: