summaryrefslogtreecommitdiff
path: root/layout.go
blob: e4fda3829bdb97310410efcb3e9f9bcf1e061c5e (plain)
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
package ui

// Recursively removes border margins and padding from controls, replaces
// nil values with stretchy spaces and reorients nested stack to have
// opposing orientations.
func resetControls(parent *Stack) {
	for i, control := range parent.controls {
		switch control.(type) {
		case *Stack:
			stack := control.(*Stack)
			stack.borderMargin = 0
			stack.orientation = !parent.orientation
			resetControls(stack)
		case nil:
			emptySpace := newStack(horizontal)
			parent.controls[i] = emptySpace
			parent.stretchy[i] = true
		}
	}
}

// Creates a new Stack from the given controls. The topmost Stack will have
// vertical orientation and margin borders, with each nested stack being
// oriented oppositely. Controls are displayed with a default padding
// between them.
func Layout(controls ...Control) *Stack {
	stack := &Stack{
		orientation:  vertical,
		controls:     controls,
		stretchy:     make([]bool, len(controls)),
		width:        make([]int, len(controls)),
		height:       make([]int, len(controls)),
		padding:      10,
		borderMargin: 15,
	}

	resetControls(stack)

	return stack
}