Skip to content

Commit e3d65d0

Browse files
committed
final tweaks
1 parent 502758b commit e3d65d0

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

Diff for: README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ Go implementation of flexbox CSS layout algorithm.
44

55
A pure GO port of [Facebook's Yoga](https://github.com/facebook/yoga).
66

7+
For usage examples look at `_test.go` files.
8+
79
Logic is currently synced up to https://github.com/facebook/yoga/commit/c9384762ee367f890a3de57ff3270d8f9c445866
810

911
## How the port was made
1012

1113
* manually ported [C code](https://github.com/facebook/yoga/tree/master/yoga) to Go, line-by-line
1214
* manually ported [tests](https://github.com/facebook/yoga/tree/master/tests) to Go
13-
* 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 C changes easier)
15+
* 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)
1416

1517
## Status
1618

17-
Unstable.
19+
The port is finished. The code works and passess all Yoga tests.
1820

19-
The code works and passes all the tests but I'm still tweaking the API to be more Go-like
21+
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.

Diff for: relayout_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "testing"
44

55
func TestDont_cache_computed_flex_basis_between_layouts(t *testing.T) {
66
config := NewConfig()
7-
ConfigSetExperimentalFeatureEnabled(config, ExperimentalFeatureWebFlexBasis, true)
7+
config.SetExperimentalFeatureEnabled(ExperimentalFeatureWebFlexBasis, true)
88

99
root := NewNodeWithConfig(config)
1010
root.StyleSetHeightPercent(100)

Diff for: yoga.go

+11-17
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type CachedMeasurement struct {
2020
// layouts should not require more than 16 entries to fit within the cache.
2121
const maxCachedResultCount = 16
2222

23-
// Layout describes layout
23+
// Layout describes position information after layout is finished
2424
type Layout struct {
2525
Position [4]float32
2626
Dimensions [2]float32
@@ -46,7 +46,7 @@ type Layout struct {
4646
cachedLayout CachedMeasurement
4747
}
4848

49-
// Style describes a style
49+
// Style describes CSS flexbox style of the node
5050
type Style struct {
5151
Direction Direction
5252
FlexDirection FlexDirection
@@ -84,7 +84,7 @@ type Config struct {
8484
Context interface{}
8585
}
8686

87-
// Node describes a node
87+
// Node describes a an element
8888
type Node struct {
8989
Style Style
9090
Layout Layout
@@ -297,7 +297,7 @@ func NewNode() *Node {
297297
return NewNodeWithConfig(&configDefaults)
298298
}
299299

300-
// NodeReset resets a node
300+
// Reset resets a node
301301
func (node *Node) Reset() {
302302
assertWithNode(node, len(node.Children) == 0, "Cannot reset a node which still has children attached")
303303
assertWithNode(node, node.Parent == nil, "Cannot reset a node still attached to a parent")
@@ -390,7 +390,7 @@ func (node *Node) deleteChild(child *Node) *Node {
390390
return nil
391391
}
392392

393-
// RemoveChild removes the child
393+
// RemoveChild removes child node
394394
func (node *Node) RemoveChild(child *Node) {
395395
if node.deleteChild(child) != nil {
396396
child.Layout = nodeDefaults.Layout // layout is no longer valid
@@ -399,7 +399,7 @@ func (node *Node) RemoveChild(child *Node) {
399399
}
400400
}
401401

402-
// GetChild returns a child
402+
// GetChild returns a child at a given index
403403
func (node *Node) GetChild(idx int) *Node {
404404
if idx < len(node.Children) {
405405
return node.Children[idx]
@@ -752,13 +752,11 @@ func flexDirectionCross(flexDirection FlexDirection, direction Direction) FlexDi
752752
return FlexDirectionColumn
753753
}
754754

755-
// nodeIsFlex returns true if node is flex
756755
func nodeIsFlex(node *Node) bool {
757756
return (node.Style.PositionType == PositionTypeRelative &&
758757
(resolveFlexGrow(node) != 0 || nodeResolveFlexShrink(node) != 0))
759758
}
760759

761-
// isBaselineLayout returns true if it's baseline layout
762760
func isBaselineLayout(node *Node) bool {
763761
if flexDirectionIsColumn(node.Style.FlexDirection) {
764762
return false
@@ -898,7 +896,6 @@ func nodeSetChildTrailingPosition(node *Node, child *Node, axis FlexDirection) {
898896
node.Layout.measuredDimensions[dim[axis]] - size - child.Layout.Position[pos[axis]]
899897
}
900898

901-
// nodeRelativePosition gets relative position.
902899
// If both left and right are defined, then use left. Otherwise return
903900
// +left or -right depending on which is defined.
904901
func nodeRelativePosition(node *Node, axis FlexDirection, axisSize float32) float32 {
@@ -980,7 +977,7 @@ func nodeComputeFlexBasisForChild(node *Node,
980977

981978
if !FloatIsUndefined(resolvedFlexBasis) && !FloatIsUndefined(mainAxisSize) {
982979
if FloatIsUndefined(child.Layout.computedFlexBasis) ||
983-
(ConfigIsExperimentalFeatureEnabled(child.Config, ExperimentalFeatureWebFlexBasis) &&
980+
(child.Config.IsExperimentalFeatureEnabled(ExperimentalFeatureWebFlexBasis) &&
984981
child.Layout.computedFlexBasisGeneration != currentGenerationCount) {
985982
child.Layout.computedFlexBasis =
986983
fmaxf(resolvedFlexBasis, nodePaddingAndBorderForAxis(child, mainAxis, parentWidth))
@@ -3051,17 +3048,16 @@ func CalculateLayout(node *Node, parentWidth float32, parentHeight float32, pare
30513048
}
30523049
}
30533050

3054-
// ConfigSetExperimentalFeatureEnabled enables experimental feature
3055-
func ConfigSetExperimentalFeatureEnabled(config *Config, feature ExperimentalFeature, enabled bool) {
3051+
// SetExperimentalFeatureEnabled enables experimental feature
3052+
func (config *Config) SetExperimentalFeatureEnabled(feature ExperimentalFeature, enabled bool) {
30563053
config.experimentalFeatures[feature] = enabled
30573054
}
30583055

3059-
// ConfigIsExperimentalFeatureEnabled returns if experimental feature is enabled
3060-
func ConfigIsExperimentalFeatureEnabled(config *Config, feature ExperimentalFeature) bool {
3056+
// IsExperimentalFeatureEnabled returns if experimental feature is enabled
3057+
func (config *Config) IsExperimentalFeatureEnabled(feature ExperimentalFeature) bool {
30613058
return config.experimentalFeatures[feature]
30623059
}
30633060

3064-
// log logs
30653061
func log(node *Node, level LogLevel, format string, args ...interface{}) {
30663062
fmt.Printf(format, args...)
30673063
}
@@ -3072,12 +3068,10 @@ func assertCond(cond bool, format string, args ...interface{}) {
30723068
}
30733069
}
30743070

3075-
// assertWithNode assert if cond is not true
30763071
func assertWithNode(node *Node, cond bool, format string, args ...interface{}) {
30773072
assertCond(cond, format, args...)
30783073
}
30793074

3080-
// assertWithConfig asserts with config
30813075
func assertWithConfig(config *Config, condition bool, message string) {
30823076
if !condition {
30833077
panic(message)

0 commit comments

Comments
 (0)