layout/style.go

Functions Structs

Functions

func Auto

Auto returns an auto-sized dimension.

func Auto() Dimension {
	return Dimension{Unit: DimensionAuto}
}

func DefaultStyle

DefaultStyle returns a Style with sensible defaults.

 

Default values:

- Display: DisplayFlex

- FlexDirection: FlexRow

- FlexShrink: 1 (items can shrink)

- All dimensions: Auto

func DefaultStyle() Style {
	return Style{
		Display:	DisplayFlex,
		FlexDirection:	FlexRow,
		FlexWrap:	FlexNoWrap,
		JustifyContent:	JustifyStart,
		AlignItems:	AlignItemsStretch,
		AlignContent:	AlignContentStretch,
		FlexGrow:	0,
		FlexShrink:	1,
		FlexBasis:	Auto(),
		Width:		Auto(),
		Height:		Auto(),
		MinWidth:	Auto(),
		MinHeight:	Auto(),
		MaxWidth:	Auto(),
		MaxHeight:	Auto(),
	}
}

func IsAuto

IsAuto returns true if the dimension is auto.

func (d Dimension) IsAuto() bool {
	return d.Unit == DimensionAuto
}

func IsHorizontal

IsHorizontal returns true if the direction is horizontal.

func (d FlexDirection) IsHorizontal() bool {
	return d == FlexRow || d == FlexRowReverse
}

func IsReversed

IsReversed returns true if the direction is reversed.

func (d FlexDirection) IsReversed() bool {
	return d == FlexRowReverse || d == FlexColumnReverse
}

func Pct

Pct returns a percentage dimension.

func Pct(value float32) Dimension {
	return Dimension{Value: value, Unit: DimensionPercent}
}

func Px

Px returns a pixel dimension.

func Px(value float32) Dimension {
	return Dimension{Value: value, Unit: DimensionPixels}
}

func Resolve

Resolve resolves the dimension to a pixel value given a reference size.

For auto dimensions, returns the fallback value.

func (d Dimension) Resolve(reference, fallback float32) float32 {
	switch d.Unit {
	case DimensionAuto:
		return fallback
	case DimensionPixels:
		return d.Value
	case DimensionPercent:
		return reference * d.Value / 100
	default:
		return fallback
	}
}

func String

String returns a string representation of justify content.

func (j JustifyContent) String() string {
	switch j {
	case JustifyStart:
		return styleStrStart
	case JustifyEnd:
		return styleStrEnd
	case JustifyCenter:
		return styleStrCenter
	case JustifySpaceBetween:
		return styleStrSpaceBetween
	case JustifySpaceAround:
		return styleStrSpaceAround
	case JustifySpaceEvenly:
		return styleStrSpaceEvenly
	default:
		return styleStrUnknown
	}
}

func String

String returns a string representation of align items.

func (a AlignItems) String() string {
	switch a {
	case AlignItemsStart:
		return styleStrStart
	case AlignItemsEnd:
		return styleStrEnd
	case AlignItemsCenter:
		return styleStrCenter
	case AlignItemsStretch:
		return styleStrStretch
	case AlignItemsBaseline:
		return styleStrBaseline
	default:
		return styleStrUnknown
	}
}

func String

String returns a string representation of the display mode.

func (d Display) String() string {
	switch d {
	case DisplayFlex:
		return "Flex"
	case DisplayGrid:
		return "Grid"
	case DisplayBlock:
		return "Block"
	case DisplayNone:
		return "None"
	default:
		return styleStrUnknown
	}
}

func String

String returns a string representation of the wrap mode.

func (w FlexWrap) String() string {
	switch w {
	case FlexNoWrap:
		return "NoWrap"
	case FlexWrapOn:
		return "Wrap"
	case FlexWrapReverse:
		return "WrapReverse"
	default:
		return styleStrUnknown
	}
}

func String

String returns a string representation of the flex direction.

func (d FlexDirection) String() string {
	switch d {
	case FlexRow:
		return "Row"
	case FlexRowReverse:
		return "RowReverse"
	case FlexColumn:
		return "Column"
	case FlexColumnReverse:
		return "ColumnReverse"
	default:
		return styleStrUnknown
	}
}

func String

String returns a string representation of align content.

func (a AlignContent) String() string {
	switch a {
	case AlignContentStart:
		return styleStrStart
	case AlignContentEnd:
		return styleStrEnd
	case AlignContentCenter:
		return styleStrCenter
	case AlignContentStretch:
		return styleStrStretch
	case AlignContentSpaceBetween:
		return styleStrSpaceBetween
	case AlignContentSpaceAround:
		return styleStrSpaceAround
	default:
		return styleStrUnknown
	}
}

func WithAlignItems

WithAlignItems returns a copy of the style with the given align items.

func (s Style) WithAlignItems(align AlignItems) Style {
	s.AlignItems = align
	return s
}

func WithDisplay

WithDisplay returns a copy of the style with the given display mode.

func (s Style) WithDisplay(display Display) Style {
	s.Display = display
	return s
}

func WithFlex

WithFlex returns a copy of the style with the given flex properties.

func (s Style) WithFlex(grow, shrink float32, basis Dimension) Style {
	s.FlexGrow = grow
	s.FlexShrink = shrink
	s.FlexBasis = basis
	return s
}

func WithFlexDirection

WithFlexDirection returns a copy of the style with the given flex direction.

func (s Style) WithFlexDirection(direction FlexDirection) Style {
	s.FlexDirection = direction
	return s
}

func WithGap

WithGap returns a copy of the style with the given gap.

func (s Style) WithGap(gap float32) Style {
	s.Gap = gap
	return s
}

func WithJustifyContent

WithJustifyContent returns a copy of the style with the given justify content.

func (s Style) WithJustifyContent(justify JustifyContent) Style {
	s.JustifyContent = justify
	return s
}

func WithMargin

WithMargin returns a copy of the style with the given margin.

func (s Style) WithMargin(margin geometry.Insets) Style {
	s.Margin = margin
	return s
}

func WithPadding

WithPadding returns a copy of the style with the given padding.

func (s Style) WithPadding(padding geometry.Insets) Style {
	s.Padding = padding
	return s
}

func WithSize

WithSize returns a copy of the style with the given width and height.

func (s Style) WithSize(width, height Dimension) Style {
	s.Width = width
	s.Height = height
	return s
}

Structs

type Dimension struct

Dimension represents a size value that can be auto, pixels, or percent.

type Dimension struct {
	Value	float32
	Unit	DimensionUnit
}

type Style struct

Style defines layout properties for a node (CSS-like).

 

Style contains all the properties that layout algorithms use to

determine how a node should be sized and positioned.

type Style struct {
	// Display mode
	Display	Display

	// Flexbox properties
	FlexDirection	FlexDirection
	FlexWrap	FlexWrap
	JustifyContent	JustifyContent
	AlignItems	AlignItems
	AlignContent	AlignContent

	// Flex item properties
	FlexGrow	float32
	FlexShrink	float32
	FlexBasis	Dimension

	// Sizing
	Width		Dimension
	Height		Dimension
	MinWidth	Dimension
	MinHeight	Dimension
	MaxWidth	Dimension
	MaxHeight	Dimension

	// Spacing
	Margin	geometry.Insets
	Padding	geometry.Insets
	Gap	float32

	// Grid properties
	GridGap		float32
	GridRowGap	float32
	GridColumnGap	float32
}