gitstack

grindlemire/go-tui code browser

3.4 KB Go 108 lines 2026-06-03 · d15bb9f raw
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
package layout

// Direction specifies the main axis for laying out children.
type Direction uint8

const (
	Row    Direction = iota // Children laid out left-to-right
	Column                  // Children laid out top-to-bottom
)

// Justify specifies how children are distributed along the main axis.
type Justify uint8

const (
	JustifyStart        Justify = iota // Pack at start
	JustifyEnd                         // Pack at end
	JustifyCenter                      // Center children
	JustifySpaceBetween                // Even space between, none at edges
	JustifySpaceAround                 // Even space around each child
	JustifySpaceEvenly                 // Equal space between and at edges
)

// Align specifies how children are positioned on the cross axis.
type Align uint8

const (
	AlignStart   Align = iota // Align to start of cross axis
	AlignEnd                  // Align to end of cross axis
	AlignCenter               // Center on cross axis
	AlignStretch              // Stretch to fill cross axis
)

// Display specifies the layout mode for an element.
type Display uint8

const (
	DisplayBlock Display = iota // Block layout: column direction, fills parent width
	DisplayFlex                 // Flex layout: explicit direction control
)

// FlexWrap controls whether flex items wrap to new lines.
type FlexWrap uint8

const (
	WrapNone    FlexWrap = iota // No wrapping (default, current behavior)
	Wrap                        // Wrap to next line
	WrapReverse                 // Wrap in reverse cross-axis order
)

// AlignContent controls how flex lines are distributed along the cross axis.
// Only applies when FlexWrap is Wrap or WrapReverse and there are multiple lines.
type AlignContent uint8

const (
	ContentStart        AlignContent = iota // Pack lines at start (default)
	ContentEnd                              // Pack lines at end
	ContentCenter                           // Center lines
	ContentStretch                          // Stretch lines to fill
	ContentSpaceBetween                     // Even space between lines
	ContentSpaceAround                      // Even space around lines
)

// Style contains all layout properties for a node.
type Style struct {
	// Sizing
	Width     Value
	Height    Value
	MinWidth  Value
	MinHeight Value
	MaxWidth  Value
	MaxHeight Value

	// Display mode
	Display Display // Block (default) or Flex

	// Flex container properties
	Direction      Direction
	JustifyContent Justify
	AlignItems     Align
	Gap            int          // Space between children (main axis only)
	FlexWrap       FlexWrap     // Wrap behavior (default WrapNone)
	AlignContent   AlignContent // Cross-axis line distribution (default ContentStart)

	// Flex item properties
	FlexGrow   float64 // How much to grow relative to siblings
	FlexShrink float64 // How much to shrink relative to siblings (default 1)
	AlignSelf  *Align  // Override parent's AlignItems (nil = inherit)

	// Spacing
	Padding Edges
	Margin  Edges
}

// DefaultStyle returns a Style with sensible defaults.
func DefaultStyle() Style {
	return Style{
		Width:      Auto(),
		Height:     Auto(),
		MinWidth:   Auto(), // auto = intrinsic size (matches CSS flexbox min-width: auto)
		MinHeight:  Auto(), // auto = intrinsic size (matches CSS flexbox min-height: auto)
		MaxWidth:   Auto(), // No maximum
		MaxHeight:  Auto(), // No maximum
		Display:    DisplayBlock,
		Direction:  Row,
		AlignItems: AlignStretch,
		FlexShrink: 1.0,
	}
}