Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Config Code
- // type CloneNode func(oldNode *Node, parent *Node, childIndex int32) *Node
- type CloneNode func(configRef ConfigRef, oldRef NodeRef, parentRef NodeRef, childIndex int32) NodeRef
- type Config struct {
- ref ConfigRef
- ctx interface{}
- onCloneNode CloneNode
- }
- var configsByContext sync.Map
- var DefaultConfig = newConfig(ConfigGetDefault())
- func NewConfig() *Config {
- return newConfig(ConfigNew())
- }
- func configFromRef(ref ConfigRef) *Config {
- if ptr := ConfigGetContext(ref); ptr != nil {
- if value, ok := configsByContext.Load(ptr); ok {
- if config, ok := value.(*Config); ok {
- return config
- }
- }
- }
- return nil
- }
- func newConfig(ref ConfigRef) *Config {
- config := configFromRef(ref)
- if config == nil {
- config = &Config{ref: ref}
- var ctx unsafe.Pointer = C.malloc(C.size_t(1))
- ConfigSetContext(ref, ctx)
- configsByContext.Store(ctx, config)
- }
- return config
- }
- // ConfigGetContext function as declared in yoga/Yoga.h:352
- func ConfigGetContext(config ConfigRef) unsafe.Pointer {
- cconfig, cconfigAllocMap := *(*C.YGConfigRef)(unsafe.Pointer(&config)), cgoAllocsUnknown
- __ret := C.YGConfigGetContext(cconfig)
- runtime.KeepAlive(cconfigAllocMap)
- __v := *(*unsafe.Pointer)(unsafe.Pointer(&__ret))
- return __v
- }
- //
- // Node code
- //
- type Node struct {
- ctx interface{}
- ref NodeRef
- style *NodeStyle
- layout *NodeLayout
- onMeasure NodeMeasure
- onBaseline NodeBaseline
- onDirtied NodeDirtied
- onPrint NodePrint
- }
- var nodesByContext sync.Map
- func NewNode() *Node {
- ref := NodeNew()
- return newNode(ref)
- }
- func NewNodeWithConfig(config *Config) *Node {
- ref := NodeNewWithConfig(config.ref)
- return newNode(ref)
- }
- func nodeFromRef(ref NodeRef) *Node {
- if ptr := NodeGetContext(ref); ptr != nil {
- if value, ok := nodesByContext.Load(ptr); ok {
- if node, ok := value.(*Node); ok {
- return node
- }
- }
- }
- return nil
- }
- func newNode(ref NodeRef) *Node {
- node := nodeFromRef(ref)
- if node == nil {
- node = &Node{
- ref: ref,
- style: &NodeStyle{
- ref: ref,
- },
- layout: &NodeLayout{
- ref: ref,
- },
- }
- var ctx unsafe.Pointer = C.malloc(C.size_t(1))
- NodeSetContext(ref, ctx)
- nodesByContext.Store(ctx, node)
- }
- return node
- }
- func (s *NodeStyle) SetPosition(edge Edge, position float32) {
- if s.ref != nil {
- NodeStyleSetPosition(s.ref, edge, position)
- }
- }
- func (l *NodeLayout) Height() float32 {
- if l.ref != nil {
- return NodeLayoutGetHeight(l.ref)
- }
- return Undefined
- }
- func (n *Node) Layout() *NodeLayout {
- return n.layout
- }
- // NodeStyleSetPosition function as declared in yoga/Yoga.h:204
- func NodeStyleSetPosition(node NodeRef, edge Edge, position float32) {
- cnode, cnodeAllocMap := *(*C.YGNodeRef)(unsafe.Pointer(&node)), cgoAllocsUnknown
- cedge, cedgeAllocMap := (C.YGEdge)(edge), cgoAllocsUnknown
- cposition, cpositionAllocMap := (C.float)(position), cgoAllocsUnknown
- C.YGNodeStyleSetPosition(cnode, cedge, cposition)
- runtime.KeepAlive(cpositionAllocMap)
- runtime.KeepAlive(cedgeAllocMap)
- runtime.KeepAlive(cnodeAllocMap)
- }
- // NodeLayoutGetHeight function as declared in yoga/Yoga.h:284
- func NodeLayoutGetHeight(node NodeRef) float32 {
- cnode, cnodeAllocMap := *(*C.YGNodeRef)(unsafe.Pointer(&node)), cgoAllocsUnknown
- __ret := C.YGNodeLayoutGetHeight(cnode)
- runtime.KeepAlive(cnodeAllocMap)
- __v := (float32)(__ret)
- return __v
- }
- func getHeight(node *Node) float32 {
- r0 := C.getCHeight(node.ref)
- r1 := C.YGNodeLayoutGetHeight(node.ref)
- _ = r1
- return float32(r0)
- }
- /*
- float getCHeight(YGNodeRef n) {
- union ufloat {
- float f;
- unsigned u;
- } r;
- r.f = YGNodeLayoutGetHeight(n);
- printf("YGNodeLayoutGetHeight: %f\t%X\n", r.f, r.u);
- return r.f;
- }
- */
- //
- // Test
- //
- func TestRoundingInnerNodeControversyHorizontal(t *testing.T) {
- config := NewConfig()
- defer config.Free()
- root := NewNodeWithConfig(config)
- defer root.Free()
- _ = getWidth(root)
- _ = getHeight(root)
- root.Style().SetFlexDirection(FlexDirectionRow)
- root.Style().SetWidth(320)
- c0 := NewNodeWithConfig(config)
- defer c0.Free()
- c0.Style().SetFlexGrow(1)
- c0.Style().SetHeight(10)
- root.InsertChild(c0, 0)
- c1 := NewNodeWithConfig(config)
- defer c1.Free()
- c1.Style().SetFlexGrow(1)
- c1.Style().SetHeight(10)
- root.InsertChild(c1, 1)
- c1c0 := NewNodeWithConfig(config)
- defer c1c0.Free()
- c1c0.Style().SetFlexGrow(1)
- c1c0.Style().SetHeight(10)
- c1.InsertChild(c1c0, 0)
- c2 := NewNodeWithConfig(config)
- defer c2.Free()
- c2.Style().SetFlexGrow(1)
- c2.Style().SetHeight(10)
- root.InsertChild(c2, 2)
- root.Style().SetDirection(DirectionLTR)
- root.CalculateLayout(Undefined, Undefined, DirectionLTR)
- _ = getWidth(root)
- _ = getHeight(root)
- assert.EqualValues(t, 0, root.Layout().Left())
- assert.EqualValues(t, 0, root.Layout().Top())
- assert.EqualValues(t, 320, root.Layout().Width())
- assert.EqualValues(t, 320, getWidth(root))
- assert.EqualValues(t, 10, root.Layout().Height()) // fails
- assert.EqualValues(t, 10, getHeight(root)) // fails
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement