package module
Version: v0.0.0-...-246c3fa Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2020 License: BSD-3-Clause Imports: 5 Imported by: 0


flex - CSS flexbox layout implementation in Go

Go implementation of flexbox CSS layout algorithm.

A pure Go port of Facebook's Yoga.

How to use

Read tutorial or look at _test.go files.


The port is finished. The code works and passess all Yoga tests.

The API is awkward by Go standards but it's the best I could do given that I want to stay close to C version.

Logic is currently synced up to https://github.com/facebook/yoga/commit/f45059e1e696727c1282742b89d2c8bf06345254

How the port was made

You can read a detailed story.

In short:

  • manually ported C code to Go, line-by-line
  • manually ported tests to Go
  • tweak the API from C style to be more Go like. The structure and logic still is very close to C code (this makes porting future C changes easy)




View Source
const (
	// EdgeCount is count of edges
	EdgeCount = 9


View Source
var (
	// ValueUndefined defines undefined YGValue
	ValueUndefined = Value{Undefined, UnitUndefined}
	// ValueAuto defines auto YGValue
	ValueAuto = Value{Undefined, UnitAuto}
View Source
var (
	NAN = math.Float32frombits(uvnan)
View Source
var (
	// Undefined defines undefined value
	Undefined = NAN
View Source
var (

	// ValueZero defines a zero value
	ValueZero = Value{Value: 0, Unit: UnitPoint}


func AlignToString

func AlignToString(value Align) string

AlignToString returns string version of Align enum

func Baseline

func Baseline(node *Node) float32

Baseline retuns baseline

func CalculateLayout

func CalculateLayout(node *Node, parentWidth float32, parentHeight float32, parentDirection Direction)

CalculateLayout calculates layout

func ConfigCopy

func ConfigCopy(dest *Config, src *Config)

ConfigCopy copies a config

func DefaultLog

func DefaultLog(config *Config, node *Node, level LogLevel, format string,
	args ...interface{}) int

DefaultLog is default logging function

func DimensionToString

func DimensionToString(value Dimension) string

DimensionToString returns string version of Dimension enum

func DirectionToString

func DirectionToString(value Direction) string

DirectionToString returns string version of Direction enum

func DisplayToString

func DisplayToString(value Display) string

DisplayToString returns string version of Display enum

func EdgeToString

func EdgeToString(value Edge) string

EdgeToString returns string version of Edge enum

func ExperimentalFeatureToString

func ExperimentalFeatureToString(value ExperimentalFeature) string

ExperimentalFeatureToString returns string version of ExperimentalFeature enum

func FlexDirectionToString

func FlexDirectionToString(value FlexDirection) string

FlexDirectionToString returns string version of FlexDirection enum

func FloatIsUndefined

func FloatIsUndefined(value float32) bool

FloatIsUndefined returns true if value is undefined

func FloatsEqual

func FloatsEqual(a float32, b float32) bool

FloatsEqual returns true if floats are approx. equal

func IsNaN

func IsNaN(f float32) (is bool)

IsNaN reports whether f is an IEEE 754 “not-a-number” value.

func JustifyToString

func JustifyToString(value Justify) string

JustifyToString returns string version of Justify enum

func LogLevelToString

func LogLevelToString(value LogLevel) string

LogLevelToString returns string version of LogLevel enum

func MeasureModeToString

func MeasureModeToString(value MeasureMode) string

MeasureModeToString returns string version of MeasureMode enum

func NaN

func NaN() float32

NaN returns an IEEE 754 “not-a-number” value.

func NodeCopyStyle

func NodeCopyStyle(dstNode *Node, srcNode *Node)

NodeCopyStyle copies style

func NodePrint

func NodePrint(node *Node, options PrintOptions)

NodePrint prints node to standard output.

func NodeStyleSetFlexBasisAuto

func NodeStyleSetFlexBasisAuto(node *Node)

NodeStyleSetFlexBasisAuto sets flex basis auto

func NodeTypeToString

func NodeTypeToString(value NodeType) string

NodeTypeToString returns string version of NodeType enum

func OverflowToString

func OverflowToString(value Overflow) string

OverflowToString returns string version of Overflow enum

func PositionTypeToString

func PositionTypeToString(value PositionType) string

PositionTypeToString returns string version of PositionType enum

func PrintOptionsToString

func PrintOptionsToString(value PrintOptions) string

PrintOptionsToString returns string version of PrintOptions enum

func UnitToString

func UnitToString(value Unit) string

UnitToString returns string version of Unit enum

func ValueEqual

func ValueEqual(a Value, b Value) bool

ValueEqual returns true if values are equal

func WrapToString

func WrapToString(value Wrap) string

WrapToString returns string version of Wrap enum


type Align

type Align int

Align describes align flex attribute

const (
	// AlignAuto is "auto"
	AlignAuto Align = iota
	// AlignFlexStart is "flex-start"
	// AlignCenter if "center"
	// AlignFlexEnd is "flex-end"
	// AlignStretch is "strech"
	// AlignBaseline is "baseline"
	// AlignSpaceBetween is "space-between"
	// AlignSpaceAround is "space-around"

type BaselineFunc

type BaselineFunc func(node *Node, width float32, height float32) float32

BaselineFunc describes function for baseline

type CachedMeasurement

type CachedMeasurement struct {
	// contains filtered or unexported fields

CachedMeasurement describes measurements

type Config

type Config struct {
	UseWebDefaults            bool
	UseLegacyStretchBehaviour bool
	PointScaleFactor          float32
	Logger                    Logger
	Context                   interface{}
	// contains filtered or unexported fields

Config describes a configuration

func ConfigGetDefault

func ConfigGetDefault() *Config

ConfigGetDefault returns default config, only for C#

func NewConfig

func NewConfig() *Config

NewConfig creates new config

func (*Config) IsExperimentalFeatureEnabled

func (config *Config) IsExperimentalFeatureEnabled(feature ExperimentalFeature) bool

IsExperimentalFeatureEnabled returns if experimental feature is enabled

func (*Config) SetExperimentalFeatureEnabled

func (config *Config) SetExperimentalFeatureEnabled(feature ExperimentalFeature, enabled bool)

SetExperimentalFeatureEnabled enables experimental feature

func (*Config) SetPointScaleFactor

func (config *Config) SetPointScaleFactor(pixelsInPoint float32)

SetPointScaleFactor sets scale factor

type Dimension

type Dimension int

Dimension represents dimention

const (
	// DimensionWidth is width
	DimensionWidth Dimension = iota
	// DimensionHeight is height

type Direction

type Direction int

Direction represents right-to-left or left-to-right direction

const (
	// DirectionInherit is "inherit"
	DirectionInherit Direction = iota
	// DirectionLTR is "ltr"
	// DirectionRTL is "rtl"

type Display

type Display int

Display is "display" property

const (
	// DisplayFlex is "flex"
	DisplayFlex Display = iota
	// DisplayNone is "none"

type Edge

type Edge int

Edge represents an edge

const (
	// EdgeLeft is left edge
	EdgeLeft Edge = iota
	// EdgeTop is top edge
	// EdgeRight is right edge
	// EdgeBottom is bottom edge
	// EdgeStart is start edge
	// EdgeEnd is end edge
	// EdgeHorizontal is horizontal edge
	// EdgeVertical is vertical edge
	// EdgeAll is all edge

type ExperimentalFeature

type ExperimentalFeature int

ExperimentalFeature defines experimental features

const (
	// ExperimentalFeatureWebFlexBasis is web flex basis
	ExperimentalFeatureWebFlexBasis ExperimentalFeature = iota

type FlexDirection

type FlexDirection int

FlexDirection describes "flex-direction" property

const (
	// FlexDirectionColumn is "column"
	FlexDirectionColumn FlexDirection = iota
	// FlexDirectionColumnReverse is "column-reverse"
	// FlexDirectionRow is "row"
	// FlexDirectionRowReverse is "row-reverse"

type Justify

type Justify int

Justify is "justify" property

const (
	// JustifyFlexStart is "flex-start"
	JustifyFlexStart Justify = iota
	// JustifyCenter is "center"
	// JustifyFlexEnd is "flex-end"
	// JustifySpaceBetween is "space-between"
	// JustifySpaceAround is "space-around"

type Layout

type Layout struct {
	Position   [4]float32
	Dimensions [2]float32
	Margin     [6]float32
	Border     [6]float32
	Padding    [6]float32
	Direction  Direction

	HadOverflow bool
	// contains filtered or unexported fields

Layout describes position information after layout is finished

type LogLevel

type LogLevel int

LogLevel represents log level

const (
	LogLevelError LogLevel = iota

type Logger

type Logger func(config *Config, node *Node, level LogLevel, format string, args ...interface{}) int

Logger defines logging function

type MeasureFunc

type MeasureFunc func(node *Node, width float32, widthMode MeasureMode, height float32, heightMode MeasureMode) Size

MeasureFunc describes function for measuring

type MeasureMode

type MeasureMode int

MeasureMode defines measurement mode

const (
	// MeasureModeUndefined is undefined
	MeasureModeUndefined MeasureMode = iota
	// MeasureModeExactly is exactly
	// MeasureModeAtMost is at-most

type Node

type Node struct {
	Style  Style
	Layout Layout

	Parent   *Node
	Children []*Node

	NextChild *Node

	Measure  MeasureFunc
	Baseline BaselineFunc
	Print    PrintFunc
	Config   *Config
	Context  interface{}

	IsDirty      bool
	HasNewLayout bool
	NodeType     NodeType
	// contains filtered or unexported fields

Node describes a an element

func NewNode

func NewNode() *Node

NewNode creates a new node

func NewNodeWithConfig

func NewNodeWithConfig(config *Config) *Node

NewNodeWithConfig creates new node with config

func (*Node) GetChild

func (node *Node) GetChild(idx int) *Node

GetChild returns a child at a given index

func (*Node) InsertChild

func (node *Node) InsertChild(child *Node, idx int)

InsertChild inserts a child

func (*Node) LayoutGetBorder

func (node *Node) LayoutGetBorder(edge Edge) float32

LayoutGetBorder gets border

func (*Node) LayoutGetBottom

func (node *Node) LayoutGetBottom() float32

LayoutGetBottom gets bottom

func (*Node) LayoutGetHeight

func (node *Node) LayoutGetHeight() float32

LayoutGetHeight gets height

func (*Node) LayoutGetLeft

func (node *Node) LayoutGetLeft() float32

LayoutGetLeft gets left

func (*Node) LayoutGetMargin

func (node *Node) LayoutGetMargin(edge Edge) float32

LayoutGetMargin gets margin

func (*Node) LayoutGetPadding

func (node *Node) LayoutGetPadding(edge Edge) float32

LayoutGetPadding gets padding

func (*Node) LayoutGetRight

func (node *Node) LayoutGetRight() float32

LayoutGetRight gets right

func (*Node) LayoutGetTop

func (node *Node) LayoutGetTop() float32

LayoutGetTop gets top

func (*Node) LayoutGetWidth

func (node *Node) LayoutGetWidth() float32

LayoutGetWidth gets width

func (*Node) MarkDirty

func (node *Node) MarkDirty()

MarkDirty marks node as dirty

func (*Node) RemoveChild

func (node *Node) RemoveChild(child *Node)

RemoveChild removes child node

func (*Node) Reset

func (node *Node) Reset()

Reset resets a node

func (*Node) SetMeasureFunc

func (node *Node) SetMeasureFunc(measureFunc MeasureFunc)

SetMeasureFunc sets measure function

func (*Node) StyleGetBorder

func (node *Node) StyleGetBorder(edge Edge) float32

StyleGetBorder gets border

func (*Node) StyleGetFlexGrow

func (node *Node) StyleGetFlexGrow() float32

StyleGetFlexGrow gets flex grow

func (*Node) StyleGetFlexShrink

func (node *Node) StyleGetFlexShrink() float32

StyleGetFlexShrink gets flex shrink

func (*Node) StyleGetHeight

func (node *Node) StyleGetHeight() Value

StyleGetHeight gets height

func (*Node) StyleGetMargin

func (node *Node) StyleGetMargin(edge Edge) Value

StyleGetMargin gets margin

func (*Node) StyleGetMaxHeight

func (node *Node) StyleGetMaxHeight() Value

StyleGetMaxHeight gets max height

func (*Node) StyleGetMaxWidth

func (node *Node) StyleGetMaxWidth() Value

StyleGetMaxWidth gets max width

func (*Node) StyleGetMinHeight

func (node *Node) StyleGetMinHeight() Value

StyleGetMinHeight gets min height

func (*Node) StyleGetMinWidth

func (node *Node) StyleGetMinWidth() Value

StyleGetMinWidth gets min width

func (*Node) StyleGetPadding

func (node *Node) StyleGetPadding(edge Edge) Value

StyleGetPadding gets padding

func (*Node) StyleGetPosition

func (node *Node) StyleGetPosition(edge Edge) Value

StyleGetPosition gets position

func (*Node) StyleGetWidth

func (node *Node) StyleGetWidth() Value

StyleGetWidth gets width

func (*Node) StyleSetAlignContent

func (node *Node) StyleSetAlignContent(alignContent Align)

StyleSetAlignContent sets align content

func (*Node) StyleSetAlignItems

func (node *Node) StyleSetAlignItems(alignItems Align)

StyleSetAlignItems sets align content

func (*Node) StyleSetAlignSelf

func (node *Node) StyleSetAlignSelf(alignSelf Align)

StyleSetAlignSelf sets align self

func (*Node) StyleSetAspectRatio

func (node *Node) StyleSetAspectRatio(aspectRatio float32)

StyleSetAspectRatio sets axpect ratio

func (*Node) StyleSetBorder

func (node *Node) StyleSetBorder(edge Edge, border float32)

StyleSetBorder sets border

func (*Node) StyleSetDirection

func (node *Node) StyleSetDirection(direction Direction)

StyleSetDirection sets direction

func (*Node) StyleSetDisplay

func (node *Node) StyleSetDisplay(display Display)

StyleSetDisplay sets display

func (*Node) StyleSetFlex

func (node *Node) StyleSetFlex(flex float32)

StyleSetFlex sets flex

func (*Node) StyleSetFlexBasis

func (node *Node) StyleSetFlexBasis(flexBasis float32)

StyleSetFlexBasis sets flex basis

func (*Node) StyleSetFlexBasisPercent

func (node *Node) StyleSetFlexBasisPercent(flexBasis float32)

StyleSetFlexBasisPercent sets flex basis percent

func (*Node) StyleSetFlexDirection

func (node *Node) StyleSetFlexDirection(flexDirection FlexDirection)

StyleSetFlexDirection sets flex directions

func (*Node) StyleSetFlexGrow

func (node *Node) StyleSetFlexGrow(flexGrow float32)

StyleSetFlexGrow sets flex grow

func (*Node) StyleSetFlexShrink

func (node *Node) StyleSetFlexShrink(flexShrink float32)

StyleSetFlexShrink sets flex shrink

func (*Node) StyleSetFlexWrap

func (node *Node) StyleSetFlexWrap(flexWrap Wrap)

StyleSetFlexWrap sets flex wrap

func (*Node) StyleSetHeight

func (node *Node) StyleSetHeight(height float32)

StyleSetHeight sets height

func (*Node) StyleSetHeightAuto

func (node *Node) StyleSetHeightAuto()

StyleSetHeightAuto sets height auto

func (*Node) StyleSetHeightPercent

func (node *Node) StyleSetHeightPercent(height float32)

StyleSetHeightPercent sets height percent

func (*Node) StyleSetJustifyContent

func (node *Node) StyleSetJustifyContent(justifyContent Justify)

StyleSetJustifyContent sets justify content

func (*Node) StyleSetMargin

func (node *Node) StyleSetMargin(edge Edge, margin float32)

StyleSetMargin sets margin

func (*Node) StyleSetMarginAuto

func (node *Node) StyleSetMarginAuto(edge Edge)

StyleSetMarginAuto sets margin auto

func (*Node) StyleSetMarginPercent

func (node *Node) StyleSetMarginPercent(edge Edge, margin float32)

StyleSetMarginPercent sets margin percent

func (*Node) StyleSetMaxHeight

func (node *Node) StyleSetMaxHeight(maxHeight float32)

StyleSetMaxHeight sets max width

func (*Node) StyleSetMaxHeightPercent

func (node *Node) StyleSetMaxHeightPercent(maxHeight float32)

StyleSetMaxHeightPercent sets max height percent

func (*Node) StyleSetMaxWidth

func (node *Node) StyleSetMaxWidth(maxWidth float32)

StyleSetMaxWidth sets max width

func (*Node) StyleSetMaxWidthPercent

func (node *Node) StyleSetMaxWidthPercent(maxWidth float32)

StyleSetMaxWidthPercent sets max width percent

func (*Node) StyleSetMinHeight

func (node *Node) StyleSetMinHeight(minHeight float32)

StyleSetMinHeight sets min width

func (*Node) StyleSetMinHeightPercent

func (node *Node) StyleSetMinHeightPercent(minHeight float32)

StyleSetMinHeightPercent sets min height percent

func (*Node) StyleSetMinWidth

func (node *Node) StyleSetMinWidth(minWidth float32)

StyleSetMinWidth sets min width

func (*Node) StyleSetMinWidthPercent

func (node *Node) StyleSetMinWidthPercent(minWidth float32)

StyleSetMinWidthPercent sets width percent

func (*Node) StyleSetOverflow

func (node *Node) StyleSetOverflow(overflow Overflow)

StyleSetOverflow sets overflow

func (*Node) StyleSetPadding

func (node *Node) StyleSetPadding(edge Edge, padding float32)

StyleSetPadding sets padding

func (*Node) StyleSetPaddingPercent

func (node *Node) StyleSetPaddingPercent(edge Edge, padding float32)

StyleSetPaddingPercent sets padding percent

func (*Node) StyleSetPosition

func (node *Node) StyleSetPosition(edge Edge, position float32)

StyleSetPosition sets position

func (*Node) StyleSetPositionPercent

func (node *Node) StyleSetPositionPercent(edge Edge, position float32)

StyleSetPositionPercent sets position percent

func (*Node) StyleSetPositionType

func (node *Node) StyleSetPositionType(positionType PositionType)

StyleSetPositionType sets position type

func (*Node) StyleSetWidth

func (node *Node) StyleSetWidth(width float32)

StyleSetWidth sets width

func (*Node) StyleSetWidthAuto

func (node *Node) StyleSetWidthAuto()

StyleSetWidthAuto sets width auto

func (*Node) StyleSetWidthPercent

func (node *Node) StyleSetWidthPercent(width float32)

StyleSetWidthPercent sets width percent

type NodePrinter

type NodePrinter struct {
	// contains filtered or unexported fields

NodePrinter node printer.

func NewNodePrinter

func NewNodePrinter(writer io.Writer, options PrintOptions) *NodePrinter

NewNodePrinter creates new node printer.

func (*NodePrinter) Print

func (printer *NodePrinter) Print(node *Node)

Print prints node.

type NodeType

type NodeType int

NodeType defines node type

const (
	// NodeTypeDefault is default node
	NodeTypeDefault NodeType = iota
	// NodeTypeText is text node

type Overflow

type Overflow int

Overflow describes "overflow" property

const (
	// OverflowVisible is "visible"
	OverflowVisible Overflow = iota
	// OverflowHidden is "hidden"
	// OverflowScroll is "scroll"

type PositionType

type PositionType int

PositionType is "position" property

const (
	// PositionTypeRelative is "relative"
	PositionTypeRelative PositionType = iota
	// PositionTypeAbsolute is "absolute"

type PrintFunc

type PrintFunc func(node *Node)

PrintFunc defines function for printing

type PrintOptions

type PrintOptions int
const (
	PrintOptionsLayout PrintOptions = 1 << iota

type Size

type Size struct {
	Width  float32
	Height float32

Size describes size

type Style

type Style struct {
	Direction      Direction
	FlexDirection  FlexDirection
	JustifyContent Justify
	AlignContent   Align
	AlignItems     Align
	AlignSelf      Align
	PositionType   PositionType
	FlexWrap       Wrap
	Overflow       Overflow
	Display        Display
	Flex           float32
	FlexGrow       float32
	FlexShrink     float32
	FlexBasis      Value
	Margin         [EdgeCount]Value
	Position       [EdgeCount]Value
	Padding        [EdgeCount]Value
	Border         [EdgeCount]Value
	Dimensions     [2]Value
	MinDimensions  [2]Value
	MaxDimensions  [2]Value

	// Yoga specific properties, not compatible with flexbox specification
	AspectRatio float32

Style describes CSS flexbox style of the node

type Unit

type Unit int

Unit is "unit" property

const (
	// UnitUndefined is "undefined"
	UnitUndefined Unit = iota
	// UnitPoint is "point"
	// UnitPercent is "percent"
	// UnitAuto is "auto"

type Value

type Value struct {
	Value float32
	Unit  Unit

Value describes value

type Wrap

type Wrap int

Wrap is "wrap" property

const (
	// WrapNoWrap is "no-wrap"
	WrapNoWrap Wrap = iota
	// WrapWrap is "wrap"
	// WrapWrapReverse is "reverse"

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL