~pierrec/giox

ref: 2c5ee028295c046e95c45a7f776b0fb6177759ad giox/layoutx/layoutx.go -rw-r--r-- 1.0 KiB
2c5ee028pierre layoutx: added Rigids and Flexes 8 months ago
                                                                                
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
package layoutx

import (
	"gioui.org/layout"
)

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func Swap(a layout.Axis) layout.Axis {
	return (a + 1) % 2
}

// Flex wraps a layout.Flex to support cross constraint rigids.
type Flex struct {
	layout.Flex
	cross int
}

func (f *Flex) Rigid(w layout.Widget) layout.FlexChild {
	return layout.Rigid(func(gtx layout.Context) layout.Dimensions {
		dims := w(gtx)
		f.cross = max(f.cross, f.Flex.Axis.Convert(dims.Size).Y)
		return dims
	})
}

// RigidCross returns a Rigid which cross axis constraint is set to the
// largest one from other non RigidCross.
func (f *Flex) RigidCross(w layout.Widget) layout.FlexChild {
	// Use a Flexed so that they are run after Rigids have been evaluated.
	return layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
		if f.Flex.Axis == layout.Horizontal {
			gtx.Constraints.Max.Y = f.cross
		} else {
			gtx.Constraints.Max.X = f.cross
		}
		return w(gtx)
	})
}