|
| 1 | +## Yoga bindings |
| 2 | +## |
| 3 | +## Yoga is licensed under the MIT license. |
| 4 | +## Copyright (c) Meta Platforms, Inc. and affiliates. |
| 5 | +## |
| 6 | +## These bindings, however, are licensed under the GNU General Public License, version 3. |
| 7 | +## Author: Trayambak Rai (xtrayambak at disroot dot org) |
| 8 | +import std/[strutils] |
| 9 | + |
| 10 | +static: |
| 11 | + let pwd = gorge("pwd") |
| 12 | + discard gorge("mkdir ../third_party/yoga/build") |
| 13 | + echo gorge( |
| 14 | + "cd ../third_party/yoga/build && cmake .. -DCMAKE_INSTALL_PREFIX=" & pwd & "/install" |
| 15 | + ) |
| 16 | + echo gorge("cd ../third_party/yoga/build && make -j$(nproc)") |
| 17 | + echo gorge("cd ../third_party/yoga/build && make install") |
| 18 | + echo pwd |
| 19 | + |
| 20 | +{.passC: "-I" & gorge("pwd") & "/install/include".} |
| 21 | +{.passL: "-L" & gorge("pwd") & "/install/lib64 -lyogacore".} |
| 22 | + |
| 23 | +{.push header: "<yoga/Yoga.h>".} |
| 24 | +type |
| 25 | + YGAlign* {.importc, size: sizeof(cint).} = enum |
| 26 | + YGAlignAuto |
| 27 | + YGAlignFlexStart |
| 28 | + YGAlignCenter |
| 29 | + YGAlignFlexEnd |
| 30 | + YGAlignStretch |
| 31 | + YGAlignBaseline |
| 32 | + YGAlignSpaceBetween |
| 33 | + YGAlignSpaceAround |
| 34 | + YGAlignSpaceEvenly |
| 35 | + |
| 36 | + YGDirection* {.importc, size: sizeof(cint).} = enum |
| 37 | + YGDirectionInherit |
| 38 | + YGDirectionLTR |
| 39 | + YGDirectionRTL |
| 40 | + |
| 41 | + YGEdge* {.importc, size: sizeof(cint).} = enum |
| 42 | + YGEdgeLeft |
| 43 | + YGEdgeTop |
| 44 | + YGEdgeRight |
| 45 | + YGEdgeBottom |
| 46 | + YGEdgeStart |
| 47 | + YGEdgeEnd |
| 48 | + YGEdgeHorizontal |
| 49 | + YGEdgeVertical |
| 50 | + YGEdgeAll |
| 51 | + |
| 52 | + YGJustify* {.importc, size: sizeof(cint).} = enum |
| 53 | + YGJustifyFlexStart |
| 54 | + YGJustifyCenter |
| 55 | + YGJustifyFlexEnd |
| 56 | + YGJustifySpaceBetween |
| 57 | + YGJustifySpaceAround |
| 58 | + YGJustifySpaceEvenly |
| 59 | + |
| 60 | + YGFlexDirection* {.importc, size: sizeof(cint).} = enum |
| 61 | + YGFlexDirectionColumn |
| 62 | + YGFlexDirectionColumnReverse |
| 63 | + YGFlexDirectionRow |
| 64 | + YGFlexDirectionRowReverse |
| 65 | + |
| 66 | + YGSize* {.bycopy, importc.} = object |
| 67 | + width*, height*: float |
| 68 | + |
| 69 | + YGNode* {.importc.} = object |
| 70 | + YGNodeRef* {.importc.} = pointer |
| 71 | + |
| 72 | +proc newYGNode*(): YGNodeRef {.importc: "YGNodeNew".} |
| 73 | +proc clone*(node: var YGNode) {.importc: "YGNodeClone".} |
| 74 | +proc free*(node: YGNodeRef) {.importc: "YGNodeFree".} |
| 75 | +proc freeRecursive*(node: YGNodeRef) {.importc: "YGNodeFreeRecursive".} |
| 76 | +proc finalize*(node: YGNodeRef) {.importc: "YGNodeFinalize".} |
| 77 | +proc reset*(node: YGNodeRef) {.importc: "YGNodeReset".} |
| 78 | +proc calculateLayout*( |
| 79 | + node: YGNodeRef, availableWidth, availableHeight: float, ownerDirection: YGDirection |
| 80 | +) {.importc: "YGNodeCalculateLayout".} |
| 81 | + |
| 82 | +func hasNewLayout*(node: var YGNode): bool {.importc: "YGNodeGetHasNewLayout".} |
| 83 | +proc `hasNewLayout=`*( |
| 84 | + node: YGNodeRef, hasNewLayout: bool |
| 85 | +) {.importc: "YGNodeSetHasNewLayout".} |
| 86 | + |
| 87 | +func isDirty*(node: var YGNode): bool {.importc: "YGNodeIsDirty".} |
| 88 | +proc markDirty*(node: YGNodeRef): bool {.importc: "YGNodeMarkDirty".} |
| 89 | +proc insertChild*( |
| 90 | + node: YGNodeRef, child: YGNodeRef, index: uint64 |
| 91 | +) {.importc: "YGNodeInsertChild".} |
| 92 | + |
| 93 | +proc swapChild*( |
| 94 | + node: YGNodeRef, child: YGNodeRef, index: uint64 |
| 95 | +) {.importc: "YGNodeInsertChild".} |
| 96 | + |
| 97 | +proc removeChild*(node: YGNodeRef, child: YGNodeRef) {.importc: "YGNodeRemoveChild".} |
| 98 | +proc removeAllChildren*(node: YGNodeRef) {.importc: "YGNodeRemoveAllChildren".} |
| 99 | +proc setChildrenInternal( |
| 100 | + owner: YGNodeRef, children: ptr UncheckedArray[YGNodeRef], count: uint64 |
| 101 | +) {.importc: "YGNodeSetChildren".} |
| 102 | + |
| 103 | +proc getChild*(node: YGNodeRef, index: uint64): YGNodeRef {.importc: "YGNodeGetChild".} |
| 104 | +proc childCount*(node: var YGNode): uint64 {.importc: "YGNodeGetChildCount".} |
| 105 | +proc getOwner*(node: YGNodeRef): YGNodeRef {.importc: "YGNodeGetOwner".} |
| 106 | +proc getParent*(node: YGNodeRef): YGNodeRef {.importc: "YGNodeGetParent".} |
| 107 | + |
| 108 | +proc getLeft*(node: var YGNode): float {.importc: "YGNodeLayoutGetLeft".} |
| 109 | +proc getTop*(node: var YGNode): float {.importc: "YGNodeLayoutGetTop".} |
| 110 | +proc getRight*(node: var YGNode): float {.importc: "YGNodeLayoutGetRight".} |
| 111 | +proc getBottom*(node: var YGNode): float {.importc: "YGNodeLayoutGetBottom".} |
| 112 | +proc getWidth*(node: var YGNode): float {.importc: "YGNodeLayoutGetWidth".} |
| 113 | +proc getHeight*(node: var YGNode): float {.importc: "YGNodeLayoutGetHeight".} |
| 114 | +proc getDirection*( |
| 115 | + node: var YGNode |
| 116 | +): YGDirection {.importc: "YGNodeLayoutGetDirection".} |
| 117 | + |
| 118 | +proc getHadOverflow*(node: var YGNode): bool {.importc: "YGNodeLayoutGetHadOverflow".} |
| 119 | +proc getMargin*( |
| 120 | + node: var YGNode, edge: YGEdge |
| 121 | +): float {.importc: "YGNodeLayoutGetMargin".} |
| 122 | + |
| 123 | +proc getBorder*( |
| 124 | + node: var YGNode, edge: YGEdge |
| 125 | +): float {.importc: "YGNodeLayoutGetBorder".} |
| 126 | + |
| 127 | +proc getPadding*( |
| 128 | + node: var YGNode, edge: YGEdge |
| 129 | +): float {.importc: "YGNodeLayoutGetPadding".} |
| 130 | + |
| 131 | +proc copyStyle*(dest: YGNodeRef, srcNode: var YGNode) {.importc: "YGNodeCopyStyle".} |
| 132 | +proc setDirection*( |
| 133 | + node: YGNodeRef, direction: YGDirection |
| 134 | +) {.importc: "YGNodeStyleSetDirection".} |
| 135 | + |
| 136 | +proc setFlexDirection*( |
| 137 | + node: YGNodeRef, flexDirection: YGFlexDirection |
| 138 | +) {.importc: "YGNodeStyleSetFlexDirection".} |
| 139 | + |
| 140 | +proc getFlexDirection*( |
| 141 | + node: var YGNode |
| 142 | +): YGFlexDirection {.importc: "YGNodeStyleGetFlexDirection".} |
| 143 | + |
| 144 | +proc setJustifyContent*( |
| 145 | + node: YGNodeRef, justifyContent: YGJustify |
| 146 | +) {.importc: "YGNodeStyleSetJustifyContent".} |
| 147 | + |
| 148 | +proc getJustifyContent*( |
| 149 | + node: var YGNode |
| 150 | +): YGJustify {.importc: "YGNodeStyleGetJustifyContent".} |
| 151 | + |
| 152 | +proc setWidth*(node: YGNodeRef, width: float) {.importc: "YGNodeStyleSetWidth".} |
| 153 | +proc setWidthPercent*( |
| 154 | + node: YGNodeRef, width: float |
| 155 | +) {.importc: "YGNodeStyleSetWidthPercent".} |
| 156 | + |
| 157 | +proc setWidthAuto*(node: YGNodeRef) {.importc: "YGNodeStyleSetWidthAuto".} |
| 158 | +proc setWidthMaxContent*(node: YGNodeRef) {.importc: "YGNodeStyleSetWidthMaxContent".} |
| 159 | +proc setWidthFitContent*(node: YGNodeRef) {.importc: "YGNodeStyleSetWidthFitContent".} |
| 160 | +proc setWidthStretch*(node: YGNodeRef) {.importc: "YGNodeStyleSetWidthStretch".} |
| 161 | + |
| 162 | +proc setHeight*(node: YGNodeRef, height: float) {.importc: "YGNodeStyleSetHeight".} |
| 163 | +proc setHeightPercent*( |
| 164 | + node: YGNodeRef, height: float |
| 165 | +) {.importc: "YGNodeStyleSetHeightPercent".} |
| 166 | + |
| 167 | +proc setHeightAuto*(node: YGNodeRef) {.importc: "YGNodeStyleSetHeightAuto".} |
| 168 | +proc setHeightMaxContent*(node: YGNodeRef) {.importc: "YGNodeStyleSetHeightMaxContent".} |
| 169 | +proc setHeightFitContent*(node: YGNodeRef) {.importc: "YGNodeStyleSetHeightFitContent".} |
| 170 | +proc setHeightStretch*(node: YGNodeRef) {.importc: "YGNodeStyleSetHeightStretch".} |
| 171 | + |
| 172 | +proc setAlignSelf*( |
| 173 | + node: YGNodeRef, align: YGAlign |
| 174 | +) {.importc: "YGNodeStyleSetAlignSelf".} |
| 175 | + |
| 176 | +{.pop.} |
| 177 | + |
| 178 | +# Wrapping code, just for convenience. |
| 179 | +proc setChildren*(owner: YGNodeRef, children: seq[YGNodeRef]) = |
| 180 | + var arr = cast[ptr UncheckedArray[YGNodeRef]](alloc(children.len * sizeof(YGNodeRef))) |
| 181 | + for i, _ in children: |
| 182 | + arr[i] = children[i] |
| 183 | + |
| 184 | + setChildrenInternal(owner, arr, children.len.uint64) |
| 185 | + dealloc(arr) |
0 commit comments