From c1bb7a90efa260ca0c7749e0cf676d3debadcc1d Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Mon, 19 Aug 2024 02:06:20 +0200 Subject: [PATCH 01/32] wip: java bindings --- Cargo.toml | 1 + bindings/java/.gitignore | 4 + bindings/java/Cargo.toml | 19 + .../java/java/com/dioxuslabs/taffy/Taffy.java | 29 + .../java/com/dioxuslabs/taffy/TaffyTree.java | 23 + .../taffy/com_dioxuslabs_taffy_TaffyTree.h | 37 + .../com/dioxuslabs/taffy/geom/TaffyLine.java | 12 + .../com/dioxuslabs/taffy/geom/TaffyPoint.java | 38 + .../com/dioxuslabs/taffy/geom/TaffyRect.java | 69 ++ .../com/dioxuslabs/taffy/geom/TaffySize.java | 12 + .../geom/grid/TaffyGenericGridPlacement.java | 51 ++ .../geom/grid/TaffyGridTrackRepetition.java | 48 ++ .../grid/TaffyMaxTrackSizingFunction.java | 69 ++ .../grid/TaffyMinTrackSizingFunction.java | 48 ++ .../TaffyNonRepeatedTrackSizingFunction.java | 15 + .../geom/grid/TaffyTrackSizingFunction.java | 51 ++ .../taffy/geom/measure/TaffyDimension.java | 31 + .../geom/measure/TaffyLengthPercentage.java | 27 + .../measure/TaffyLengthPercentageAuto.java | 31 + .../taffy/style/TaffyAlignContent.java | 58 ++ .../taffy/style/TaffyAlignItems.java | 47 ++ .../taffy/style/TaffyBoxSizing.java | 33 + .../dioxuslabs/taffy/style/TaffyDisplay.java | 31 + .../taffy/style/TaffyFlexDirection.java | 41 + .../dioxuslabs/taffy/style/TaffyFlexWrap.java | 23 + .../taffy/style/TaffyGridAutoFlow.java | 29 + .../dioxuslabs/taffy/style/TaffyOverflow.java | 46 ++ .../dioxuslabs/taffy/style/TaffyPosition.java | 28 + .../dioxuslabs/taffy/style/TaffyStyle.java | 236 ++++++ .../taffy/style/TaffyTextAlign.java | 23 + bindings/java/run.sh | 7 + bindings/java/src/conversions.rs | 742 ++++++++++++++++++ bindings/java/src/lib.rs | 66 ++ 33 files changed, 2025 insertions(+) create mode 100644 bindings/java/.gitignore create mode 100644 bindings/java/Cargo.toml create mode 100644 bindings/java/java/com/dioxuslabs/taffy/Taffy.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/TaffyLine.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/TaffyPoint.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/TaffyRect.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyDisplay.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyOverflow.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyStyle.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java create mode 100644 bindings/java/run.sh create mode 100644 bindings/java/src/conversions.rs create mode 100644 bindings/java/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index 1dfcb1eed..338876623 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,4 +102,5 @@ members = [ "scripts/import-yoga-tests", "benches", "taffy_stylo", + "bindings/java" ] diff --git a/bindings/java/.gitignore b/bindings/java/.gitignore new file mode 100644 index 000000000..a1d0ee321 --- /dev/null +++ b/bindings/java/.gitignore @@ -0,0 +1,4 @@ +.idea +*.iml +out +*.class diff --git a/bindings/java/Cargo.toml b/bindings/java/Cargo.toml new file mode 100644 index 000000000..dd681feb8 --- /dev/null +++ b/bindings/java/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "jtaffy" +version = "0.0.1" +authors = [ + "Arby Djabaev " +] +edition = "2021" +include = ["src/**/*", "Cargo.toml"] +description = "Java bindings to Taffy (A flexible UI layout library)" +repository = "https://github.com/DioxusLabs/taffy" +license = "MIT" + +[lib] +crate-type = ["cdylib"] +name = "jtaffy" + +[dependencies] +jni = "0.21.1" +taffy = { path = "../.." } diff --git a/bindings/java/java/com/dioxuslabs/taffy/Taffy.java b/bindings/java/java/com/dioxuslabs/taffy/Taffy.java new file mode 100644 index 000000000..968319f77 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/Taffy.java @@ -0,0 +1,29 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.geom.TaffyPoint; +import com.dioxuslabs.taffy.geom.TaffyRect; +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentageAuto; +import com.dioxuslabs.taffy.style.TaffyOverflow; +import com.dioxuslabs.taffy.style.TaffyStyle; + +class Taffy { + static { + System.loadLibrary("jtaffy"); + } + + public static void main(String[] args) { + TaffyTree tree = new TaffyTree(); + System.out.println(tree.ptr); + + long id = tree.newLeaf(TaffyStyle.builder() + .overflow(new TaffyPoint<>(TaffyOverflow.SCROLL, TaffyOverflow.HIDDEN)) + .inset(new TaffyRect<>(TaffyLengthPercentageAuto.auto(), TaffyLengthPercentageAuto.length(1), TaffyLengthPercentageAuto.length(1), TaffyLengthPercentageAuto.length(1))) + ); + + System.out.println("Leaf id: " + id); + + int children = tree.childCount(id); + + System.out.println("Child count: " + children); + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java b/bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java new file mode 100644 index 000000000..5c1fcbbd3 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java @@ -0,0 +1,23 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.style.TaffyStyle; + +public class TaffyTree { + public final long ptr; + + public TaffyTree() { + this.ptr = newTaffyTree(); + } + + public long newLeaf(TaffyStyle style) { + return newLeaf(this.ptr, style); + } + + public int childCount(long nodeId) { + return childCount(this.ptr, nodeId); + } + + private static native long newTaffyTree(); + private static native long newLeaf(long pointer, TaffyStyle style); + private static native int childCount(long pointer, long nodeId); +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h b/bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h new file mode 100644 index 000000000..3cc49a3ce --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h @@ -0,0 +1,37 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class com_dioxuslabs_taffy_TaffyTree */ + +#ifndef _Included_com_dioxuslabs_taffy_TaffyTree +#define _Included_com_dioxuslabs_taffy_TaffyTree +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: newTaffyTree + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree + (JNIEnv *, jclass); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: newLeaf + * Signature: (JLcom/dioxuslabs/taffy/style/TaffyStyle;)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newLeaf + (JNIEnv *, jclass, jlong, jobject); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: childCount + * Signature: (JJ)I + */ +JNIEXPORT jint JNICALL Java_com_dioxuslabs_taffy_TaffyTree_childCount + (JNIEnv *, jclass, jlong, jlong); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyLine.java b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyLine.java new file mode 100644 index 000000000..6ebe34724 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyLine.java @@ -0,0 +1,12 @@ +package com.dioxuslabs.taffy.geom; + +/** + * An abstract "line". Represents any type that has a start and an end + * @param start The start position of a line + * @param end The end position of a line + */ +public record TaffyLine( + T start, + T end +) { +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyPoint.java b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyPoint.java new file mode 100644 index 000000000..e3f9c1063 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyPoint.java @@ -0,0 +1,38 @@ +package com.dioxuslabs.taffy.geom; + +/** + * A 2-dimensional coordinate. + *

+ * When used in association with a {@link TaffyRect}, represents the top-left corner. + */ +public class TaffyPoint { + /** + * The x-coordinate + */ + private T x; + /** + * The y-coordinate + */ + private T y; + + public TaffyPoint(T x, T y) { + this.x = x; + this.y = y; + } + + public T x() { + return x; + } + + public T y() { + return y; + } + + public void x(T x) { + this.x = x; + } + + public void y(T y) { + this.y = y; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyRect.java b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyRect.java new file mode 100644 index 000000000..be3cbb4c0 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyRect.java @@ -0,0 +1,69 @@ +package com.dioxuslabs.taffy.geom; + +/** + * An axis-aligned UI rectangle + */ +public class TaffyRect{ + /** + * This can represent either the x-coordinate of the starting edge, + * or the amount of padding on the starting side. + *

+ * The starting edge is the left edge when working with LTR text, + * and the right edge when working with RTL text. + */ + private T left; + /** + * This can represent either the x-coordinate of the ending edge, + * or the amount of padding on the ending side. + */ + private T right; + /** + * This can represent either the y-coordinate of the top edge, + * or the amount of padding on the top side. + */ + private T top; + /** + * This can represent either the y-coordinate of the bottom edge, + * or the amount of padding on the bottom side. + */ + private T bottom; + + public TaffyRect(T left, T right, T top, T bottom) { + this.left = left; + this.right = right; + this.top = top; + this.bottom = bottom; + } + + public T left() { + return left; + } + + public T right() { + return right; + } + + public T top() { + return top; + } + + public T bottom() { + return bottom; + } + + public void left(T left) { + this.left = left; + } + + public void right(T right) { + this.right = right; + } + + public void top(T top) { + this.top = top; + } + + public void bottom(T bottom) { + this.bottom = bottom; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java new file mode 100644 index 000000000..bea390574 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java @@ -0,0 +1,12 @@ +package com.dioxuslabs.taffy.geom; + +/** + * The width and height of a {@link TaffyRect} + * @param width The x extent of the rectangle + * @param height The y extent of the rectangle + */ +public record TaffySize( + T width, + T height +) { +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java new file mode 100644 index 000000000..9be3e667e --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java @@ -0,0 +1,51 @@ +package com.dioxuslabs.taffy.geom.grid; + +import com.dioxuslabs.taffy.style.TaffyGridAutoFlow; + +/** + * A grid line placement specification which is generic over the coordinate system that it uses to define + * grid line positions. + *

+ * GenericGridPlacement is aliased as GridPlacement and is exposed to users of Taffy to define styles. + * GenericGridPlacement is aliased as OriginZeroGridPlacement and is used internally for placement computations. + *

+ * See [`crate::compute::grid::type::coordinates`] for documentation on the different coordinate systems. + */ +public class TaffyGenericGridPlacement { + private final byte type; + private final short value; + + private TaffyGenericGridPlacement(byte type, short value) { + this.type = type; + this.value = value; + } + + /** + * Place item according to the auto-placement algorithm, and the parent's {@link TaffyGridAutoFlow} property + */ + public static TaffyGenericGridPlacement auto() { + return new TaffyGenericGridPlacement((byte) 0, (short) 0); + } + + /** + * Place item at specified line (column or row) index + */ + public static TaffyGenericGridPlacement line(short value) { + return new TaffyGenericGridPlacement((byte) 1, value); + } + + /** + * Item should span specified number of tracks (columns or rows) + */ + public static TaffyGenericGridPlacement span(short value) { + return new TaffyGenericGridPlacement((byte) 2, value); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof TaffyGenericGridPlacement gpg)) { + return false; + } + return type == gpg.type && value == gpg.value; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java new file mode 100644 index 000000000..8900a5cb0 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java @@ -0,0 +1,48 @@ +package com.dioxuslabs.taffy.geom.grid; + +/** + * The first argument to a repeated track definition. This type represents the type of automatic repetition to perform. + *

+ * See ... for an explanation of how auto-repeated + * track definitions work and the difference between AutoFit and AutoFill. + */ +public class TaffyGridTrackRepetition { + private final byte type; + private final short value; + + private TaffyGridTrackRepetition(byte type, short value) { + this.type = type; + this.value = value; + } + + /** + * Auto-repeating tracks should be generated to fit the container + * See: ... + */ + public static TaffyGridTrackRepetition autoFill() { + return new TaffyGridTrackRepetition((byte) 0, (short) 0); + } + + /** + * Auto-repeating tracks should be generated to fit the container + * See: ... + */ + public static TaffyGridTrackRepetition autoFit() { + return new TaffyGridTrackRepetition((byte) 1, (short) 0); + } + + /** + * The specified tracks should be repeated exacts N times + */ + public static TaffyGridTrackRepetition count(short count) { + return new TaffyGridTrackRepetition((byte) 2, count); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof TaffyGridTrackRepetition lpo)) { + return false; + } + return type == lpo.type && value == lpo.value; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java new file mode 100644 index 000000000..7c7da5535 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java @@ -0,0 +1,69 @@ +package com.dioxuslabs.taffy.geom.grid; + +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; + +/** + * Maximum track sizing function + *

+ * Specifies the maximum size of a grid track. A grid track will automatically size between it's minimum and maximum size based + * on the size of it's contents, the amount of available space, and the sizing constraint the grid is being size under. + * See ... + */ +public class TaffyMaxTrackSizingFunction { + private final byte type; + private final Object value; + + private TaffyMaxTrackSizingFunction(byte type, TaffyLengthPercentage value) { + this.type = type; + this.value = value; + } + + private TaffyMaxTrackSizingFunction(byte type, float value) { + this.type = type; + this.value = value; + } + + /** + * Track maximum size should be a fixed length or percentage value + */ + public static TaffyMaxTrackSizingFunction fixed(TaffyLengthPercentage value) { + return new TaffyMaxTrackSizingFunction((byte) 0, value); + } + + /** + * Track maximum size should be content sized under a min-content constraint + */ + public static TaffyMaxTrackSizingFunction minContent() { + return new TaffyMaxTrackSizingFunction((byte) 1, null); + } + + /** + * Track maximum size should be content sized under a max-content constraint + */ + public static TaffyMaxTrackSizingFunction maxContent() { + return new TaffyMaxTrackSizingFunction((byte) 2, null); + } + + /** + * Track maximum size should be sized according to the fit-content formula + */ + public static TaffyMaxTrackSizingFunction fitContent(TaffyLengthPercentage value) { + return new TaffyMaxTrackSizingFunction((byte) 3, value); + } + + /** + * Track maximum size should be automatically sized + */ + public static TaffyMaxTrackSizingFunction auto() { + return new TaffyMaxTrackSizingFunction((byte) 4, null); + } + + /** + * The dimension as a fraction of the total available grid space (`fr` units in CSS) + * Specified value is the numerator of the fraction. Denominator is the sum of all fraction specified in that grid dimension + * Spec: Spec + */ + public static TaffyMaxTrackSizingFunction fraction(float value) { + return new TaffyMaxTrackSizingFunction((byte) 5, value); + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java new file mode 100644 index 000000000..26ee6d884 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java @@ -0,0 +1,48 @@ +package com.dioxuslabs.taffy.geom.grid; + +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; + +/** + * Minimum track sizing function + *

+ * Specifies the minimum size of a grid track. A grid track will automatically size between it's minimum and maximum size based + * on the size of it's contents, the amount of available space, and the sizing constraint the grid is being size under. + * See ... + */ +public class TaffyMinTrackSizingFunction { + private final byte type; + private final TaffyLengthPercentage value; + + private TaffyMinTrackSizingFunction(byte type, TaffyLengthPercentage value) { + this.type = type; + this.value = value; + } + + /** + * Track minimum size should be a fixed length or percentage value + */ + public static TaffyMinTrackSizingFunction fixed(TaffyLengthPercentage value) { + return new TaffyMinTrackSizingFunction((byte) 0, value); + } + + /** + * Track minimum size should be content sized under a min-content constraint + */ + public static TaffyMinTrackSizingFunction minContent() { + return new TaffyMinTrackSizingFunction((byte) 1, null); + } + + /** + * Track minimum size should be content sized under a max-content constraint + */ + public static TaffyMinTrackSizingFunction maxContent() { + return new TaffyMinTrackSizingFunction((byte) 2, null); + } + + /** + * Track minimum size should be automatically sized + */ + public static TaffyMinTrackSizingFunction auto() { + return new TaffyMinTrackSizingFunction((byte) 3, null); + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java new file mode 100644 index 000000000..f39d49ccd --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java @@ -0,0 +1,15 @@ +package com.dioxuslabs.taffy.geom.grid; + +/** + * The sizing function for a grid track (row/column) (either auto-track or template track) + * May either be a MinMax variant which specifies separate values for the min-/max- track sizing functions + * or a scalar value which applies to both track sizing functions. + * + * @param min The value representing the minimum + * @param max The value representing the maximum + */ +public record TaffyNonRepeatedTrackSizingFunction( + TaffyMinTrackSizingFunction min, + TaffyMaxTrackSizingFunction max +) { +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java new file mode 100644 index 000000000..4317d75eb --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java @@ -0,0 +1,51 @@ +package com.dioxuslabs.taffy.geom.grid; + +import java.util.List; + +/** + * The sizing function for a grid track (row/column) + * See ... + */ +public abstract class TaffyTrackSizingFunction { + private byte type; + + private TaffyTrackSizingFunction(byte type) { + this.type = type; + } + + /** + * A single non-repeated track + */ + public static TaffyTrackSizingFunction single(TaffyNonRepeatedTrackSizingFunction func) { + return new TaffyTrackSingleSizingFunction(func); + } + + /** + * Automatically generate grid tracks to fit the available space using the specified definite track lengths + * Only valid if every track in template (not just the repetition) has a fixed size. + */ + public static TaffyTrackSizingFunction repeat(TaffyGridTrackRepetition reps, List list) { + return new TaffyTrackRepeatSizingFunction(reps, list); + } + + private static class TaffyTrackSingleSizingFunction extends TaffyTrackSizingFunction { + private final TaffyNonRepeatedTrackSizingFunction func; + + private TaffyTrackSingleSizingFunction(TaffyNonRepeatedTrackSizingFunction func) { + super((byte) 0); + this.func = func; + } + } + + private static class TaffyTrackRepeatSizingFunction extends TaffyTrackSizingFunction { + private final TaffyGridTrackRepetition repetitions; + private final List functions; + + private TaffyTrackRepeatSizingFunction(TaffyGridTrackRepetition repetitions, + List functions) { + super((byte) 1); + this.repetitions = repetitions; + this.functions = functions; + } + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java b/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java new file mode 100644 index 000000000..17e8bb26f --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java @@ -0,0 +1,31 @@ +package com.dioxuslabs.taffy.geom.measure; + +public class TaffyDimension { + private final byte type; + private final float value; + + private TaffyDimension(byte type, float value) { + this.type = type; + this.value = value; + } + + public static TaffyDimension length(float value) { + return new TaffyDimension((byte) 0, value); + } + + public static TaffyDimension percent(float value) { + return new TaffyDimension((byte) 1, value); + } + + public static TaffyDimension auto() { + return new TaffyDimension((byte) 2, 0); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof TaffyDimension lpo)) { + return false; + } + return type == lpo.type && value == lpo.value; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java b/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java new file mode 100644 index 000000000..c199443a4 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java @@ -0,0 +1,27 @@ +package com.dioxuslabs.taffy.geom.measure; + +public class TaffyLengthPercentage { + private final byte type; + private final float value; + + private TaffyLengthPercentage(byte type, float value) { + this.type = type; + this.value = value; + } + + public static TaffyLengthPercentage length(float value) { + return new TaffyLengthPercentage((byte) 0, value); + } + + public static TaffyLengthPercentage percentage(float value) { + return new TaffyLengthPercentage((byte) 1, value); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof TaffyLengthPercentage lpo)) { + return false; + } + return type == lpo.type && value == lpo.value; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java b/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java new file mode 100644 index 000000000..03e42636f --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java @@ -0,0 +1,31 @@ +package com.dioxuslabs.taffy.geom.measure; + +public class TaffyLengthPercentageAuto { + private final byte type; + private final float value; + + private TaffyLengthPercentageAuto(byte type, float value) { + this.type = type; + this.value = value; + } + + public static TaffyLengthPercentageAuto length(float value) { + return new TaffyLengthPercentageAuto((byte) 0, value); + } + + public static TaffyLengthPercentageAuto percent(float value) { + return new TaffyLengthPercentageAuto((byte) 1, value); + } + + public static TaffyLengthPercentageAuto auto() { + return new TaffyLengthPercentageAuto((byte) 2, 0); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof TaffyLengthPercentageAuto lpo)) { + return false; + } + return type == lpo.type && value == lpo.value; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java new file mode 100644 index 000000000..7aded2bde --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java @@ -0,0 +1,58 @@ +package com.dioxuslabs.taffy.style; + +/** + * Sets the distribution of space between and around content items + * For Flexbox it controls alignment in the cross axis + * For Grid it controls alignment in the block axis + *

+ * MDN + */ +public enum TaffyAlignContent { + /** + * Items are packed toward the start of the axis + */ + START, + /** + * Items are packed toward the end of the axis + */ + END, + /** + * Items are packed towards the flex-relative start of the axis. + *

+ * For flex containers with {@link TaffyFlexDirection#ROW_REVERSE} or {@link TaffyFlexDirection#COLUMN_REVERSE} + * this is equivalent to {@link TaffyAlignContent#END}. In all other cases it is equivalent to + * {@link TaffyAlignContent#START}. + */ + FLEX_START, + /** + * Items are packed towards the flex-relative end of the axis. + *

+ * For flex containers with {@link TaffyFlexDirection#ROW_REVERSE} or {@link TaffyFlexDirection#COLUMN_REVERSE} + * this is equivalent to {@link TaffyAlignContent#START}. In all other cases it is equivalent to + * {@link TaffyAlignContent#END}. + */ + FLEX_END, + /** + * Items are centered along the middle of the cross axis + */ + CENTER, + /** + * Items are stretched to fill the container + */ + STRETCH, + /** + * The first and last items are aligned flush with the edges of the container (no gap) + * The gap between items is distributed evenly. + */ + SPACE_BETWEEN, + /** + * The gap between the first and last items is exactly THE SAME as the gap between items. + * The gaps are distributed evenly + */ + SPACE_EVENLY, + /** + * The gap between the first and last items is exactly HALF the gap between items. + * The gaps are distributed evenly in proportion to these ratios. + */ + SPACE_AROUND +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java new file mode 100644 index 000000000..1c5a9eb67 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java @@ -0,0 +1,47 @@ +package com.dioxuslabs.taffy.style; + +/** + * Used to control how child nodes are aligned. + * For Flexbox it controls alignment in the cross axis + * For Grid it controls alignment in the block axis + *

+ * MDN + */ +public enum TaffyAlignItems { + /** + * Items are packed toward the start of the axis + */ + START, + /** + * Items are packed toward the end of the axis + */ + END, + /** + * Items are packed towards the flex-relative start of the axis. + *

+ * For flex containers with {@link TaffyFlexDirection#ROW_REVERSE} or {@link TaffyFlexDirection#COLUMN_REVERSE} + * this is equivalent to {@link TaffyAlignItems#END}. In all other cases it is equivalent to + * {@link TaffyAlignItems#START}. + */ + FLEX_START, + /** + * Items are packed towards the flex-relative end of the axis. + *

+ * For flex containers with {@link TaffyFlexDirection#ROW_REVERSE} or {@link TaffyFlexDirection#COLUMN_REVERSE} + * this is equivalent to {@link TaffyAlignItems#START}. In all other cases it is equivalent to + * {@link TaffyAlignItems#END}. + */ + FLEX_END, + /** + * Items are packed along the center of the cross axis + */ + CENTER, + /** + * Items are aligned such as their baselines align + */ + BASELINE, + /** + * Stretch to fill the container + */ + STRETCH +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java new file mode 100644 index 000000000..c98311cc3 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java @@ -0,0 +1,33 @@ +package com.dioxuslabs.taffy.style; + +/** + * Specifies whether size styles for this node are assigned to the node's "content box" or "border box" + *

+ * - The "content box" is the node's inner size excluding padding, border and margin + * - The "border box" is the node's outer size including padding and border (but still excluding margin) + *

+ * This property modifies the application of the following styles: + *

+ * - `size` + * - `min_size` + * - `max_size` + * - `flex_basis` + *

... + */ +public enum TaffyBoxSizing { + /** + * Size styles such size, min_size, max_size specify the box's "content box" (the size excluding padding/border/margin) + */ + BORDER_BOX, + /** + * Size styles such size, min_size, max_size specify the box's "border box" (the size excluding margin but including padding/border) + */ + CONTENT_BOX; + + private final int internal; + + TaffyBoxSizing() { + internal = ordinal(); + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyDisplay.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyDisplay.java new file mode 100644 index 000000000..ac545acbc --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyDisplay.java @@ -0,0 +1,31 @@ +package com.dioxuslabs.taffy.style; + +/** + * Sets the layout used for the children of this node + *

+ * The default values depends on on which feature flags are enabled. The order of precedence is: Flex, Grid, Block, None. + */ +public enum TaffyDisplay { + /** + * The children will follow the block layout algorithm + */ + BLOCK, + /** + * The children will follow the flexbox layout algorithm + */ + FLEX, + /** + * The children will follow the CSS Grid layout algorithm + */ + GRID, + /** + * The node is hidden, and it's children will also be hidden + */ + NONE; + + private final int internal; + + TaffyDisplay() { + internal = ordinal(); + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java new file mode 100644 index 000000000..ca08b141d --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java @@ -0,0 +1,41 @@ +package com.dioxuslabs.taffy.style; + +/** + * The direction of the flexbox layout main axis. + *

+ * There are always two perpendicular layout axes: main (or primary) and cross (or secondary). + * Adding items will cause them to be positioned adjacent to each other along the main axis. + * By varying this value throughout your tree, you can create complex axis-aligned layouts. + *

+ * Items are always aligned relative to the cross axis, and justified relative to the main axis. + *

+ * The default behavior is {@link TaffyFlexDirection#ROW}. + *

+ * Specification + */ +public enum TaffyFlexDirection { + /** + * Defines +x as the main axis + *

+ * Items will be added from left to right in a row. + */ + ROW, + /** + * Defines +y as the main axis + *

+ * Items will be added from top to bottom in a column. + */ + COLUMN, + /** + * Defines -x as the main axis + *

+ * Items will be added from right to left in a row. + */ + ROW_REVERSE, + /** + * Defines -y as the main axis + *

+ * Items will be added from bottom to top in a column. + */ + COLUMN_REVERSE +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java new file mode 100644 index 000000000..922ed072b --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java @@ -0,0 +1,23 @@ +package com.dioxuslabs.taffy.style; + +/** + * Controls whether flex items are forced onto one line or can wrap onto multiple lines. + *

+ * Defaults to {@link TaffyFlexWrap#NO_WRAP} + *

+ * Specification + */ +public enum TaffyFlexWrap { + /** + * Items will not wrap and stay on a single line + */ + NO_WRAP, + /** + * Items will wrap according to this item's {@link TaffyFlexDirection} + */ + WRAP, + /** + * Items will wrap in the opposite direction to this item's {@link TaffyFlexDirection} + */ + WRAP_REVERSE +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java new file mode 100644 index 000000000..8b09a710a --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java @@ -0,0 +1,29 @@ +package com.dioxuslabs.taffy.style; + +/** + * Controls whether grid items are placed row-wise or column-wise. And whether the sparse or dense packing algorithm is used. + *

+ * The "dense" packing algorithm attempts to fill in holes earlier in the grid, if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items. + *

+ * Defaults to [`GridAutoFlow::Row`] + *

+ * MDN + */ +public enum TaffyGridAutoFlow { + /** + * Items are placed by filling each row in turn, adding new rows as necessary + */ + ROW, + /** + * Items are placed by filling each column in turn, adding new columns as necessary. + */ + COLUMN, + /** + * Combines ROW with the dense packing algorithm. + */ + ROW_DENSE, + /** + * Combines COLUMN with the dense packing algorithm. + */ + COLUMN_DENSE +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyOverflow.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyOverflow.java new file mode 100644 index 000000000..0f3848631 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyOverflow.java @@ -0,0 +1,46 @@ +package com.dioxuslabs.taffy.style; + +/** + * How children overflowing their container should affect layout + *

+ * In CSS the primary effect of this property is to control whether contents of a parent container that overflow that container should + * be displayed anyway, be clipped, or trigger the container to become a scroll container. However it also has secondary effects on layout, + * the main ones being: + *

+ * - The automatic minimum size Flexbox/CSS Grid items with non-`Visible` overflow is `0` rather than being content based + * - `Overflow::Scroll` nodes have space in the layout reserved for a scrollbar (width controlled by the `scrollbar_width` property) + *

+ * In Taffy, we only implement the layout related secondary effects as we are not concerned with drawing/painting. The amount of space reserved for + * a scrollbar is controlled by the `scrollbar_width` property. If this is `0` then `Scroll` behaves identically to `Hidden`. + *

+ * ... + */ +public enum TaffyOverflow { + /** + * The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content. + * Content that overflows this node *should* contribute to the scroll region of its parent. + */ + VISIBLE, + /** + * The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content. + * Content that overflows this node should *not* contribute to the scroll region of its parent. + */ + CLIP, + /** + * The automatic minimum size of this node as a flexbox/grid item should be `0`. + * Content that overflows this node should *not* contribute to the scroll region of its parent. + */ + HIDDEN, + /** + * The automatic minimum size of this node as a flexbox/grid item should be `0`. Additionally, space should be reserved + * for a scrollbar. The amount of space reserved is controlled by the `scrollbar_width` property. + * Content that overflows this node should *not* contribute to the scroll region of its parent. + */ + SCROLL; + + private final int internal; + + TaffyOverflow() { + internal = ordinal(); + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java new file mode 100644 index 000000000..ce1209e8a --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java @@ -0,0 +1,28 @@ +package com.dioxuslabs.taffy.style; + +/** + * The positioning strategy for this item. + *

+ * This controls both how the origin is determined for the [`Style::position`] field, + * and whether or not the item will be controlled by flexbox's layout algorithm. + *

+ * WARNING: this enum follows the behavior of CSS's `position` property, + * which can be unintuitive. + *

+ * {@link TaffyPosition#RELATIVE} is the default value, in contrast to the default behavior in CSS. + */ +public enum TaffyPosition { + /** + * The offset is computed relative to the final position given by the layout algorithm. + * Offsets do not affect the position of any other items; they are effectively a correction factor applied at the end. + */ + RELATIVE, + /** + * The offset is computed relative to this item's closest positioned ancestor, if any. + * Otherwise, it is placed relative to the origin. + * No space is created for the item in the page layout, and its size will not be altered. + *

+ * WARNING: to opt-out of layouting entirely, you must use {@link TaffyDisplay#NONE} instead on your {@link TaffyStyle} object. + */ + ABSOLUTE +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyStyle.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyStyle.java new file mode 100644 index 000000000..3e67dceee --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyStyle.java @@ -0,0 +1,236 @@ +package com.dioxuslabs.taffy.style; + +import com.dioxuslabs.taffy.geom.TaffyLine; +import com.dioxuslabs.taffy.geom.TaffyPoint; +import com.dioxuslabs.taffy.geom.TaffyRect; +import com.dioxuslabs.taffy.geom.TaffySize; +import com.dioxuslabs.taffy.geom.grid.TaffyGenericGridPlacement; +import com.dioxuslabs.taffy.geom.grid.TaffyNonRepeatedTrackSizingFunction; +import com.dioxuslabs.taffy.geom.grid.TaffyTrackSizingFunction; +import com.dioxuslabs.taffy.geom.measure.TaffyDimension; +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentageAuto; + +import java.util.List; + +@SuppressWarnings("all") +public class TaffyStyle { + private TaffyDisplay display; + private boolean itemIsTable; + private TaffyBoxSizing boxSizing; + + private TaffyPoint overflow; + private float scrollbarWidth; + + private TaffyPosition position; + private TaffyRect inset; + + private TaffySize size; + private TaffySize minSize; + private TaffySize maxSize; + private Float aspectRatio; + + private TaffyRect margin; + private TaffyRect padding; + private TaffyRect border; + + private TaffyAlignItems alignItems; + private TaffyAlignItems alignSelf; + private TaffyAlignItems justifyItems; + private TaffyAlignItems justifySelf; + private TaffyAlignContent alignContent; + private TaffyAlignContent justifyContent; + private TaffySize gap; + + private TaffyTextAlign textAlign; + + private TaffyFlexDirection flexDirection; + private TaffyFlexWrap flexWrap; + + private TaffyDimension flexBasis; + private float flexGrow; + private float flexShrink; + + private List gridTemplateRows; + private List gridTemplateColumns; + private List gridAutoRows; + private List gridAutoColumns; + private TaffyGridAutoFlow gridAutoFlow; + + private TaffyLine gridRow; + private TaffyLine gridColumn; + + public static TaffyStyle builder() { + return new TaffyStyle(); + } + + public TaffyStyle display(TaffyDisplay display) { + this.display = display; + return this; + } + + public TaffyStyle itemIsTable(boolean itemIsTable) { + this.itemIsTable = itemIsTable; + return this; + } + + public TaffyStyle boxSizing(TaffyBoxSizing boxSizing) { + this.boxSizing = boxSizing; + return this; + } + + public TaffyStyle overflow(TaffyPoint overflow) { + this.overflow = overflow; + return this; + } + + public TaffyStyle scrollbarWidth(float scrollbarWidth) { + this.scrollbarWidth = scrollbarWidth; + return this; + } + + public TaffyStyle position(TaffyPosition position) { + this.position = position; + return this; + } + + public TaffyStyle inset(TaffyRect inset) { + this.inset = inset; + return this; + } + + public TaffyStyle size(TaffySize size) { + this.size = size; + return this; + } + + public TaffyStyle minSize(TaffySize minSize) { + this.minSize = minSize; + return this; + } + + public TaffyStyle maxSize(TaffySize maxSize) { + this.maxSize = maxSize; + return this; + } + + public TaffyStyle aspectRatio(Float aspectRatio) { + this.aspectRatio = aspectRatio; + return this; + } + + public TaffyStyle margin(TaffyRect margin) { + this.margin = margin; + return this; + } + + public TaffyStyle padding(TaffyRect padding) { + this.padding = padding; + return this; + } + + public TaffyStyle border(TaffyRect border) { + this.border = border; + return this; + } + + public TaffyStyle alignItems(TaffyAlignItems alignItems) { + this.alignItems = alignItems; + return this; + } + + public TaffyStyle alignSelf(TaffyAlignItems alignSelf) { + this.alignSelf = alignSelf; + return this; + } + + public TaffyStyle justifyItems(TaffyAlignItems justifyItems) { + this.justifyItems = justifyItems; + return this; + } + + public TaffyStyle justifySelf(TaffyAlignItems justifySelf) { + this.justifySelf = justifySelf; + return this; + } + + public TaffyStyle alignContent(TaffyAlignContent alignContent) { + this.alignContent = alignContent; + return this; + } + + public TaffyStyle justifyContent(TaffyAlignContent justifyContent) { + this.justifyContent = justifyContent; + return this; + } + + public TaffyStyle gap(TaffySize gap) { + this.gap = gap; + return this; + } + + public TaffyStyle textAlign(TaffyTextAlign textAlign) { + this.textAlign = textAlign; + return this; + } + + public TaffyStyle flexDirection(TaffyFlexDirection flexDirection) { + this.flexDirection = flexDirection; + return this; + } + + public TaffyStyle flexWrap(TaffyFlexWrap flexWrap) { + this.flexWrap = flexWrap; + return this; + } + + public TaffyStyle flexBasis(TaffyDimension flexBasis) { + this.flexBasis = flexBasis; + return this; + } + + public TaffyStyle flexGrow(float flexGrow) { + this.flexGrow = flexGrow; + return this; + } + + public TaffyStyle flexShrink(float flexShrink) { + this.flexShrink = flexShrink; + return this; + } + + public TaffyStyle gridTemplateRows(List gridTemplateRows) { + this.gridTemplateRows = gridTemplateRows; + return this; + } + + public TaffyStyle gridTemplateColumns(List gridTemplateColumns) { + this.gridTemplateColumns = gridTemplateColumns; + return this; + } + + public TaffyStyle gridAutoRows(List gridAutoRows) { + this.gridAutoRows = gridAutoRows; + return this; + } + + public TaffyStyle gridAutoColumns(List gridAutoColumns) { + this.gridAutoColumns = gridAutoColumns; + return this; + } + + public TaffyStyle gridAutoFlow(TaffyGridAutoFlow gridAutoFlow) { + this.gridAutoFlow = gridAutoFlow; + return this; + } + + public TaffyStyle gridRow(TaffyLine gridRow) { + this.gridRow = gridRow; + return this; + } + + public TaffyStyle gridColumn(TaffyLine gridColumn) { + this.gridColumn = gridColumn; + return this; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java new file mode 100644 index 000000000..e66079a3c --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java @@ -0,0 +1,23 @@ +package com.dioxuslabs.taffy.style; + +/** + * Used by block layout to implement the legacy behaviour of `

` and `
` + */ +public enum TaffyTextAlign { + /** + * No special legacy text align behaviour. + */ + AUTO, + /** + * Corresponds to `-webkit-left` or `-moz-left` in browsers + */ + LEGACY_LEFT, + /** + * Corresponds to `-webkit-right` or `-moz-right` in browsers + */ + LEGACY_RIGHT, + /** + * Corresponds to `-webkit-center` or `-moz-center` in browsers + */ + LEGACY_CENTER +} diff --git a/bindings/java/run.sh b/bindings/java/run.sh new file mode 100644 index 000000000..bd1ae517c --- /dev/null +++ b/bindings/java/run.sh @@ -0,0 +1,7 @@ +cargo build && +cd java && +javac $(find . -name "*.java") && +cd com/dioxuslabs/taffy && +javac -h . ./**/*.java && +cd ../../.. && +java -Djava.library.path="../../../target/debug" com.dioxuslabs.taffy.Taffy diff --git a/bindings/java/src/conversions.rs b/bindings/java/src/conversions.rs new file mode 100644 index 000000000..b2e44f148 --- /dev/null +++ b/bindings/java/src/conversions.rs @@ -0,0 +1,742 @@ +use jni::JNIEnv; +use jni::objects::{JObject, JValue}; +use taffy::{AlignContent, AlignItems, BoxSizing, Dimension, Display, FlexDirection, FlexWrap, GridAutoFlow, GridPlacement, GridTrackRepetition, LengthPercentage, LengthPercentageAuto, Line, MaxTrackSizingFunction, MinTrackSizingFunction, NonRepeatedTrackSizingFunction, Overflow, Point, Position, Rect, Size, Style, TextAlign, TrackSizingFunction}; +use taffy::style_helpers::{TaffyGridLine, TaffyGridSpan}; + +pub(crate) fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local>) -> Style { + let display = get_enum_equiv(env, style, "display", "Lcom/dioxuslabs/taffy/style/TaffyDisplay;", get_display); + let item_is_table = get_field_bool(env, style, "itemIsTable", false); + let box_sizing = get_enum_equiv(env, style, "boxSizing", "Lcom/dioxuslabs/taffy/style/TaffyBoxSizing;", get_box_sizing); + + let overflow = get_field_point(env, style, "overflow", get_overflow, || Overflow::default()); + let scrollbar_width = get_field_float(env, style, "scrollbarWidth", 0.0); + + let position = get_enum_equiv(env, style, "position", "Lcom/dioxuslabs/taffy/style/TaffyPosition;", get_position); + let inset = get_field_rect(env, style, "inset", get_length_percentage_auto, || LengthPercentageAuto::Auto); + + let size = get_field_size(env, style, "size", get_dimension, || Dimension::Auto); + let min_size = get_field_size(env, style, "minSize", get_dimension, || Dimension::Auto); + let max_size = get_field_size(env, style, "maxSize", get_dimension, || Dimension::Auto); + let aspect_ratio = get_float(env, style, "aspectRatio", || None); + + let margin = get_field_rect(env, style, "margin", get_length_percentage_auto, || LengthPercentageAuto::Length(0.0)); + let padding = get_field_rect(env, style, "padding", get_length_percentage, || LengthPercentage::Length(0.0)); + let border = get_field_rect(env, style, "border", get_length_percentage, || LengthPercentage::Length(0.0)); + + let align_items = get_align_items(env, style, "alignItems"); + let align_self = get_align_items(env, style, "alignSelf"); + let justify_items = get_align_items(env, style, "justifyItems"); + let justify_self = get_align_items(env, style, "justifySelf"); + let align_content = get_align_content(env, style, "alignContent"); + let justify_content = get_align_content(env, style, "justifyContent"); + let gap = get_field_size(env, style, "gap", get_length_percentage, || LengthPercentage::Length(0.0)); + + let text_align = get_enum_equiv(env, style, "textAlign", "Lcom/dioxuslabs/taffy/style/TaffyTextAlign;", get_text_align); + let flex_direction = get_enum_equiv(env, style, "flexDirection", "Lcom/dioxuslabs/taffy/style/TaffyFlexDirection;", get_flex_direction); + let flex_wrap = get_enum_equiv(env, style, "flexWrap", "Lcom/dioxuslabs/taffy/style/TaffyFlexWrap;", get_flex_wrap); + + let flex_basis = resolve_field(env, style, "flexBasis", "Lcom/dioxuslabs/taffy/geom/measure/TaffyDimension;", get_dimension, || Dimension::Auto); + let flex_grow = get_field_float(env, style, "flexGrow", 0.0); + let flex_shrink = get_field_float(env, style, "flexShrink", 1.0); + + let grid_template_rows = get_field_list(env, style, "gridTemplateRows", get_track_sizing_function); + let grid_template_columns = get_field_list(env, style, "gridTemplateColumns", get_track_sizing_function); + let grid_auto_rows = get_field_list(env, style, "gridAutoRows", get_non_repeated_track_sizing_function); + let grid_auto_columns = get_field_list(env, style, "gridAutoColumns", get_non_repeated_track_sizing_function); + let grid_auto_flow = get_enum_equiv(env, style, "gridAutoFlow", "Lcom/dioxuslabs/taffy/style/TaffyGridAutoFlow;", get_grid_auto_flow); + + let grid_row = get_field_line(env, style, "gridRow", get_grid_placement, || GridPlacement::Auto); + let grid_column = get_field_line(env, style, "gridColumn", get_grid_placement, || GridPlacement::Auto); + + println!("Inset: {:?}", inset); + + Style { + display, + item_is_table, + box_sizing, + + overflow, + scrollbar_width, + + position, + inset, + + size, + min_size, + max_size, + aspect_ratio, + + margin, + padding, + border, + + align_items, + align_self, + justify_items, + justify_self, + align_content, + justify_content, + gap, + + text_align, + flex_direction, + flex_wrap, + + flex_basis, + flex_grow, + flex_shrink, + + grid_template_rows, + grid_template_columns, + grid_auto_rows, + grid_auto_columns, + grid_auto_flow, + + grid_row, + grid_column + } +} + +fn get_float<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, def: fn() -> Option) -> Option { + let float_obj = get_field_as(env, object, field, "Ljava/lang/Float;"); + + if float_obj.is_null() { + return def(); + } + + Some( + env + .call_method(float_obj, "floatValue", "()F", &[]) + .expect("Couldn't call floatValue on java/lang/Float") + .f() + .unwrap() as f32 + ) +} + +fn get_field_point<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F, def: fn() -> T) -> Point +where + F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, +{ + let point_field = &get_field_as(env, object, field, "Lcom/dioxuslabs/taffy/geom/TaffyPoint;"); + if point_field.is_null() { + return Point { + x: def(), + y: def(), + }; + } + + let x_field = &get_field_as(env, point_field, "x", "Ljava/lang/Object;"); + let y_field = &get_field_as(env, point_field, "y", "Ljava/lang/Object;"); + + Point { + x: f(env, x_field, def), + y: f(env, y_field, def), + } +} + +fn get_field_line<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F, def: fn() -> T) -> Line +where + F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, +{ + let line_field = &get_field_as(env, object, field, "Lcom/dioxuslabs/taffy/geom/TaffyLine;"); + if line_field.is_null() { + return Line { + start: def(), + end: def(), + }; + } + + let s_field = &get_field_as(env, line_field, "start", "Ljava/lang/Object;"); + let e_field = &get_field_as(env, line_field, "end", "Ljava/lang/Object;"); + + Line { + start: f(env, s_field, def), + end: f(env, e_field, def), + } +} + +fn get_field_list<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F) -> Vec +where + F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, +{ + let list_field = &get_field_as(env, object, field, "Ljava/util/List;"); + + if list_field.is_null() { + return Vec::new() + } + + let list_size = env + .call_method(list_field, "size", "()I", &[]) + .expect("Couldn't call size on List") + .i() + .unwrap(); + + let mut objects = Vec::new(); + for i in 0..list_size { + let object = env + .call_method(list_field, "get", "(I)Ljava/lang/Object;", &[JValue::from(i)]) + .expect("Couldn't call get on List") + .l() + .unwrap(); + + objects.push(f(env, &object)); + } + + objects +} + +fn get_field_size<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F, def: fn() -> T) -> Size +where + F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, +{ + let point_field = &get_field_as(env, object, field, "Lcom/dioxuslabs/taffy/geom/TaffySize;"); + if point_field.is_null() { + return Size { + width: def(), + height: def(), + }; + } + + let w_field = &get_field_as(env, point_field, "width", "Ljava/lang/Object;"); + let h_field = &get_field_as(env, point_field, "height", "Ljava/lang/Object;"); + + Size { + width: f(env, w_field, def), + height: f(env, h_field, def), + } +} + +fn get_field_rect<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F, def: fn() -> T) -> Rect +where + F: Fn(&mut JNIEnv<'local>, &JObject<'local>, fn() -> T) -> T, +{ + let point_field = &get_field_as(env, object, field, "Lcom/dioxuslabs/taffy/geom/TaffyRect;"); + if point_field.is_null() { + return Rect { + left: def(), + right: def(), + top: def(), + bottom: def(), + }; + } + + let l_field = &get_field_as(env, point_field, "left", "Ljava/lang/Object;"); + let r_field = &get_field_as(env, point_field, "right", "Ljava/lang/Object;"); + let t_field = &get_field_as(env, point_field, "top", "Ljava/lang/Object;"); + let b_field = &get_field_as(env, point_field, "bottom", "Ljava/lang/Object;"); + + Rect { + left: f(env, l_field, def), + right: f(env, r_field, def), + top: f(env, t_field, def), + bottom: f(env, b_field, def), + } +} + +fn get_field_as<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, jtype: &str) -> JObject<'local> { + env + .get_field(object, field, jtype) + .expect("Couldn't get field") + .l() + .unwrap() +} + +fn resolve_field<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, jtype: &str, f: F, def: fn() -> T) -> T +where + F: Fn(&mut JNIEnv<'local>, &JObject<'local>, fn() -> T) -> T, +{ + let field = env + .get_field(object, field, jtype) + .expect("Couldn't get field") + .l() + .unwrap(); + + f(env, &field, def) +} + +fn get_field_bool(env: &mut JNIEnv, object: &JObject, field: &str, def: bool) -> bool { + env + .get_field(object, field, "Z") + .expect("Couldn't get field") + .z() + .unwrap_or(def) +} + +fn get_field_float(env: &mut JNIEnv, object: &JObject, field: &str, def: f32) -> f32 { + env + .get_field(object, field, "F") + .expect("Couldn't get field") + .f() + .unwrap_or(def) +} + +fn get_enum_value(env: &mut JNIEnv, object: &JObject) -> i32 { + env + .get_field(object, "internal", "I") + .expect("Couldn't get field") + .i() + .unwrap() +} + +fn get_enum_equiv<'local, T, F>(env: &mut JNIEnv<'local>, style: &JObject<'local>, field: &str, jtype: &str, + f: F) -> T where + F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, +{ + let field = &get_field_as(env, style, field, jtype); + f(env, field) +} + +fn get_display<'local>(env: &mut JNIEnv<'local>, display: &JObject<'local>) -> Display { + if display.is_null() { + return Display::default(); + } + + let internal = get_enum_value(env, display); + + match internal { + 0 => Display::Block, + 1 => Display::Flex, + 2 => Display::Grid, + _ => Display::None, + } +} + +fn get_box_sizing<'local>(env: &mut JNIEnv<'local>, box_sizing: &JObject<'local>) -> BoxSizing { + if box_sizing.is_null() { + return BoxSizing::default(); + } + + let internal = get_enum_value(env, box_sizing); + + match internal { + 0 => BoxSizing::BorderBox, + _ => BoxSizing::ContentBox + } +} + +fn get_length_percentage_auto<'local>(env: &mut JNIEnv<'local>, + length_percentage_auto: &JObject<'local>, + def: fn() -> LengthPercentageAuto) -> LengthPercentageAuto { + if length_percentage_auto.is_null() { + return def(); + } + + let internal = env + .get_field(length_percentage_auto, "type", "B") + .expect("Couldn't get field") + .b() + .unwrap() as i8; + + match internal { + 0 => LengthPercentageAuto::Length( + env + .get_field(length_percentage_auto, "value", "F") + .expect("Couldn't get field") + .f() + .unwrap() as f32 + ), + 1 => LengthPercentageAuto::Percent( + env + .get_field(length_percentage_auto, "value", "F") + .expect("Couldn't get field") + .f() + .unwrap() as f32 + ), + _ => LengthPercentageAuto::Auto + } +} + +fn get_non_repeated_track_sizing_function<'local>(env: &mut JNIEnv<'local>, + non_repeated_track_sizing_function: &JObject<'local>, + def: fn() -> NonRepeatedTrackSizingFunction) -> NonRepeatedTrackSizingFunction { + if non_repeated_track_sizing_function.is_null() { + return def(); + } + + let n_field = &get_field_as(env, non_repeated_track_sizing_function, "min", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction;"); + let x_field = &get_field_as(env, non_repeated_track_sizing_function, "max", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction;"); + + let min = get_min_track_sizing_function(env, n_field, || MinTrackSizingFunction::Auto); + let max = get_max_track_sizing_function(env, x_field, || MaxTrackSizingFunction::Auto); + + NonRepeatedTrackSizingFunction { + min, + max, + } +} + +fn get_track_sizing_function<'local>(env: &mut JNIEnv<'local>, + track_sizing_function: &JObject<'local>, + def: fn() -> TrackSizingFunction) -> TrackSizingFunction { + if track_sizing_function.is_null() { + return def(); + } + + let internal = env + .get_field(track_sizing_function, "type", "B") + .expect("Couldn't get field") + .b() + .unwrap() as i8; + + match internal { + 0 => TrackSizingFunction::Single({ + let func_field = &get_field_as(env, track_sizing_function, "func", "Lcom/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction;"); + + get_non_repeated_track_sizing_function( + env, + func_field, + || NonRepeatedTrackSizingFunction { + min: MinTrackSizingFunction::Auto, + max: MaxTrackSizingFunction::Auto, + }, + ) + }), + _ => TrackSizingFunction::Repeat( + { + let reps_field = &get_field_as(env, track_sizing_function, "repetitions", "Lcom/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition;"); + get_grid_track_repetition(env, reps_field, || GridTrackRepetition::AutoFill) + }, + { + get_field_list(env, track_sizing_function, "functions", |env, func| { + let func_field = &get_field_as(env, func, "func", "Lcom/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction;"); + + get_non_repeated_track_sizing_function( + env, + func_field, + || NonRepeatedTrackSizingFunction { + min: MinTrackSizingFunction::Auto, + max: MaxTrackSizingFunction::Auto, + }, + ) + }) + }, + ) + } +} + +fn get_min_track_sizing_function<'local>(env: &mut JNIEnv<'local>, + min_track_sizing_function: &JObject<'local>, + def: fn() -> MinTrackSizingFunction) -> MinTrackSizingFunction { + if min_track_sizing_function.is_null() { + return def(); + } + + let internal = env + .get_field(min_track_sizing_function, "type", "B") + .expect("Couldn't get field") + .b() + .unwrap() as i8; + + match internal { + 0 => MinTrackSizingFunction::Fixed( + resolve_field( + env, + min_track_sizing_function, + "value", + "Lcom/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage;", + get_length_percentage, + || LengthPercentage::Length(0.0), + ) + ), + 1 => MinTrackSizingFunction::MinContent, + 2 => MinTrackSizingFunction::MaxContent, + _ => MinTrackSizingFunction::Auto + } +} + +fn get_max_track_sizing_function<'local>(env: &mut JNIEnv<'local>, + min_track_sizing_function: &JObject<'local>, + def: fn() -> MaxTrackSizingFunction) -> MaxTrackSizingFunction { + if min_track_sizing_function.is_null() { + return def(); + } + + let internal = env + .get_field(min_track_sizing_function, "type", "B") + .expect("Couldn't get field") + .b() + .unwrap() as i8; + + match internal { + 0 => MaxTrackSizingFunction::Fixed( + resolve_field( + env, + min_track_sizing_function, + "value", + "Ljava/lang/Object;", + get_length_percentage, + || LengthPercentage::Length(0.0), + ) + ), + 1 => MaxTrackSizingFunction::MinContent, + 2 => MaxTrackSizingFunction::MaxContent, + 3 => MaxTrackSizingFunction::FitContent( + resolve_field( + env, + min_track_sizing_function, + "value", + "Ljava/lang/Object;", + get_length_percentage, + || LengthPercentage::Length(0.0), + ) + ), + 4 => MaxTrackSizingFunction::Auto, + _ => MaxTrackSizingFunction::Fraction( + env + .get_field(min_track_sizing_function, "value", "Ljava/lang/Object;") + .expect("Couldn't get field") + .f() + .unwrap() as f32 + ), + } +} + +fn get_dimension<'local>(env: &mut JNIEnv<'local>, + dimension: &JObject<'local>, + def: fn() -> Dimension) -> Dimension { + if dimension.is_null() { + return def(); + } + + let internal = env + .get_field(dimension, "type", "B") + .expect("Couldn't get field") + .b() + .unwrap() as i8; + + match internal { + 0 => Dimension::Length( + env + .get_field(dimension, "value", "F") + .expect("Couldn't get field") + .f() + .unwrap() as f32 + ), + 1 => Dimension::Percent( + env + .get_field(dimension, "value", "F") + .expect("Couldn't get field") + .f() + .unwrap() as f32 + ), + _ => Dimension::Auto + } +} + +fn get_grid_track_repetition<'local>(env: &mut JNIEnv<'local>, + grid_track_repetition: &JObject<'local>, + def: fn() -> GridTrackRepetition) -> GridTrackRepetition { + if grid_track_repetition.is_null() { + return def(); + } + + let internal = env + .get_field(grid_track_repetition, "type", "B") + .expect("Couldn't get field") + .b() + .unwrap() as i8; + + match internal { + 0 => GridTrackRepetition::AutoFill, + 1 => GridTrackRepetition::AutoFit, + _ => GridTrackRepetition::Count( + env + .get_field(grid_track_repetition, "value", "S") + .expect("Couldn't get field") + .s() + .unwrap() as u16 + ) + } +} + +fn get_length_percentage<'local>(env: &mut JNIEnv<'local>, + length_percentage: &JObject<'local>, + def: fn() -> LengthPercentage) -> LengthPercentage { + if length_percentage.is_null() { + return def(); + } + + let internal = env + .get_field(length_percentage, "type", "B") + .expect("Couldn't get field") + .b() + .unwrap() as i8; + + match internal { + 0 => LengthPercentage::Length( + env + .get_field(length_percentage, "value", "F") + .expect("Couldn't get field") + .f() + .unwrap() as f32 + ), + _ => LengthPercentage::Percent( + env + .get_field(length_percentage, "value", "F") + .expect("Couldn't get field") + .f() + .unwrap() as f32 + ), + } +} + +fn get_grid_placement<'local>(env: &mut JNIEnv<'local>, + grid_placement: &JObject<'local>, + def: fn() -> GridPlacement) -> GridPlacement { + if grid_placement.is_null() { + return def(); + } + + let internal = env + .get_field(grid_placement, "type", "B") + .expect("Couldn't get field") + .b() + .unwrap() as i8; + + match internal { + 0 => GridPlacement::Auto, + 1 => GridPlacement::from_line_index( + env + .get_field(grid_placement, "value", "S") + .expect("Couldn't get field") + .s() + .unwrap() as i16 + ), + _ => GridPlacement::from_span( + env + .get_field(grid_placement, "value", "F") + .expect("Couldn't get field") + .f() + .unwrap() as u16 + ), + } +} + +fn get_overflow<'local>(env: &mut JNIEnv<'local>, overflow: &JObject<'local>) -> Overflow { + if overflow.is_null() { + return Overflow::default(); + } + + let internal = get_enum_value(env, overflow); + + match internal { + 0 => Overflow::Visible, + 1 => Overflow::Clip, + 2 => Overflow::Hidden, + _ => Overflow::Scroll + } +} + +fn get_position<'local>(env: &mut JNIEnv<'local>, position: &JObject<'local>) -> Position { + if position.is_null() { + return Position::default(); + } + + let internal = get_enum_value(env, position); + + match internal { + 0 => Position::Relative, + _ => Position::Absolute + } +} + +fn get_text_align<'local>(env: &mut JNIEnv<'local>, text_align: &JObject<'local>) -> TextAlign { + if text_align.is_null() { + return TextAlign::default(); + } + + let internal = get_enum_value(env, text_align); + + match internal { + 0 => TextAlign::Auto, + 1 => TextAlign::LegacyLeft, + 2 => TextAlign::LegacyRight, + _ => TextAlign::LegacyCenter + } +} + +fn get_flex_direction<'local>(env: &mut JNIEnv<'local>, flex_direction: &JObject<'local>) -> FlexDirection { + if flex_direction.is_null() { + return FlexDirection::default(); + } + + let internal = get_enum_value(env, flex_direction); + + match internal { + 0 => FlexDirection::Row, + 1 => FlexDirection::Column, + 2 => FlexDirection::RowReverse, + _ => FlexDirection::ColumnReverse + } +} + +fn get_flex_wrap<'local>(env: &mut JNIEnv<'local>, flex_wrap: &JObject<'local>) -> FlexWrap { + if flex_wrap.is_null() { + return FlexWrap::default(); + } + + let internal = get_enum_value(env, flex_wrap); + + match internal { + 0 => FlexWrap::NoWrap, + 1 => FlexWrap::Wrap, + _ => FlexWrap::WrapReverse + } +} + +fn get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, grid_auto_flow: &JObject<'local>) -> GridAutoFlow { + if grid_auto_flow.is_null() { + return GridAutoFlow::default(); + } + + let internal = get_enum_value(env, grid_auto_flow); + + match internal { + 0 => GridAutoFlow::Row, + 1 => GridAutoFlow::Column, + 2 => GridAutoFlow::RowDense, + _ => GridAutoFlow::ColumnDense + } +} + +fn get_align_items<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local>, field: &str) -> Option { + let align_items = &get_field_as(env, style, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignItems;"); + + if align_items.is_null() { + return None; + } + + let internal = get_enum_value(env, align_items); + + match internal { + 0 => Some(AlignItems::Start), + 1 => Some(AlignItems::End), + 2 => Some(AlignItems::FlexStart), + 3 => Some(AlignItems::FlexEnd), + 4 => Some(AlignItems::Center), + 5 => Some(AlignItems::Baseline), + _ => Some(AlignItems::Stretch) + } +} + +fn get_align_content<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local>, field: &str) -> Option { + let align_content = &get_field_as(env, style, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignContent;"); + + if align_content.is_null() { + return None; + } + + let internal = get_enum_value(env, align_content); + + match internal { + 0 => Some(AlignContent::Start), + 1 => Some(AlignContent::End), + 2 => Some(AlignContent::FlexStart), + 3 => Some(AlignContent::FlexEnd), + 4 => Some(AlignContent::Center), + 5 => Some(AlignContent::Stretch), + 6 => Some(AlignContent::SpaceBetween), + 7 => Some(AlignContent::SpaceEvenly), + _ => Some(AlignContent::SpaceAround) + } +} diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs new file mode 100644 index 000000000..bed0568ed --- /dev/null +++ b/bindings/java/src/lib.rs @@ -0,0 +1,66 @@ +mod conversions; + +use std::panic; +// This is the interface to the JVM that we'll call the majority of our +// methods on. +use jni::JNIEnv; + +// These objects are what you should use as arguments to your native +// function. They carry extra lifetime information to prevent them escaping +// this context and getting used after being GC'd. +use jni::objects::{JClass, JObject}; + +// This is just a pointer. We'll be returning it from our function. We +// can't return one of the objects with lifetime information because the +// lifetime checker won't let us. +use jni::sys::{jint, jlong, jobject}; + +use taffy::{NodeId, TaffyTree, TraversePartialTree}; + +use conversions::get_style; + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree<'local>(_env: JNIEnv<'local>, + _class: JClass<'local>) + -> jlong { + let tree: Box> = Box::new(TaffyTree::new()); + Box::into_raw(tree) as jlong +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>(mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + style: jobject) + -> jlong { + unsafe { + let style = get_style(&mut env, &JObject::from_raw(style)); + + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + return -2; + } + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + let res = tree.new_leaf(style); + res.map_or(-1, |v| v.0 as jlong) as jlong + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_childCount<'local>(_env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node_id: jlong) + -> jint { + let result = panic::catch_unwind(|| { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + return -2; + } + let tree: &mut TaffyTree<()> = unsafe { &mut *raw_ptr }; + let res = tree.child_count(NodeId::from(node_id as u64)); + res as jint + }); + + result.unwrap_or_else(|_| -9) +} From 21ec83db2114fc77dd67035e1a18d8978d5ac0ce Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 20 Aug 2024 06:35:37 +0200 Subject: [PATCH 02/32] Pretty much usable (there are missing functions such as remove and so, but I think the main parts are done (mainly parsing Style, returning Layout)) --- .../java/java/com/dioxuslabs/taffy/Taffy.java | 28 +- .../java/com/dioxuslabs/taffy/TaffyTree.java | 25 + .../taffy/com_dioxuslabs_taffy_TaffyTree.h | 24 + .../com/dioxuslabs/taffy/geom/TaffySize.java | 26 + .../geom/measure/TaffyAvailableSpace.java | 31 + .../taffy/style/TaffyAlignContent.java | 8 +- .../taffy/style/TaffyAlignItems.java | 8 +- .../taffy/style/TaffyFlexDirection.java | 8 +- .../dioxuslabs/taffy/style/TaffyFlexWrap.java | 8 +- .../taffy/style/TaffyGridAutoFlow.java | 8 +- .../dioxuslabs/taffy/style/TaffyPosition.java | 8 +- .../taffy/style/TaffyTextAlign.java | 8 +- .../dioxuslabs/taffy/tree/TaffyLayout.java | 34 + bindings/java/src/collections.rs | 55 ++ bindings/java/src/conversions.rs | 752 ++---------------- bindings/java/src/enums.rs | 233 ++++++ bindings/java/src/geom.rs | 177 +++++ bindings/java/src/java.rs | 90 +++ bindings/java/src/lib.rs | 107 ++- bindings/java/src/measure.rs | 265 ++++++ bindings/java/src/primitives.rs | 257 ++++++ 21 files changed, 1424 insertions(+), 736 deletions(-) create mode 100644 bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java create mode 100644 bindings/java/java/com/dioxuslabs/taffy/tree/TaffyLayout.java create mode 100644 bindings/java/src/collections.rs create mode 100644 bindings/java/src/enums.rs create mode 100644 bindings/java/src/geom.rs create mode 100644 bindings/java/src/java.rs create mode 100644 bindings/java/src/measure.rs create mode 100644 bindings/java/src/primitives.rs diff --git a/bindings/java/java/com/dioxuslabs/taffy/Taffy.java b/bindings/java/java/com/dioxuslabs/taffy/Taffy.java index 968319f77..9139dd59a 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/Taffy.java +++ b/bindings/java/java/com/dioxuslabs/taffy/Taffy.java @@ -1,10 +1,10 @@ package com.dioxuslabs.taffy; -import com.dioxuslabs.taffy.geom.TaffyPoint; import com.dioxuslabs.taffy.geom.TaffyRect; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentageAuto; -import com.dioxuslabs.taffy.style.TaffyOverflow; +import com.dioxuslabs.taffy.geom.TaffySize; +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; import com.dioxuslabs.taffy.style.TaffyStyle; +import com.dioxuslabs.taffy.tree.TaffyLayout; class Taffy { static { @@ -12,18 +12,22 @@ class Taffy { } public static void main(String[] args) { - TaffyTree tree = new TaffyTree(); - System.out.println(tree.ptr); - - long id = tree.newLeaf(TaffyStyle.builder() - .overflow(new TaffyPoint<>(TaffyOverflow.SCROLL, TaffyOverflow.HIDDEN)) - .inset(new TaffyRect<>(TaffyLengthPercentageAuto.auto(), TaffyLengthPercentageAuto.length(1), TaffyLengthPercentageAuto.length(1), TaffyLengthPercentageAuto.length(1))) + TaffyTree taffy = new TaffyTree(); + long node = taffy.newLeaf(TaffyStyle.builder() + .padding(new TaffyRect<>( + TaffyLengthPercentage.percentage(1f), + TaffyLengthPercentage.length(0f), + TaffyLengthPercentage.percentage(1f), + TaffyLengthPercentage.length(0f) + )) ); - System.out.println("Leaf id: " + id); + taffy.computeLayout(node, TaffySize.definiteAvailableSize(200, 100)); - int children = tree.childCount(id); + System.out.println("Getting the layout of nodes"); - System.out.println("Child count: " + children); + TaffyLayout layout = taffy.layout(node); + System.out.println("Width: " + layout.size().width() + " = 200.0"); + System.out.println("Height: " + layout.size().height() + " = 200.0"); } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java b/bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java index 5c1fcbbd3..79f58151e 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java +++ b/bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java @@ -1,6 +1,11 @@ package com.dioxuslabs.taffy; +import com.dioxuslabs.taffy.geom.TaffySize; +import com.dioxuslabs.taffy.geom.measure.TaffyAvailableSpace; import com.dioxuslabs.taffy.style.TaffyStyle; +import com.dioxuslabs.taffy.tree.TaffyLayout; + +import java.util.List; public class TaffyTree { public final long ptr; @@ -13,11 +18,31 @@ public long newLeaf(TaffyStyle style) { return newLeaf(this.ptr, style); } + public long newWithChildren(TaffyStyle style, List children) { + return newWithChildren(this.ptr, style, children); + } + public int childCount(long nodeId) { return childCount(this.ptr, nodeId); } + public void computeLayout(long node, TaffySize availableSize) { + computeLayout(this.ptr, node, availableSize); + } + + public TaffyLayout layout(long node) { + return layout(this.ptr, node); + } + private static native long newTaffyTree(); + private static native long newLeaf(long pointer, TaffyStyle style); + + private static native long newWithChildren(long pointer, TaffyStyle style, List children); + private static native int childCount(long pointer, long nodeId); + + private static native void computeLayout(long pointer, long node, TaffySize availableSize); + + private static native TaffyLayout layout(long pointer, long node); } diff --git a/bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h b/bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h index 3cc49a3ce..a7e3f01f3 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h +++ b/bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h @@ -23,6 +23,14 @@ JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newLeaf (JNIEnv *, jclass, jlong, jobject); +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: newWithChildren + * Signature: (JLcom/dioxuslabs/taffy/style/TaffyStyle;Ljava/util/List;)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren + (JNIEnv *, jclass, jlong, jobject, jobject); + /* * Class: com_dioxuslabs_taffy_TaffyTree * Method: childCount @@ -31,6 +39,22 @@ JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newLeaf JNIEXPORT jint JNICALL Java_com_dioxuslabs_taffy_TaffyTree_childCount (JNIEnv *, jclass, jlong, jlong); +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: computeLayout + * Signature: (JJLcom/dioxuslabs/taffy/geom/TaffySize;)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_computeLayout + (JNIEnv *, jclass, jlong, jlong, jobject); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: layout + * Signature: (JJ)Lcom/dioxuslabs/taffy/tree/TaffyLayout; + */ +JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_layout + (JNIEnv *, jclass, jlong, jlong); + #ifdef __cplusplus } #endif diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java index bea390574..d050f7fa6 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java @@ -1,5 +1,8 @@ package com.dioxuslabs.taffy.geom; +import com.dioxuslabs.taffy.geom.measure.TaffyAvailableSpace; +import com.dioxuslabs.taffy.geom.measure.TaffyDimension; + /** * The width and height of a {@link TaffyRect} * @param width The x extent of the rectangle @@ -9,4 +12,27 @@ public record TaffySize( T width, T height ) { + public static TaffySize lengthDimension(float width, float height) { + return new TaffySize<>(TaffyDimension.length(width), TaffyDimension.length(height)); + } + + public static TaffySize percentDimension(float width, float height) { + return new TaffySize<>(TaffyDimension.percent(width), TaffyDimension.percent(height)); + } + + public static TaffySize autoDimension() { + return new TaffySize<>(TaffyDimension.auto(), TaffyDimension.auto()); + } + + public static TaffySize definiteAvailableSize(float width, float height) { + return new TaffySize<>(TaffyAvailableSpace.definite(width), TaffyAvailableSpace.definite(height)); + } + + public static TaffySize minContentAvailableSize() { + return new TaffySize<>(TaffyAvailableSpace.minContent(), TaffyAvailableSpace.minContent()); + } + + public static TaffySize maxContentAvailableSize() { + return new TaffySize<>(TaffyAvailableSpace.maxContent(), TaffyAvailableSpace.maxContent()); + } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java b/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java new file mode 100644 index 000000000..e4eaff7d3 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java @@ -0,0 +1,31 @@ +package com.dioxuslabs.taffy.geom.measure; + +public class TaffyAvailableSpace { + private final byte type; + private final float value; + + private TaffyAvailableSpace(byte type, float value) { + this.type = type; + this.value = value; + } + + public static TaffyAvailableSpace definite(float value) { + return new TaffyAvailableSpace((byte) 0, value); + } + + public static TaffyAvailableSpace minContent() { + return new TaffyAvailableSpace((byte) 1, 0); + } + + public static TaffyAvailableSpace maxContent() { + return new TaffyAvailableSpace((byte) 2, 0); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof TaffyAvailableSpace as)) { + return false; + } + return type == as.type && value == as.value; + } +} diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java index 7aded2bde..5ccf4379c 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java @@ -54,5 +54,11 @@ public enum TaffyAlignContent { * The gap between the first and last items is exactly HALF the gap between items. * The gaps are distributed evenly in proportion to these ratios. */ - SPACE_AROUND + SPACE_AROUND; + + private final int internal; + + TaffyAlignContent() { + internal = ordinal(); + } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java index 1c5a9eb67..6c418efa0 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java @@ -43,5 +43,11 @@ public enum TaffyAlignItems { /** * Stretch to fill the container */ - STRETCH + STRETCH; + + private final int internal; + + TaffyAlignItems() { + internal = ordinal(); + } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java index ca08b141d..0dff87b81 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java @@ -37,5 +37,11 @@ public enum TaffyFlexDirection { *

* Items will be added from bottom to top in a column. */ - COLUMN_REVERSE + COLUMN_REVERSE; + + private final int internal; + + TaffyFlexDirection() { + internal = ordinal(); + } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java index 922ed072b..c2cd36dbb 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java @@ -19,5 +19,11 @@ public enum TaffyFlexWrap { /** * Items will wrap in the opposite direction to this item's {@link TaffyFlexDirection} */ - WRAP_REVERSE + WRAP_REVERSE; + + private final int internal; + + TaffyFlexWrap() { + internal = ordinal(); + } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java index 8b09a710a..b4a0e40a7 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java @@ -25,5 +25,11 @@ public enum TaffyGridAutoFlow { /** * Combines COLUMN with the dense packing algorithm. */ - COLUMN_DENSE + COLUMN_DENSE; + + private final int internal; + + TaffyGridAutoFlow() { + internal = ordinal(); + } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java index ce1209e8a..9c20fc5ef 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java @@ -24,5 +24,11 @@ public enum TaffyPosition { *

* WARNING: to opt-out of layouting entirely, you must use {@link TaffyDisplay#NONE} instead on your {@link TaffyStyle} object. */ - ABSOLUTE + ABSOLUTE; + + private final int internal; + + TaffyPosition() { + internal = ordinal(); + } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java index e66079a3c..72aca7987 100644 --- a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java +++ b/bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java @@ -19,5 +19,11 @@ public enum TaffyTextAlign { /** * Corresponds to `-webkit-center` or `-moz-center` in browsers */ - LEGACY_CENTER + LEGACY_CENTER; + + private final int internal; + + TaffyTextAlign() { + internal = ordinal(); + } } diff --git a/bindings/java/java/com/dioxuslabs/taffy/tree/TaffyLayout.java b/bindings/java/java/com/dioxuslabs/taffy/tree/TaffyLayout.java new file mode 100644 index 000000000..a62838b60 --- /dev/null +++ b/bindings/java/java/com/dioxuslabs/taffy/tree/TaffyLayout.java @@ -0,0 +1,34 @@ +package com.dioxuslabs.taffy.tree; + +import com.dioxuslabs.taffy.geom.TaffyPoint; +import com.dioxuslabs.taffy.geom.TaffyRect; +import com.dioxuslabs.taffy.geom.TaffySize; + +/** + * The final result of a layout algorithm for a single node. + * @param order The relative ordering of the node + *

+ * Nodes with a higher order should be rendered on top of those with a lower order. This is + * effectively a topological sort of each tree. + * @param location The top-left corner of the node + * @param size The width and height of the node + * @param contentSize The width and height of the content inside the node. This may be larger than the size + * of the node in the case of overflowing content and is useful for computing a "scroll + * width/height" for scrollable nodes + * @param scrollbarSize The size of the scrollbars in each dimension. If there is no scrollbar then the + * size will be zero. + * @param border The size of the borders of the node + * @param padding The size of the padding of the node + * @param margin The size of the margin of the node + */ +public record TaffyLayout( + int order, + TaffyPoint location, + TaffySize size, + TaffySize contentSize, + TaffySize scrollbarSize, + TaffyRect border, + TaffyRect padding, + TaffyRect margin +) { +} diff --git a/bindings/java/src/collections.rs b/bindings/java/src/collections.rs new file mode 100644 index 000000000..ab9326017 --- /dev/null +++ b/bindings/java/src/collections.rs @@ -0,0 +1,55 @@ +use std::convert::TryInto; +use jni::JNIEnv; +use jni::objects::{JObject, JValue, JValueOwned}; +use crate::conversions::f_get_value; + +/// Unwraps a Java List into a Rust Vec +pub unsafe fn get_list<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F) -> Vec +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> Option, +{ + let jlist_object = value.l().unwrap(); + + if jlist_object.is_null() { + return Vec::with_capacity(0); + } + + let list = env.get_list(&jlist_object).unwrap(); + + let jlist_size = env + .call_method(&list, "size", "()I", &[]) + .expect("Couldn't call size on List") + .i() + .unwrap(); + + let mut vec: Vec = Vec::new(); + for i in 0..jlist_size { + let element = env.call_method(&list, "get", "(I)Ljava/lang/Object;", &[JValue::Int(i)]).unwrap(); + + let value = f(env, element); + if value.is_some() { + vec.push(value.unwrap()); + } + } + + vec +} + +/// Unwraps a Java List into a Rust array, it basically uses `unwrap_jlist` and then tries to convert the Vec into an array +#[allow(dead_code)] +pub unsafe fn get_array<'local, T, F, const N: usize>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F) -> [T; N] +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> Option, +{ + let vec = get_list(env, value, f); + vec.try_into() + .unwrap_or_else(|v: Vec| panic!("Expected a Vec of length {} but it was {}", N, v.len())) +} + +pub unsafe fn f_get_list<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F) -> Vec +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> Option, +{ + let value = f_get_value(env, base, field, "Ljava/util/List;"); + get_list(env, value, f) +} diff --git a/bindings/java/src/conversions.rs b/bindings/java/src/conversions.rs index b2e44f148..0654fac8a 100644 --- a/bindings/java/src/conversions.rs +++ b/bindings/java/src/conversions.rs @@ -1,54 +1,62 @@ use jni::JNIEnv; -use jni::objects::{JObject, JValue}; -use taffy::{AlignContent, AlignItems, BoxSizing, Dimension, Display, FlexDirection, FlexWrap, GridAutoFlow, GridPlacement, GridTrackRepetition, LengthPercentage, LengthPercentageAuto, Line, MaxTrackSizingFunction, MinTrackSizingFunction, NonRepeatedTrackSizingFunction, Overflow, Point, Position, Rect, Size, Style, TextAlign, TrackSizingFunction}; -use taffy::style_helpers::{TaffyGridLine, TaffyGridSpan}; - -pub(crate) fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local>) -> Style { - let display = get_enum_equiv(env, style, "display", "Lcom/dioxuslabs/taffy/style/TaffyDisplay;", get_display); - let item_is_table = get_field_bool(env, style, "itemIsTable", false); - let box_sizing = get_enum_equiv(env, style, "boxSizing", "Lcom/dioxuslabs/taffy/style/TaffyBoxSizing;", get_box_sizing); - - let overflow = get_field_point(env, style, "overflow", get_overflow, || Overflow::default()); - let scrollbar_width = get_field_float(env, style, "scrollbarWidth", 0.0); - - let position = get_enum_equiv(env, style, "position", "Lcom/dioxuslabs/taffy/style/TaffyPosition;", get_position); - let inset = get_field_rect(env, style, "inset", get_length_percentage_auto, || LengthPercentageAuto::Auto); - - let size = get_field_size(env, style, "size", get_dimension, || Dimension::Auto); - let min_size = get_field_size(env, style, "minSize", get_dimension, || Dimension::Auto); - let max_size = get_field_size(env, style, "maxSize", get_dimension, || Dimension::Auto); - let aspect_ratio = get_float(env, style, "aspectRatio", || None); - - let margin = get_field_rect(env, style, "margin", get_length_percentage_auto, || LengthPercentageAuto::Length(0.0)); - let padding = get_field_rect(env, style, "padding", get_length_percentage, || LengthPercentage::Length(0.0)); - let border = get_field_rect(env, style, "border", get_length_percentage, || LengthPercentage::Length(0.0)); - - let align_items = get_align_items(env, style, "alignItems"); - let align_self = get_align_items(env, style, "alignSelf"); - let justify_items = get_align_items(env, style, "justifyItems"); - let justify_self = get_align_items(env, style, "justifySelf"); - let align_content = get_align_content(env, style, "alignContent"); - let justify_content = get_align_content(env, style, "justifyContent"); - let gap = get_field_size(env, style, "gap", get_length_percentage, || LengthPercentage::Length(0.0)); - - let text_align = get_enum_equiv(env, style, "textAlign", "Lcom/dioxuslabs/taffy/style/TaffyTextAlign;", get_text_align); - let flex_direction = get_enum_equiv(env, style, "flexDirection", "Lcom/dioxuslabs/taffy/style/TaffyFlexDirection;", get_flex_direction); - let flex_wrap = get_enum_equiv(env, style, "flexWrap", "Lcom/dioxuslabs/taffy/style/TaffyFlexWrap;", get_flex_wrap); - - let flex_basis = resolve_field(env, style, "flexBasis", "Lcom/dioxuslabs/taffy/geom/measure/TaffyDimension;", get_dimension, || Dimension::Auto); - let flex_grow = get_field_float(env, style, "flexGrow", 0.0); - let flex_shrink = get_field_float(env, style, "flexShrink", 1.0); - - let grid_template_rows = get_field_list(env, style, "gridTemplateRows", get_track_sizing_function); - let grid_template_columns = get_field_list(env, style, "gridTemplateColumns", get_track_sizing_function); - let grid_auto_rows = get_field_list(env, style, "gridAutoRows", get_non_repeated_track_sizing_function); - let grid_auto_columns = get_field_list(env, style, "gridAutoColumns", get_non_repeated_track_sizing_function); - let grid_auto_flow = get_enum_equiv(env, style, "gridAutoFlow", "Lcom/dioxuslabs/taffy/style/TaffyGridAutoFlow;", get_grid_auto_flow); - - let grid_row = get_field_line(env, style, "gridRow", get_grid_placement, || GridPlacement::Auto); - let grid_column = get_field_line(env, style, "gridColumn", get_grid_placement, || GridPlacement::Auto); - - println!("Inset: {:?}", inset); +use jni::objects::{JObject, JValueOwned}; +use taffy::{Dimension, GridPlacement, Line, Overflow, Point, Rect, Size, Style}; +use crate::collections::f_get_list; +use crate::enums::{f_get_align_content, f_get_align_items, f_get_box_sizing, f_get_display, f_get_flex_direction, f_get_flex_wrap, f_get_grid_auto_flow, f_get_position, f_get_text_align, get_overflow}; +use crate::geom::{f_get_line, f_get_point, f_get_rect, f_get_size, get_opt_non_repeated_track_sizing_function}; +use crate::measure::{f_get_dimension_or, get_dimension, get_grid_placement, get_length_percentage, get_length_percentage_auto, get_opt_track_sizing_function}; +use crate::primitives::{f_bool_from_primitive, f_f32_from_primitive, f_opt_f32_from_object}; + +pub unsafe fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local>) -> Style { + let display = f_get_display(env, style, "display"); + let item_is_table = f_bool_from_primitive(env, style, "itemIsTable", || false); + let box_sizing = f_get_box_sizing(env, style, "boxSizing"); + + let overflow = f_get_point(env, style, "overflow", get_overflow, + || Point { x: Overflow::Visible, y: Overflow::Visible }); + let scrollbar_width = f_f32_from_primitive(env, style, "scrollbarWidth", || 0.0); + + let position = f_get_position(env, style, "position"); + let inset = f_get_rect(env, style, "inset", get_length_percentage_auto, + || Rect::auto()); + + let size = f_get_size(env, style, "size", get_dimension, || Size::auto()); + let min_size = f_get_size(env, style, "minSize", get_dimension, || Size::auto()); + let max_size = f_get_size(env, style, "maxSize", get_dimension, || Size::auto()); + let aspect_ratio = f_opt_f32_from_object(env, style, "aspectRatio", || None); + + let margin = f_get_rect(env, style, "margin", get_length_percentage_auto, || Rect::zero()); + let padding = f_get_rect(env, style, "padding", get_length_percentage, || Rect::zero()); + let border = f_get_rect(env, style, "border", get_length_percentage, || Rect::zero()); + + let align_items = f_get_align_items(env, style, "alignItems"); + let align_self = f_get_align_items(env, style, "alignSelf"); + let justify_items = f_get_align_items(env, style, "justifyItems"); + let justify_self = f_get_align_items(env, style, "justifySelf"); + let align_content = f_get_align_content(env, style, "alignContent"); + let justify_content = f_get_align_content(env, style, "justifyContent"); + let gap = f_get_size(env, style, "gap", get_length_percentage, || Size::zero()); + + + let text_align = f_get_text_align(env, style, "textAlign"); + let flex_direction = f_get_flex_direction(env, style, "flexDirection"); + let flex_wrap = f_get_flex_wrap(env, style, "flexWrap"); + + let flex_basis = f_get_dimension_or(env, style, "flexBasis", || Dimension::Auto); + let flex_grow = f_f32_from_primitive(env, style, "flexGrow", || 0.0); + let flex_shrink = f_f32_from_primitive(env, style, "flexShrink", || 1.0); + + let grid_template_rows = f_get_list(env, style, "gridTemplateRows", get_opt_track_sizing_function); + let grid_template_columns = f_get_list(env, style, "gridTemplateColumns", get_opt_track_sizing_function); + let grid_auto_rows = f_get_list(env, style, "gridAutoRows", get_opt_non_repeated_track_sizing_function); + let grid_auto_columns = f_get_list(env, style, "gridAutoColumns", get_opt_non_repeated_track_sizing_function); + let grid_auto_flow = f_get_grid_auto_flow(env, style, "gridAutoFlow"); + + let grid_row = f_get_line(env, style, "gridRow", get_grid_placement, + || Line { start: GridPlacement::Auto, end: GridPlacement::Auto }); + + let grid_column = f_get_line(env, style, "gridColumn", get_grid_placement, + || Line { start: GridPlacement::Auto, end: GridPlacement::Auto }); Style { display, @@ -93,650 +101,12 @@ pub(crate) fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local grid_auto_flow, grid_row, - grid_column - } -} - -fn get_float<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, def: fn() -> Option) -> Option { - let float_obj = get_field_as(env, object, field, "Ljava/lang/Float;"); - - if float_obj.is_null() { - return def(); - } - - Some( - env - .call_method(float_obj, "floatValue", "()F", &[]) - .expect("Couldn't call floatValue on java/lang/Float") - .f() - .unwrap() as f32 - ) -} - -fn get_field_point<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F, def: fn() -> T) -> Point -where - F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, -{ - let point_field = &get_field_as(env, object, field, "Lcom/dioxuslabs/taffy/geom/TaffyPoint;"); - if point_field.is_null() { - return Point { - x: def(), - y: def(), - }; - } - - let x_field = &get_field_as(env, point_field, "x", "Ljava/lang/Object;"); - let y_field = &get_field_as(env, point_field, "y", "Ljava/lang/Object;"); - - Point { - x: f(env, x_field, def), - y: f(env, y_field, def), - } -} - -fn get_field_line<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F, def: fn() -> T) -> Line -where - F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, -{ - let line_field = &get_field_as(env, object, field, "Lcom/dioxuslabs/taffy/geom/TaffyLine;"); - if line_field.is_null() { - return Line { - start: def(), - end: def(), - }; - } - - let s_field = &get_field_as(env, line_field, "start", "Ljava/lang/Object;"); - let e_field = &get_field_as(env, line_field, "end", "Ljava/lang/Object;"); - - Line { - start: f(env, s_field, def), - end: f(env, e_field, def), - } -} - -fn get_field_list<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F) -> Vec -where - F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, -{ - let list_field = &get_field_as(env, object, field, "Ljava/util/List;"); - - if list_field.is_null() { - return Vec::new() - } - - let list_size = env - .call_method(list_field, "size", "()I", &[]) - .expect("Couldn't call size on List") - .i() - .unwrap(); - - let mut objects = Vec::new(); - for i in 0..list_size { - let object = env - .call_method(list_field, "get", "(I)Ljava/lang/Object;", &[JValue::from(i)]) - .expect("Couldn't call get on List") - .l() - .unwrap(); - - objects.push(f(env, &object)); - } - - objects -} - -fn get_field_size<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F, def: fn() -> T) -> Size -where - F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, -{ - let point_field = &get_field_as(env, object, field, "Lcom/dioxuslabs/taffy/geom/TaffySize;"); - if point_field.is_null() { - return Size { - width: def(), - height: def(), - }; - } - - let w_field = &get_field_as(env, point_field, "width", "Ljava/lang/Object;"); - let h_field = &get_field_as(env, point_field, "height", "Ljava/lang/Object;"); - - Size { - width: f(env, w_field, def), - height: f(env, h_field, def), - } -} - -fn get_field_rect<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, f: F, def: fn() -> T) -> Rect -where - F: Fn(&mut JNIEnv<'local>, &JObject<'local>, fn() -> T) -> T, -{ - let point_field = &get_field_as(env, object, field, "Lcom/dioxuslabs/taffy/geom/TaffyRect;"); - if point_field.is_null() { - return Rect { - left: def(), - right: def(), - top: def(), - bottom: def(), - }; - } - - let l_field = &get_field_as(env, point_field, "left", "Ljava/lang/Object;"); - let r_field = &get_field_as(env, point_field, "right", "Ljava/lang/Object;"); - let t_field = &get_field_as(env, point_field, "top", "Ljava/lang/Object;"); - let b_field = &get_field_as(env, point_field, "bottom", "Ljava/lang/Object;"); - - Rect { - left: f(env, l_field, def), - right: f(env, r_field, def), - top: f(env, t_field, def), - bottom: f(env, b_field, def), + grid_column, } } -fn get_field_as<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, jtype: &str) -> JObject<'local> { +pub fn f_get_value<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, jtype: &str) -> JValueOwned<'local> { env .get_field(object, field, jtype) - .expect("Couldn't get field") - .l() - .unwrap() -} - -fn resolve_field<'local, T, F>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, jtype: &str, f: F, def: fn() -> T) -> T -where - F: Fn(&mut JNIEnv<'local>, &JObject<'local>, fn() -> T) -> T, -{ - let field = env - .get_field(object, field, jtype) - .expect("Couldn't get field") - .l() - .unwrap(); - - f(env, &field, def) -} - -fn get_field_bool(env: &mut JNIEnv, object: &JObject, field: &str, def: bool) -> bool { - env - .get_field(object, field, "Z") - .expect("Couldn't get field") - .z() - .unwrap_or(def) -} - -fn get_field_float(env: &mut JNIEnv, object: &JObject, field: &str, def: f32) -> f32 { - env - .get_field(object, field, "F") - .expect("Couldn't get field") - .f() - .unwrap_or(def) -} - -fn get_enum_value(env: &mut JNIEnv, object: &JObject) -> i32 { - env - .get_field(object, "internal", "I") - .expect("Couldn't get field") - .i() - .unwrap() -} - -fn get_enum_equiv<'local, T, F>(env: &mut JNIEnv<'local>, style: &JObject<'local>, field: &str, jtype: &str, - f: F) -> T where - F: Fn(&mut JNIEnv<'local>, &JObject<'local>) -> T, -{ - let field = &get_field_as(env, style, field, jtype); - f(env, field) -} - -fn get_display<'local>(env: &mut JNIEnv<'local>, display: &JObject<'local>) -> Display { - if display.is_null() { - return Display::default(); - } - - let internal = get_enum_value(env, display); - - match internal { - 0 => Display::Block, - 1 => Display::Flex, - 2 => Display::Grid, - _ => Display::None, - } -} - -fn get_box_sizing<'local>(env: &mut JNIEnv<'local>, box_sizing: &JObject<'local>) -> BoxSizing { - if box_sizing.is_null() { - return BoxSizing::default(); - } - - let internal = get_enum_value(env, box_sizing); - - match internal { - 0 => BoxSizing::BorderBox, - _ => BoxSizing::ContentBox - } -} - -fn get_length_percentage_auto<'local>(env: &mut JNIEnv<'local>, - length_percentage_auto: &JObject<'local>, - def: fn() -> LengthPercentageAuto) -> LengthPercentageAuto { - if length_percentage_auto.is_null() { - return def(); - } - - let internal = env - .get_field(length_percentage_auto, "type", "B") - .expect("Couldn't get field") - .b() - .unwrap() as i8; - - match internal { - 0 => LengthPercentageAuto::Length( - env - .get_field(length_percentage_auto, "value", "F") - .expect("Couldn't get field") - .f() - .unwrap() as f32 - ), - 1 => LengthPercentageAuto::Percent( - env - .get_field(length_percentage_auto, "value", "F") - .expect("Couldn't get field") - .f() - .unwrap() as f32 - ), - _ => LengthPercentageAuto::Auto - } -} - -fn get_non_repeated_track_sizing_function<'local>(env: &mut JNIEnv<'local>, - non_repeated_track_sizing_function: &JObject<'local>, - def: fn() -> NonRepeatedTrackSizingFunction) -> NonRepeatedTrackSizingFunction { - if non_repeated_track_sizing_function.is_null() { - return def(); - } - - let n_field = &get_field_as(env, non_repeated_track_sizing_function, "min", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction;"); - let x_field = &get_field_as(env, non_repeated_track_sizing_function, "max", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction;"); - - let min = get_min_track_sizing_function(env, n_field, || MinTrackSizingFunction::Auto); - let max = get_max_track_sizing_function(env, x_field, || MaxTrackSizingFunction::Auto); - - NonRepeatedTrackSizingFunction { - min, - max, - } -} - -fn get_track_sizing_function<'local>(env: &mut JNIEnv<'local>, - track_sizing_function: &JObject<'local>, - def: fn() -> TrackSizingFunction) -> TrackSizingFunction { - if track_sizing_function.is_null() { - return def(); - } - - let internal = env - .get_field(track_sizing_function, "type", "B") - .expect("Couldn't get field") - .b() - .unwrap() as i8; - - match internal { - 0 => TrackSizingFunction::Single({ - let func_field = &get_field_as(env, track_sizing_function, "func", "Lcom/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction;"); - - get_non_repeated_track_sizing_function( - env, - func_field, - || NonRepeatedTrackSizingFunction { - min: MinTrackSizingFunction::Auto, - max: MaxTrackSizingFunction::Auto, - }, - ) - }), - _ => TrackSizingFunction::Repeat( - { - let reps_field = &get_field_as(env, track_sizing_function, "repetitions", "Lcom/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition;"); - get_grid_track_repetition(env, reps_field, || GridTrackRepetition::AutoFill) - }, - { - get_field_list(env, track_sizing_function, "functions", |env, func| { - let func_field = &get_field_as(env, func, "func", "Lcom/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction;"); - - get_non_repeated_track_sizing_function( - env, - func_field, - || NonRepeatedTrackSizingFunction { - min: MinTrackSizingFunction::Auto, - max: MaxTrackSizingFunction::Auto, - }, - ) - }) - }, - ) - } -} - -fn get_min_track_sizing_function<'local>(env: &mut JNIEnv<'local>, - min_track_sizing_function: &JObject<'local>, - def: fn() -> MinTrackSizingFunction) -> MinTrackSizingFunction { - if min_track_sizing_function.is_null() { - return def(); - } - - let internal = env - .get_field(min_track_sizing_function, "type", "B") - .expect("Couldn't get field") - .b() - .unwrap() as i8; - - match internal { - 0 => MinTrackSizingFunction::Fixed( - resolve_field( - env, - min_track_sizing_function, - "value", - "Lcom/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage;", - get_length_percentage, - || LengthPercentage::Length(0.0), - ) - ), - 1 => MinTrackSizingFunction::MinContent, - 2 => MinTrackSizingFunction::MaxContent, - _ => MinTrackSizingFunction::Auto - } -} - -fn get_max_track_sizing_function<'local>(env: &mut JNIEnv<'local>, - min_track_sizing_function: &JObject<'local>, - def: fn() -> MaxTrackSizingFunction) -> MaxTrackSizingFunction { - if min_track_sizing_function.is_null() { - return def(); - } - - let internal = env - .get_field(min_track_sizing_function, "type", "B") - .expect("Couldn't get field") - .b() - .unwrap() as i8; - - match internal { - 0 => MaxTrackSizingFunction::Fixed( - resolve_field( - env, - min_track_sizing_function, - "value", - "Ljava/lang/Object;", - get_length_percentage, - || LengthPercentage::Length(0.0), - ) - ), - 1 => MaxTrackSizingFunction::MinContent, - 2 => MaxTrackSizingFunction::MaxContent, - 3 => MaxTrackSizingFunction::FitContent( - resolve_field( - env, - min_track_sizing_function, - "value", - "Ljava/lang/Object;", - get_length_percentage, - || LengthPercentage::Length(0.0), - ) - ), - 4 => MaxTrackSizingFunction::Auto, - _ => MaxTrackSizingFunction::Fraction( - env - .get_field(min_track_sizing_function, "value", "Ljava/lang/Object;") - .expect("Couldn't get field") - .f() - .unwrap() as f32 - ), - } -} - -fn get_dimension<'local>(env: &mut JNIEnv<'local>, - dimension: &JObject<'local>, - def: fn() -> Dimension) -> Dimension { - if dimension.is_null() { - return def(); - } - - let internal = env - .get_field(dimension, "type", "B") - .expect("Couldn't get field") - .b() - .unwrap() as i8; - - match internal { - 0 => Dimension::Length( - env - .get_field(dimension, "value", "F") - .expect("Couldn't get field") - .f() - .unwrap() as f32 - ), - 1 => Dimension::Percent( - env - .get_field(dimension, "value", "F") - .expect("Couldn't get field") - .f() - .unwrap() as f32 - ), - _ => Dimension::Auto - } -} - -fn get_grid_track_repetition<'local>(env: &mut JNIEnv<'local>, - grid_track_repetition: &JObject<'local>, - def: fn() -> GridTrackRepetition) -> GridTrackRepetition { - if grid_track_repetition.is_null() { - return def(); - } - - let internal = env - .get_field(grid_track_repetition, "type", "B") - .expect("Couldn't get field") - .b() - .unwrap() as i8; - - match internal { - 0 => GridTrackRepetition::AutoFill, - 1 => GridTrackRepetition::AutoFit, - _ => GridTrackRepetition::Count( - env - .get_field(grid_track_repetition, "value", "S") - .expect("Couldn't get field") - .s() - .unwrap() as u16 - ) - } -} - -fn get_length_percentage<'local>(env: &mut JNIEnv<'local>, - length_percentage: &JObject<'local>, - def: fn() -> LengthPercentage) -> LengthPercentage { - if length_percentage.is_null() { - return def(); - } - - let internal = env - .get_field(length_percentage, "type", "B") - .expect("Couldn't get field") - .b() - .unwrap() as i8; - - match internal { - 0 => LengthPercentage::Length( - env - .get_field(length_percentage, "value", "F") - .expect("Couldn't get field") - .f() - .unwrap() as f32 - ), - _ => LengthPercentage::Percent( - env - .get_field(length_percentage, "value", "F") - .expect("Couldn't get field") - .f() - .unwrap() as f32 - ), - } -} - -fn get_grid_placement<'local>(env: &mut JNIEnv<'local>, - grid_placement: &JObject<'local>, - def: fn() -> GridPlacement) -> GridPlacement { - if grid_placement.is_null() { - return def(); - } - - let internal = env - .get_field(grid_placement, "type", "B") - .expect("Couldn't get field") - .b() - .unwrap() as i8; - - match internal { - 0 => GridPlacement::Auto, - 1 => GridPlacement::from_line_index( - env - .get_field(grid_placement, "value", "S") - .expect("Couldn't get field") - .s() - .unwrap() as i16 - ), - _ => GridPlacement::from_span( - env - .get_field(grid_placement, "value", "F") - .expect("Couldn't get field") - .f() - .unwrap() as u16 - ), - } -} - -fn get_overflow<'local>(env: &mut JNIEnv<'local>, overflow: &JObject<'local>) -> Overflow { - if overflow.is_null() { - return Overflow::default(); - } - - let internal = get_enum_value(env, overflow); - - match internal { - 0 => Overflow::Visible, - 1 => Overflow::Clip, - 2 => Overflow::Hidden, - _ => Overflow::Scroll - } -} - -fn get_position<'local>(env: &mut JNIEnv<'local>, position: &JObject<'local>) -> Position { - if position.is_null() { - return Position::default(); - } - - let internal = get_enum_value(env, position); - - match internal { - 0 => Position::Relative, - _ => Position::Absolute - } -} - -fn get_text_align<'local>(env: &mut JNIEnv<'local>, text_align: &JObject<'local>) -> TextAlign { - if text_align.is_null() { - return TextAlign::default(); - } - - let internal = get_enum_value(env, text_align); - - match internal { - 0 => TextAlign::Auto, - 1 => TextAlign::LegacyLeft, - 2 => TextAlign::LegacyRight, - _ => TextAlign::LegacyCenter - } -} - -fn get_flex_direction<'local>(env: &mut JNIEnv<'local>, flex_direction: &JObject<'local>) -> FlexDirection { - if flex_direction.is_null() { - return FlexDirection::default(); - } - - let internal = get_enum_value(env, flex_direction); - - match internal { - 0 => FlexDirection::Row, - 1 => FlexDirection::Column, - 2 => FlexDirection::RowReverse, - _ => FlexDirection::ColumnReverse - } -} - -fn get_flex_wrap<'local>(env: &mut JNIEnv<'local>, flex_wrap: &JObject<'local>) -> FlexWrap { - if flex_wrap.is_null() { - return FlexWrap::default(); - } - - let internal = get_enum_value(env, flex_wrap); - - match internal { - 0 => FlexWrap::NoWrap, - 1 => FlexWrap::Wrap, - _ => FlexWrap::WrapReverse - } -} - -fn get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, grid_auto_flow: &JObject<'local>) -> GridAutoFlow { - if grid_auto_flow.is_null() { - return GridAutoFlow::default(); - } - - let internal = get_enum_value(env, grid_auto_flow); - - match internal { - 0 => GridAutoFlow::Row, - 1 => GridAutoFlow::Column, - 2 => GridAutoFlow::RowDense, - _ => GridAutoFlow::ColumnDense - } -} - -fn get_align_items<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local>, field: &str) -> Option { - let align_items = &get_field_as(env, style, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignItems;"); - - if align_items.is_null() { - return None; - } - - let internal = get_enum_value(env, align_items); - - match internal { - 0 => Some(AlignItems::Start), - 1 => Some(AlignItems::End), - 2 => Some(AlignItems::FlexStart), - 3 => Some(AlignItems::FlexEnd), - 4 => Some(AlignItems::Center), - 5 => Some(AlignItems::Baseline), - _ => Some(AlignItems::Stretch) - } -} - -fn get_align_content<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local>, field: &str) -> Option { - let align_content = &get_field_as(env, style, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignContent;"); - - if align_content.is_null() { - return None; - } - - let internal = get_enum_value(env, align_content); - - match internal { - 0 => Some(AlignContent::Start), - 1 => Some(AlignContent::End), - 2 => Some(AlignContent::FlexStart), - 3 => Some(AlignContent::FlexEnd), - 4 => Some(AlignContent::Center), - 5 => Some(AlignContent::Stretch), - 6 => Some(AlignContent::SpaceBetween), - 7 => Some(AlignContent::SpaceEvenly), - _ => Some(AlignContent::SpaceAround) - } + .expect(format!("Couldn't get field {}, {}", field, jtype).as_str()) } diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs new file mode 100644 index 000000000..65a177976 --- /dev/null +++ b/bindings/java/src/enums.rs @@ -0,0 +1,233 @@ +use jni::JNIEnv; +use jni::objects::{JObject, JValueOwned}; +use taffy::{AlignContent, AlignItems, BoxSizing, Display, FlexDirection, FlexWrap, GridAutoFlow, Overflow, Position, TextAlign}; +use crate::conversions::f_get_value; +use crate::primitives::f_i32_from_primitive; + +pub fn get_overflow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Overflow { + let obj = &value.l().unwrap(); + if obj.is_null() { + return Overflow::default(); + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => Overflow::Visible, + 1 => Overflow::Clip, + 2 => Overflow::Hidden, + _ => Overflow::Scroll + } +} + +#[allow(dead_code)] +pub fn f_get_overflow<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Overflow { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyOverflow;"); + + get_overflow(env, obj) +} + +pub fn get_position<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Position { + let obj = &value.l().unwrap(); + if obj.is_null() { + return Position::default(); + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => Position::Relative, + _ => Position::Absolute + } +} + +pub fn f_get_position<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Position { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyPosition;"); + + get_position(env, obj) +} + +pub fn get_text_align<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> TextAlign) -> TextAlign { + let obj = &value.l().unwrap(); + if obj.is_null() { + return def(); + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => TextAlign::Auto, + 1 => TextAlign::LegacyLeft, + 2 => TextAlign::LegacyRight, + _ => TextAlign::LegacyCenter + } +} + +pub fn f_get_text_align<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> TextAlign { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyTextAlign;"); + + get_text_align(env, obj, TextAlign::default) +} + +pub fn get_flex_direction<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> FlexDirection) -> FlexDirection { + let obj = &value.l().unwrap(); + if obj.is_null() { + return def(); + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => FlexDirection::Row, + 1 => FlexDirection::Column, + 2 => FlexDirection::RowReverse, + _ => FlexDirection::ColumnReverse + } +} + +pub fn f_get_flex_direction<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> FlexDirection { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyFlexDirection;"); + + get_flex_direction(env, obj, FlexDirection::default) +} + +pub fn get_flex_wrap<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> FlexWrap) -> FlexWrap { + let obj = &value.l().unwrap(); + if obj.is_null() { + return def(); + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => FlexWrap::NoWrap, + 1 => FlexWrap::Wrap, + _ => FlexWrap::WrapReverse + } +} + +pub fn f_get_flex_wrap<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> FlexWrap { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyFlexWrap;"); + + get_flex_wrap(env, obj, FlexWrap::default) +} + +pub fn get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> GridAutoFlow) -> GridAutoFlow { + let obj = &value.l().unwrap(); + if obj.is_null() { + return def(); + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => GridAutoFlow::Row, + 1 => GridAutoFlow::Column, + 2 => GridAutoFlow::RowDense, + _ => GridAutoFlow::ColumnDense + } +} + +pub fn f_get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> GridAutoFlow { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyGridAutoFlow;"); + + get_grid_auto_flow(env, obj, GridAutoFlow::default) +} + +pub fn get_align_items<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Option { + let obj = &value.l().unwrap(); + if obj.is_null() { + return None; + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => Some(AlignItems::Start), + 1 => Some(AlignItems::End), + 2 => Some(AlignItems::FlexStart), + 3 => Some(AlignItems::FlexEnd), + 4 => Some(AlignItems::Center), + 5 => Some(AlignItems::Baseline), + _ => Some(AlignItems::Stretch) + } +} + +pub fn f_get_align_items<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Option { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignItems;"); + + get_align_items(env, obj) +} + +pub fn get_align_content<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Option { + let obj = &value.l().unwrap(); + if obj.is_null() { + return None; + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => Some(AlignContent::Start), + 1 => Some(AlignContent::End), + 2 => Some(AlignContent::FlexStart), + 3 => Some(AlignContent::FlexEnd), + 4 => Some(AlignContent::Center), + 5 => Some(AlignContent::Stretch), + 6 => Some(AlignContent::SpaceBetween), + 7 => Some(AlignContent::SpaceEvenly), + _ => Some(AlignContent::SpaceAround) + } +} + +pub fn f_get_align_content<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Option { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignContent;"); + + get_align_content(env, obj) +} + +pub fn get_display<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Display { + let obj = &value.l().unwrap(); + if obj.is_null() { + return Display::default(); + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => Display::Block, + 1 => Display::Flex, + 2 => Display::Grid, + _ => Display::None, + } +} + +pub fn f_get_display<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Display { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyDisplay;"); + + get_display(env, obj) +} + +pub fn get_box_sizing<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> BoxSizing { + let obj = &value.l().unwrap(); + if obj.is_null() { + return BoxSizing::default(); + } + + let internal = get_enum_value(env, obj); + + match internal { + 0 => BoxSizing::BorderBox, + _ => BoxSizing::ContentBox + } +} + +pub fn f_get_box_sizing<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> BoxSizing { + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyBoxSizing;"); + + get_box_sizing(env, obj) +} + +fn get_enum_value<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>) -> i32 { + f_i32_from_primitive(env, object, "internal", || 0) +} diff --git a/bindings/java/src/geom.rs b/bindings/java/src/geom.rs new file mode 100644 index 000000000..3ea4840bd --- /dev/null +++ b/bindings/java/src/geom.rs @@ -0,0 +1,177 @@ +use jni::JNIEnv; +use jni::objects::{JObject, JValueOwned}; +use taffy::{Line, MaxTrackSizingFunction, MinTrackSizingFunction, NonRepeatedTrackSizingFunction, Point, Rect, Size}; +use crate::conversions::{f_get_value}; +use crate::measure::{get_max_track_sizing_function, get_min_track_sizing_function}; + +/// Get a Point from its Java counterpart, using a JValueOwned +pub fn get_point<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F, def: fn() -> Point) -> Point +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, +{ + let point_object = &value.l().unwrap(); + if point_object.is_null() { + return def(); + } + + let x_field = f_get_value(env, point_object, "x", "Ljava/lang/Object;"); + let y_field = f_get_value(env, point_object, "y", "Ljava/lang/Object;"); + + Point { + x: f(env, x_field), + y: f(env, y_field), + } +} + +/// Get a Rect from its Java counterpart, using a JValueOwned +pub fn get_rect<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F, def: fn() -> Rect) -> Rect +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, +{ + let rect_object = &value.l().unwrap(); + if rect_object.is_null() { + return def(); + } + + let l_field = f_get_value(env, rect_object, "left", "Ljava/lang/Object;"); + let r_field = f_get_value(env, rect_object, "right", "Ljava/lang/Object;"); + let t_field = f_get_value(env, rect_object, "top", "Ljava/lang/Object;"); + let b_field = f_get_value(env, rect_object, "bottom", "Ljava/lang/Object;"); + + + Rect { + left: f(env, l_field), + right: f(env, r_field), + top: f(env, t_field), + bottom: f(env, b_field), + } +} + +/// Get a Size from its Java counterpart, using a JValueOwned +pub fn get_size<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F, def: fn() -> Size) -> Size +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, +{ + let size_object = &value.l().unwrap(); + if size_object.is_null() { + return def(); + } + + let w_field = f_get_value(env, size_object, "width", "Ljava/lang/Object;"); + let h_field = f_get_value(env, size_object, "height", "Ljava/lang/Object;"); + + Size { + width: f(env, w_field), + height: f(env, h_field), + } +} + +/// Get a Line from its Java counterpart, using a JValueOwned +pub fn get_line<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F, def: fn() -> Line) -> Line +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, +{ + let line_object = &value.l().unwrap(); + if line_object.is_null() { + return def(); + } + + let s_field = f_get_value(env, line_object, "start", "Ljava/lang/Object;"); + let e_field = f_get_value(env, line_object, "end", "Ljava/lang/Object;"); + + Line { + start: f(env, s_field), + end: f(env, e_field), + } +} + +/// Get a NonRepeatedTrackSizingFunction from its Java counterpart, using a JValueOwned +pub fn get_non_repeated_track_sizing_function<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local> +) -> NonRepeatedTrackSizingFunction { + let nrtsf_object = &value.l().unwrap(); + if nrtsf_object.is_null() { + panic!("NonRepeatedTrackSizingFunction cannot be null"); + } + + let n_val = f_get_value(env, nrtsf_object, "min", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction;"); + let x_val = f_get_value(env, nrtsf_object, "max", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction;"); + + let min = get_min_track_sizing_function(env, n_val, || MinTrackSizingFunction::Auto); + let max = get_max_track_sizing_function(env, x_val, || MaxTrackSizingFunction::Auto); + + NonRepeatedTrackSizingFunction { + min, + max, + } +} + +/// Get an Option from its Java counterpart, using a JValueOwned +pub fn get_opt_non_repeated_track_sizing_function<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local> +) -> Option { + let nrtsf_object = &value.l().unwrap(); + if nrtsf_object.is_null() { + return None + } + + let n_val = f_get_value(env, nrtsf_object, "min", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction;"); + let x_val = f_get_value(env, nrtsf_object, "max", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction;"); + + let min = get_min_track_sizing_function(env, n_val, || MinTrackSizingFunction::Auto); + let max = get_max_track_sizing_function(env, x_val, || MaxTrackSizingFunction::Auto); + + Some(NonRepeatedTrackSizingFunction { + min, + max, + }) +} + +/// Get a Point from its Java counterpart, using a field name +pub fn f_get_point<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F, def: fn() -> Point) -> Point +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, +{ + let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/TaffyPoint;"); + + get_point(env, point_field, f, def) +} + +/// Get a Rect from its Java counterpart, using a field name +pub fn f_get_rect<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F, def: fn() -> Rect) -> Rect +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, +{ + let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/TaffyRect;"); + + get_rect(env, point_field, f, def) +} + +/// Get a Size from its Java counterpart, using a field name +pub fn f_get_size<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F, def: fn() -> Size) -> Size +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, +{ + let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/TaffySize;"); + + get_size(env, point_field, f, def) +} + +/// Get a Line from its Java counterpart, using a field name +pub fn f_get_line<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F, def: fn() -> Line) -> Line +where + F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, +{ + let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/TaffyLine;"); + + get_line(env, point_field, f, def) +} + +/// Get a NonRepeatedTrackSizingFunction from its Java counterpart, using a field name +pub fn f_get_non_repeated_track_sizing_function<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> NonRepeatedTrackSizingFunction { + let nrtsft_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction;"); + + get_non_repeated_track_sizing_function(env, nrtsft_field) +} \ No newline at end of file diff --git a/bindings/java/src/java.rs b/bindings/java/src/java.rs new file mode 100644 index 000000000..30efe2cc1 --- /dev/null +++ b/bindings/java/src/java.rs @@ -0,0 +1,90 @@ +use jni::JNIEnv; +use jni::objects::{JObject, JValue}; +use jni::sys::jobject; +use taffy::{Layout, Point, Rect, Size}; + +pub fn to_java_layout<'local>(env: &mut JNIEnv<'local>, layout: &Layout) -> jobject { + let layout_class = &env.find_class("com/dioxuslabs/taffy/tree/TaffyLayout").unwrap(); + + let location = &f32_point_to_java(env, layout.location); + let size = &f32_size_to_java(env, layout.size); + let content_size = &f32_size_to_java(env, layout.content_size); + let scrollbar_size = &f32_size_to_java(env, layout.scrollbar_size); + let border = &f32_rect_to_java(env, layout.border); + let padding = &f32_rect_to_java(env, layout.padding); + let margin = &f32_rect_to_java(env, layout.margin); + + *env.new_object(layout_class, "(ILcom/dioxuslabs/taffy/geom/TaffyPoint;Lcom/dioxuslabs/taffy/geom/TaffySize;Lcom/dioxuslabs/taffy/geom/TaffySize;Lcom/dioxuslabs/taffy/geom/TaffySize;Lcom/dioxuslabs/taffy/geom/TaffyRect;Lcom/dioxuslabs/taffy/geom/TaffyRect;Lcom/dioxuslabs/taffy/geom/TaffyRect;)V", &[ + JValue::Int(layout.order as i32), + JValue::Object(location), + JValue::Object(size), + JValue::Object(content_size), + JValue::Object(scrollbar_size), + JValue::Object(border), + JValue::Object(padding), + JValue::Object(margin), + ]).unwrap() +} + +pub fn f32_point_to_java<'local>(env: &mut JNIEnv<'local>, point: Point) -> JObject<'local> { + let class = &env.find_class("com/dioxuslabs/taffy/geom/TaffyPoint").unwrap(); + + let a = &f32_to_java(env, point.x); + let b = &f32_to_java(env, point.y); + + env.new_object( + class, + "(Ljava/lang/Object;Ljava/lang/Object;)V", + &[ + JValue::Object(a), + JValue::Object(b), + ], + ).unwrap() +} + +pub fn f32_size_to_java<'local>(env: &mut JNIEnv<'local>, size: Size) -> JObject<'local> { + let class = &env.find_class("com/dioxuslabs/taffy/geom/TaffySize").unwrap(); + + let a = &f32_to_java(env, size.width); + let b = &f32_to_java(env, size.height); + + env.new_object( + class, + "(Ljava/lang/Object;Ljava/lang/Object;)V", + &[ + JValue::Object(a), + JValue::Object(b), + ], + ).unwrap() +} + +pub fn f32_rect_to_java<'local>(env: &mut JNIEnv<'local>, rect: Rect) -> JObject<'local> { + let class = &env.find_class("com/dioxuslabs/taffy/geom/TaffyRect").unwrap(); + + + let a = &f32_to_java(env, rect.left); + let b = &f32_to_java(env, rect.right); + let c = &f32_to_java(env, rect.top); + let d = &f32_to_java(env, rect.bottom); + + env.new_object( + class, + "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V", + &[ + JValue::Object(a), + JValue::Object(b), + JValue::Object(c), + JValue::Object(d), + ], + ).unwrap() +} + +pub fn f32_to_java<'local>(env: &mut JNIEnv<'local>, value: f32) -> JObject<'local> { + let class = &env.find_class("java/lang/Float").unwrap(); + + env.new_object( + class, + "(F)V", + &[JValue::Float(value)], + ).unwrap() +} \ No newline at end of file diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs index bed0568ed..0996b7802 100644 --- a/bindings/java/src/lib.rs +++ b/bindings/java/src/lib.rs @@ -1,23 +1,22 @@ mod conversions; +mod collections; +mod primitives; +mod geom; +mod enums; +mod measure; +mod java; use std::panic; -// This is the interface to the JVM that we'll call the majority of our -// methods on. use jni::JNIEnv; - -// These objects are what you should use as arguments to your native -// function. They carry extra lifetime information to prevent them escaping -// this context and getting used after being GC'd. -use jni::objects::{JClass, JObject}; - -// This is just a pointer. We'll be returning it from our function. We -// can't return one of the objects with lifetime information because the -// lifetime checker won't let us. +use jni::objects::{JClass, JObject, JValueOwned}; use jni::sys::{jint, jlong, jobject}; - use taffy::{NodeId, TaffyTree, TraversePartialTree}; - -use conversions::get_style; +use crate::collections::{get_list}; +use crate::conversions::get_style; +use crate::geom::get_size; +use crate::java::to_java_layout; +use crate::measure::get_available_space; +use crate::primitives::{node_id_from_primitive, opt_node_id_from_object}; #[no_mangle] pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree<'local>(_env: JNIEnv<'local>, @@ -42,25 +41,81 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>(mut e } let tree: &mut TaffyTree<()> = &mut *raw_ptr; let res = tree.new_leaf(style); - res.map_or(-1, |v| v.0 as jlong) as jlong + res.map_or(-1, |v| >::into(v) as jlong) as jlong } } #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_childCount<'local>(_env: JNIEnv<'local>, - _class: JClass<'local>, - pointer: jlong, - node_id: jlong) - -> jint { - let result = panic::catch_unwind(|| { +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren<'local>(mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + style: jobject, + children: jobject) + -> jlong { + unsafe { + let style = get_style(&mut env, &JObject::from_raw(style)); + let list = JValueOwned::from(JObject::from_raw(children)); + let children = &get_list(&mut env, list, opt_node_id_from_object); + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; if raw_ptr.is_null() { return -2; } - let tree: &mut TaffyTree<()> = unsafe { &mut *raw_ptr }; - let res = tree.child_count(NodeId::from(node_id as u64)); - res as jint - }); + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + let res = tree.new_with_children(style, children); + res.map_or(-1, |v| >::into(v) as jlong) as jlong + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_computeLayout<'local>(mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong, + available_size: jobject) { + unsafe { + let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); + let as_object = JValueOwned::from(JObject::from_raw(available_size)); + let available_size = get_size(&mut env, as_object, get_available_space, + || panic!("AvailableSize cannot be null")); - result.unwrap_or_else(|_| -9) + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + return; + } + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + tree.compute_layout(node, available_size) + .expect("Failed to compute layout"); + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_childCount<'local>(mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong) + -> jint { + let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + return -2; + } + let tree: &mut TaffyTree<()> = unsafe { &mut *raw_ptr }; + let res = tree.child_count(node); + res as jint +} +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_layout<'local>(mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong) + -> jobject { + let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + panic!("Pointer cannot be null"); + } + let tree: &mut TaffyTree<()> = unsafe { &mut *raw_ptr }; + let res = tree.layout(node); + to_java_layout(&mut env, res.unwrap()) } diff --git a/bindings/java/src/measure.rs b/bindings/java/src/measure.rs new file mode 100644 index 000000000..0477f98d3 --- /dev/null +++ b/bindings/java/src/measure.rs @@ -0,0 +1,265 @@ +use jni::JNIEnv; +use jni::objects::{JObject, JValueOwned}; +use taffy::{AvailableSpace, Dimension, GridPlacement, GridTrackRepetition, LengthPercentage, LengthPercentageAuto, MaxTrackSizingFunction, MinTrackSizingFunction, TrackSizingFunction}; +use taffy::style_helpers::{TaffyGridLine, TaffyGridSpan}; +use crate::collections::f_get_list; +use crate::conversions::f_get_value; +use crate::geom::{f_get_non_repeated_track_sizing_function, get_opt_non_repeated_track_sizing_function}; +use crate::primitives::{f_f32_from_primitive, f_i16_from_primitive, f_i8_from_primitive}; + +pub fn get_length_percentage<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, +) -> LengthPercentage { + let obj = &value.l().unwrap(); + + if obj.is_null() { + panic!("LengthPercentage cannot be null"); + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => LengthPercentage::Length( + f_f32_from_primitive(env, obj, "value", || 0.0) + ), + _ => LengthPercentage::Percent( + f_f32_from_primitive(env, obj, "value", || 0.0) + ) + } +} + +pub fn get_length_percentage_auto<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, +) -> LengthPercentageAuto { + let obj = &value.l().unwrap(); + + if obj.is_null() { + panic!("LengthPercentageAuto cannot be null"); + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => LengthPercentageAuto::Length( + f_f32_from_primitive(env, obj, "value", || 0.0) + ), + 1 => LengthPercentageAuto::Percent( + f_f32_from_primitive(env, obj, "value", || 0.0) + ), + _ => LengthPercentageAuto::Auto + } +} + +pub fn get_dimension<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, +) -> Dimension { + let obj = &value.l().unwrap(); + + if obj.is_null() { + panic!("Dimension cannot be null"); + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => Dimension::Length( + f_f32_from_primitive(env, obj, "value", || 0.0) + ), + 1 => Dimension::Percent( + f_f32_from_primitive(env, obj, "value", || 0.0) + ), + _ => Dimension::Auto + } +} + +pub fn get_available_space<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, +) -> AvailableSpace { + let obj = &value.l().unwrap(); + + if obj.is_null() { + panic!("AvailableSpace cannot be null"); + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => AvailableSpace::Definite( + f_f32_from_primitive(env, obj, "value", || 0.0) + ), + 1 => AvailableSpace::MinContent, + _ => AvailableSpace::MaxContent + } +} + +pub fn get_dimension_or<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + f: fn() -> Dimension, +) -> Dimension { + let obj = &value.l().unwrap(); + + if obj.is_null() { + return f() + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => Dimension::Length( + f_f32_from_primitive(env, obj, "value", || 0.0) + ), + 1 => Dimension::Percent( + f_f32_from_primitive(env, obj, "value", || 0.0) + ), + _ => Dimension::Auto + } +} + +pub fn get_grid_track_repetition<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, +) -> GridTrackRepetition { + let obj = &value.l().unwrap(); + + if obj.is_null() { + panic!("GridTrackRepetition cannot be null"); + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => GridTrackRepetition::AutoFill, + 1 => GridTrackRepetition::AutoFit, + _ => GridTrackRepetition::Count( + f_i16_from_primitive(env, obj, "value", || 0) as u16 + ) + } +} + +pub fn get_grid_placement<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, +) -> GridPlacement { + let obj = &value.l().unwrap(); + + if obj.is_null() { + panic!("GridPlacement cannot be null"); + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => GridPlacement::Auto, + 1 => GridPlacement::from_line_index( + f_i16_from_primitive(env, obj, "value", || 0) + ), + _ => GridPlacement::from_span( + f_i16_from_primitive(env, obj, "value", || 0) as u16 + ) + } +} + +pub fn get_min_track_sizing_function<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + def: fn() -> MinTrackSizingFunction, +) -> MinTrackSizingFunction { + let obj = &value.l().unwrap(); + + if obj.is_null() { + return def(); + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => MinTrackSizingFunction::Fixed( + f_get_length_percentage(env, obj, "value") + ), + 1 => MinTrackSizingFunction::MinContent, + 2 => MinTrackSizingFunction::MaxContent, + _ => MinTrackSizingFunction::Auto + } +} + +pub fn get_max_track_sizing_function<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + def: fn() -> MaxTrackSizingFunction, +) -> MaxTrackSizingFunction { + let obj = &value.l().unwrap(); + + if obj.is_null() { + return def(); + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + match internal { + 0 => MaxTrackSizingFunction::Fixed( + f_get_length_percentage(env, obj, "value") + ), + 1 => MaxTrackSizingFunction::MinContent, + 2 => MaxTrackSizingFunction::MaxContent, + 3 => MaxTrackSizingFunction::FitContent( + f_get_length_percentage(env, obj, "value") + ), + 4 => MaxTrackSizingFunction::Auto, + _ => MaxTrackSizingFunction::Fraction( + f_f32_from_primitive(env, obj, "value", || 0.0) + ) + } +} + +pub fn get_opt_track_sizing_function<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, +) -> Option { + let obj = &value.l().unwrap(); + + if obj.is_null() { + return None + } + + let internal = f_i8_from_primitive(env, obj, "type", || 0); + + Some(match internal { + 0 => TrackSizingFunction::Single( + f_get_non_repeated_track_sizing_function(env, obj, "func") + ), + _ => TrackSizingFunction::Repeat( + f_get_grid_track_repetition(env, obj, "repetitions"), + unsafe { f_get_list(env, obj, "functions", get_opt_non_repeated_track_sizing_function) }, + ) + }) +} + +pub fn f_get_length_percentage<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> LengthPercentage { + let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage;"); + + get_length_percentage(env, value) +} + +#[allow(dead_code)] +pub fn f_get_dimension<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Dimension { + let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/TaffyDimension;"); + + get_dimension(env, value) +} + +pub fn f_get_dimension_or<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: fn() -> Dimension) -> Dimension { + let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/TaffyDimension;"); + + get_dimension_or(env, value, f) +} + +pub fn f_get_grid_track_repetition<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> GridTrackRepetition { + let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition;"); + + get_grid_track_repetition(env, value) +} \ No newline at end of file diff --git a/bindings/java/src/primitives.rs b/bindings/java/src/primitives.rs new file mode 100644 index 000000000..d88e998fe --- /dev/null +++ b/bindings/java/src/primitives.rs @@ -0,0 +1,257 @@ +use jni::JNIEnv; +use jni::objects::{JObject, JValueOwned}; +use taffy::NodeId; +use crate::conversions::f_get_value; + +/// Convert Java float to f32 +pub fn f32_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> f32) -> f32 { + value + .f() + .unwrap_or(def()) +} + +/// Convert Java Float object to f32 +#[allow(dead_code)] +pub fn f32_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> f32) -> f32 { + let object = value.l().unwrap(); + + if object.is_null() { + return def(); + } + + env + .call_method(object, "floatValue", "()F", &[]) + .unwrap() + .f() + .unwrap() as f32 +} + +/// Convert Java Float object to Option +pub fn opt_f32_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> Option) -> Option { + let object = value.l().unwrap(); + + if object.is_null() { + return def(); + } + + Some(env + .call_method(object, "floatValue", "()F", &[]) + .unwrap() + .f() + .unwrap()) +} + +/// Convert Java byte to i8 +pub fn i8_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i8) -> i8 { + value + .b() + .unwrap_or(def()) as i8 +} + +/// Convert Java Byte object to i8 +#[allow(dead_code)] +pub fn i8_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i8) -> i8 { + let object = value.l().unwrap(); + + if object.is_null() { + return def(); + } + + env + .call_method(object, "byteValue", "()B", &[]) + .unwrap() + .b() + .unwrap() as i8 +} + +/// Convert Java short to i16 +pub fn i16_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i16) -> i16 { + value + .s() + .unwrap_or(def()) as i16 +} + +/// Convert Java Short object to i16 +#[allow(dead_code)] +pub fn i16_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i16) -> i16 { + let object = value.l().unwrap(); + + if object.is_null() { + return def(); + } + + env + .call_method(object, "shortValue", "()S", &[]) + .unwrap() + .s() + .unwrap() as i16 +} + +/// Convert Java int to i32 +pub fn i32_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i32) -> i32 { + value + .i() + .unwrap_or(def()) as i32 +} + +/// Convert Java Integer object to i32 +#[allow(dead_code)] +pub fn i32_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i32) -> i32 { + let object = value.l().unwrap(); + + if object.is_null() { + return def(); + } + + env + .call_method(object, "intValue", "()I", &[]) + .unwrap() + .i() + .unwrap() as i32 +} + +/// Convert Java long to i64 +#[allow(dead_code)] +pub fn i64_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i64) -> i64 { + value + .j() + .unwrap_or(def()) as i64 +} + +/// Convert Java Long object to i64 +#[allow(dead_code)] +pub fn i64_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i64) -> i64 { + let object = value.l().unwrap(); + + if object.is_null() { + return def(); + } + + env + .call_method(object, "longValue", "()J", &[]) + .unwrap() + .j() + .unwrap() as i64 +} + +/// Convert Java Long object to Option +pub fn opt_i64_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Option { + let object = value.l().unwrap(); + + if object.is_null() { + return None; + } + + Some(env + .call_method(object, "longValue", "()J", &[]) + .unwrap() + .j() + .unwrap() as i64) +} + +/// Convert Java boolean to bool +pub fn bool_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> bool) -> bool { + value + .z() + .unwrap_or(def()) +} + +/// Convert Java Boolean object to bool +#[allow(dead_code)] +pub fn bool_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> bool) -> bool { + let object = value.l().unwrap(); + + if object.is_null() { + return def(); + } + + env + .call_method(object, "booleanValue", "()Z", &[]) + .unwrap() + .z() + .unwrap() +} + +/// Convert Java Boolean object to Option +#[allow(dead_code)] +pub fn opt_bool_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> Option) -> Option { + let object = value.l().unwrap(); + + if object.is_null() { + return def(); + } + + Some(env + .call_method(object, "booleanValue", "()Z", &[]) + .unwrap() + .z() + .unwrap()) +} + +/// Convert Java long to NodeId +#[allow(dead_code)] +pub fn node_id_from_primitive<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> NodeId { + NodeId::from(i64_from_primitive(env, value, || 0) as u64) +} + +/// Convert Java Long to NodeId +#[allow(dead_code)] +pub fn node_id_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> NodeId { + NodeId::from(i64_from_object(env, value, || 0) as u64) +} + +/// Convert Java Long to Option +pub fn opt_node_id_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Option { + let o = opt_i64_from_object(env, value); + if o.is_none() { None } else { Some(NodeId::from(o? as u64)) } +} + +/// Convert Java float to f32, using a field name +pub fn f_f32_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> f32) -> f32 { + let value = f_get_value(env, base, field, "F"); + + f32_from_primitive(env, value, def) +} + +/// Convert Java float to f32, using a field name +pub fn f_opt_f32_from_object<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> Option) -> Option { + let value = f_get_value(env, base, field, "Ljava/lang/Float;"); + + opt_f32_from_object(env, value, def) +} + +/// Convert Java byte to i8, using a field name +pub fn f_i8_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> i8) -> i8 { + let value = f_get_value(env, base, field, "B"); + + i8_from_primitive(env, value, def) +} + +/// Convert Java short to i16, using a field name +pub fn f_i16_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> i16) -> i16 { + let value = f_get_value(env, base, field, "B"); + + i16_from_primitive(env, value, def) +} + +/// Convert Java int to i32, using a field name +pub fn f_i32_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> i32) -> i32 { + let value = f_get_value(env, base, field, "I"); + + i32_from_primitive(env, value, def) +} + +/// Convert Java long to i64, using a field name +#[allow(dead_code)] +pub fn f_i64_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> i64) -> i64 { + let value = f_get_value(env, base, field, "J"); + + i64_from_primitive(env, value, def) +} + +/// Convert Java boolean to bool, using a field name +pub fn f_bool_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> bool) -> bool { + let value = f_get_value(env, base, field, "Z"); + + bool_from_primitive(env, value, def) +} From 65db9eb7ce3625d1388b1709db387f8384ea7848 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 20 Aug 2024 17:27:37 +0200 Subject: [PATCH 03/32] rust fmt formatting (did i figure it out?) --- bindings/java/src/collections.rs | 21 +++-- bindings/java/src/conversions.rs | 47 ++++++---- bindings/java/src/enums.rs | 50 +++++++---- bindings/java/src/geom.rs | 116 +++++++++++++++--------- bindings/java/src/java.rs | 39 ++------ bindings/java/src/lib.rs | 92 ++++++++++--------- bindings/java/src/measure.rs | 146 ++++++++++++------------------ bindings/java/src/primitives.rs | 149 +++++++++++++++---------------- 8 files changed, 332 insertions(+), 328 deletions(-) diff --git a/bindings/java/src/collections.rs b/bindings/java/src/collections.rs index ab9326017..083ca12c3 100644 --- a/bindings/java/src/collections.rs +++ b/bindings/java/src/collections.rs @@ -1,7 +1,7 @@ -use std::convert::TryInto; -use jni::JNIEnv; -use jni::objects::{JObject, JValue, JValueOwned}; use crate::conversions::f_get_value; +use jni::objects::{JObject, JValue, JValueOwned}; +use jni::JNIEnv; +use std::convert::TryInto; /// Unwraps a Java List into a Rust Vec pub unsafe fn get_list<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F) -> Vec @@ -16,11 +16,7 @@ where let list = env.get_list(&jlist_object).unwrap(); - let jlist_size = env - .call_method(&list, "size", "()I", &[]) - .expect("Couldn't call size on List") - .i() - .unwrap(); + let jlist_size = env.call_method(&list, "size", "()I", &[]).expect("Couldn't call size on List").i().unwrap(); let mut vec: Vec = Vec::new(); for i in 0..jlist_size { @@ -37,13 +33,16 @@ where /// Unwraps a Java List into a Rust array, it basically uses `unwrap_jlist` and then tries to convert the Vec into an array #[allow(dead_code)] -pub unsafe fn get_array<'local, T, F, const N: usize>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F) -> [T; N] +pub unsafe fn get_array<'local, T, F, const N: usize>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + f: F, +) -> [T; N] where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> Option, { let vec = get_list(env, value, f); - vec.try_into() - .unwrap_or_else(|v: Vec| panic!("Expected a Vec of length {} but it was {}", N, v.len())) + vec.try_into().unwrap_or_else(|v: Vec| panic!("Expected a Vec of length {} but it was {}", N, v.len())) } pub unsafe fn f_get_list<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F) -> Vec diff --git a/bindings/java/src/conversions.rs b/bindings/java/src/conversions.rs index 0654fac8a..f1a610b64 100644 --- a/bindings/java/src/conversions.rs +++ b/bindings/java/src/conversions.rs @@ -1,24 +1,29 @@ -use jni::JNIEnv; -use jni::objects::{JObject, JValueOwned}; -use taffy::{Dimension, GridPlacement, Line, Overflow, Point, Rect, Size, Style}; use crate::collections::f_get_list; -use crate::enums::{f_get_align_content, f_get_align_items, f_get_box_sizing, f_get_display, f_get_flex_direction, f_get_flex_wrap, f_get_grid_auto_flow, f_get_position, f_get_text_align, get_overflow}; +use crate::enums::{ + f_get_align_content, f_get_align_items, f_get_box_sizing, f_get_display, f_get_flex_direction, f_get_flex_wrap, + f_get_grid_auto_flow, f_get_position, f_get_text_align, get_overflow, +}; use crate::geom::{f_get_line, f_get_point, f_get_rect, f_get_size, get_opt_non_repeated_track_sizing_function}; -use crate::measure::{f_get_dimension_or, get_dimension, get_grid_placement, get_length_percentage, get_length_percentage_auto, get_opt_track_sizing_function}; +use crate::measure::{ + f_get_dimension_or, get_dimension, get_grid_placement, get_length_percentage, get_length_percentage_auto, + get_opt_track_sizing_function, +}; use crate::primitives::{f_bool_from_primitive, f_f32_from_primitive, f_opt_f32_from_object}; +use jni::objects::{JObject, JValueOwned}; +use jni::JNIEnv; +use taffy::{Dimension, GridPlacement, Line, Overflow, Point, Rect, Size, Style}; pub unsafe fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local>) -> Style { let display = f_get_display(env, style, "display"); let item_is_table = f_bool_from_primitive(env, style, "itemIsTable", || false); let box_sizing = f_get_box_sizing(env, style, "boxSizing"); - let overflow = f_get_point(env, style, "overflow", get_overflow, - || Point { x: Overflow::Visible, y: Overflow::Visible }); + let overflow = + f_get_point(env, style, "overflow", get_overflow, || Point { x: Overflow::Visible, y: Overflow::Visible }); let scrollbar_width = f_f32_from_primitive(env, style, "scrollbarWidth", || 0.0); let position = f_get_position(env, style, "position"); - let inset = f_get_rect(env, style, "inset", get_length_percentage_auto, - || Rect::auto()); + let inset = f_get_rect(env, style, "inset", get_length_percentage_auto, || Rect::auto()); let size = f_get_size(env, style, "size", get_dimension, || Size::auto()); let min_size = f_get_size(env, style, "minSize", get_dimension, || Size::auto()); @@ -37,7 +42,6 @@ pub unsafe fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local let justify_content = f_get_align_content(env, style, "justifyContent"); let gap = f_get_size(env, style, "gap", get_length_percentage, || Size::zero()); - let text_align = f_get_text_align(env, style, "textAlign"); let flex_direction = f_get_flex_direction(env, style, "flexDirection"); let flex_wrap = f_get_flex_wrap(env, style, "flexWrap"); @@ -52,11 +56,15 @@ pub unsafe fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local let grid_auto_columns = f_get_list(env, style, "gridAutoColumns", get_opt_non_repeated_track_sizing_function); let grid_auto_flow = f_get_grid_auto_flow(env, style, "gridAutoFlow"); - let grid_row = f_get_line(env, style, "gridRow", get_grid_placement, - || Line { start: GridPlacement::Auto, end: GridPlacement::Auto }); + let grid_row = f_get_line(env, style, "gridRow", get_grid_placement, || Line { + start: GridPlacement::Auto, + end: GridPlacement::Auto, + }); - let grid_column = f_get_line(env, style, "gridColumn", get_grid_placement, - || Line { start: GridPlacement::Auto, end: GridPlacement::Auto }); + let grid_column = f_get_line(env, style, "gridColumn", get_grid_placement, || Line { + start: GridPlacement::Auto, + end: GridPlacement::Auto, + }); Style { display, @@ -105,8 +113,11 @@ pub unsafe fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local } } -pub fn f_get_value<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>, field: &str, jtype: &str) -> JValueOwned<'local> { - env - .get_field(object, field, jtype) - .expect(format!("Couldn't get field {}, {}", field, jtype).as_str()) +pub fn f_get_value<'local>( + env: &mut JNIEnv<'local>, + object: &JObject<'local>, + field: &str, + jtype: &str, +) -> JValueOwned<'local> { + env.get_field(object, field, jtype).expect(format!("Couldn't get field {}, {}", field, jtype).as_str()) } diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index 65a177976..9c7b1a5e2 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -1,8 +1,10 @@ -use jni::JNIEnv; -use jni::objects::{JObject, JValueOwned}; -use taffy::{AlignContent, AlignItems, BoxSizing, Display, FlexDirection, FlexWrap, GridAutoFlow, Overflow, Position, TextAlign}; use crate::conversions::f_get_value; use crate::primitives::f_i32_from_primitive; +use jni::objects::{JObject, JValueOwned}; +use jni::JNIEnv; +use taffy::{ + AlignContent, AlignItems, BoxSizing, Display, FlexDirection, FlexWrap, GridAutoFlow, Overflow, Position, TextAlign, +}; pub fn get_overflow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Overflow { let obj = &value.l().unwrap(); @@ -16,7 +18,7 @@ pub fn get_overflow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local> 0 => Overflow::Visible, 1 => Overflow::Clip, 2 => Overflow::Hidden, - _ => Overflow::Scroll + _ => Overflow::Scroll, } } @@ -37,7 +39,7 @@ pub fn get_position<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local> match internal { 0 => Position::Relative, - _ => Position::Absolute + _ => Position::Absolute, } } @@ -47,7 +49,11 @@ pub fn f_get_position<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, get_position(env, obj) } -pub fn get_text_align<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> TextAlign) -> TextAlign { +pub fn get_text_align<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + def: fn() -> TextAlign, +) -> TextAlign { let obj = &value.l().unwrap(); if obj.is_null() { return def(); @@ -59,7 +65,7 @@ pub fn get_text_align<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loca 0 => TextAlign::Auto, 1 => TextAlign::LegacyLeft, 2 => TextAlign::LegacyRight, - _ => TextAlign::LegacyCenter + _ => TextAlign::LegacyCenter, } } @@ -69,7 +75,11 @@ pub fn f_get_text_align<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local> get_text_align(env, obj, TextAlign::default) } -pub fn get_flex_direction<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> FlexDirection) -> FlexDirection { +pub fn get_flex_direction<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + def: fn() -> FlexDirection, +) -> FlexDirection { let obj = &value.l().unwrap(); if obj.is_null() { return def(); @@ -81,7 +91,7 @@ pub fn get_flex_direction<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<' 0 => FlexDirection::Row, 1 => FlexDirection::Column, 2 => FlexDirection::RowReverse, - _ => FlexDirection::ColumnReverse + _ => FlexDirection::ColumnReverse, } } @@ -102,7 +112,7 @@ pub fn get_flex_wrap<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local match internal { 0 => FlexWrap::NoWrap, 1 => FlexWrap::Wrap, - _ => FlexWrap::WrapReverse + _ => FlexWrap::WrapReverse, } } @@ -112,7 +122,11 @@ pub fn f_get_flex_wrap<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, get_flex_wrap(env, obj, FlexWrap::default) } -pub fn get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> GridAutoFlow) -> GridAutoFlow { +pub fn get_grid_auto_flow<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + def: fn() -> GridAutoFlow, +) -> GridAutoFlow { let obj = &value.l().unwrap(); if obj.is_null() { return def(); @@ -124,7 +138,7 @@ pub fn get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<' 0 => GridAutoFlow::Row, 1 => GridAutoFlow::Column, 2 => GridAutoFlow::RowDense, - _ => GridAutoFlow::ColumnDense + _ => GridAutoFlow::ColumnDense, } } @@ -149,7 +163,7 @@ pub fn get_align_items<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc 3 => Some(AlignItems::FlexEnd), 4 => Some(AlignItems::Center), 5 => Some(AlignItems::Baseline), - _ => Some(AlignItems::Stretch) + _ => Some(AlignItems::Stretch), } } @@ -176,11 +190,15 @@ pub fn get_align_content<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'l 5 => Some(AlignContent::Stretch), 6 => Some(AlignContent::SpaceBetween), 7 => Some(AlignContent::SpaceEvenly), - _ => Some(AlignContent::SpaceAround) + _ => Some(AlignContent::SpaceAround), } } -pub fn f_get_align_content<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Option { +pub fn f_get_align_content<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, +) -> Option { let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignContent;"); get_align_content(env, obj) @@ -218,7 +236,7 @@ pub fn get_box_sizing<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loca match internal { 0 => BoxSizing::BorderBox, - _ => BoxSizing::ContentBox + _ => BoxSizing::ContentBox, } } diff --git a/bindings/java/src/geom.rs b/bindings/java/src/geom.rs index 3ea4840bd..851f4ffb5 100644 --- a/bindings/java/src/geom.rs +++ b/bindings/java/src/geom.rs @@ -1,11 +1,16 @@ -use jni::JNIEnv; +use crate::conversions::f_get_value; +use crate::measure::{get_max_track_sizing_function, get_min_track_sizing_function}; use jni::objects::{JObject, JValueOwned}; +use jni::JNIEnv; use taffy::{Line, MaxTrackSizingFunction, MinTrackSizingFunction, NonRepeatedTrackSizingFunction, Point, Rect, Size}; -use crate::conversions::{f_get_value}; -use crate::measure::{get_max_track_sizing_function, get_min_track_sizing_function}; /// Get a Point from its Java counterpart, using a JValueOwned -pub fn get_point<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F, def: fn() -> Point) -> Point +pub fn get_point<'local, T, F>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + f: F, + def: fn() -> Point, +) -> Point where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { @@ -17,14 +22,16 @@ where let x_field = f_get_value(env, point_object, "x", "Ljava/lang/Object;"); let y_field = f_get_value(env, point_object, "y", "Ljava/lang/Object;"); - Point { - x: f(env, x_field), - y: f(env, y_field), - } + Point { x: f(env, x_field), y: f(env, y_field) } } /// Get a Rect from its Java counterpart, using a JValueOwned -pub fn get_rect<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F, def: fn() -> Rect) -> Rect +pub fn get_rect<'local, T, F>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + f: F, + def: fn() -> Rect, +) -> Rect where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { @@ -38,17 +45,16 @@ where let t_field = f_get_value(env, rect_object, "top", "Ljava/lang/Object;"); let b_field = f_get_value(env, rect_object, "bottom", "Ljava/lang/Object;"); - - Rect { - left: f(env, l_field), - right: f(env, r_field), - top: f(env, t_field), - bottom: f(env, b_field), - } + Rect { left: f(env, l_field), right: f(env, r_field), top: f(env, t_field), bottom: f(env, b_field) } } /// Get a Size from its Java counterpart, using a JValueOwned -pub fn get_size<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F, def: fn() -> Size) -> Size +pub fn get_size<'local, T, F>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + f: F, + def: fn() -> Size, +) -> Size where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { @@ -60,14 +66,16 @@ where let w_field = f_get_value(env, size_object, "width", "Ljava/lang/Object;"); let h_field = f_get_value(env, size_object, "height", "Ljava/lang/Object;"); - Size { - width: f(env, w_field), - height: f(env, h_field), - } + Size { width: f(env, w_field), height: f(env, h_field) } } /// Get a Line from its Java counterpart, using a JValueOwned -pub fn get_line<'local, T, F>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, f: F, def: fn() -> Line) -> Line +pub fn get_line<'local, T, F>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + f: F, + def: fn() -> Line, +) -> Line where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { @@ -79,16 +87,13 @@ where let s_field = f_get_value(env, line_object, "start", "Ljava/lang/Object;"); let e_field = f_get_value(env, line_object, "end", "Ljava/lang/Object;"); - Line { - start: f(env, s_field), - end: f(env, e_field), - } + Line { start: f(env, s_field), end: f(env, e_field) } } /// Get a NonRepeatedTrackSizingFunction from its Java counterpart, using a JValueOwned pub fn get_non_repeated_track_sizing_function<'local>( env: &mut JNIEnv<'local>, - value: JValueOwned<'local> + value: JValueOwned<'local>, ) -> NonRepeatedTrackSizingFunction { let nrtsf_object = &value.l().unwrap(); if nrtsf_object.is_null() { @@ -101,20 +106,17 @@ pub fn get_non_repeated_track_sizing_function<'local>( let min = get_min_track_sizing_function(env, n_val, || MinTrackSizingFunction::Auto); let max = get_max_track_sizing_function(env, x_val, || MaxTrackSizingFunction::Auto); - NonRepeatedTrackSizingFunction { - min, - max, - } + NonRepeatedTrackSizingFunction { min, max } } /// Get an Option from its Java counterpart, using a JValueOwned pub fn get_opt_non_repeated_track_sizing_function<'local>( env: &mut JNIEnv<'local>, - value: JValueOwned<'local> + value: JValueOwned<'local>, ) -> Option { let nrtsf_object = &value.l().unwrap(); if nrtsf_object.is_null() { - return None + return None; } let n_val = f_get_value(env, nrtsf_object, "min", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction;"); @@ -123,14 +125,17 @@ pub fn get_opt_non_repeated_track_sizing_function<'local>( let min = get_min_track_sizing_function(env, n_val, || MinTrackSizingFunction::Auto); let max = get_max_track_sizing_function(env, x_val, || MaxTrackSizingFunction::Auto); - Some(NonRepeatedTrackSizingFunction { - min, - max, - }) + Some(NonRepeatedTrackSizingFunction { min, max }) } /// Get a Point from its Java counterpart, using a field name -pub fn f_get_point<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F, def: fn() -> Point) -> Point +pub fn f_get_point<'local, T, F>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + f: F, + def: fn() -> Point, +) -> Point where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { @@ -140,7 +145,13 @@ where } /// Get a Rect from its Java counterpart, using a field name -pub fn f_get_rect<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F, def: fn() -> Rect) -> Rect +pub fn f_get_rect<'local, T, F>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + f: F, + def: fn() -> Rect, +) -> Rect where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { @@ -150,7 +161,13 @@ where } /// Get a Size from its Java counterpart, using a field name -pub fn f_get_size<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F, def: fn() -> Size) -> Size +pub fn f_get_size<'local, T, F>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + f: F, + def: fn() -> Size, +) -> Size where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { @@ -160,7 +177,13 @@ where } /// Get a Line from its Java counterpart, using a field name -pub fn f_get_line<'local, T, F>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: F, def: fn() -> Line) -> Line +pub fn f_get_line<'local, T, F>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + f: F, + def: fn() -> Line, +) -> Line where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { @@ -170,8 +193,13 @@ where } /// Get a NonRepeatedTrackSizingFunction from its Java counterpart, using a field name -pub fn f_get_non_repeated_track_sizing_function<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> NonRepeatedTrackSizingFunction { - let nrtsft_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction;"); +pub fn f_get_non_repeated_track_sizing_function<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, +) -> NonRepeatedTrackSizingFunction { + let nrtsft_field = + f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction;"); get_non_repeated_track_sizing_function(env, nrtsft_field) -} \ No newline at end of file +} diff --git a/bindings/java/src/java.rs b/bindings/java/src/java.rs index 30efe2cc1..1cba18da1 100644 --- a/bindings/java/src/java.rs +++ b/bindings/java/src/java.rs @@ -1,6 +1,6 @@ -use jni::JNIEnv; use jni::objects::{JObject, JValue}; use jni::sys::jobject; +use jni::JNIEnv; use taffy::{Layout, Point, Rect, Size}; pub fn to_java_layout<'local>(env: &mut JNIEnv<'local>, layout: &Layout) -> jobject { @@ -32,14 +32,7 @@ pub fn f32_point_to_java<'local>(env: &mut JNIEnv<'local>, point: Point) -> let a = &f32_to_java(env, point.x); let b = &f32_to_java(env, point.y); - env.new_object( - class, - "(Ljava/lang/Object;Ljava/lang/Object;)V", - &[ - JValue::Object(a), - JValue::Object(b), - ], - ).unwrap() + env.new_object(class, "(Ljava/lang/Object;Ljava/lang/Object;)V", &[JValue::Object(a), JValue::Object(b)]).unwrap() } pub fn f32_size_to_java<'local>(env: &mut JNIEnv<'local>, size: Size) -> JObject<'local> { @@ -48,20 +41,12 @@ pub fn f32_size_to_java<'local>(env: &mut JNIEnv<'local>, size: Size) -> JO let a = &f32_to_java(env, size.width); let b = &f32_to_java(env, size.height); - env.new_object( - class, - "(Ljava/lang/Object;Ljava/lang/Object;)V", - &[ - JValue::Object(a), - JValue::Object(b), - ], - ).unwrap() + env.new_object(class, "(Ljava/lang/Object;Ljava/lang/Object;)V", &[JValue::Object(a), JValue::Object(b)]).unwrap() } pub fn f32_rect_to_java<'local>(env: &mut JNIEnv<'local>, rect: Rect) -> JObject<'local> { let class = &env.find_class("com/dioxuslabs/taffy/geom/TaffyRect").unwrap(); - let a = &f32_to_java(env, rect.left); let b = &f32_to_java(env, rect.right); let c = &f32_to_java(env, rect.top); @@ -70,21 +55,13 @@ pub fn f32_rect_to_java<'local>(env: &mut JNIEnv<'local>, rect: Rect) -> JO env.new_object( class, "(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V", - &[ - JValue::Object(a), - JValue::Object(b), - JValue::Object(c), - JValue::Object(d), - ], - ).unwrap() + &[JValue::Object(a), JValue::Object(b), JValue::Object(c), JValue::Object(d)], + ) + .unwrap() } pub fn f32_to_java<'local>(env: &mut JNIEnv<'local>, value: f32) -> JObject<'local> { let class = &env.find_class("java/lang/Float").unwrap(); - env.new_object( - class, - "(F)V", - &[JValue::Float(value)], - ).unwrap() -} \ No newline at end of file + env.new_object(class, "(F)V", &[JValue::Float(value)]).unwrap() +} diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs index 0996b7802..ecc51c887 100644 --- a/bindings/java/src/lib.rs +++ b/bindings/java/src/lib.rs @@ -1,37 +1,39 @@ -mod conversions; mod collections; -mod primitives; -mod geom; +mod conversions; mod enums; -mod measure; +mod geom; mod java; +mod measure; +mod primitives; -use std::panic; -use jni::JNIEnv; -use jni::objects::{JClass, JObject, JValueOwned}; -use jni::sys::{jint, jlong, jobject}; -use taffy::{NodeId, TaffyTree, TraversePartialTree}; -use crate::collections::{get_list}; +use crate::collections::get_list; use crate::conversions::get_style; use crate::geom::get_size; use crate::java::to_java_layout; use crate::measure::get_available_space; use crate::primitives::{node_id_from_primitive, opt_node_id_from_object}; +use jni::objects::{JClass, JObject, JValueOwned}; +use jni::sys::{jint, jlong, jobject}; +use jni::JNIEnv; +use std::panic; +use taffy::{NodeId, TaffyTree, TraversePartialTree}; #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree<'local>(_env: JNIEnv<'local>, - _class: JClass<'local>) - -> jlong { +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree<'local>( + _env: JNIEnv<'local>, + _class: JClass<'local>, +) -> jlong { let tree: Box> = Box::new(TaffyTree::new()); Box::into_raw(tree) as jlong } #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>(mut env: JNIEnv<'local>, - _class: JClass<'local>, - pointer: jlong, - style: jobject) - -> jlong { +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + style: jobject, +) -> jlong { unsafe { let style = get_style(&mut env, &JObject::from_raw(style)); @@ -46,12 +48,13 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>(mut e } #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren<'local>(mut env: JNIEnv<'local>, - _class: JClass<'local>, - pointer: jlong, - style: jobject, - children: jobject) - -> jlong { +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + style: jobject, + children: jobject, +) -> jlong { unsafe { let style = get_style(&mut env, &JObject::from_raw(style)); let list = JValueOwned::from(JObject::from_raw(children)); @@ -68,33 +71,35 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren<'loca } #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_computeLayout<'local>(mut env: JNIEnv<'local>, - _class: JClass<'local>, - pointer: jlong, - node: jlong, - available_size: jobject) { +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_computeLayout<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong, + available_size: jobject, +) { unsafe { let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); let as_object = JValueOwned::from(JObject::from_raw(available_size)); - let available_size = get_size(&mut env, as_object, get_available_space, - || panic!("AvailableSize cannot be null")); + let available_size = + get_size(&mut env, as_object, get_available_space, || panic!("AvailableSize cannot be null")); let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; if raw_ptr.is_null() { return; } let tree: &mut TaffyTree<()> = &mut *raw_ptr; - tree.compute_layout(node, available_size) - .expect("Failed to compute layout"); + tree.compute_layout(node, available_size).expect("Failed to compute layout"); } } #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_childCount<'local>(mut env: JNIEnv<'local>, - _class: JClass<'local>, - pointer: jlong, - node: jlong) - -> jint { +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_childCount<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong, +) -> jint { let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; if raw_ptr.is_null() { @@ -105,11 +110,12 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_childCount<'local>(mu res as jint } #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_layout<'local>(mut env: JNIEnv<'local>, - _class: JClass<'local>, - pointer: jlong, - node: jlong) - -> jobject { +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_layout<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong, +) -> jobject { let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; if raw_ptr.is_null() { diff --git a/bindings/java/src/measure.rs b/bindings/java/src/measure.rs index 0477f98d3..6a052555d 100644 --- a/bindings/java/src/measure.rs +++ b/bindings/java/src/measure.rs @@ -1,16 +1,16 @@ -use jni::JNIEnv; -use jni::objects::{JObject, JValueOwned}; -use taffy::{AvailableSpace, Dimension, GridPlacement, GridTrackRepetition, LengthPercentage, LengthPercentageAuto, MaxTrackSizingFunction, MinTrackSizingFunction, TrackSizingFunction}; -use taffy::style_helpers::{TaffyGridLine, TaffyGridSpan}; use crate::collections::f_get_list; use crate::conversions::f_get_value; use crate::geom::{f_get_non_repeated_track_sizing_function, get_opt_non_repeated_track_sizing_function}; use crate::primitives::{f_f32_from_primitive, f_i16_from_primitive, f_i8_from_primitive}; +use jni::objects::{JObject, JValueOwned}; +use jni::JNIEnv; +use taffy::style_helpers::{TaffyGridLine, TaffyGridSpan}; +use taffy::{ + AvailableSpace, Dimension, GridPlacement, GridTrackRepetition, LengthPercentage, LengthPercentageAuto, + MaxTrackSizingFunction, MinTrackSizingFunction, TrackSizingFunction, +}; -pub fn get_length_percentage<'local>( - env: &mut JNIEnv<'local>, - value: JValueOwned<'local>, -) -> LengthPercentage { +pub fn get_length_percentage<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> LengthPercentage { let obj = &value.l().unwrap(); if obj.is_null() { @@ -20,12 +20,8 @@ pub fn get_length_percentage<'local>( let internal = f_i8_from_primitive(env, obj, "type", || 0); match internal { - 0 => LengthPercentage::Length( - f_f32_from_primitive(env, obj, "value", || 0.0) - ), - _ => LengthPercentage::Percent( - f_f32_from_primitive(env, obj, "value", || 0.0) - ) + 0 => LengthPercentage::Length(f_f32_from_primitive(env, obj, "value", || 0.0)), + _ => LengthPercentage::Percent(f_f32_from_primitive(env, obj, "value", || 0.0)), } } @@ -42,20 +38,13 @@ pub fn get_length_percentage_auto<'local>( let internal = f_i8_from_primitive(env, obj, "type", || 0); match internal { - 0 => LengthPercentageAuto::Length( - f_f32_from_primitive(env, obj, "value", || 0.0) - ), - 1 => LengthPercentageAuto::Percent( - f_f32_from_primitive(env, obj, "value", || 0.0) - ), - _ => LengthPercentageAuto::Auto + 0 => LengthPercentageAuto::Length(f_f32_from_primitive(env, obj, "value", || 0.0)), + 1 => LengthPercentageAuto::Percent(f_f32_from_primitive(env, obj, "value", || 0.0)), + _ => LengthPercentageAuto::Auto, } } -pub fn get_dimension<'local>( - env: &mut JNIEnv<'local>, - value: JValueOwned<'local>, -) -> Dimension { +pub fn get_dimension<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Dimension { let obj = &value.l().unwrap(); if obj.is_null() { @@ -65,20 +54,13 @@ pub fn get_dimension<'local>( let internal = f_i8_from_primitive(env, obj, "type", || 0); match internal { - 0 => Dimension::Length( - f_f32_from_primitive(env, obj, "value", || 0.0) - ), - 1 => Dimension::Percent( - f_f32_from_primitive(env, obj, "value", || 0.0) - ), - _ => Dimension::Auto + 0 => Dimension::Length(f_f32_from_primitive(env, obj, "value", || 0.0)), + 1 => Dimension::Percent(f_f32_from_primitive(env, obj, "value", || 0.0)), + _ => Dimension::Auto, } } -pub fn get_available_space<'local>( - env: &mut JNIEnv<'local>, - value: JValueOwned<'local>, -) -> AvailableSpace { +pub fn get_available_space<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> AvailableSpace { let obj = &value.l().unwrap(); if obj.is_null() { @@ -88,11 +70,9 @@ pub fn get_available_space<'local>( let internal = f_i8_from_primitive(env, obj, "type", || 0); match internal { - 0 => AvailableSpace::Definite( - f_f32_from_primitive(env, obj, "value", || 0.0) - ), + 0 => AvailableSpace::Definite(f_f32_from_primitive(env, obj, "value", || 0.0)), 1 => AvailableSpace::MinContent, - _ => AvailableSpace::MaxContent + _ => AvailableSpace::MaxContent, } } @@ -104,26 +84,19 @@ pub fn get_dimension_or<'local>( let obj = &value.l().unwrap(); if obj.is_null() { - return f() + return f(); } let internal = f_i8_from_primitive(env, obj, "type", || 0); match internal { - 0 => Dimension::Length( - f_f32_from_primitive(env, obj, "value", || 0.0) - ), - 1 => Dimension::Percent( - f_f32_from_primitive(env, obj, "value", || 0.0) - ), - _ => Dimension::Auto + 0 => Dimension::Length(f_f32_from_primitive(env, obj, "value", || 0.0)), + 1 => Dimension::Percent(f_f32_from_primitive(env, obj, "value", || 0.0)), + _ => Dimension::Auto, } } -pub fn get_grid_track_repetition<'local>( - env: &mut JNIEnv<'local>, - value: JValueOwned<'local>, -) -> GridTrackRepetition { +pub fn get_grid_track_repetition<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> GridTrackRepetition { let obj = &value.l().unwrap(); if obj.is_null() { @@ -135,16 +108,11 @@ pub fn get_grid_track_repetition<'local>( match internal { 0 => GridTrackRepetition::AutoFill, 1 => GridTrackRepetition::AutoFit, - _ => GridTrackRepetition::Count( - f_i16_from_primitive(env, obj, "value", || 0) as u16 - ) + _ => GridTrackRepetition::Count(f_i16_from_primitive(env, obj, "value", || 0) as u16), } } -pub fn get_grid_placement<'local>( - env: &mut JNIEnv<'local>, - value: JValueOwned<'local>, -) -> GridPlacement { +pub fn get_grid_placement<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> GridPlacement { let obj = &value.l().unwrap(); if obj.is_null() { @@ -155,12 +123,8 @@ pub fn get_grid_placement<'local>( match internal { 0 => GridPlacement::Auto, - 1 => GridPlacement::from_line_index( - f_i16_from_primitive(env, obj, "value", || 0) - ), - _ => GridPlacement::from_span( - f_i16_from_primitive(env, obj, "value", || 0) as u16 - ) + 1 => GridPlacement::from_line_index(f_i16_from_primitive(env, obj, "value", || 0)), + _ => GridPlacement::from_span(f_i16_from_primitive(env, obj, "value", || 0) as u16), } } @@ -178,12 +142,10 @@ pub fn get_min_track_sizing_function<'local>( let internal = f_i8_from_primitive(env, obj, "type", || 0); match internal { - 0 => MinTrackSizingFunction::Fixed( - f_get_length_percentage(env, obj, "value") - ), + 0 => MinTrackSizingFunction::Fixed(f_get_length_percentage(env, obj, "value")), 1 => MinTrackSizingFunction::MinContent, 2 => MinTrackSizingFunction::MaxContent, - _ => MinTrackSizingFunction::Auto + _ => MinTrackSizingFunction::Auto, } } @@ -201,18 +163,12 @@ pub fn get_max_track_sizing_function<'local>( let internal = f_i8_from_primitive(env, obj, "type", || 0); match internal { - 0 => MaxTrackSizingFunction::Fixed( - f_get_length_percentage(env, obj, "value") - ), + 0 => MaxTrackSizingFunction::Fixed(f_get_length_percentage(env, obj, "value")), 1 => MaxTrackSizingFunction::MinContent, 2 => MaxTrackSizingFunction::MaxContent, - 3 => MaxTrackSizingFunction::FitContent( - f_get_length_percentage(env, obj, "value") - ), + 3 => MaxTrackSizingFunction::FitContent(f_get_length_percentage(env, obj, "value")), 4 => MaxTrackSizingFunction::Auto, - _ => MaxTrackSizingFunction::Fraction( - f_f32_from_primitive(env, obj, "value", || 0.0) - ) + _ => MaxTrackSizingFunction::Fraction(f_f32_from_primitive(env, obj, "value", || 0.0)), } } @@ -223,23 +179,24 @@ pub fn get_opt_track_sizing_function<'local>( let obj = &value.l().unwrap(); if obj.is_null() { - return None + return None; } let internal = f_i8_from_primitive(env, obj, "type", || 0); Some(match internal { - 0 => TrackSizingFunction::Single( - f_get_non_repeated_track_sizing_function(env, obj, "func") - ), - _ => TrackSizingFunction::Repeat( - f_get_grid_track_repetition(env, obj, "repetitions"), - unsafe { f_get_list(env, obj, "functions", get_opt_non_repeated_track_sizing_function) }, - ) + 0 => TrackSizingFunction::Single(f_get_non_repeated_track_sizing_function(env, obj, "func")), + _ => TrackSizingFunction::Repeat(f_get_grid_track_repetition(env, obj, "repetitions"), unsafe { + f_get_list(env, obj, "functions", get_opt_non_repeated_track_sizing_function) + }), }) } -pub fn f_get_length_percentage<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> LengthPercentage { +pub fn f_get_length_percentage<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, +) -> LengthPercentage { let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage;"); get_length_percentage(env, value) @@ -252,14 +209,23 @@ pub fn f_get_dimension<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, get_dimension(env, value) } -pub fn f_get_dimension_or<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, f: fn() -> Dimension) -> Dimension { +pub fn f_get_dimension_or<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + f: fn() -> Dimension, +) -> Dimension { let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/TaffyDimension;"); get_dimension_or(env, value, f) } -pub fn f_get_grid_track_repetition<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> GridTrackRepetition { +pub fn f_get_grid_track_repetition<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, +) -> GridTrackRepetition { let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition;"); get_grid_track_repetition(env, value) -} \ No newline at end of file +} diff --git a/bindings/java/src/primitives.rs b/bindings/java/src/primitives.rs index d88e998fe..83e3db196 100644 --- a/bindings/java/src/primitives.rs +++ b/bindings/java/src/primitives.rs @@ -1,13 +1,11 @@ -use jni::JNIEnv; +use crate::conversions::f_get_value; use jni::objects::{JObject, JValueOwned}; +use jni::JNIEnv; use taffy::NodeId; -use crate::conversions::f_get_value; /// Convert Java float to f32 pub fn f32_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> f32) -> f32 { - value - .f() - .unwrap_or(def()) + value.f().unwrap_or(def()) } /// Convert Java Float object to f32 @@ -19,33 +17,27 @@ pub fn f32_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc return def(); } - env - .call_method(object, "floatValue", "()F", &[]) - .unwrap() - .f() - .unwrap() as f32 + env.call_method(object, "floatValue", "()F", &[]).unwrap().f().unwrap() as f32 } /// Convert Java Float object to Option -pub fn opt_f32_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> Option) -> Option { +pub fn opt_f32_from_object<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + def: fn() -> Option, +) -> Option { let object = value.l().unwrap(); if object.is_null() { return def(); } - Some(env - .call_method(object, "floatValue", "()F", &[]) - .unwrap() - .f() - .unwrap()) + Some(env.call_method(object, "floatValue", "()F", &[]).unwrap().f().unwrap()) } /// Convert Java byte to i8 pub fn i8_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i8) -> i8 { - value - .b() - .unwrap_or(def()) as i8 + value.b().unwrap_or(def()) as i8 } /// Convert Java Byte object to i8 @@ -57,18 +49,12 @@ pub fn i8_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loca return def(); } - env - .call_method(object, "byteValue", "()B", &[]) - .unwrap() - .b() - .unwrap() as i8 + env.call_method(object, "byteValue", "()B", &[]).unwrap().b().unwrap() as i8 } /// Convert Java short to i16 pub fn i16_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i16) -> i16 { - value - .s() - .unwrap_or(def()) as i16 + value.s().unwrap_or(def()) as i16 } /// Convert Java Short object to i16 @@ -80,18 +66,12 @@ pub fn i16_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc return def(); } - env - .call_method(object, "shortValue", "()S", &[]) - .unwrap() - .s() - .unwrap() as i16 + env.call_method(object, "shortValue", "()S", &[]).unwrap().s().unwrap() as i16 } /// Convert Java int to i32 pub fn i32_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i32) -> i32 { - value - .i() - .unwrap_or(def()) as i32 + value.i().unwrap_or(def()) as i32 } /// Convert Java Integer object to i32 @@ -103,19 +83,13 @@ pub fn i32_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc return def(); } - env - .call_method(object, "intValue", "()I", &[]) - .unwrap() - .i() - .unwrap() as i32 + env.call_method(object, "intValue", "()I", &[]).unwrap().i().unwrap() as i32 } /// Convert Java long to i64 #[allow(dead_code)] pub fn i64_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i64) -> i64 { - value - .j() - .unwrap_or(def()) as i64 + value.j().unwrap_or(def()) as i64 } /// Convert Java Long object to i64 @@ -127,11 +101,7 @@ pub fn i64_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc return def(); } - env - .call_method(object, "longValue", "()J", &[]) - .unwrap() - .j() - .unwrap() as i64 + env.call_method(object, "longValue", "()J", &[]).unwrap().j().unwrap() as i64 } /// Convert Java Long object to Option @@ -142,18 +112,12 @@ pub fn opt_i64_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned< return None; } - Some(env - .call_method(object, "longValue", "()J", &[]) - .unwrap() - .j() - .unwrap() as i64) + Some(env.call_method(object, "longValue", "()J", &[]).unwrap().j().unwrap() as i64) } /// Convert Java boolean to bool pub fn bool_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> bool) -> bool { - value - .z() - .unwrap_or(def()) + value.z().unwrap_or(def()) } /// Convert Java Boolean object to bool @@ -165,27 +129,23 @@ pub fn bool_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'lo return def(); } - env - .call_method(object, "booleanValue", "()Z", &[]) - .unwrap() - .z() - .unwrap() + env.call_method(object, "booleanValue", "()Z", &[]).unwrap().z().unwrap() } /// Convert Java Boolean object to Option #[allow(dead_code)] -pub fn opt_bool_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> Option) -> Option { +pub fn opt_bool_from_object<'local>( + env: &mut JNIEnv<'local>, + value: JValueOwned<'local>, + def: fn() -> Option, +) -> Option { let object = value.l().unwrap(); if object.is_null() { return def(); } - Some(env - .call_method(object, "booleanValue", "()Z", &[]) - .unwrap() - .z() - .unwrap()) + Some(env.call_method(object, "booleanValue", "()Z", &[]).unwrap().z().unwrap()) } /// Convert Java long to NodeId @@ -203,39 +163,68 @@ pub fn node_id_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned< /// Convert Java Long to Option pub fn opt_node_id_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Option { let o = opt_i64_from_object(env, value); - if o.is_none() { None } else { Some(NodeId::from(o? as u64)) } + if o.is_none() { + None + } else { + Some(NodeId::from(o? as u64)) + } } /// Convert Java float to f32, using a field name -pub fn f_f32_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> f32) -> f32 { +pub fn f_f32_from_primitive<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + def: fn() -> f32, +) -> f32 { let value = f_get_value(env, base, field, "F"); f32_from_primitive(env, value, def) } /// Convert Java float to f32, using a field name -pub fn f_opt_f32_from_object<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> Option) -> Option { +pub fn f_opt_f32_from_object<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + def: fn() -> Option, +) -> Option { let value = f_get_value(env, base, field, "Ljava/lang/Float;"); opt_f32_from_object(env, value, def) } /// Convert Java byte to i8, using a field name -pub fn f_i8_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> i8) -> i8 { +pub fn f_i8_from_primitive<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + def: fn() -> i8, +) -> i8 { let value = f_get_value(env, base, field, "B"); i8_from_primitive(env, value, def) } /// Convert Java short to i16, using a field name -pub fn f_i16_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> i16) -> i16 { +pub fn f_i16_from_primitive<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + def: fn() -> i16, +) -> i16 { let value = f_get_value(env, base, field, "B"); i16_from_primitive(env, value, def) } /// Convert Java int to i32, using a field name -pub fn f_i32_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> i32) -> i32 { +pub fn f_i32_from_primitive<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + def: fn() -> i32, +) -> i32 { let value = f_get_value(env, base, field, "I"); i32_from_primitive(env, value, def) @@ -243,14 +232,24 @@ pub fn f_i32_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'lo /// Convert Java long to i64, using a field name #[allow(dead_code)] -pub fn f_i64_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> i64) -> i64 { +pub fn f_i64_from_primitive<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + def: fn() -> i64, +) -> i64 { let value = f_get_value(env, base, field, "J"); i64_from_primitive(env, value, def) } /// Convert Java boolean to bool, using a field name -pub fn f_bool_from_primitive<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str, def: fn() -> bool) -> bool { +pub fn f_bool_from_primitive<'local>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, + def: fn() -> bool, +) -> bool { let value = f_get_value(env, base, field, "Z"); bool_from_primitive(env, value, def) From 4fe722b6b54a7085c8fbed4d58a32278cadbac08 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 20 Aug 2024 17:40:16 +0200 Subject: [PATCH 04/32] Fix Clippy issues --- bindings/java/src/collections.rs | 4 ++-- bindings/java/src/conversions.rs | 18 +++++++++--------- bindings/java/src/java.rs | 2 +- bindings/java/src/lib.rs | 3 +++ bindings/java/src/primitives.rs | 20 ++++++++++---------- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/bindings/java/src/collections.rs b/bindings/java/src/collections.rs index 083ca12c3..6e4f8cbb1 100644 --- a/bindings/java/src/collections.rs +++ b/bindings/java/src/collections.rs @@ -23,8 +23,8 @@ where let element = env.call_method(&list, "get", "(I)Ljava/lang/Object;", &[JValue::Int(i)]).unwrap(); let value = f(env, element); - if value.is_some() { - vec.push(value.unwrap()); + if let Some(value) = value { + vec.push(value); } } diff --git a/bindings/java/src/conversions.rs b/bindings/java/src/conversions.rs index f1a610b64..f0c4f5aad 100644 --- a/bindings/java/src/conversions.rs +++ b/bindings/java/src/conversions.rs @@ -23,16 +23,16 @@ pub unsafe fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local let scrollbar_width = f_f32_from_primitive(env, style, "scrollbarWidth", || 0.0); let position = f_get_position(env, style, "position"); - let inset = f_get_rect(env, style, "inset", get_length_percentage_auto, || Rect::auto()); + let inset = f_get_rect(env, style, "inset", get_length_percentage_auto, Rect::auto); - let size = f_get_size(env, style, "size", get_dimension, || Size::auto()); - let min_size = f_get_size(env, style, "minSize", get_dimension, || Size::auto()); - let max_size = f_get_size(env, style, "maxSize", get_dimension, || Size::auto()); + let size = f_get_size(env, style, "size", get_dimension, Size::auto); + let min_size = f_get_size(env, style, "minSize", get_dimension, Size::auto); + let max_size = f_get_size(env, style, "maxSize", get_dimension, Size::auto); let aspect_ratio = f_opt_f32_from_object(env, style, "aspectRatio", || None); - let margin = f_get_rect(env, style, "margin", get_length_percentage_auto, || Rect::zero()); - let padding = f_get_rect(env, style, "padding", get_length_percentage, || Rect::zero()); - let border = f_get_rect(env, style, "border", get_length_percentage, || Rect::zero()); + let margin = f_get_rect(env, style, "margin", get_length_percentage_auto, Rect::zero); + let padding = f_get_rect(env, style, "padding", get_length_percentage, Rect::zero); + let border = f_get_rect(env, style, "border", get_length_percentage, Rect::zero); let align_items = f_get_align_items(env, style, "alignItems"); let align_self = f_get_align_items(env, style, "alignSelf"); @@ -40,7 +40,7 @@ pub unsafe fn get_style<'local>(env: &mut JNIEnv<'local>, style: &JObject<'local let justify_self = f_get_align_items(env, style, "justifySelf"); let align_content = f_get_align_content(env, style, "alignContent"); let justify_content = f_get_align_content(env, style, "justifyContent"); - let gap = f_get_size(env, style, "gap", get_length_percentage, || Size::zero()); + let gap = f_get_size(env, style, "gap", get_length_percentage, Size::zero); let text_align = f_get_text_align(env, style, "textAlign"); let flex_direction = f_get_flex_direction(env, style, "flexDirection"); @@ -119,5 +119,5 @@ pub fn f_get_value<'local>( field: &str, jtype: &str, ) -> JValueOwned<'local> { - env.get_field(object, field, jtype).expect(format!("Couldn't get field {}, {}", field, jtype).as_str()) + env.get_field(object, field, jtype).unwrap_or_else(|_| panic!("Couldn't get field {}, {}", field, jtype)) } diff --git a/bindings/java/src/java.rs b/bindings/java/src/java.rs index 1cba18da1..eef811d95 100644 --- a/bindings/java/src/java.rs +++ b/bindings/java/src/java.rs @@ -3,7 +3,7 @@ use jni::sys::jobject; use jni::JNIEnv; use taffy::{Layout, Point, Rect, Size}; -pub fn to_java_layout<'local>(env: &mut JNIEnv<'local>, layout: &Layout) -> jobject { +pub fn to_java_layout(env: &mut JNIEnv, layout: &Layout) -> jobject { let layout_class = &env.find_class("com/dioxuslabs/taffy/tree/TaffyLayout").unwrap(); let location = &f32_point_to_java(env, layout.location); diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs index ecc51c887..f32399eb7 100644 --- a/bindings/java/src/lib.rs +++ b/bindings/java/src/lib.rs @@ -28,6 +28,7 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree<'local>( } #[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, @@ -48,6 +49,7 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>( } #[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, @@ -71,6 +73,7 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren<'loca } #[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_computeLayout<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, diff --git a/bindings/java/src/primitives.rs b/bindings/java/src/primitives.rs index 83e3db196..9f604a97a 100644 --- a/bindings/java/src/primitives.rs +++ b/bindings/java/src/primitives.rs @@ -17,7 +17,7 @@ pub fn f32_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc return def(); } - env.call_method(object, "floatValue", "()F", &[]).unwrap().f().unwrap() as f32 + env.call_method(object, "floatValue", "()F", &[]).unwrap().f().unwrap() } /// Convert Java Float object to Option @@ -37,7 +37,7 @@ pub fn opt_f32_from_object<'local>( /// Convert Java byte to i8 pub fn i8_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i8) -> i8 { - value.b().unwrap_or(def()) as i8 + value.b().unwrap_or(def()) } /// Convert Java Byte object to i8 @@ -49,12 +49,12 @@ pub fn i8_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loca return def(); } - env.call_method(object, "byteValue", "()B", &[]).unwrap().b().unwrap() as i8 + env.call_method(object, "byteValue", "()B", &[]).unwrap().b().unwrap() } /// Convert Java short to i16 pub fn i16_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i16) -> i16 { - value.s().unwrap_or(def()) as i16 + value.s().unwrap_or(def()) } /// Convert Java Short object to i16 @@ -66,12 +66,12 @@ pub fn i16_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc return def(); } - env.call_method(object, "shortValue", "()S", &[]).unwrap().s().unwrap() as i16 + env.call_method(object, "shortValue", "()S", &[]).unwrap().s().unwrap() } /// Convert Java int to i32 pub fn i32_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i32) -> i32 { - value.i().unwrap_or(def()) as i32 + value.i().unwrap_or(def()) } /// Convert Java Integer object to i32 @@ -83,13 +83,13 @@ pub fn i32_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc return def(); } - env.call_method(object, "intValue", "()I", &[]).unwrap().i().unwrap() as i32 + env.call_method(object, "intValue", "()I", &[]).unwrap().i().unwrap() } /// Convert Java long to i64 #[allow(dead_code)] pub fn i64_from_primitive<'local>(_env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> i64) -> i64 { - value.j().unwrap_or(def()) as i64 + value.j().unwrap_or(def()) } /// Convert Java Long object to i64 @@ -101,7 +101,7 @@ pub fn i64_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc return def(); } - env.call_method(object, "longValue", "()J", &[]).unwrap().j().unwrap() as i64 + env.call_method(object, "longValue", "()J", &[]).unwrap().j().unwrap() } /// Convert Java Long object to Option @@ -112,7 +112,7 @@ pub fn opt_i64_from_object<'local>(env: &mut JNIEnv<'local>, value: JValueOwned< return None; } - Some(env.call_method(object, "longValue", "()J", &[]).unwrap().j().unwrap() as i64) + Some(env.call_method(object, "longValue", "()J", &[]).unwrap().j().unwrap()) } /// Convert Java boolean to bool From 9891bbb2d76cd08297a412d603a38ded50002405 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 20 Aug 2024 17:53:30 +0200 Subject: [PATCH 05/32] java: turn into a maven project --- bindings/java/java/pom.xml | 26 +++++++++++++++ .../java}/com/dioxuslabs/taffy/Taffy.java | 0 .../java}/com/dioxuslabs/taffy/TaffyTree.java | 0 .../taffy/com_dioxuslabs_taffy_TaffyTree.h | 0 .../com/dioxuslabs/taffy/geom/TaffyLine.java | 0 .../com/dioxuslabs/taffy/geom/TaffyPoint.java | 0 .../com/dioxuslabs/taffy/geom/TaffyRect.java | 0 .../com/dioxuslabs/taffy/geom/TaffySize.java | 0 .../geom/grid/TaffyGenericGridPlacement.java | 0 .../geom/grid/TaffyGridTrackRepetition.java | 0 .../grid/TaffyMaxTrackSizingFunction.java | 0 .../grid/TaffyMinTrackSizingFunction.java | 0 .../TaffyNonRepeatedTrackSizingFunction.java | 0 .../geom/grid/TaffyTrackSizingFunction.java | 0 .../geom/measure/TaffyAvailableSpace.java | 0 .../taffy/geom/measure/TaffyDimension.java | 0 .../geom/measure/TaffyLengthPercentage.java | 0 .../measure/TaffyLengthPercentageAuto.java | 0 .../taffy/style/TaffyAlignContent.java | 0 .../taffy/style/TaffyAlignItems.java | 0 .../taffy/style/TaffyBoxSizing.java | 0 .../dioxuslabs/taffy/style/TaffyDisplay.java | 0 .../taffy/style/TaffyFlexDirection.java | 0 .../dioxuslabs/taffy/style/TaffyFlexWrap.java | 0 .../taffy/style/TaffyGridAutoFlow.java | 0 .../dioxuslabs/taffy/style/TaffyOverflow.java | 0 .../dioxuslabs/taffy/style/TaffyPosition.java | 0 .../dioxuslabs/taffy/style/TaffyStyle.java | 0 .../taffy/style/TaffyTextAlign.java | 0 .../dioxuslabs/taffy/tree/TaffyLayout.java | 0 .../taffy/BorderAndPaddingTests.java | 32 +++++++++++++++++++ 31 files changed, 58 insertions(+) create mode 100644 bindings/java/java/pom.xml rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/Taffy.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/TaffyTree.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/TaffyLine.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/TaffyPoint.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/TaffyRect.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/TaffySize.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyAlignContent.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyAlignItems.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyBoxSizing.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyDisplay.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyFlexDirection.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyFlexWrap.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyOverflow.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyPosition.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyStyle.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/style/TaffyTextAlign.java (100%) rename bindings/java/java/{ => src/main/java}/com/dioxuslabs/taffy/tree/TaffyLayout.java (100%) create mode 100644 bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java diff --git a/bindings/java/java/pom.xml b/bindings/java/java/pom.xml new file mode 100644 index 000000000..3227e8d8d --- /dev/null +++ b/bindings/java/java/pom.xml @@ -0,0 +1,26 @@ + + + 4.0.0 + + com.dioxuslabs.taffy + JTaffy + 0.0.1-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + + org.junit.jupiter + junit-jupiter-api + 5.11.0 + test + + + + diff --git a/bindings/java/java/com/dioxuslabs/taffy/Taffy.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/Taffy.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/Taffy.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/Taffy.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/TaffyTree.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyLine.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyLine.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/TaffyLine.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyLine.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyPoint.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyPoint.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/TaffyPoint.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyPoint.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffyRect.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyRect.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/TaffyRect.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyRect.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/TaffySize.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyDisplay.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyDisplay.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyDisplay.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyDisplay.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyOverflow.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyOverflow.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyOverflow.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyOverflow.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyPosition.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyPosition.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyPosition.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyStyle.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyStyle.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyStyle.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyStyle.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java diff --git a/bindings/java/java/com/dioxuslabs/taffy/tree/TaffyLayout.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/TaffyLayout.java similarity index 100% rename from bindings/java/java/com/dioxuslabs/taffy/tree/TaffyLayout.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/TaffyLayout.java diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java new file mode 100644 index 000000000..537c3088a --- /dev/null +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java @@ -0,0 +1,32 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.geom.TaffyRect; +import com.dioxuslabs.taffy.geom.TaffySize; +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; +import com.dioxuslabs.taffy.style.TaffyStyle; +import com.dioxuslabs.taffy.tree.TaffyLayout; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BorderAndPaddingTests { + @Test + public void verticalBorderAndPaddingPercentageValuesUseAvailableSpaceCorrectly() { + TaffyTree taffy = new TaffyTree(); + long node = taffy.newLeaf(TaffyStyle.builder() + .padding(new TaffyRect<>( + TaffyLengthPercentage.percentage(1f), + TaffyLengthPercentage.length(0f), + TaffyLengthPercentage.percentage(1f), + TaffyLengthPercentage.length(0f) + )) + ); + + taffy.computeLayout(node, TaffySize.definiteAvailableSize(200, 100)); + + System.out.println("Getting the layout of nodes"); + + TaffyLayout layout = taffy.layout(node); + Assertions.assertEquals(200.0f, layout.size().width()); + Assertions.assertEquals(200.0f, layout.size().height()); + } +} From 47bea2e087f624b753e99f12867f1147b1edba5a Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Wed, 21 Aug 2024 02:37:22 +0200 Subject: [PATCH 06/32] java: implement a few tests to ensure everything is working as intended --- bindings/java/java/pom.xml | 12 +++ .../taffy/BorderAndPaddingTests.java | 87 +++++++++++++++++-- .../com/dioxuslabs/taffy/RoundingTests.java | 43 +++++++++ .../java/com/dioxuslabs/taffy/TaffyTests.java | 9 ++ 4 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java create mode 100644 bindings/java/java/src/test/java/com/dioxuslabs/taffy/TaffyTests.java diff --git a/bindings/java/java/pom.xml b/bindings/java/java/pom.xml index 3227e8d8d..69da8cdef 100644 --- a/bindings/java/java/pom.xml +++ b/bindings/java/java/pom.xml @@ -12,6 +12,7 @@ 17 17 UTF-8 + ${pom.basedir}/../../../target/debug @@ -23,4 +24,15 @@ + + + + maven-surefire-plugin + + -Djava.library.path="${rust.project.debug.dir}" + + + + + diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java index 537c3088a..8bd4bb7c9 100644 --- a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java @@ -5,10 +5,88 @@ import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; import com.dioxuslabs.taffy.style.TaffyStyle; import com.dioxuslabs.taffy.tree.TaffyLayout; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.Arrays; + +import static com.dioxuslabs.taffy.TaffyTests.assertEquals; + public class BorderAndPaddingTests { + static { + System.loadLibrary("jtaffy"); + } + + public TaffyRect arrToRect(T[] items) { + return new TaffyRect<>(items[0], items[1], items[2], items[3]); + } + + @Test + public void borderOnASingleAxisDoesntIncreaseSize() { + for (int i = 0; i < 4; i++) { + TaffyTree taffy = new TaffyTree(); + + TaffyLengthPercentage[] lengths = new TaffyLengthPercentage[4]; + Arrays.fill(lengths, TaffyLengthPercentage.length(0f)); + lengths[i] = TaffyLengthPercentage.length(10f); + TaffyRect rect = arrToRect(lengths); + + long node = taffy.newLeaf(TaffyStyle.builder() + .border(rect) + ); + + taffy.computeLayout(node, TaffySize.definiteAvailableSize(100, 100)); + + TaffyLayout layout = taffy.layout(node); + + assertEquals(layout.size().width() * layout.size().height(), 0.0f); + } + } + + @Test + public void paddingOnASingleAxisDoesntIncreaseSize() { + for (int i = 0; i < 4; i++) { + TaffyTree taffy = new TaffyTree(); + + TaffyLengthPercentage[] lengths = new TaffyLengthPercentage[4]; + Arrays.fill(lengths, TaffyLengthPercentage.length(0f)); + lengths[i] = TaffyLengthPercentage.length(10f); + TaffyRect rect = arrToRect(lengths); + + long node = taffy.newLeaf(TaffyStyle.builder() + .padding(rect) + ); + + taffy.computeLayout(node, TaffySize.definiteAvailableSize(100, 100)); + + TaffyLayout layout = taffy.layout(node); + + assertEquals(layout.size().width() * layout.size().height(), 0.0f); + } + } + + @Test + public void borderAndPaddingOnASingleAxisDoesntIncreaseSize() { + for (int i = 0; i < 4; i++) { + TaffyTree taffy = new TaffyTree(); + + TaffyLengthPercentage[] lengths = new TaffyLengthPercentage[4]; + Arrays.fill(lengths, TaffyLengthPercentage.length(0f)); + lengths[i] = TaffyLengthPercentage.length(10f); + TaffyRect rect = arrToRect(lengths); + + long node = taffy.newLeaf(TaffyStyle.builder() + .border(rect) + .padding(rect) + ); + + taffy.computeLayout(node, TaffySize.definiteAvailableSize(100, 100)); + + TaffyLayout layout = taffy.layout(node); + + assertEquals(layout.size().width() * layout.size().height(), 0.0f); + } + } + @Test public void verticalBorderAndPaddingPercentageValuesUseAvailableSpaceCorrectly() { TaffyTree taffy = new TaffyTree(); @@ -23,10 +101,9 @@ public void verticalBorderAndPaddingPercentageValuesUseAvailableSpaceCorrectly() taffy.computeLayout(node, TaffySize.definiteAvailableSize(200, 100)); - System.out.println("Getting the layout of nodes"); - TaffyLayout layout = taffy.layout(node); - Assertions.assertEquals(200.0f, layout.size().width()); - Assertions.assertEquals(200.0f, layout.size().height()); + + assertEquals(layout.size().width(), 200.0f); + assertEquals(layout.size().height(), 200.0f); } } diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java new file mode 100644 index 000000000..56e45f0ac --- /dev/null +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java @@ -0,0 +1,43 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.geom.TaffySize; +import com.dioxuslabs.taffy.geom.measure.TaffyDimension; +import com.dioxuslabs.taffy.style.TaffyAlignContent; +import com.dioxuslabs.taffy.style.TaffyStyle; +import com.dioxuslabs.taffy.tree.TaffyLayout; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static com.dioxuslabs.taffy.TaffyTests.assertEquals; + +public class RoundingTests { + static { + System.loadLibrary("jtaffy"); + } + + @Test + public void roundingDoesntLeaveGaps() { + // First create an instance of TaffyTree + TaffyTree taffy = new TaffyTree(); + + TaffySize wSquare = TaffySize.lengthDimension(100.3f, 100.3f); + + long childA = taffy.newLeaf(TaffyStyle.builder().size(wSquare)); + long childB = taffy.newLeaf(TaffyStyle.builder().size(wSquare)); + + long rootNode = taffy.newWithChildren( + TaffyStyle.builder() + .size(TaffySize.lengthDimension(963.3333f, 1000f)) + .justifyContent(TaffyAlignContent.CENTER), + Arrays.asList(childA, childB) + ); + + taffy.computeLayout(rootNode, TaffySize.maxContentAvailableSize()); + + TaffyLayout layoutA = taffy.layout(childA); + TaffyLayout layoutB = taffy.layout(childB); + + assertEquals(layoutA.location().x() + layoutA.size().width(), layoutB.location().x()); + } +} diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/TaffyTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/TaffyTests.java new file mode 100644 index 000000000..a7a42fcb0 --- /dev/null +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/TaffyTests.java @@ -0,0 +1,9 @@ +package com.dioxuslabs.taffy; + +import org.junit.jupiter.api.Assertions; + +public class TaffyTests { + public static void assertEquals(Object actual, Object expected) { + Assertions.assertEquals(expected, actual); + } +} From 3eabd6c05d83a636be7e9510108ea7539cb9c867 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Wed, 21 Aug 2024 04:20:34 +0200 Subject: [PATCH 07/32] rust-java: Add most of methods in TaffyTree --- .../main/java/com/dioxuslabs/taffy/Taffy.java | 33 -- .../java/com/dioxuslabs/taffy/TaffyTree.java | 142 +++++++- .../taffy/com_dioxuslabs_taffy_TaffyTree.h | 168 +++++++++- bindings/java/run.sh | 7 - bindings/java/src/lib.rs | 315 +++++++++++++++++- 5 files changed, 582 insertions(+), 83 deletions(-) delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/Taffy.java delete mode 100644 bindings/java/run.sh diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/Taffy.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/Taffy.java deleted file mode 100644 index 9139dd59a..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/Taffy.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.dioxuslabs.taffy; - -import com.dioxuslabs.taffy.geom.TaffyRect; -import com.dioxuslabs.taffy.geom.TaffySize; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; -import com.dioxuslabs.taffy.style.TaffyStyle; -import com.dioxuslabs.taffy.tree.TaffyLayout; - -class Taffy { - static { - System.loadLibrary("jtaffy"); - } - - public static void main(String[] args) { - TaffyTree taffy = new TaffyTree(); - long node = taffy.newLeaf(TaffyStyle.builder() - .padding(new TaffyRect<>( - TaffyLengthPercentage.percentage(1f), - TaffyLengthPercentage.length(0f), - TaffyLengthPercentage.percentage(1f), - TaffyLengthPercentage.length(0f) - )) - ); - - taffy.computeLayout(node, TaffySize.definiteAvailableSize(200, 100)); - - System.out.println("Getting the layout of nodes"); - - TaffyLayout layout = taffy.layout(node); - System.out.println("Width: " + layout.size().width() + " = 200.0"); - System.out.println("Height: " + layout.size().height() + " = 200.0"); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java index 79f58151e..eeab59f75 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java @@ -4,45 +4,161 @@ import com.dioxuslabs.taffy.geom.measure.TaffyAvailableSpace; import com.dioxuslabs.taffy.style.TaffyStyle; import com.dioxuslabs.taffy.tree.TaffyLayout; +import com.sun.jdi.NativeMethodException; import java.util.List; +import java.util.Optional; public class TaffyTree { public final long ptr; public TaffyTree() { - this.ptr = newTaffyTree(); + this.ptr = nvNewTaffyTree(); + } + + public TaffyTree(int capacity) { + this.ptr = nvNewTaffyTreeWithCapacity(capacity); + } + + public void enableRounding() { + nvEnableRounding(this.ptr); + } + + public void disableRounding() { + nvDisableRounding(this.ptr); } public long newLeaf(TaffyStyle style) { - return newLeaf(this.ptr, style); + return nvNewLeaf(this.ptr, style); } public long newWithChildren(TaffyStyle style, List children) { - return newWithChildren(this.ptr, style, children); + return nvNewWithChildren(this.ptr, style, children); } public int childCount(long nodeId) { - return childCount(this.ptr, nodeId); + return nvChildCount(this.ptr, nodeId); } - public void computeLayout(long node, TaffySize availableSize) { - computeLayout(this.ptr, node, availableSize); + public void clear() { + nvClear(this.ptr); + } + + public long remove(long nodeId) { + return nvRemove(this.ptr, nodeId); + } + + public void addChild(long parent, long child) { + nvAddChild(this.ptr, parent, child); + } + + public void insertChildAtIndex(long parent, int childIndex, int child) { + nvInsertChildAtIndex(this.ptr, parent, childIndex, child); + } + + public void setChildren(long parent, List children) { + nvSetChildren(this.ptr, parent, children); + } + + public long removeChild(long parent, long child) { + return nvRemoveChild(this.ptr, parent, child); + } + + public long removeChildAtIndex(long parent, int childIndex) { + return nvRemoveChildAtIndex(this.ptr, parent, childIndex); + } + + public long replaceChildAtIndex(long parent, int childIndex, long newChild) { + return nvReplaceChildAtIndex(this.ptr, parent, childIndex, newChild); + } + + public long childAtIndex(long parent, int childIndex) { + return nvChildAtIndex(this.ptr, parent, childIndex); + } + + /** + * Returns the total number of nodes in the tree + */ + public int totalNodeCount() { + return nvTotalNodeCount(this.ptr); + } + + public Optional parent(long nodeId) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + /** + * Returns a list of children that belong to the parent node + */ + public List children(long nodeId) { + throw new UnsupportedOperationException("Not implemented yet"); + } + + public void setStyle(long node, TaffyStyle style) { + nvSetStyle(this.ptr, node, style); + } + + public TaffyStyle style(long node) { + throw new UnsupportedOperationException("Not implemented yet"); } public TaffyLayout layout(long node) { - return layout(this.ptr, node); + return nvLayout(this.ptr, node); + } + + public void computeLayout(long node, TaffySize availableSize) { + nvComputeLayout(this.ptr, node, availableSize); + } + + public void printTree(long node) { + nvPrintTree(this.ptr, node); } - private static native long newTaffyTree(); + private static native long nvNewTaffyTree(); + + private static native long nvNewTaffyTreeWithCapacity(int capacity); + + private static native void nvEnableRounding(long pointer); + + private static native void nvDisableRounding(long pointer); + + private static native long nvNewLeaf(long pointer, TaffyStyle style); + + private static native long nvNewWithChildren(long pointer, TaffyStyle style, List children); + + private static native int nvChildCount(long pointer, long nodeId); + + private static native void nvClear(long pointer); + + private static native long nvRemove(long pointer, long node); + + private static native void nvAddChild(long pointer, long parent, long child); + + private static native void nvInsertChildAtIndex(long pointer, long parent, int childIndex, long child); + + private static native void nvSetChildren(long pointer, long parent, List children); + + private static native long nvRemoveChild(long pointer, long parent, long child); + + private static native long nvRemoveChildAtIndex(long pointer, long parent, int childIndex); + + private static native long nvReplaceChildAtIndex(long pointer, long parent, int childIndex, long newChild); + + private static native long nvChildAtIndex(long pointer, long parent, int childIndex); + + private static native int nvTotalNodeCount(long pointer); + + private static native long nvParent(long pointer, long nodeId); + + private static native List nvChildren(long pointer, long nodeId); - private static native long newLeaf(long pointer, TaffyStyle style); + private static native void nvSetStyle(long pointer, long node, TaffyStyle style); - private static native long newWithChildren(long pointer, TaffyStyle style, List children); + private static native TaffyStyle nvStyle(long pointer, long node); - private static native int childCount(long pointer, long nodeId); + private static native void nvComputeLayout(long pointer, long node, TaffySize availableSize); - private static native void computeLayout(long pointer, long node, TaffySize availableSize); + private static native void nvPrintTree(long pointer, long node); - private static native TaffyLayout layout(long pointer, long node); + private static native TaffyLayout nvLayout(long pointer, long node); } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h index a7e3f01f3..4a009441c 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h @@ -9,50 +9,194 @@ extern "C" { #endif /* * Class: com_dioxuslabs_taffy_TaffyTree - * Method: newTaffyTree + * Method: nvNewTaffyTree * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvNewTaffyTree (JNIEnv *, jclass); /* * Class: com_dioxuslabs_taffy_TaffyTree - * Method: newLeaf + * Method: nvNewTaffyTreeWithCapacity + * Signature: (I)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvNewTaffyTreeWithCapacity + (JNIEnv *, jclass, jint); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvEnableRounding + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvEnableRounding + (JNIEnv *, jclass, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvDisableRounding + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvDisableRounding + (JNIEnv *, jclass, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvNewLeaf * Signature: (JLcom/dioxuslabs/taffy/style/TaffyStyle;)J */ -JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newLeaf +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvNewLeaf (JNIEnv *, jclass, jlong, jobject); /* * Class: com_dioxuslabs_taffy_TaffyTree - * Method: newWithChildren + * Method: nvNewWithChildren * Signature: (JLcom/dioxuslabs/taffy/style/TaffyStyle;Ljava/util/List;)J */ -JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvNewWithChildren (JNIEnv *, jclass, jlong, jobject, jobject); /* * Class: com_dioxuslabs_taffy_TaffyTree - * Method: childCount + * Method: nvChildCount * Signature: (JJ)I */ -JNIEXPORT jint JNICALL Java_com_dioxuslabs_taffy_TaffyTree_childCount +JNIEXPORT jint JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvChildCount + (JNIEnv *, jclass, jlong, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvClear + * Signature: (J)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvClear + (JNIEnv *, jclass, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvRemove + * Signature: (JJ)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvRemove (JNIEnv *, jclass, jlong, jlong); /* * Class: com_dioxuslabs_taffy_TaffyTree - * Method: computeLayout + * Method: nvAddChild + * Signature: (JJJ)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvAddChild + (JNIEnv *, jclass, jlong, jlong, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvInsertChildAtIndex + * Signature: (JJIJ)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvInsertChildAtIndex + (JNIEnv *, jclass, jlong, jlong, jint, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvSetChildren + * Signature: (JJLjava/util/List;)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvSetChildren + (JNIEnv *, jclass, jlong, jlong, jobject); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvRemoveChild + * Signature: (JJJ)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvRemoveChild + (JNIEnv *, jclass, jlong, jlong, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvRemoveChildAtIndex + * Signature: (JJI)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvRemoveChildAtIndex + (JNIEnv *, jclass, jlong, jlong, jint); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvReplaceChildAtIndex + * Signature: (JJIJ)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvReplaceChildAtIndex + (JNIEnv *, jclass, jlong, jlong, jint, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvChildAtIndex + * Signature: (JJI)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvChildAtIndex + (JNIEnv *, jclass, jlong, jlong, jint); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvTotalNodeCount + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvTotalNodeCount + (JNIEnv *, jclass, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvParent + * Signature: (JJ)J + */ +JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvParent + (JNIEnv *, jclass, jlong, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvChildren + * Signature: (JJ)Ljava/util/List; + */ +JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvChildren + (JNIEnv *, jclass, jlong, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvSetStyle + * Signature: (JJLcom/dioxuslabs/taffy/style/TaffyStyle;)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvSetStyle + (JNIEnv *, jclass, jlong, jlong, jobject); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvStyle + * Signature: (JJ)Lcom/dioxuslabs/taffy/style/TaffyStyle; + */ +JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvStyle + (JNIEnv *, jclass, jlong, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvComputeLayout * Signature: (JJLcom/dioxuslabs/taffy/geom/TaffySize;)V */ -JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_computeLayout +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvComputeLayout (JNIEnv *, jclass, jlong, jlong, jobject); /* * Class: com_dioxuslabs_taffy_TaffyTree - * Method: layout + * Method: nvPrintTree + * Signature: (JJ)V + */ +JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvPrintTree + (JNIEnv *, jclass, jlong, jlong); + +/* + * Class: com_dioxuslabs_taffy_TaffyTree + * Method: nvLayout * Signature: (JJ)Lcom/dioxuslabs/taffy/tree/TaffyLayout; */ -JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_layout +JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvLayout (JNIEnv *, jclass, jlong, jlong); #ifdef __cplusplus diff --git a/bindings/java/run.sh b/bindings/java/run.sh deleted file mode 100644 index bd1ae517c..000000000 --- a/bindings/java/run.sh +++ /dev/null @@ -1,7 +0,0 @@ -cargo build && -cd java && -javac $(find . -name "*.java") && -cd com/dioxuslabs/taffy && -javac -h . ./**/*.java && -cd ../../.. && -java -Djava.library.path="../../../target/debug" com.dioxuslabs.taffy.Taffy diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs index f32399eb7..aaf7a4e11 100644 --- a/bindings/java/src/lib.rs +++ b/bindings/java/src/lib.rs @@ -19,7 +19,7 @@ use std::panic; use taffy::{NodeId, TaffyTree, TraversePartialTree}; #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree<'local>( +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvNewTaffyTree<'local>( _env: JNIEnv<'local>, _class: JClass<'local>, ) -> jlong { @@ -27,9 +27,51 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newTaffyTree<'local>( Box::into_raw(tree) as jlong } +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvNewTaffyTreeWithCapacity<'local>( + _env: JNIEnv<'local>, + _class: JClass<'local>, + capacity: jint, +) -> jlong { + let tree: Box> = Box::new(TaffyTree::with_capacity(capacity as usize)); + Box::into_raw(tree) as jlong +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvEnableRounding<'local>( + _env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, +) { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + return; + } + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + tree.enable_rounding() + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvDisableRounding<'local>( + _env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, +) { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + return; + } + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + tree.disable_rounding() + } +} + #[no_mangle] #[allow(clippy::not_unsafe_ptr_arg_deref)] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>( +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvNewLeaf<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, pointer: jlong, @@ -39,9 +81,6 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>( let style = get_style(&mut env, &JObject::from_raw(style)); let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; - if raw_ptr.is_null() { - return -2; - } let tree: &mut TaffyTree<()> = &mut *raw_ptr; let res = tree.new_leaf(style); res.map_or(-1, |v| >::into(v) as jlong) as jlong @@ -50,7 +89,7 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newLeaf<'local>( #[no_mangle] #[allow(clippy::not_unsafe_ptr_arg_deref)] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren<'local>( +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvNewWithChildren<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, pointer: jlong, @@ -62,16 +101,254 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_newWithChildren<'loca let list = JValueOwned::from(JObject::from_raw(children)); let children = &get_list(&mut env, list, opt_node_id_from_object); + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + let res = tree.new_with_children(style, children); + res.map_or(-1, |v| >::into(v) as jlong) as jlong + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvChildCount<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong, +) -> jint { + let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + return -2; + } + let tree: &mut TaffyTree<()> = unsafe { &mut *raw_ptr }; + tree.child_count(node) as jint +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvClear<'local>( + _env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, +) { + unsafe { let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; if raw_ptr.is_null() { - return -2; + return; } let tree: &mut TaffyTree<()> = &mut *raw_ptr; - let res = tree.new_with_children(style, children); + tree.clear() + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvRemove<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong, +) -> jlong { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); + let res = tree.remove(node); + + res.map_or(-1, |v| >::into(v) as jlong) as jlong + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvAddChild<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + parent: jlong, + child: jlong, +) { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let parent_node = node_id_from_primitive(&mut env, JValueOwned::from(parent)); + let child_node = node_id_from_primitive(&mut env, JValueOwned::from(child)); + tree.add_child(parent_node, child_node).expect("Failed to add child"); + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvInsertChildAtIndex<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + parent: jlong, + child_index: jint, + child: jlong, +) { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let parent_node = node_id_from_primitive(&mut env, JValueOwned::from(parent)); + let child_node = node_id_from_primitive(&mut env, JValueOwned::from(child)); + tree.insert_child_at_index(parent_node, child_index as usize, child_node).expect("Failed to insert child"); + } +} + +#[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvSetChildren<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + parent: jlong, + children: jobject, +) { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let parent_node = node_id_from_primitive(&mut env, JValueOwned::from(parent)); + + let list = JValueOwned::from(JObject::from_raw(children)); + let children = &get_list(&mut env, list, opt_node_id_from_object); + + tree.set_children(parent_node, children).expect("Failed to set children"); + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvRemoveChild<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + parent: jlong, + child: jlong, +) -> jlong { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let parent_node = node_id_from_primitive(&mut env, JValueOwned::from(parent)); + let child_node = node_id_from_primitive(&mut env, JValueOwned::from(child)); + + let res = tree.remove_child(parent_node, child_node); + res.map_or(-1, |v| >::into(v) as jlong) as jlong + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvRemoveChildAtIndex<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + parent: jlong, + child_index: jint, +) -> jlong { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let parent_node = node_id_from_primitive(&mut env, JValueOwned::from(parent)); + + let res = tree.remove_child_at_index(parent_node, child_index as usize); + res.map_or(-1, |v| >::into(v) as jlong) as jlong + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvReplaceChildAtIndex<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + parent: jlong, + child_index: jint, + new_child: jlong, +) -> jlong { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let parent_node = node_id_from_primitive(&mut env, JValueOwned::from(parent)); + let new_child_node = node_id_from_primitive(&mut env, JValueOwned::from(new_child)); + + let res = tree.replace_child_at_index(parent_node, child_index as usize, new_child_node); + res.map_or(-1, |v| >::into(v) as jlong) as jlong + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvChildAtIndex<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + parent: jlong, + child_index: jint, +) -> jlong { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let parent_node = node_id_from_primitive(&mut env, JValueOwned::from(parent)); + + let res = tree.child_at_index(parent_node, child_index as usize); + res.map_or(-1, |v| >::into(v) as jlong) as jlong + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvTotalNodeCount<'local>( + _env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, +) -> jint { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + tree.total_node_count() as jint + } +} + +#[no_mangle] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvParent<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + child_id: jlong +) -> jlong { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let child_node = node_id_from_primitive(&mut env, JValueOwned::from(child_id)); + + let res = tree.parent(child_node); res.map_or(-1, |v| >::into(v) as jlong) as jlong } } +#[no_mangle] +#[allow(clippy::not_unsafe_ptr_arg_deref)] +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvSetStyle<'local>( + mut env: JNIEnv<'local>, + _class: JClass<'local>, + pointer: jlong, + node: jlong, + style: jobject, +) { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + + let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); + + let style = get_style(&mut env, &JObject::from_raw(style)); + + tree.set_style(node, style).expect("Failed to set children"); + } +} + #[no_mangle] #[allow(clippy::not_unsafe_ptr_arg_deref)] pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_computeLayout<'local>( @@ -97,21 +374,23 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_computeLayout<'local> } #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_childCount<'local>( +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvPrintTree<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, pointer: jlong, - node: jlong, -) -> jint { - let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); - let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; - if raw_ptr.is_null() { - return -2; + node: jlong +) { + unsafe { + let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; + if raw_ptr.is_null() { + return; + } + let tree: &mut TaffyTree<()> = &mut *raw_ptr; + let node = node_id_from_primitive(&mut env, JValueOwned::from(node)); + tree.print_tree(node) } - let tree: &mut TaffyTree<()> = unsafe { &mut *raw_ptr }; - let res = tree.child_count(node); - res as jint } + #[no_mangle] pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_layout<'local>( mut env: JNIEnv<'local>, From 587c81053f7e6de137364fb13eaafc340230b02d Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Wed, 21 Aug 2024 04:22:24 +0200 Subject: [PATCH 08/32] rust: rustfmt --- bindings/java/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs index aaf7a4e11..43de5d8d5 100644 --- a/bindings/java/src/lib.rs +++ b/bindings/java/src/lib.rs @@ -315,7 +315,7 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvParent<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, pointer: jlong, - child_id: jlong + child_id: jlong, ) -> jlong { unsafe { let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; @@ -378,7 +378,7 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvPrintTree<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, pointer: jlong, - node: jlong + node: jlong, ) { unsafe { let raw_ptr: *mut TaffyTree<()> = pointer as *mut TaffyTree<()>; From 81dcdcdc8abe420555375eaa446fbd4172c12315 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Wed, 21 Aug 2024 05:34:04 +0200 Subject: [PATCH 09/32] java: utility --- .../com/dioxuslabs/taffy/geom/TaffySize.java | 142 ++++++++++++++++++ .../geom/measure/TaffyLengthPercentage.java | 2 +- .../taffy/BorderAndPaddingTests.java | 4 +- .../com/dioxuslabs/taffy/RoundingTests.java | 2 +- 4 files changed, 146 insertions(+), 4 deletions(-) diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java index d050f7fa6..262fae6f3 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java @@ -2,6 +2,8 @@ import com.dioxuslabs.taffy.geom.measure.TaffyAvailableSpace; import com.dioxuslabs.taffy.geom.measure.TaffyDimension; +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; +import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentageAuto; /** * The width and height of a {@link TaffyRect} @@ -12,26 +14,166 @@ public record TaffySize( T width, T height ) { + /** + * Dynamic function to create a {@link TaffySize} with a fixed width and height + * + * @param clazz The type of the {@link TaffySize} + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + * @param The type between {@link TaffyDimension}, {@link TaffyAvailableSpace}, {@link TaffyLengthPercentage} and {@link TaffyLengthPercentageAuto} + */ + @SuppressWarnings("unchecked") + public static TaffySize length(Class clazz, float width, float height) { + if (clazz == TaffyDimension.class) { + return (TaffySize) lengthDimension(width, height); + } else if (clazz == TaffyAvailableSpace.class) { + return (TaffySize) definiteAvailableSize(width, height); + } else if (clazz == TaffyLengthPercentage.class) { + return (TaffySize) lengthLengthPercentage(width, height); + } else if (clazz == TaffyLengthPercentageAuto.class) { + return (TaffySize) lengthLengthPercentageAuto(width, height); + } + return null; + } + + /** + * Dynamic function to create a {@link TaffySize} with a percentage width and height + * + * @param clazz The type of the {@link TaffySize} + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + * @param The type between {@link TaffyDimension}, {@link TaffyLengthPercentage} and {@link TaffyLengthPercentageAuto} + */ + @SuppressWarnings("unchecked") + public static TaffySize percent(Class clazz, float width, float height) { + if (clazz == TaffyDimension.class) { + return (TaffySize) percentDimension(width, height); + } else if (clazz == TaffyLengthPercentage.class) { + return (TaffySize) percentLengthPercentage(width, height); + } else if (clazz == TaffyLengthPercentageAuto.class) { + return (TaffySize) percentLengthPercentageAuto(width, height); + } + return null; + } + + + /** + * Dynamic function to create a {@link TaffySize} with auto as values + * + * @param clazz The type of the {@link TaffySize} + * @return A new {@link TaffySize} with auto as width and height + * @param The type between {@link TaffyDimension} and {@link TaffyLengthPercentageAuto} + */ + @SuppressWarnings("unchecked") + public static TaffySize auto(Class clazz) { + if (clazz == TaffyDimension.class) { + return (TaffySize) autoDimension(); + } else if (clazz == TaffyLengthPercentageAuto.class) { + return (TaffySize) autoLengthPercentageAuto(); + } + return null; + } + + /** + * Creates a {@link TaffyLengthPercentageAuto} {@link TaffySize} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + */ + public static TaffySize lengthLengthPercentageAuto(float width, float height) { + return new TaffySize<>(TaffyLengthPercentageAuto.length(width), TaffyLengthPercentageAuto.length(height)); + } + + /** + * Creates a {@link TaffyLengthPercentageAuto} {@link TaffySize} with a percentage width and height + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + */ + public static TaffySize percentLengthPercentageAuto(float width, float height) { + return new TaffySize<>(TaffyLengthPercentageAuto.percent(width), TaffyLengthPercentageAuto.percent(height)); + } + + /** + * Creates a {@link TaffyLengthPercentageAuto} {@link TaffySize} with auto as width and height + * @return A new {@link TaffySize} with auto as width and height + */ + public static TaffySize autoLengthPercentageAuto() { + return new TaffySize<>(TaffyLengthPercentageAuto.auto(), TaffyLengthPercentageAuto.auto()); + } + + /** + * Creates a {@link TaffyLengthPercentage} {@link TaffySize} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + */ + public static TaffySize lengthLengthPercentage(float width, float height) { + return new TaffySize<>(TaffyLengthPercentage.length(width), TaffyLengthPercentage.length(height)); + } + + /** + * Creates a {@link TaffyLengthPercentage} {@link TaffySize} with a percentage width and height + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + */ + public static TaffySize percentLengthPercentage(float width, float height) { + return new TaffySize<>(TaffyLengthPercentage.percent(width), TaffyLengthPercentage.percent(height)); + } + + /** + * Creates a {@link TaffyDimension} {@link TaffySize} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + */ public static TaffySize lengthDimension(float width, float height) { return new TaffySize<>(TaffyDimension.length(width), TaffyDimension.length(height)); } + /** + * Creates a {@link TaffyDimension} {@link TaffySize} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + */ public static TaffySize percentDimension(float width, float height) { return new TaffySize<>(TaffyDimension.percent(width), TaffyDimension.percent(height)); } + /** + * Creates a {@link TaffyDimension} {@link TaffySize} with auto as width and height + * @return A new {@link TaffySize} with auto as width and height + */ public static TaffySize autoDimension() { return new TaffySize<>(TaffyDimension.auto(), TaffyDimension.auto()); } + /** + * Creates a {@link TaffyAvailableSpace} {@link TaffySize} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link TaffySize} with the given width and height + */ public static TaffySize definiteAvailableSize(float width, float height) { return new TaffySize<>(TaffyAvailableSpace.definite(width), TaffyAvailableSpace.definite(height)); } + /** + * Creates a {@link TaffyDimension} {@link TaffySize} with MinContent as width and height + * @return A new {@link TaffySize} with MinContent as width and height + */ public static TaffySize minContentAvailableSize() { return new TaffySize<>(TaffyAvailableSpace.minContent(), TaffyAvailableSpace.minContent()); } + /** + * Creates a {@link TaffyDimension} {@link TaffySize} with MaxContent as width and height + * @return A new {@link TaffySize} with MaxContent as width and height + */ public static TaffySize maxContentAvailableSize() { return new TaffySize<>(TaffyAvailableSpace.maxContent(), TaffyAvailableSpace.maxContent()); } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java index c199443a4..7618c36e0 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java @@ -13,7 +13,7 @@ public static TaffyLengthPercentage length(float value) { return new TaffyLengthPercentage((byte) 0, value); } - public static TaffyLengthPercentage percentage(float value) { + public static TaffyLengthPercentage percent(float value) { return new TaffyLengthPercentage((byte) 1, value); } diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java index 8bd4bb7c9..bf8171589 100644 --- a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java @@ -92,9 +92,9 @@ public void verticalBorderAndPaddingPercentageValuesUseAvailableSpaceCorrectly() TaffyTree taffy = new TaffyTree(); long node = taffy.newLeaf(TaffyStyle.builder() .padding(new TaffyRect<>( - TaffyLengthPercentage.percentage(1f), + TaffyLengthPercentage.percent(1f), TaffyLengthPercentage.length(0f), - TaffyLengthPercentage.percentage(1f), + TaffyLengthPercentage.percent(1f), TaffyLengthPercentage.length(0f) )) ); diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java index 56e45f0ac..5f152d629 100644 --- a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java @@ -28,7 +28,7 @@ public void roundingDoesntLeaveGaps() { long rootNode = taffy.newWithChildren( TaffyStyle.builder() - .size(TaffySize.lengthDimension(963.3333f, 1000f)) + .size(TaffySize.length(TaffyDimension.class, 963.3333f, 1000f)) .justifyContent(TaffyAlignContent.CENTER), Arrays.asList(childA, childB) ); From b53ea74ae3c38748ce37960eb176e1695faffe7e Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Wed, 21 Aug 2024 05:42:26 +0200 Subject: [PATCH 10/32] rust: fix signatures --- .../src/test/java/com/dioxuslabs/taffy/RoundingTests.java | 1 + bindings/java/src/lib.rs | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java index 5f152d629..4b53fc5d7 100644 --- a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java @@ -34,6 +34,7 @@ public void roundingDoesntLeaveGaps() { ); taffy.computeLayout(rootNode, TaffySize.maxContentAvailableSize()); + taffy.printTree(rootNode); TaffyLayout layoutA = taffy.layout(childA); TaffyLayout layoutB = taffy.layout(childB); diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs index 43de5d8d5..932ef0588 100644 --- a/bindings/java/src/lib.rs +++ b/bindings/java/src/lib.rs @@ -351,7 +351,7 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvSetStyle<'local>( #[no_mangle] #[allow(clippy::not_unsafe_ptr_arg_deref)] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_computeLayout<'local>( +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvComputeLayout<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, pointer: jlong, @@ -392,7 +392,7 @@ pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvPrintTree<'local>( } #[no_mangle] -pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_layout<'local>( +pub extern "system" fn Java_com_dioxuslabs_taffy_TaffyTree_nvLayout<'local>( mut env: JNIEnv<'local>, _class: JClass<'local>, pointer: jlong, From a0dab922414bc326168689d1cd628d69400d0a8f Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Wed, 21 Aug 2024 22:33:22 +0200 Subject: [PATCH 11/32] rust-java: remove Taffy from classes & change signatures --- .../java/com/dioxuslabs/taffy/TaffyTree.java | 33 ++- .../taffy/com_dioxuslabs_taffy_TaffyTree.h | 12 +- .../taffy/geom/{TaffyLine.java => Line.java} | 2 +- .../geom/{TaffyPoint.java => Point.java} | 6 +- .../taffy/geom/{TaffyRect.java => Rect.java} | 4 +- .../java/com/dioxuslabs/taffy/geom/Size.java | 179 +++++++++++++ .../com/dioxuslabs/taffy/geom/TaffySize.java | 180 ------------- ...acement.java => GenericGridPlacement.java} | 22 +- ...petition.java => GridTrackRepetition.java} | 18 +- ...ction.java => MaxTrackSizingFunction.java} | 32 +-- ...ction.java => MinTrackSizingFunction.java} | 24 +- ...va => NonRepeatedTrackSizingFunction.java} | 6 +- .../geom/grid/TaffyTrackSizingFunction.java | 51 ---- .../taffy/geom/grid/TrackSizingFunction.java | 51 ++++ .../taffy/geom/measure/AvailableSpace.java | 31 +++ .../taffy/geom/measure/Dimension.java | 31 +++ .../taffy/geom/measure/LengthPercentage.java | 27 ++ .../geom/measure/LengthPercentageAuto.java | 31 +++ .../geom/measure/TaffyAvailableSpace.java | 31 --- .../taffy/geom/measure/TaffyDimension.java | 31 --- .../geom/measure/TaffyLengthPercentage.java | 27 -- .../measure/TaffyLengthPercentageAuto.java | 31 --- ...ffyAlignContent.java => AlignContent.java} | 16 +- .../{TaffyAlignItems.java => AlignItems.java} | 16 +- .../{TaffyBoxSizing.java => BoxSizing.java} | 4 +- .../style/{TaffyDisplay.java => Display.java} | 4 +- ...yFlexDirection.java => FlexDirection.java} | 6 +- .../{TaffyFlexWrap.java => FlexWrap.java} | 10 +- ...ffyGridAutoFlow.java => GridAutoFlow.java} | 4 +- .../{TaffyOverflow.java => Overflow.java} | 4 +- .../{TaffyPosition.java => Position.java} | 8 +- .../com/dioxuslabs/taffy/style/Style.java | 236 ++++++++++++++++++ .../dioxuslabs/taffy/style/TaffyStyle.java | 236 ------------------ .../{TaffyTextAlign.java => TextAlign.java} | 4 +- .../tree/{TaffyLayout.java => Layout.java} | 22 +- .../taffy/BorderAndPaddingTests.java | 72 +++--- .../com/dioxuslabs/taffy/RoundingTests.java | 28 +-- bindings/java/src/enums.rs | 20 +- bindings/java/src/geom.rs | 18 +- bindings/java/src/java.rs | 10 +- bindings/java/src/measure.rs | 8 +- 41 files changed, 792 insertions(+), 794 deletions(-) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/{TaffyLine.java => Line.java} (89%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/{TaffyPoint.java => Point.java} (74%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/{TaffyRect.java => Rect.java} (94%) create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/{TaffyGenericGridPlacement.java => GenericGridPlacement.java} (62%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/{TaffyGridTrackRepetition.java => GridTrackRepetition.java} (67%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/{TaffyMaxTrackSizingFunction.java => MaxTrackSizingFunction.java} (57%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/{TaffyMinTrackSizingFunction.java => MinTrackSizingFunction.java} (54%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/{TaffyNonRepeatedTrackSizingFunction.java => NonRepeatedTrackSizingFunction.java} (76%) delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TrackSizingFunction.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/AvailableSpace.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/Dimension.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentage.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentageAuto.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyAlignContent.java => AlignContent.java} (72%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyAlignItems.java => AlignItems.java} (63%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyBoxSizing.java => BoxSizing.java} (94%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyDisplay.java => Display.java} (93%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyFlexDirection.java => FlexDirection.java} (90%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyFlexWrap.java => FlexWrap.java} (72%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyGridAutoFlow.java => GridAutoFlow.java} (94%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyOverflow.java => Overflow.java} (97%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyPosition.java => Position.java} (83%) create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyStyle.java rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/{TaffyTextAlign.java => TextAlign.java} (92%) rename bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/{TaffyLayout.java => Layout.java} (72%) diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java index eeab59f75..c76363fd4 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java @@ -1,10 +1,9 @@ package com.dioxuslabs.taffy; -import com.dioxuslabs.taffy.geom.TaffySize; -import com.dioxuslabs.taffy.geom.measure.TaffyAvailableSpace; -import com.dioxuslabs.taffy.style.TaffyStyle; -import com.dioxuslabs.taffy.tree.TaffyLayout; -import com.sun.jdi.NativeMethodException; +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.AvailableSpace; +import com.dioxuslabs.taffy.style.Style; +import com.dioxuslabs.taffy.tree.Layout; import java.util.List; import java.util.Optional; @@ -28,11 +27,11 @@ public void disableRounding() { nvDisableRounding(this.ptr); } - public long newLeaf(TaffyStyle style) { + public long newLeaf(Style style) { return nvNewLeaf(this.ptr, style); } - public long newWithChildren(TaffyStyle style, List children) { + public long newWithChildren(Style style, List children) { return nvNewWithChildren(this.ptr, style, children); } @@ -94,19 +93,19 @@ public List children(long nodeId) { throw new UnsupportedOperationException("Not implemented yet"); } - public void setStyle(long node, TaffyStyle style) { + public void setStyle(long node, Style style) { nvSetStyle(this.ptr, node, style); } - public TaffyStyle style(long node) { + public Style style(long node) { throw new UnsupportedOperationException("Not implemented yet"); } - public TaffyLayout layout(long node) { + public Layout layout(long node) { return nvLayout(this.ptr, node); } - public void computeLayout(long node, TaffySize availableSize) { + public void computeLayout(long node, Size availableSize) { nvComputeLayout(this.ptr, node, availableSize); } @@ -122,9 +121,9 @@ public void printTree(long node) { private static native void nvDisableRounding(long pointer); - private static native long nvNewLeaf(long pointer, TaffyStyle style); + private static native long nvNewLeaf(long pointer, Style style); - private static native long nvNewWithChildren(long pointer, TaffyStyle style, List children); + private static native long nvNewWithChildren(long pointer, Style style, List children); private static native int nvChildCount(long pointer, long nodeId); @@ -152,13 +151,13 @@ public void printTree(long node) { private static native List nvChildren(long pointer, long nodeId); - private static native void nvSetStyle(long pointer, long node, TaffyStyle style); + private static native void nvSetStyle(long pointer, long node, Style style); - private static native TaffyStyle nvStyle(long pointer, long node); + private static native Style nvStyle(long pointer, long node); - private static native void nvComputeLayout(long pointer, long node, TaffySize availableSize); + private static native void nvComputeLayout(long pointer, long node, Size availableSize); private static native void nvPrintTree(long pointer, long node); - private static native TaffyLayout nvLayout(long pointer, long node); + private static native Layout nvLayout(long pointer, long node); } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h index 4a009441c..6ae211ce6 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/com_dioxuslabs_taffy_TaffyTree.h @@ -42,7 +42,7 @@ JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvDisableRounding /* * Class: com_dioxuslabs_taffy_TaffyTree * Method: nvNewLeaf - * Signature: (JLcom/dioxuslabs/taffy/style/TaffyStyle;)J + * Signature: (JLcom/dioxuslabs/taffy/style/Style;)J */ JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvNewLeaf (JNIEnv *, jclass, jlong, jobject); @@ -50,7 +50,7 @@ JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvNewLeaf /* * Class: com_dioxuslabs_taffy_TaffyTree * Method: nvNewWithChildren - * Signature: (JLcom/dioxuslabs/taffy/style/TaffyStyle;Ljava/util/List;)J + * Signature: (JLcom/dioxuslabs/taffy/style/Style;Ljava/util/List;)J */ JNIEXPORT jlong JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvNewWithChildren (JNIEnv *, jclass, jlong, jobject, jobject); @@ -162,7 +162,7 @@ JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvChildren /* * Class: com_dioxuslabs_taffy_TaffyTree * Method: nvSetStyle - * Signature: (JJLcom/dioxuslabs/taffy/style/TaffyStyle;)V + * Signature: (JJLcom/dioxuslabs/taffy/style/Style;)V */ JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvSetStyle (JNIEnv *, jclass, jlong, jlong, jobject); @@ -170,7 +170,7 @@ JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvSetStyle /* * Class: com_dioxuslabs_taffy_TaffyTree * Method: nvStyle - * Signature: (JJ)Lcom/dioxuslabs/taffy/style/TaffyStyle; + * Signature: (JJ)Lcom/dioxuslabs/taffy/style/Style; */ JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvStyle (JNIEnv *, jclass, jlong, jlong); @@ -178,7 +178,7 @@ JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvStyle /* * Class: com_dioxuslabs_taffy_TaffyTree * Method: nvComputeLayout - * Signature: (JJLcom/dioxuslabs/taffy/geom/TaffySize;)V + * Signature: (JJLcom/dioxuslabs/taffy/geom/Size;)V */ JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvComputeLayout (JNIEnv *, jclass, jlong, jlong, jobject); @@ -194,7 +194,7 @@ JNIEXPORT void JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvPrintTree /* * Class: com_dioxuslabs_taffy_TaffyTree * Method: nvLayout - * Signature: (JJ)Lcom/dioxuslabs/taffy/tree/TaffyLayout; + * Signature: (JJ)Lcom/dioxuslabs/taffy/tree/Layout; */ JNIEXPORT jobject JNICALL Java_com_dioxuslabs_taffy_TaffyTree_nvLayout (JNIEnv *, jclass, jlong, jlong); diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyLine.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Line.java similarity index 89% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyLine.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Line.java index 6ebe34724..366228403 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyLine.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Line.java @@ -5,7 +5,7 @@ * @param start The start position of a line * @param end The end position of a line */ -public record TaffyLine( +public record Line( T start, T end ) { diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyPoint.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Point.java similarity index 74% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyPoint.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Point.java index e3f9c1063..027be6396 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyPoint.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Point.java @@ -3,9 +3,9 @@ /** * A 2-dimensional coordinate. *

- * When used in association with a {@link TaffyRect}, represents the top-left corner. + * When used in association with a {@link Rect}, represents the top-left corner. */ -public class TaffyPoint { +public class Point { /** * The x-coordinate */ @@ -15,7 +15,7 @@ public class TaffyPoint { */ private T y; - public TaffyPoint(T x, T y) { + public Point(T x, T y) { this.x = x; this.y = y; } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyRect.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Rect.java similarity index 94% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyRect.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Rect.java index be3cbb4c0..6fc287047 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffyRect.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Rect.java @@ -3,7 +3,7 @@ /** * An axis-aligned UI rectangle */ -public class TaffyRect{ +public class Rect{ /** * This can represent either the x-coordinate of the starting edge, * or the amount of padding on the starting side. @@ -28,7 +28,7 @@ public class TaffyRect{ */ private T bottom; - public TaffyRect(T left, T right, T top, T bottom) { + public Rect(T left, T right, T top, T bottom) { this.left = left; this.right = right; this.top = top; diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java new file mode 100644 index 000000000..4c48dd23a --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java @@ -0,0 +1,179 @@ +package com.dioxuslabs.taffy.geom; + +import com.dioxuslabs.taffy.geom.measure.AvailableSpace; +import com.dioxuslabs.taffy.geom.measure.Dimension; +import com.dioxuslabs.taffy.geom.measure.LengthPercentage; +import com.dioxuslabs.taffy.geom.measure.LengthPercentageAuto; + +/** + * The width and height of a {@link Rect} + * @param width The x extent of the rectangle + * @param height The y extent of the rectangle + */ +public record Size( + T width, + T height +) { + /** + * Dynamic function to create a {@link Size} with a fixed width and height + * + * @param clazz The type of the {@link Size} + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + * @param The type between {@link Dimension}, {@link AvailableSpace}, {@link LengthPercentage} and {@link LengthPercentageAuto} + */ + @SuppressWarnings("unchecked") + public static Size length(Class clazz, float width, float height) { + if (clazz == Dimension.class) { + return (Size) lengthDimension(width, height); + } else if (clazz == AvailableSpace.class) { + return (Size) definiteAvailableSize(width, height); + } else if (clazz == LengthPercentage.class) { + return (Size) lengthLengthPercentage(width, height); + } else if (clazz == LengthPercentageAuto.class) { + return (Size) lengthLengthPercentageAuto(width, height); + } + return null; + } + + /** + * Dynamic function to create a {@link Size} with a percentage width and height + * + * @param clazz The type of the {@link Size} + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + * @param The type between {@link Dimension}, {@link LengthPercentage} and {@link LengthPercentageAuto} + */ + @SuppressWarnings("unchecked") + public static Size percent(Class clazz, float width, float height) { + if (clazz == Dimension.class) { + return (Size) percentDimension(width, height); + } else if (clazz == LengthPercentage.class) { + return (Size) percentLengthPercentage(width, height); + } else if (clazz == LengthPercentageAuto.class) { + return (Size) percentLengthPercentageAuto(width, height); + } + return null; + } + + /** + * Dynamic function to create a {@link Size} with auto as values + * + * @param clazz The type of the {@link Size} + * @return A new {@link Size} with auto as width and height + * @param The type between {@link Dimension} and {@link LengthPercentageAuto} + */ + @SuppressWarnings("unchecked") + public static Size auto(Class clazz) { + if (clazz == Dimension.class) { + return (Size) autoDimension(); + } else if (clazz == LengthPercentageAuto.class) { + return (Size) autoLengthPercentageAuto(); + } + return null; + } + + /** + * Creates a {@link LengthPercentageAuto} {@link Size} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + */ + public static Size lengthLengthPercentageAuto(float width, float height) { + return new Size<>(LengthPercentageAuto.length(width), LengthPercentageAuto.length(height)); + } + + /** + * Creates a {@link LengthPercentageAuto} {@link Size} with a percentage width and height + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + */ + public static Size percentLengthPercentageAuto(float width, float height) { + return new Size<>(LengthPercentageAuto.percent(width), LengthPercentageAuto.percent(height)); + } + + /** + * Creates a {@link LengthPercentageAuto} {@link Size} with auto as width and height + * @return A new {@link Size} with auto as width and height + */ + public static Size autoLengthPercentageAuto() { + return new Size<>(LengthPercentageAuto.auto(), LengthPercentageAuto.auto()); + } + + /** + * Creates a {@link LengthPercentage} {@link Size} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + */ + public static Size lengthLengthPercentage(float width, float height) { + return new Size<>(LengthPercentage.length(width), LengthPercentage.length(height)); + } + + /** + * Creates a {@link LengthPercentage} {@link Size} with a percentage width and height + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + */ + public static Size percentLengthPercentage(float width, float height) { + return new Size<>(LengthPercentage.percent(width), LengthPercentage.percent(height)); + } + + /** + * Creates a {@link Dimension} {@link Size} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + */ + public static Size lengthDimension(float width, float height) { + return new Size<>(Dimension.length(width), Dimension.length(height)); + } + + /** + * Creates a {@link Dimension} {@link Size} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + */ + public static Size percentDimension(float width, float height) { + return new Size<>(Dimension.percent(width), Dimension.percent(height)); + } + + /** + * Creates a {@link Dimension} {@link Size} with auto as width and height + * @return A new {@link Size} with auto as width and height + */ + public static Size autoDimension() { + return new Size<>(Dimension.auto(), Dimension.auto()); + } + + /** + * Creates a {@link AvailableSpace} {@link Size} with a fixed width and height + * @param width The width + * @param height The height + * @return A new {@link Size} with the given width and height + */ + public static Size definiteAvailableSize(float width, float height) { + return new Size<>(AvailableSpace.definite(width), AvailableSpace.definite(height)); + } + + /** + * Creates a {@link Dimension} {@link Size} with MinContent as width and height + * @return A new {@link Size} with MinContent as width and height + */ + public static Size minContentAvailableSize() { + return new Size<>(AvailableSpace.minContent(), AvailableSpace.minContent()); + } + + /** + * Creates a {@link Dimension} {@link Size} with MaxContent as width and height + * @return A new {@link Size} with MaxContent as width and height + */ + public static Size maxContentAvailableSize() { + return new Size<>(AvailableSpace.maxContent(), AvailableSpace.maxContent()); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java deleted file mode 100644 index 262fae6f3..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/TaffySize.java +++ /dev/null @@ -1,180 +0,0 @@ -package com.dioxuslabs.taffy.geom; - -import com.dioxuslabs.taffy.geom.measure.TaffyAvailableSpace; -import com.dioxuslabs.taffy.geom.measure.TaffyDimension; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentageAuto; - -/** - * The width and height of a {@link TaffyRect} - * @param width The x extent of the rectangle - * @param height The y extent of the rectangle - */ -public record TaffySize( - T width, - T height -) { - /** - * Dynamic function to create a {@link TaffySize} with a fixed width and height - * - * @param clazz The type of the {@link TaffySize} - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - * @param The type between {@link TaffyDimension}, {@link TaffyAvailableSpace}, {@link TaffyLengthPercentage} and {@link TaffyLengthPercentageAuto} - */ - @SuppressWarnings("unchecked") - public static TaffySize length(Class clazz, float width, float height) { - if (clazz == TaffyDimension.class) { - return (TaffySize) lengthDimension(width, height); - } else if (clazz == TaffyAvailableSpace.class) { - return (TaffySize) definiteAvailableSize(width, height); - } else if (clazz == TaffyLengthPercentage.class) { - return (TaffySize) lengthLengthPercentage(width, height); - } else if (clazz == TaffyLengthPercentageAuto.class) { - return (TaffySize) lengthLengthPercentageAuto(width, height); - } - return null; - } - - /** - * Dynamic function to create a {@link TaffySize} with a percentage width and height - * - * @param clazz The type of the {@link TaffySize} - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - * @param The type between {@link TaffyDimension}, {@link TaffyLengthPercentage} and {@link TaffyLengthPercentageAuto} - */ - @SuppressWarnings("unchecked") - public static TaffySize percent(Class clazz, float width, float height) { - if (clazz == TaffyDimension.class) { - return (TaffySize) percentDimension(width, height); - } else if (clazz == TaffyLengthPercentage.class) { - return (TaffySize) percentLengthPercentage(width, height); - } else if (clazz == TaffyLengthPercentageAuto.class) { - return (TaffySize) percentLengthPercentageAuto(width, height); - } - return null; - } - - - /** - * Dynamic function to create a {@link TaffySize} with auto as values - * - * @param clazz The type of the {@link TaffySize} - * @return A new {@link TaffySize} with auto as width and height - * @param The type between {@link TaffyDimension} and {@link TaffyLengthPercentageAuto} - */ - @SuppressWarnings("unchecked") - public static TaffySize auto(Class clazz) { - if (clazz == TaffyDimension.class) { - return (TaffySize) autoDimension(); - } else if (clazz == TaffyLengthPercentageAuto.class) { - return (TaffySize) autoLengthPercentageAuto(); - } - return null; - } - - /** - * Creates a {@link TaffyLengthPercentageAuto} {@link TaffySize} with a fixed width and height - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - */ - public static TaffySize lengthLengthPercentageAuto(float width, float height) { - return new TaffySize<>(TaffyLengthPercentageAuto.length(width), TaffyLengthPercentageAuto.length(height)); - } - - /** - * Creates a {@link TaffyLengthPercentageAuto} {@link TaffySize} with a percentage width and height - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - */ - public static TaffySize percentLengthPercentageAuto(float width, float height) { - return new TaffySize<>(TaffyLengthPercentageAuto.percent(width), TaffyLengthPercentageAuto.percent(height)); - } - - /** - * Creates a {@link TaffyLengthPercentageAuto} {@link TaffySize} with auto as width and height - * @return A new {@link TaffySize} with auto as width and height - */ - public static TaffySize autoLengthPercentageAuto() { - return new TaffySize<>(TaffyLengthPercentageAuto.auto(), TaffyLengthPercentageAuto.auto()); - } - - /** - * Creates a {@link TaffyLengthPercentage} {@link TaffySize} with a fixed width and height - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - */ - public static TaffySize lengthLengthPercentage(float width, float height) { - return new TaffySize<>(TaffyLengthPercentage.length(width), TaffyLengthPercentage.length(height)); - } - - /** - * Creates a {@link TaffyLengthPercentage} {@link TaffySize} with a percentage width and height - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - */ - public static TaffySize percentLengthPercentage(float width, float height) { - return new TaffySize<>(TaffyLengthPercentage.percent(width), TaffyLengthPercentage.percent(height)); - } - - /** - * Creates a {@link TaffyDimension} {@link TaffySize} with a fixed width and height - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - */ - public static TaffySize lengthDimension(float width, float height) { - return new TaffySize<>(TaffyDimension.length(width), TaffyDimension.length(height)); - } - - /** - * Creates a {@link TaffyDimension} {@link TaffySize} with a fixed width and height - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - */ - public static TaffySize percentDimension(float width, float height) { - return new TaffySize<>(TaffyDimension.percent(width), TaffyDimension.percent(height)); - } - - /** - * Creates a {@link TaffyDimension} {@link TaffySize} with auto as width and height - * @return A new {@link TaffySize} with auto as width and height - */ - public static TaffySize autoDimension() { - return new TaffySize<>(TaffyDimension.auto(), TaffyDimension.auto()); - } - - /** - * Creates a {@link TaffyAvailableSpace} {@link TaffySize} with a fixed width and height - * @param width The width - * @param height The height - * @return A new {@link TaffySize} with the given width and height - */ - public static TaffySize definiteAvailableSize(float width, float height) { - return new TaffySize<>(TaffyAvailableSpace.definite(width), TaffyAvailableSpace.definite(height)); - } - - /** - * Creates a {@link TaffyDimension} {@link TaffySize} with MinContent as width and height - * @return A new {@link TaffySize} with MinContent as width and height - */ - public static TaffySize minContentAvailableSize() { - return new TaffySize<>(TaffyAvailableSpace.minContent(), TaffyAvailableSpace.minContent()); - } - - /** - * Creates a {@link TaffyDimension} {@link TaffySize} with MaxContent as width and height - * @return A new {@link TaffySize} with MaxContent as width and height - */ - public static TaffySize maxContentAvailableSize() { - return new TaffySize<>(TaffyAvailableSpace.maxContent(), TaffyAvailableSpace.maxContent()); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GenericGridPlacement.java similarity index 62% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GenericGridPlacement.java index 9be3e667e..2edad1667 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGenericGridPlacement.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GenericGridPlacement.java @@ -1,6 +1,6 @@ package com.dioxuslabs.taffy.geom.grid; -import com.dioxuslabs.taffy.style.TaffyGridAutoFlow; +import com.dioxuslabs.taffy.style.GridAutoFlow; /** * A grid line placement specification which is generic over the coordinate system that it uses to define @@ -11,39 +11,39 @@ *

* See [`crate::compute::grid::type::coordinates`] for documentation on the different coordinate systems. */ -public class TaffyGenericGridPlacement { +public class GenericGridPlacement { private final byte type; private final short value; - private TaffyGenericGridPlacement(byte type, short value) { + private GenericGridPlacement(byte type, short value) { this.type = type; this.value = value; } /** - * Place item according to the auto-placement algorithm, and the parent's {@link TaffyGridAutoFlow} property + * Place item according to the auto-placement algorithm, and the parent's {@link GridAutoFlow} property */ - public static TaffyGenericGridPlacement auto() { - return new TaffyGenericGridPlacement((byte) 0, (short) 0); + public static GenericGridPlacement auto() { + return new GenericGridPlacement((byte) 0, (short) 0); } /** * Place item at specified line (column or row) index */ - public static TaffyGenericGridPlacement line(short value) { - return new TaffyGenericGridPlacement((byte) 1, value); + public static GenericGridPlacement line(short value) { + return new GenericGridPlacement((byte) 1, value); } /** * Item should span specified number of tracks (columns or rows) */ - public static TaffyGenericGridPlacement span(short value) { - return new TaffyGenericGridPlacement((byte) 2, value); + public static GenericGridPlacement span(short value) { + return new GenericGridPlacement((byte) 2, value); } @Override public boolean equals(Object obj) { - if (!(obj instanceof TaffyGenericGridPlacement gpg)) { + if (!(obj instanceof GenericGridPlacement gpg)) { return false; } return type == gpg.type && value == gpg.value; diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GridTrackRepetition.java similarity index 67% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GridTrackRepetition.java index 8900a5cb0..27178350f 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GridTrackRepetition.java @@ -6,11 +6,11 @@ * See ... for an explanation of how auto-repeated * track definitions work and the difference between AutoFit and AutoFill. */ -public class TaffyGridTrackRepetition { +public class GridTrackRepetition { private final byte type; private final short value; - private TaffyGridTrackRepetition(byte type, short value) { + private GridTrackRepetition(byte type, short value) { this.type = type; this.value = value; } @@ -19,28 +19,28 @@ private TaffyGridTrackRepetition(byte type, short value) { * Auto-repeating tracks should be generated to fit the container * See: ... */ - public static TaffyGridTrackRepetition autoFill() { - return new TaffyGridTrackRepetition((byte) 0, (short) 0); + public static GridTrackRepetition autoFill() { + return new GridTrackRepetition((byte) 0, (short) 0); } /** * Auto-repeating tracks should be generated to fit the container * See: ... */ - public static TaffyGridTrackRepetition autoFit() { - return new TaffyGridTrackRepetition((byte) 1, (short) 0); + public static GridTrackRepetition autoFit() { + return new GridTrackRepetition((byte) 1, (short) 0); } /** * The specified tracks should be repeated exacts N times */ - public static TaffyGridTrackRepetition count(short count) { - return new TaffyGridTrackRepetition((byte) 2, count); + public static GridTrackRepetition count(short count) { + return new GridTrackRepetition((byte) 2, count); } @Override public boolean equals(Object obj) { - if (!(obj instanceof TaffyGridTrackRepetition lpo)) { + if (!(obj instanceof GridTrackRepetition lpo)) { return false; } return type == lpo.type && value == lpo.value; diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/MaxTrackSizingFunction.java similarity index 57% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/MaxTrackSizingFunction.java index 7c7da5535..d52f1953a 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/MaxTrackSizingFunction.java @@ -1,6 +1,6 @@ package com.dioxuslabs.taffy.geom.grid; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; +import com.dioxuslabs.taffy.geom.measure.LengthPercentage; /** * Maximum track sizing function @@ -9,16 +9,16 @@ * on the size of it's contents, the amount of available space, and the sizing constraint the grid is being size under. * See ... */ -public class TaffyMaxTrackSizingFunction { +public class MaxTrackSizingFunction { private final byte type; private final Object value; - private TaffyMaxTrackSizingFunction(byte type, TaffyLengthPercentage value) { + private MaxTrackSizingFunction(byte type, LengthPercentage value) { this.type = type; this.value = value; } - private TaffyMaxTrackSizingFunction(byte type, float value) { + private MaxTrackSizingFunction(byte type, float value) { this.type = type; this.value = value; } @@ -26,36 +26,36 @@ private TaffyMaxTrackSizingFunction(byte type, float value) { /** * Track maximum size should be a fixed length or percentage value */ - public static TaffyMaxTrackSizingFunction fixed(TaffyLengthPercentage value) { - return new TaffyMaxTrackSizingFunction((byte) 0, value); + public static MaxTrackSizingFunction fixed(LengthPercentage value) { + return new MaxTrackSizingFunction((byte) 0, value); } /** * Track maximum size should be content sized under a min-content constraint */ - public static TaffyMaxTrackSizingFunction minContent() { - return new TaffyMaxTrackSizingFunction((byte) 1, null); + public static MaxTrackSizingFunction minContent() { + return new MaxTrackSizingFunction((byte) 1, null); } /** * Track maximum size should be content sized under a max-content constraint */ - public static TaffyMaxTrackSizingFunction maxContent() { - return new TaffyMaxTrackSizingFunction((byte) 2, null); + public static MaxTrackSizingFunction maxContent() { + return new MaxTrackSizingFunction((byte) 2, null); } /** * Track maximum size should be sized according to the fit-content formula */ - public static TaffyMaxTrackSizingFunction fitContent(TaffyLengthPercentage value) { - return new TaffyMaxTrackSizingFunction((byte) 3, value); + public static MaxTrackSizingFunction fitContent(LengthPercentage value) { + return new MaxTrackSizingFunction((byte) 3, value); } /** * Track maximum size should be automatically sized */ - public static TaffyMaxTrackSizingFunction auto() { - return new TaffyMaxTrackSizingFunction((byte) 4, null); + public static MaxTrackSizingFunction auto() { + return new MaxTrackSizingFunction((byte) 4, null); } /** @@ -63,7 +63,7 @@ public static TaffyMaxTrackSizingFunction auto() { * Specified value is the numerator of the fraction. Denominator is the sum of all fraction specified in that grid dimension * Spec: Spec */ - public static TaffyMaxTrackSizingFunction fraction(float value) { - return new TaffyMaxTrackSizingFunction((byte) 5, value); + public static MaxTrackSizingFunction fraction(float value) { + return new MaxTrackSizingFunction((byte) 5, value); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/MinTrackSizingFunction.java similarity index 54% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/MinTrackSizingFunction.java index 26ee6d884..8ae955813 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/MinTrackSizingFunction.java @@ -1,6 +1,6 @@ package com.dioxuslabs.taffy.geom.grid; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; +import com.dioxuslabs.taffy.geom.measure.LengthPercentage; /** * Minimum track sizing function @@ -9,11 +9,11 @@ * on the size of it's contents, the amount of available space, and the sizing constraint the grid is being size under. * See ... */ -public class TaffyMinTrackSizingFunction { +public class MinTrackSizingFunction { private final byte type; - private final TaffyLengthPercentage value; + private final LengthPercentage value; - private TaffyMinTrackSizingFunction(byte type, TaffyLengthPercentage value) { + private MinTrackSizingFunction(byte type, LengthPercentage value) { this.type = type; this.value = value; } @@ -21,28 +21,28 @@ private TaffyMinTrackSizingFunction(byte type, TaffyLengthPercentage value) { /** * Track minimum size should be a fixed length or percentage value */ - public static TaffyMinTrackSizingFunction fixed(TaffyLengthPercentage value) { - return new TaffyMinTrackSizingFunction((byte) 0, value); + public static MinTrackSizingFunction fixed(LengthPercentage value) { + return new MinTrackSizingFunction((byte) 0, value); } /** * Track minimum size should be content sized under a min-content constraint */ - public static TaffyMinTrackSizingFunction minContent() { - return new TaffyMinTrackSizingFunction((byte) 1, null); + public static MinTrackSizingFunction minContent() { + return new MinTrackSizingFunction((byte) 1, null); } /** * Track minimum size should be content sized under a max-content constraint */ - public static TaffyMinTrackSizingFunction maxContent() { - return new TaffyMinTrackSizingFunction((byte) 2, null); + public static MinTrackSizingFunction maxContent() { + return new MinTrackSizingFunction((byte) 2, null); } /** * Track minimum size should be automatically sized */ - public static TaffyMinTrackSizingFunction auto() { - return new TaffyMinTrackSizingFunction((byte) 3, null); + public static MinTrackSizingFunction auto() { + return new MinTrackSizingFunction((byte) 3, null); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/NonRepeatedTrackSizingFunction.java similarity index 76% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/NonRepeatedTrackSizingFunction.java index f39d49ccd..ed5e9c62e 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/NonRepeatedTrackSizingFunction.java @@ -8,8 +8,8 @@ * @param min The value representing the minimum * @param max The value representing the maximum */ -public record TaffyNonRepeatedTrackSizingFunction( - TaffyMinTrackSizingFunction min, - TaffyMaxTrackSizingFunction max +public record NonRepeatedTrackSizingFunction( + MinTrackSizingFunction min, + MaxTrackSizingFunction max ) { } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java deleted file mode 100644 index 4317d75eb..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TaffyTrackSizingFunction.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.dioxuslabs.taffy.geom.grid; - -import java.util.List; - -/** - * The sizing function for a grid track (row/column) - * See ... - */ -public abstract class TaffyTrackSizingFunction { - private byte type; - - private TaffyTrackSizingFunction(byte type) { - this.type = type; - } - - /** - * A single non-repeated track - */ - public static TaffyTrackSizingFunction single(TaffyNonRepeatedTrackSizingFunction func) { - return new TaffyTrackSingleSizingFunction(func); - } - - /** - * Automatically generate grid tracks to fit the available space using the specified definite track lengths - * Only valid if every track in template (not just the repetition) has a fixed size. - */ - public static TaffyTrackSizingFunction repeat(TaffyGridTrackRepetition reps, List list) { - return new TaffyTrackRepeatSizingFunction(reps, list); - } - - private static class TaffyTrackSingleSizingFunction extends TaffyTrackSizingFunction { - private final TaffyNonRepeatedTrackSizingFunction func; - - private TaffyTrackSingleSizingFunction(TaffyNonRepeatedTrackSizingFunction func) { - super((byte) 0); - this.func = func; - } - } - - private static class TaffyTrackRepeatSizingFunction extends TaffyTrackSizingFunction { - private final TaffyGridTrackRepetition repetitions; - private final List functions; - - private TaffyTrackRepeatSizingFunction(TaffyGridTrackRepetition repetitions, - List functions) { - super((byte) 1); - this.repetitions = repetitions; - this.functions = functions; - } - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TrackSizingFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TrackSizingFunction.java new file mode 100644 index 000000000..eaeccb14f --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/TrackSizingFunction.java @@ -0,0 +1,51 @@ +package com.dioxuslabs.taffy.geom.grid; + +import java.util.List; + +/** + * The sizing function for a grid track (row/column) + * See ... + */ +public abstract class TrackSizingFunction { + private byte type; + + private TrackSizingFunction(byte type) { + this.type = type; + } + + /** + * A single non-repeated track + */ + public static TrackSizingFunction single(NonRepeatedTrackSizingFunction func) { + return new TrackSingleSizingFunction(func); + } + + /** + * Automatically generate grid tracks to fit the available space using the specified definite track lengths + * Only valid if every track in template (not just the repetition) has a fixed size. + */ + public static TrackSizingFunction repeat(GridTrackRepetition reps, List list) { + return new TrackRepeatSizingFunction(reps, list); + } + + private static class TrackSingleSizingFunction extends TrackSizingFunction { + private final NonRepeatedTrackSizingFunction func; + + private TrackSingleSizingFunction(NonRepeatedTrackSizingFunction func) { + super((byte) 0); + this.func = func; + } + } + + private static class TrackRepeatSizingFunction extends TrackSizingFunction { + private final GridTrackRepetition repetitions; + private final List functions; + + private TrackRepeatSizingFunction(GridTrackRepetition repetitions, + List functions) { + super((byte) 1); + this.repetitions = repetitions; + this.functions = functions; + } + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/AvailableSpace.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/AvailableSpace.java new file mode 100644 index 000000000..4938787df --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/AvailableSpace.java @@ -0,0 +1,31 @@ +package com.dioxuslabs.taffy.geom.measure; + +public class AvailableSpace { + private final byte type; + private final float value; + + private AvailableSpace(byte type, float value) { + this.type = type; + this.value = value; + } + + public static AvailableSpace definite(float value) { + return new AvailableSpace((byte) 0, value); + } + + public static AvailableSpace minContent() { + return new AvailableSpace((byte) 1, 0); + } + + public static AvailableSpace maxContent() { + return new AvailableSpace((byte) 2, 0); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof AvailableSpace as)) { + return false; + } + return type == as.type && value == as.value; + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/Dimension.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/Dimension.java new file mode 100644 index 000000000..0ca0f755d --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/Dimension.java @@ -0,0 +1,31 @@ +package com.dioxuslabs.taffy.geom.measure; + +public class Dimension { + private final byte type; + private final float value; + + private Dimension(byte type, float value) { + this.type = type; + this.value = value; + } + + public static Dimension length(float value) { + return new Dimension((byte) 0, value); + } + + public static Dimension percent(float value) { + return new Dimension((byte) 1, value); + } + + public static Dimension auto() { + return new Dimension((byte) 2, 0); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Dimension lpo)) { + return false; + } + return type == lpo.type && value == lpo.value; + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentage.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentage.java new file mode 100644 index 000000000..b6504547a --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentage.java @@ -0,0 +1,27 @@ +package com.dioxuslabs.taffy.geom.measure; + +public class LengthPercentage { + private final byte type; + private final float value; + + private LengthPercentage(byte type, float value) { + this.type = type; + this.value = value; + } + + public static LengthPercentage length(float value) { + return new LengthPercentage((byte) 0, value); + } + + public static LengthPercentage percent(float value) { + return new LengthPercentage((byte) 1, value); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof LengthPercentage lpo)) { + return false; + } + return type == lpo.type && value == lpo.value; + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentageAuto.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentageAuto.java new file mode 100644 index 000000000..0a1950015 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentageAuto.java @@ -0,0 +1,31 @@ +package com.dioxuslabs.taffy.geom.measure; + +public class LengthPercentageAuto { + private final byte type; + private final float value; + + private LengthPercentageAuto(byte type, float value) { + this.type = type; + this.value = value; + } + + public static LengthPercentageAuto length(float value) { + return new LengthPercentageAuto((byte) 0, value); + } + + public static LengthPercentageAuto percent(float value) { + return new LengthPercentageAuto((byte) 1, value); + } + + public static LengthPercentageAuto auto() { + return new LengthPercentageAuto((byte) 2, 0); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof LengthPercentageAuto lpo)) { + return false; + } + return type == lpo.type && value == lpo.value; + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java deleted file mode 100644 index e4eaff7d3..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyAvailableSpace.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.dioxuslabs.taffy.geom.measure; - -public class TaffyAvailableSpace { - private final byte type; - private final float value; - - private TaffyAvailableSpace(byte type, float value) { - this.type = type; - this.value = value; - } - - public static TaffyAvailableSpace definite(float value) { - return new TaffyAvailableSpace((byte) 0, value); - } - - public static TaffyAvailableSpace minContent() { - return new TaffyAvailableSpace((byte) 1, 0); - } - - public static TaffyAvailableSpace maxContent() { - return new TaffyAvailableSpace((byte) 2, 0); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof TaffyAvailableSpace as)) { - return false; - } - return type == as.type && value == as.value; - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java deleted file mode 100644 index 17e8bb26f..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyDimension.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.dioxuslabs.taffy.geom.measure; - -public class TaffyDimension { - private final byte type; - private final float value; - - private TaffyDimension(byte type, float value) { - this.type = type; - this.value = value; - } - - public static TaffyDimension length(float value) { - return new TaffyDimension((byte) 0, value); - } - - public static TaffyDimension percent(float value) { - return new TaffyDimension((byte) 1, value); - } - - public static TaffyDimension auto() { - return new TaffyDimension((byte) 2, 0); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof TaffyDimension lpo)) { - return false; - } - return type == lpo.type && value == lpo.value; - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java deleted file mode 100644 index 7618c36e0..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.dioxuslabs.taffy.geom.measure; - -public class TaffyLengthPercentage { - private final byte type; - private final float value; - - private TaffyLengthPercentage(byte type, float value) { - this.type = type; - this.value = value; - } - - public static TaffyLengthPercentage length(float value) { - return new TaffyLengthPercentage((byte) 0, value); - } - - public static TaffyLengthPercentage percent(float value) { - return new TaffyLengthPercentage((byte) 1, value); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof TaffyLengthPercentage lpo)) { - return false; - } - return type == lpo.type && value == lpo.value; - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java deleted file mode 100644 index 03e42636f..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/TaffyLengthPercentageAuto.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.dioxuslabs.taffy.geom.measure; - -public class TaffyLengthPercentageAuto { - private final byte type; - private final float value; - - private TaffyLengthPercentageAuto(byte type, float value) { - this.type = type; - this.value = value; - } - - public static TaffyLengthPercentageAuto length(float value) { - return new TaffyLengthPercentageAuto((byte) 0, value); - } - - public static TaffyLengthPercentageAuto percent(float value) { - return new TaffyLengthPercentageAuto((byte) 1, value); - } - - public static TaffyLengthPercentageAuto auto() { - return new TaffyLengthPercentageAuto((byte) 2, 0); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof TaffyLengthPercentageAuto lpo)) { - return false; - } - return type == lpo.type && value == lpo.value; - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignContent.java similarity index 72% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignContent.java index 5ccf4379c..5873e826d 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignContent.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignContent.java @@ -7,7 +7,7 @@ *

* MDN */ -public enum TaffyAlignContent { +public enum AlignContent { /** * Items are packed toward the start of the axis */ @@ -19,17 +19,17 @@ public enum TaffyAlignContent { /** * Items are packed towards the flex-relative start of the axis. *

- * For flex containers with {@link TaffyFlexDirection#ROW_REVERSE} or {@link TaffyFlexDirection#COLUMN_REVERSE} - * this is equivalent to {@link TaffyAlignContent#END}. In all other cases it is equivalent to - * {@link TaffyAlignContent#START}. + * For flex containers with {@link FlexDirection#ROW_REVERSE} or {@link FlexDirection#COLUMN_REVERSE} + * this is equivalent to {@link AlignContent#END}. In all other cases it is equivalent to + * {@link AlignContent#START}. */ FLEX_START, /** * Items are packed towards the flex-relative end of the axis. *

- * For flex containers with {@link TaffyFlexDirection#ROW_REVERSE} or {@link TaffyFlexDirection#COLUMN_REVERSE} - * this is equivalent to {@link TaffyAlignContent#START}. In all other cases it is equivalent to - * {@link TaffyAlignContent#END}. + * For flex containers with {@link FlexDirection#ROW_REVERSE} or {@link FlexDirection#COLUMN_REVERSE} + * this is equivalent to {@link AlignContent#START}. In all other cases it is equivalent to + * {@link AlignContent#END}. */ FLEX_END, /** @@ -58,7 +58,7 @@ public enum TaffyAlignContent { private final int internal; - TaffyAlignContent() { + AlignContent() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignItems.java similarity index 63% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignItems.java index 6c418efa0..c8c50d815 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyAlignItems.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignItems.java @@ -7,7 +7,7 @@ *

* MDN */ -public enum TaffyAlignItems { +public enum AlignItems { /** * Items are packed toward the start of the axis */ @@ -19,17 +19,17 @@ public enum TaffyAlignItems { /** * Items are packed towards the flex-relative start of the axis. *

- * For flex containers with {@link TaffyFlexDirection#ROW_REVERSE} or {@link TaffyFlexDirection#COLUMN_REVERSE} - * this is equivalent to {@link TaffyAlignItems#END}. In all other cases it is equivalent to - * {@link TaffyAlignItems#START}. + * For flex containers with {@link FlexDirection#ROW_REVERSE} or {@link FlexDirection#COLUMN_REVERSE} + * this is equivalent to {@link AlignItems#END}. In all other cases it is equivalent to + * {@link AlignItems#START}. */ FLEX_START, /** * Items are packed towards the flex-relative end of the axis. *

- * For flex containers with {@link TaffyFlexDirection#ROW_REVERSE} or {@link TaffyFlexDirection#COLUMN_REVERSE} - * this is equivalent to {@link TaffyAlignItems#START}. In all other cases it is equivalent to - * {@link TaffyAlignItems#END}. + * For flex containers with {@link FlexDirection#ROW_REVERSE} or {@link FlexDirection#COLUMN_REVERSE} + * this is equivalent to {@link AlignItems#START}. In all other cases it is equivalent to + * {@link AlignItems#END}. */ FLEX_END, /** @@ -47,7 +47,7 @@ public enum TaffyAlignItems { private final int internal; - TaffyAlignItems() { + AlignItems() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/BoxSizing.java similarity index 94% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/BoxSizing.java index c98311cc3..06c4a8a55 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyBoxSizing.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/BoxSizing.java @@ -15,7 +15,7 @@ *

... */ -public enum TaffyBoxSizing { +public enum BoxSizing { /** * Size styles such size, min_size, max_size specify the box's "content box" (the size excluding padding/border/margin) */ @@ -27,7 +27,7 @@ public enum TaffyBoxSizing { private final int internal; - TaffyBoxSizing() { + BoxSizing() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyDisplay.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Display.java similarity index 93% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyDisplay.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Display.java index ac545acbc..655739378 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyDisplay.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Display.java @@ -5,7 +5,7 @@ *

* The default values depends on on which feature flags are enabled. The order of precedence is: Flex, Grid, Block, None. */ -public enum TaffyDisplay { +public enum Display { /** * The children will follow the block layout algorithm */ @@ -25,7 +25,7 @@ public enum TaffyDisplay { private final int internal; - TaffyDisplay() { + Display() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexDirection.java similarity index 90% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexDirection.java index 0dff87b81..8d65fbec9 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexDirection.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexDirection.java @@ -9,11 +9,11 @@ *

* Items are always aligned relative to the cross axis, and justified relative to the main axis. *

- * The default behavior is {@link TaffyFlexDirection#ROW}. + * The default behavior is {@link FlexDirection#ROW}. *

* Specification */ -public enum TaffyFlexDirection { +public enum FlexDirection { /** * Defines +x as the main axis *

@@ -41,7 +41,7 @@ public enum TaffyFlexDirection { private final int internal; - TaffyFlexDirection() { + FlexDirection() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexWrap.java similarity index 72% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexWrap.java index c2cd36dbb..f379c4768 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyFlexWrap.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexWrap.java @@ -3,27 +3,27 @@ /** * Controls whether flex items are forced onto one line or can wrap onto multiple lines. *

- * Defaults to {@link TaffyFlexWrap#NO_WRAP} + * Defaults to {@link FlexWrap#NO_WRAP} *

* Specification */ -public enum TaffyFlexWrap { +public enum FlexWrap { /** * Items will not wrap and stay on a single line */ NO_WRAP, /** - * Items will wrap according to this item's {@link TaffyFlexDirection} + * Items will wrap according to this item's {@link FlexDirection} */ WRAP, /** - * Items will wrap in the opposite direction to this item's {@link TaffyFlexDirection} + * Items will wrap in the opposite direction to this item's {@link FlexDirection} */ WRAP_REVERSE; private final int internal; - TaffyFlexWrap() { + FlexWrap() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/GridAutoFlow.java similarity index 94% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/GridAutoFlow.java index b4a0e40a7..34945502f 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyGridAutoFlow.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/GridAutoFlow.java @@ -9,7 +9,7 @@ *

* MDN */ -public enum TaffyGridAutoFlow { +public enum GridAutoFlow { /** * Items are placed by filling each row in turn, adding new rows as necessary */ @@ -29,7 +29,7 @@ public enum TaffyGridAutoFlow { private final int internal; - TaffyGridAutoFlow() { + GridAutoFlow() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyOverflow.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Overflow.java similarity index 97% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyOverflow.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Overflow.java index 0f3848631..8eef0a837 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyOverflow.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Overflow.java @@ -15,7 +15,7 @@ *

* ... */ -public enum TaffyOverflow { +public enum Overflow { /** * The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content. * Content that overflows this node *should* contribute to the scroll region of its parent. @@ -40,7 +40,7 @@ public enum TaffyOverflow { private final int internal; - TaffyOverflow() { + Overflow() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyPosition.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Position.java similarity index 83% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyPosition.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Position.java index 9c20fc5ef..62101110e 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyPosition.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Position.java @@ -9,9 +9,9 @@ * WARNING: this enum follows the behavior of CSS's `position` property, * which can be unintuitive. *

- * {@link TaffyPosition#RELATIVE} is the default value, in contrast to the default behavior in CSS. + * {@link Position#RELATIVE} is the default value, in contrast to the default behavior in CSS. */ -public enum TaffyPosition { +public enum Position { /** * The offset is computed relative to the final position given by the layout algorithm. * Offsets do not affect the position of any other items; they are effectively a correction factor applied at the end. @@ -22,13 +22,13 @@ public enum TaffyPosition { * Otherwise, it is placed relative to the origin. * No space is created for the item in the page layout, and its size will not be altered. *

- * WARNING: to opt-out of layouting entirely, you must use {@link TaffyDisplay#NONE} instead on your {@link TaffyStyle} object. + * WARNING: to opt-out of layouting entirely, you must use {@link Display#NONE} instead on your {@link Style} object. */ ABSOLUTE; private final int internal; - TaffyPosition() { + Position() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java new file mode 100644 index 000000000..c0ae7d8a5 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java @@ -0,0 +1,236 @@ +package com.dioxuslabs.taffy.style; + +import com.dioxuslabs.taffy.geom.Line; +import com.dioxuslabs.taffy.geom.Point; +import com.dioxuslabs.taffy.geom.Rect; +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.grid.GenericGridPlacement; +import com.dioxuslabs.taffy.geom.grid.NonRepeatedTrackSizingFunction; +import com.dioxuslabs.taffy.geom.grid.TrackSizingFunction; +import com.dioxuslabs.taffy.geom.measure.Dimension; +import com.dioxuslabs.taffy.geom.measure.LengthPercentage; +import com.dioxuslabs.taffy.geom.measure.LengthPercentageAuto; + +import java.util.List; + +@SuppressWarnings("all") +public class Style { + private Display display; + private boolean itemIsTable; + private BoxSizing boxSizing; + + private Point overflow; + private float scrollbarWidth; + + private Position position; + private Rect inset; + + private Size size; + private Size minSize; + private Size maxSize; + private Float aspectRatio; + + private Rect margin; + private Rect padding; + private Rect border; + + private AlignItems alignItems; + private AlignItems alignSelf; + private AlignItems justifyItems; + private AlignItems justifySelf; + private AlignContent alignContent; + private AlignContent justifyContent; + private Size gap; + + private TextAlign textAlign; + + private FlexDirection flexDirection; + private FlexWrap flexWrap; + + private Dimension flexBasis; + private float flexGrow; + private float flexShrink; + + private List gridTemplateRows; + private List gridTemplateColumns; + private List gridAutoRows; + private List gridAutoColumns; + private GridAutoFlow gridAutoFlow; + + private Line gridRow; + private Line gridColumn; + + public static Style builder() { + return new Style(); + } + + public Style display(Display display) { + this.display = display; + return this; + } + + public Style itemIsTable(boolean itemIsTable) { + this.itemIsTable = itemIsTable; + return this; + } + + public Style boxSizing(BoxSizing boxSizing) { + this.boxSizing = boxSizing; + return this; + } + + public Style overflow(Point overflow) { + this.overflow = overflow; + return this; + } + + public Style scrollbarWidth(float scrollbarWidth) { + this.scrollbarWidth = scrollbarWidth; + return this; + } + + public Style position(Position position) { + this.position = position; + return this; + } + + public Style inset(Rect inset) { + this.inset = inset; + return this; + } + + public Style size(Size size) { + this.size = size; + return this; + } + + public Style minSize(Size minSize) { + this.minSize = minSize; + return this; + } + + public Style maxSize(Size maxSize) { + this.maxSize = maxSize; + return this; + } + + public Style aspectRatio(Float aspectRatio) { + this.aspectRatio = aspectRatio; + return this; + } + + public Style margin(Rect margin) { + this.margin = margin; + return this; + } + + public Style padding(Rect padding) { + this.padding = padding; + return this; + } + + public Style border(Rect border) { + this.border = border; + return this; + } + + public Style alignItems(AlignItems alignItems) { + this.alignItems = alignItems; + return this; + } + + public Style alignSelf(AlignItems alignSelf) { + this.alignSelf = alignSelf; + return this; + } + + public Style justifyItems(AlignItems justifyItems) { + this.justifyItems = justifyItems; + return this; + } + + public Style justifySelf(AlignItems justifySelf) { + this.justifySelf = justifySelf; + return this; + } + + public Style alignContent(AlignContent alignContent) { + this.alignContent = alignContent; + return this; + } + + public Style justifyContent(AlignContent justifyContent) { + this.justifyContent = justifyContent; + return this; + } + + public Style gap(Size gap) { + this.gap = gap; + return this; + } + + public Style textAlign(TextAlign textAlign) { + this.textAlign = textAlign; + return this; + } + + public Style flexDirection(FlexDirection flexDirection) { + this.flexDirection = flexDirection; + return this; + } + + public Style flexWrap(FlexWrap flexWrap) { + this.flexWrap = flexWrap; + return this; + } + + public Style flexBasis(Dimension flexBasis) { + this.flexBasis = flexBasis; + return this; + } + + public Style flexGrow(float flexGrow) { + this.flexGrow = flexGrow; + return this; + } + + public Style flexShrink(float flexShrink) { + this.flexShrink = flexShrink; + return this; + } + + public Style gridTemplateRows(List gridTemplateRows) { + this.gridTemplateRows = gridTemplateRows; + return this; + } + + public Style gridTemplateColumns(List gridTemplateColumns) { + this.gridTemplateColumns = gridTemplateColumns; + return this; + } + + public Style gridAutoRows(List gridAutoRows) { + this.gridAutoRows = gridAutoRows; + return this; + } + + public Style gridAutoColumns(List gridAutoColumns) { + this.gridAutoColumns = gridAutoColumns; + return this; + } + + public Style gridAutoFlow(GridAutoFlow gridAutoFlow) { + this.gridAutoFlow = gridAutoFlow; + return this; + } + + public Style gridRow(Line gridRow) { + this.gridRow = gridRow; + return this; + } + + public Style gridColumn(Line gridColumn) { + this.gridColumn = gridColumn; + return this; + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyStyle.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyStyle.java deleted file mode 100644 index 3e67dceee..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyStyle.java +++ /dev/null @@ -1,236 +0,0 @@ -package com.dioxuslabs.taffy.style; - -import com.dioxuslabs.taffy.geom.TaffyLine; -import com.dioxuslabs.taffy.geom.TaffyPoint; -import com.dioxuslabs.taffy.geom.TaffyRect; -import com.dioxuslabs.taffy.geom.TaffySize; -import com.dioxuslabs.taffy.geom.grid.TaffyGenericGridPlacement; -import com.dioxuslabs.taffy.geom.grid.TaffyNonRepeatedTrackSizingFunction; -import com.dioxuslabs.taffy.geom.grid.TaffyTrackSizingFunction; -import com.dioxuslabs.taffy.geom.measure.TaffyDimension; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentageAuto; - -import java.util.List; - -@SuppressWarnings("all") -public class TaffyStyle { - private TaffyDisplay display; - private boolean itemIsTable; - private TaffyBoxSizing boxSizing; - - private TaffyPoint overflow; - private float scrollbarWidth; - - private TaffyPosition position; - private TaffyRect inset; - - private TaffySize size; - private TaffySize minSize; - private TaffySize maxSize; - private Float aspectRatio; - - private TaffyRect margin; - private TaffyRect padding; - private TaffyRect border; - - private TaffyAlignItems alignItems; - private TaffyAlignItems alignSelf; - private TaffyAlignItems justifyItems; - private TaffyAlignItems justifySelf; - private TaffyAlignContent alignContent; - private TaffyAlignContent justifyContent; - private TaffySize gap; - - private TaffyTextAlign textAlign; - - private TaffyFlexDirection flexDirection; - private TaffyFlexWrap flexWrap; - - private TaffyDimension flexBasis; - private float flexGrow; - private float flexShrink; - - private List gridTemplateRows; - private List gridTemplateColumns; - private List gridAutoRows; - private List gridAutoColumns; - private TaffyGridAutoFlow gridAutoFlow; - - private TaffyLine gridRow; - private TaffyLine gridColumn; - - public static TaffyStyle builder() { - return new TaffyStyle(); - } - - public TaffyStyle display(TaffyDisplay display) { - this.display = display; - return this; - } - - public TaffyStyle itemIsTable(boolean itemIsTable) { - this.itemIsTable = itemIsTable; - return this; - } - - public TaffyStyle boxSizing(TaffyBoxSizing boxSizing) { - this.boxSizing = boxSizing; - return this; - } - - public TaffyStyle overflow(TaffyPoint overflow) { - this.overflow = overflow; - return this; - } - - public TaffyStyle scrollbarWidth(float scrollbarWidth) { - this.scrollbarWidth = scrollbarWidth; - return this; - } - - public TaffyStyle position(TaffyPosition position) { - this.position = position; - return this; - } - - public TaffyStyle inset(TaffyRect inset) { - this.inset = inset; - return this; - } - - public TaffyStyle size(TaffySize size) { - this.size = size; - return this; - } - - public TaffyStyle minSize(TaffySize minSize) { - this.minSize = minSize; - return this; - } - - public TaffyStyle maxSize(TaffySize maxSize) { - this.maxSize = maxSize; - return this; - } - - public TaffyStyle aspectRatio(Float aspectRatio) { - this.aspectRatio = aspectRatio; - return this; - } - - public TaffyStyle margin(TaffyRect margin) { - this.margin = margin; - return this; - } - - public TaffyStyle padding(TaffyRect padding) { - this.padding = padding; - return this; - } - - public TaffyStyle border(TaffyRect border) { - this.border = border; - return this; - } - - public TaffyStyle alignItems(TaffyAlignItems alignItems) { - this.alignItems = alignItems; - return this; - } - - public TaffyStyle alignSelf(TaffyAlignItems alignSelf) { - this.alignSelf = alignSelf; - return this; - } - - public TaffyStyle justifyItems(TaffyAlignItems justifyItems) { - this.justifyItems = justifyItems; - return this; - } - - public TaffyStyle justifySelf(TaffyAlignItems justifySelf) { - this.justifySelf = justifySelf; - return this; - } - - public TaffyStyle alignContent(TaffyAlignContent alignContent) { - this.alignContent = alignContent; - return this; - } - - public TaffyStyle justifyContent(TaffyAlignContent justifyContent) { - this.justifyContent = justifyContent; - return this; - } - - public TaffyStyle gap(TaffySize gap) { - this.gap = gap; - return this; - } - - public TaffyStyle textAlign(TaffyTextAlign textAlign) { - this.textAlign = textAlign; - return this; - } - - public TaffyStyle flexDirection(TaffyFlexDirection flexDirection) { - this.flexDirection = flexDirection; - return this; - } - - public TaffyStyle flexWrap(TaffyFlexWrap flexWrap) { - this.flexWrap = flexWrap; - return this; - } - - public TaffyStyle flexBasis(TaffyDimension flexBasis) { - this.flexBasis = flexBasis; - return this; - } - - public TaffyStyle flexGrow(float flexGrow) { - this.flexGrow = flexGrow; - return this; - } - - public TaffyStyle flexShrink(float flexShrink) { - this.flexShrink = flexShrink; - return this; - } - - public TaffyStyle gridTemplateRows(List gridTemplateRows) { - this.gridTemplateRows = gridTemplateRows; - return this; - } - - public TaffyStyle gridTemplateColumns(List gridTemplateColumns) { - this.gridTemplateColumns = gridTemplateColumns; - return this; - } - - public TaffyStyle gridAutoRows(List gridAutoRows) { - this.gridAutoRows = gridAutoRows; - return this; - } - - public TaffyStyle gridAutoColumns(List gridAutoColumns) { - this.gridAutoColumns = gridAutoColumns; - return this; - } - - public TaffyStyle gridAutoFlow(TaffyGridAutoFlow gridAutoFlow) { - this.gridAutoFlow = gridAutoFlow; - return this; - } - - public TaffyStyle gridRow(TaffyLine gridRow) { - this.gridRow = gridRow; - return this; - } - - public TaffyStyle gridColumn(TaffyLine gridColumn) { - this.gridColumn = gridColumn; - return this; - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TextAlign.java similarity index 92% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TextAlign.java index 72aca7987..ab2b576db 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TaffyTextAlign.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TextAlign.java @@ -3,7 +3,7 @@ /** * Used by block layout to implement the legacy behaviour of `

` and `
` */ -public enum TaffyTextAlign { +public enum TextAlign { /** * No special legacy text align behaviour. */ @@ -23,7 +23,7 @@ public enum TaffyTextAlign { private final int internal; - TaffyTextAlign() { + TextAlign() { internal = ordinal(); } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/TaffyLayout.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/Layout.java similarity index 72% rename from bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/TaffyLayout.java rename to bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/Layout.java index a62838b60..4665722a6 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/TaffyLayout.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/Layout.java @@ -1,8 +1,8 @@ package com.dioxuslabs.taffy.tree; -import com.dioxuslabs.taffy.geom.TaffyPoint; -import com.dioxuslabs.taffy.geom.TaffyRect; -import com.dioxuslabs.taffy.geom.TaffySize; +import com.dioxuslabs.taffy.geom.Point; +import com.dioxuslabs.taffy.geom.Rect; +import com.dioxuslabs.taffy.geom.Size; /** * The final result of a layout algorithm for a single node. @@ -21,14 +21,14 @@ * @param padding The size of the padding of the node * @param margin The size of the margin of the node */ -public record TaffyLayout( +public record Layout( int order, - TaffyPoint location, - TaffySize size, - TaffySize contentSize, - TaffySize scrollbarSize, - TaffyRect border, - TaffyRect padding, - TaffyRect margin + Point location, + Size size, + Size contentSize, + Size scrollbarSize, + Rect border, + Rect padding, + Rect margin ) { } diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java index bf8171589..5c394b00e 100644 --- a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/BorderAndPaddingTests.java @@ -1,10 +1,10 @@ package com.dioxuslabs.taffy; -import com.dioxuslabs.taffy.geom.TaffyRect; -import com.dioxuslabs.taffy.geom.TaffySize; -import com.dioxuslabs.taffy.geom.measure.TaffyLengthPercentage; -import com.dioxuslabs.taffy.style.TaffyStyle; -import com.dioxuslabs.taffy.tree.TaffyLayout; +import com.dioxuslabs.taffy.geom.Rect; +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.LengthPercentage; +import com.dioxuslabs.taffy.style.Style; +import com.dioxuslabs.taffy.tree.Layout; import org.junit.jupiter.api.Test; import java.util.Arrays; @@ -16,8 +16,8 @@ public class BorderAndPaddingTests { System.loadLibrary("jtaffy"); } - public TaffyRect arrToRect(T[] items) { - return new TaffyRect<>(items[0], items[1], items[2], items[3]); + public Rect arrToRect(T[] items) { + return new Rect<>(items[0], items[1], items[2], items[3]); } @Test @@ -25,18 +25,18 @@ public void borderOnASingleAxisDoesntIncreaseSize() { for (int i = 0; i < 4; i++) { TaffyTree taffy = new TaffyTree(); - TaffyLengthPercentage[] lengths = new TaffyLengthPercentage[4]; - Arrays.fill(lengths, TaffyLengthPercentage.length(0f)); - lengths[i] = TaffyLengthPercentage.length(10f); - TaffyRect rect = arrToRect(lengths); + LengthPercentage[] lengths = new LengthPercentage[4]; + Arrays.fill(lengths, LengthPercentage.length(0f)); + lengths[i] = LengthPercentage.length(10f); + Rect rect = arrToRect(lengths); - long node = taffy.newLeaf(TaffyStyle.builder() + long node = taffy.newLeaf(Style.builder() .border(rect) ); - taffy.computeLayout(node, TaffySize.definiteAvailableSize(100, 100)); + taffy.computeLayout(node, Size.definiteAvailableSize(100, 100)); - TaffyLayout layout = taffy.layout(node); + Layout layout = taffy.layout(node); assertEquals(layout.size().width() * layout.size().height(), 0.0f); } @@ -47,18 +47,18 @@ public void paddingOnASingleAxisDoesntIncreaseSize() { for (int i = 0; i < 4; i++) { TaffyTree taffy = new TaffyTree(); - TaffyLengthPercentage[] lengths = new TaffyLengthPercentage[4]; - Arrays.fill(lengths, TaffyLengthPercentage.length(0f)); - lengths[i] = TaffyLengthPercentage.length(10f); - TaffyRect rect = arrToRect(lengths); + LengthPercentage[] lengths = new LengthPercentage[4]; + Arrays.fill(lengths, LengthPercentage.length(0f)); + lengths[i] = LengthPercentage.length(10f); + Rect rect = arrToRect(lengths); - long node = taffy.newLeaf(TaffyStyle.builder() + long node = taffy.newLeaf(Style.builder() .padding(rect) ); - taffy.computeLayout(node, TaffySize.definiteAvailableSize(100, 100)); + taffy.computeLayout(node, Size.definiteAvailableSize(100, 100)); - TaffyLayout layout = taffy.layout(node); + Layout layout = taffy.layout(node); assertEquals(layout.size().width() * layout.size().height(), 0.0f); } @@ -69,19 +69,19 @@ public void borderAndPaddingOnASingleAxisDoesntIncreaseSize() { for (int i = 0; i < 4; i++) { TaffyTree taffy = new TaffyTree(); - TaffyLengthPercentage[] lengths = new TaffyLengthPercentage[4]; - Arrays.fill(lengths, TaffyLengthPercentage.length(0f)); - lengths[i] = TaffyLengthPercentage.length(10f); - TaffyRect rect = arrToRect(lengths); + LengthPercentage[] lengths = new LengthPercentage[4]; + Arrays.fill(lengths, LengthPercentage.length(0f)); + lengths[i] = LengthPercentage.length(10f); + Rect rect = arrToRect(lengths); - long node = taffy.newLeaf(TaffyStyle.builder() + long node = taffy.newLeaf(Style.builder() .border(rect) .padding(rect) ); - taffy.computeLayout(node, TaffySize.definiteAvailableSize(100, 100)); + taffy.computeLayout(node, Size.definiteAvailableSize(100, 100)); - TaffyLayout layout = taffy.layout(node); + Layout layout = taffy.layout(node); assertEquals(layout.size().width() * layout.size().height(), 0.0f); } @@ -90,18 +90,18 @@ public void borderAndPaddingOnASingleAxisDoesntIncreaseSize() { @Test public void verticalBorderAndPaddingPercentageValuesUseAvailableSpaceCorrectly() { TaffyTree taffy = new TaffyTree(); - long node = taffy.newLeaf(TaffyStyle.builder() - .padding(new TaffyRect<>( - TaffyLengthPercentage.percent(1f), - TaffyLengthPercentage.length(0f), - TaffyLengthPercentage.percent(1f), - TaffyLengthPercentage.length(0f) + long node = taffy.newLeaf(Style.builder() + .padding(new Rect<>( + LengthPercentage.percent(1f), + LengthPercentage.length(0f), + LengthPercentage.percent(1f), + LengthPercentage.length(0f) )) ); - taffy.computeLayout(node, TaffySize.definiteAvailableSize(200, 100)); + taffy.computeLayout(node, Size.definiteAvailableSize(200, 100)); - TaffyLayout layout = taffy.layout(node); + Layout layout = taffy.layout(node); assertEquals(layout.size().width(), 200.0f); assertEquals(layout.size().height(), 200.0f); diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java index 4b53fc5d7..8a29b50c2 100644 --- a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java @@ -1,10 +1,10 @@ package com.dioxuslabs.taffy; -import com.dioxuslabs.taffy.geom.TaffySize; -import com.dioxuslabs.taffy.geom.measure.TaffyDimension; -import com.dioxuslabs.taffy.style.TaffyAlignContent; -import com.dioxuslabs.taffy.style.TaffyStyle; -import com.dioxuslabs.taffy.tree.TaffyLayout; +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.Dimension; +import com.dioxuslabs.taffy.style.AlignContent; +import com.dioxuslabs.taffy.style.Style; +import com.dioxuslabs.taffy.tree.Layout; import org.junit.jupiter.api.Test; import java.util.Arrays; @@ -21,23 +21,23 @@ public void roundingDoesntLeaveGaps() { // First create an instance of TaffyTree TaffyTree taffy = new TaffyTree(); - TaffySize wSquare = TaffySize.lengthDimension(100.3f, 100.3f); + Size wSquare = Size.lengthDimension(100.3f, 100.3f); - long childA = taffy.newLeaf(TaffyStyle.builder().size(wSquare)); - long childB = taffy.newLeaf(TaffyStyle.builder().size(wSquare)); + long childA = taffy.newLeaf(Style.builder().size(wSquare)); + long childB = taffy.newLeaf(Style.builder().size(wSquare)); long rootNode = taffy.newWithChildren( - TaffyStyle.builder() - .size(TaffySize.length(TaffyDimension.class, 963.3333f, 1000f)) - .justifyContent(TaffyAlignContent.CENTER), + Style.builder() + .size(Size.length(Dimension.class, 963.3333f, 1000f)) + .justifyContent(AlignContent.CENTER), Arrays.asList(childA, childB) ); - taffy.computeLayout(rootNode, TaffySize.maxContentAvailableSize()); + taffy.computeLayout(rootNode, Size.maxContentAvailableSize()); taffy.printTree(rootNode); - TaffyLayout layoutA = taffy.layout(childA); - TaffyLayout layoutB = taffy.layout(childB); + Layout layoutA = taffy.layout(childA); + Layout layoutB = taffy.layout(childB); assertEquals(layoutA.location().x() + layoutA.size().width(), layoutB.location().x()); } diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index 9c7b1a5e2..fca834b14 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -24,7 +24,7 @@ pub fn get_overflow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local> #[allow(dead_code)] pub fn f_get_overflow<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Overflow { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyOverflow;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/Overflow;"); get_overflow(env, obj) } @@ -44,7 +44,7 @@ pub fn get_position<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local> } pub fn f_get_position<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Position { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyPosition;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/Position;"); get_position(env, obj) } @@ -70,7 +70,7 @@ pub fn get_text_align<'local>( } pub fn f_get_text_align<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> TextAlign { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyTextAlign;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TextAlign;"); get_text_align(env, obj, TextAlign::default) } @@ -96,7 +96,7 @@ pub fn get_flex_direction<'local>( } pub fn f_get_flex_direction<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> FlexDirection { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyFlexDirection;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/FlexDirection;"); get_flex_direction(env, obj, FlexDirection::default) } @@ -117,7 +117,7 @@ pub fn get_flex_wrap<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local } pub fn f_get_flex_wrap<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> FlexWrap { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyFlexWrap;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/FlexWrap;"); get_flex_wrap(env, obj, FlexWrap::default) } @@ -143,7 +143,7 @@ pub fn get_grid_auto_flow<'local>( } pub fn f_get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> GridAutoFlow { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyGridAutoFlow;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/GridAutoFlow;"); get_grid_auto_flow(env, obj, GridAutoFlow::default) } @@ -168,7 +168,7 @@ pub fn get_align_items<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc } pub fn f_get_align_items<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Option { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignItems;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/AlignItems;"); get_align_items(env, obj) } @@ -199,7 +199,7 @@ pub fn f_get_align_content<'local>( base: &JObject<'local>, field: &str, ) -> Option { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyAlignContent;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/AlignContent;"); get_align_content(env, obj) } @@ -221,7 +221,7 @@ pub fn get_display<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) } pub fn f_get_display<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Display { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyDisplay;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/Display;"); get_display(env, obj) } @@ -241,7 +241,7 @@ pub fn get_box_sizing<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loca } pub fn f_get_box_sizing<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> BoxSizing { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TaffyBoxSizing;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/BoxSizing;"); get_box_sizing(env, obj) } diff --git a/bindings/java/src/geom.rs b/bindings/java/src/geom.rs index 851f4ffb5..90cf3fd3a 100644 --- a/bindings/java/src/geom.rs +++ b/bindings/java/src/geom.rs @@ -100,8 +100,8 @@ pub fn get_non_repeated_track_sizing_function<'local>( panic!("NonRepeatedTrackSizingFunction cannot be null"); } - let n_val = f_get_value(env, nrtsf_object, "min", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction;"); - let x_val = f_get_value(env, nrtsf_object, "max", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction;"); + let n_val = f_get_value(env, nrtsf_object, "min", "Lcom/dioxuslabs/taffy/geom/grid/MinTrackSizingFunction;"); + let x_val = f_get_value(env, nrtsf_object, "max", "Lcom/dioxuslabs/taffy/geom/grid/MaxTrackSizingFunction;"); let min = get_min_track_sizing_function(env, n_val, || MinTrackSizingFunction::Auto); let max = get_max_track_sizing_function(env, x_val, || MaxTrackSizingFunction::Auto); @@ -119,8 +119,8 @@ pub fn get_opt_non_repeated_track_sizing_function<'local>( return None; } - let n_val = f_get_value(env, nrtsf_object, "min", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMinTrackSizingFunction;"); - let x_val = f_get_value(env, nrtsf_object, "max", "Lcom/dioxuslabs/taffy/geom/grid/TaffyMaxTrackSizingFunction;"); + let n_val = f_get_value(env, nrtsf_object, "min", "Lcom/dioxuslabs/taffy/geom/grid/MinTrackSizingFunction;"); + let x_val = f_get_value(env, nrtsf_object, "max", "Lcom/dioxuslabs/taffy/geom/grid/MaxTrackSizingFunction;"); let min = get_min_track_sizing_function(env, n_val, || MinTrackSizingFunction::Auto); let max = get_max_track_sizing_function(env, x_val, || MaxTrackSizingFunction::Auto); @@ -139,7 +139,7 @@ pub fn f_get_point<'local, T, F>( where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { - let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/TaffyPoint;"); + let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/Point;"); get_point(env, point_field, f, def) } @@ -155,7 +155,7 @@ pub fn f_get_rect<'local, T, F>( where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { - let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/TaffyRect;"); + let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/Rect;"); get_rect(env, point_field, f, def) } @@ -171,7 +171,7 @@ pub fn f_get_size<'local, T, F>( where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { - let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/TaffySize;"); + let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/Size;"); get_size(env, point_field, f, def) } @@ -187,7 +187,7 @@ pub fn f_get_line<'local, T, F>( where F: Fn(&mut JNIEnv<'local>, JValueOwned<'local>) -> T, { - let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/TaffyLine;"); + let point_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/Line;"); get_line(env, point_field, f, def) } @@ -199,7 +199,7 @@ pub fn f_get_non_repeated_track_sizing_function<'local>( field: &str, ) -> NonRepeatedTrackSizingFunction { let nrtsft_field = - f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/TaffyNonRepeatedTrackSizingFunction;"); + f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/NonRepeatedTrackSizingFunction;"); get_non_repeated_track_sizing_function(env, nrtsft_field) } diff --git a/bindings/java/src/java.rs b/bindings/java/src/java.rs index eef811d95..376869ee0 100644 --- a/bindings/java/src/java.rs +++ b/bindings/java/src/java.rs @@ -4,7 +4,7 @@ use jni::JNIEnv; use taffy::{Layout, Point, Rect, Size}; pub fn to_java_layout(env: &mut JNIEnv, layout: &Layout) -> jobject { - let layout_class = &env.find_class("com/dioxuslabs/taffy/tree/TaffyLayout").unwrap(); + let layout_class = &env.find_class("com/dioxuslabs/taffy/tree/Layout").unwrap(); let location = &f32_point_to_java(env, layout.location); let size = &f32_size_to_java(env, layout.size); @@ -14,7 +14,7 @@ pub fn to_java_layout(env: &mut JNIEnv, layout: &Layout) -> jobject { let padding = &f32_rect_to_java(env, layout.padding); let margin = &f32_rect_to_java(env, layout.margin); - *env.new_object(layout_class, "(ILcom/dioxuslabs/taffy/geom/TaffyPoint;Lcom/dioxuslabs/taffy/geom/TaffySize;Lcom/dioxuslabs/taffy/geom/TaffySize;Lcom/dioxuslabs/taffy/geom/TaffySize;Lcom/dioxuslabs/taffy/geom/TaffyRect;Lcom/dioxuslabs/taffy/geom/TaffyRect;Lcom/dioxuslabs/taffy/geom/TaffyRect;)V", &[ + *env.new_object(layout_class, "(ILcom/dioxuslabs/taffy/geom/Point;Lcom/dioxuslabs/taffy/geom/Size;Lcom/dioxuslabs/taffy/geom/Size;Lcom/dioxuslabs/taffy/geom/Size;Lcom/dioxuslabs/taffy/geom/Rect;Lcom/dioxuslabs/taffy/geom/Rect;Lcom/dioxuslabs/taffy/geom/Rect;)V", &[ JValue::Int(layout.order as i32), JValue::Object(location), JValue::Object(size), @@ -27,7 +27,7 @@ pub fn to_java_layout(env: &mut JNIEnv, layout: &Layout) -> jobject { } pub fn f32_point_to_java<'local>(env: &mut JNIEnv<'local>, point: Point) -> JObject<'local> { - let class = &env.find_class("com/dioxuslabs/taffy/geom/TaffyPoint").unwrap(); + let class = &env.find_class("com/dioxuslabs/taffy/geom/Point").unwrap(); let a = &f32_to_java(env, point.x); let b = &f32_to_java(env, point.y); @@ -36,7 +36,7 @@ pub fn f32_point_to_java<'local>(env: &mut JNIEnv<'local>, point: Point) -> } pub fn f32_size_to_java<'local>(env: &mut JNIEnv<'local>, size: Size) -> JObject<'local> { - let class = &env.find_class("com/dioxuslabs/taffy/geom/TaffySize").unwrap(); + let class = &env.find_class("com/dioxuslabs/taffy/geom/Size").unwrap(); let a = &f32_to_java(env, size.width); let b = &f32_to_java(env, size.height); @@ -45,7 +45,7 @@ pub fn f32_size_to_java<'local>(env: &mut JNIEnv<'local>, size: Size) -> JO } pub fn f32_rect_to_java<'local>(env: &mut JNIEnv<'local>, rect: Rect) -> JObject<'local> { - let class = &env.find_class("com/dioxuslabs/taffy/geom/TaffyRect").unwrap(); + let class = &env.find_class("com/dioxuslabs/taffy/geom/Rect").unwrap(); let a = &f32_to_java(env, rect.left); let b = &f32_to_java(env, rect.right); diff --git a/bindings/java/src/measure.rs b/bindings/java/src/measure.rs index 6a052555d..5fb4217d0 100644 --- a/bindings/java/src/measure.rs +++ b/bindings/java/src/measure.rs @@ -197,14 +197,14 @@ pub fn f_get_length_percentage<'local>( base: &JObject<'local>, field: &str, ) -> LengthPercentage { - let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/TaffyLengthPercentage;"); + let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/LengthPercentage;"); get_length_percentage(env, value) } #[allow(dead_code)] pub fn f_get_dimension<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Dimension { - let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/TaffyDimension;"); + let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/Dimension;"); get_dimension(env, value) } @@ -215,7 +215,7 @@ pub fn f_get_dimension_or<'local>( field: &str, f: fn() -> Dimension, ) -> Dimension { - let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/TaffyDimension;"); + let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/measure/Dimension;"); get_dimension_or(env, value, f) } @@ -225,7 +225,7 @@ pub fn f_get_grid_track_repetition<'local>( base: &JObject<'local>, field: &str, ) -> GridTrackRepetition { - let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/TaffyGridTrackRepetition;"); + let value = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/GridTrackRepetition;"); get_grid_track_repetition(env, value) } From d778ff299e2c114048745f3d5914df39fa615951 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Mon, 26 Aug 2024 20:04:58 +0200 Subject: [PATCH 12/32] enums: enum generator (just to collect feedback) --- Cargo.toml | 3 +- bindings/java/src/geom.rs | 3 +- bindings/scripts/Cargo.toml | 13 ++++++++ bindings/scripts/src/main.rs | 64 ++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 bindings/scripts/Cargo.toml create mode 100644 bindings/scripts/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index 338876623..e78b866fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,5 +102,6 @@ members = [ "scripts/import-yoga-tests", "benches", "taffy_stylo", - "bindings/java" + "bindings/java", + "bindings/scripts" ] diff --git a/bindings/java/src/geom.rs b/bindings/java/src/geom.rs index 90cf3fd3a..8cbf4f9e4 100644 --- a/bindings/java/src/geom.rs +++ b/bindings/java/src/geom.rs @@ -198,8 +198,7 @@ pub fn f_get_non_repeated_track_sizing_function<'local>( base: &JObject<'local>, field: &str, ) -> NonRepeatedTrackSizingFunction { - let nrtsft_field = - f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/NonRepeatedTrackSizingFunction;"); + let nrtsft_field = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/geom/grid/NonRepeatedTrackSizingFunction;"); get_non_repeated_track_sizing_function(env, nrtsft_field) } diff --git a/bindings/scripts/Cargo.toml b/bindings/scripts/Cargo.toml new file mode 100644 index 000000000..b5400c23b --- /dev/null +++ b/bindings/scripts/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "bindings_scripts" +version = "0.0.1" +authors = [ + "Arby Djabaev " +] +edition = "2021" +description = "Scripts to generate bindings related stuff for Taffy (A flexible UI layout library)" +repository = "https://github.com/DioxusLabs/taffy" +license = "MIT" + +[dependencies] +convert_case = "0.6.0" diff --git a/bindings/scripts/src/main.rs b/bindings/scripts/src/main.rs new file mode 100644 index 000000000..81a0da65a --- /dev/null +++ b/bindings/scripts/src/main.rs @@ -0,0 +1,64 @@ +use std::collections::HashMap; + +fn main() { + let mut enums: HashMap<&str, Vec<&str>> = HashMap::new(); + enums.insert("Display", vec!["Block", "Flex", "Grid", "None"]); + enums.insert("BoxGenerationMode", vec!["Normal", "None"]); + enums.insert("Position", vec!["Relative", "Absolute"]); + enums.insert("BoxSizing", vec!["BorderBox", "ContentBox"]); + enums.insert("Overflow", vec!["Visible", "Clip", "Hidden", "Scroll"]); + enums.insert("TextAlign", vec!["Auto", "LegacyLeft", "LegacyRight", "LegacyCenter"]); + enums.insert("GridAutoFlow", vec!["Row", "Column", "RowDense", "ColumnDense"]); + enums.insert("FlexWrap", vec!["NoWrap", "Wrap", "WrapReverse"]); + enums.insert("FlexDirection", vec!["Row", "Column", "RowReverse", "ColumnReverse"]); + + for (key, value) in enums.into_iter() { + create_enum(key, value); + } +} + +fn create_enum(name: &str, values: Vec<&str>) { + println!("{}", create_java_enum(name, values)); +} + +fn create_java_enum(name: &str, values: Vec<&str>) -> String { + use convert_case::{Case, Casing}; + + let package = "java.com.dioxuslabs.taffy.enums"; + let enum_name = name.to_case(Case::Pascal); + + let mut result = format!("package {};\n\npublic enum {} {{\n", package, enum_name); + + for (index, value) in values.iter().enumerate() { + let java_value = value.to_case(Case::UpperSnake); + result.push_str(" "); + result.push_str(&java_value); + result.push('('); + result.push_str(&index.to_string()); + result.push_str("),\n"); + } + + // eliminate the last comma + if !values.is_empty() { + result.pop(); + result.pop(); + result.push('\n'); + } + + result.push('\n'); + result.push_str(" ;\n"); + result.push('\n'); + + result.push_str(" private final int ordinal;\n"); + + result.push('\n'); + result.push_str(" "); + result.push_str(enum_name.as_str()); + result.push_str("() {\n"); + result.push_str(" this.ordinal = ordinal();\n"); + result.push_str(" }\n"); + + result.push_str("}\n"); + + result +} From 6d1beb224d861603d65e135bf38b5dfb95b37cb8 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Mon, 26 Aug 2024 20:15:20 +0200 Subject: [PATCH 13/32] enums: fix index parameter as we can get it with ordinal() --- bindings/scripts/src/main.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/bindings/scripts/src/main.rs b/bindings/scripts/src/main.rs index 81a0da65a..0c36df89a 100644 --- a/bindings/scripts/src/main.rs +++ b/bindings/scripts/src/main.rs @@ -29,13 +29,10 @@ fn create_java_enum(name: &str, values: Vec<&str>) -> String { let mut result = format!("package {};\n\npublic enum {} {{\n", package, enum_name); - for (index, value) in values.iter().enumerate() { - let java_value = value.to_case(Case::UpperSnake); + for value in values.iter() { result.push_str(" "); - result.push_str(&java_value); - result.push('('); - result.push_str(&index.to_string()); - result.push_str("),\n"); + result.push_str(&value.to_case(Case::UpperSnake)); + result.push_str(",\n"); } // eliminate the last comma From 71064b0d4b243ae914cf6a227fd9e0543b14c54b Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Mon, 26 Aug 2024 22:33:36 +0200 Subject: [PATCH 14/32] enums: write files to enum folder --- bindings/scripts/src/main.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/bindings/scripts/src/main.rs b/bindings/scripts/src/main.rs index 0c36df89a..02d5b98cb 100644 --- a/bindings/scripts/src/main.rs +++ b/bindings/scripts/src/main.rs @@ -1,4 +1,7 @@ use std::collections::HashMap; +use std::fs; +use std::fs::File; +use std::io::Write; fn main() { let mut enums: HashMap<&str, Vec<&str>> = HashMap::new(); @@ -18,16 +21,23 @@ fn main() { } fn create_enum(name: &str, values: Vec<&str>) { - println!("{}", create_java_enum(name, values)); + create_java_enum(name, values); } -fn create_java_enum(name: &str, values: Vec<&str>) -> String { +fn create_java_enum(name: &str, values: Vec<&str>) { use convert_case::{Case, Casing}; - let package = "java.com.dioxuslabs.taffy.enums"; + let package = "com.dioxuslabs.taffy.enums"; let enum_name = name.to_case(Case::Pascal); - let mut result = format!("package {};\n\npublic enum {} {{\n", package, enum_name); + let auto_gen_comment = "/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/\n"; + + let mut result = format!("{}package {};\n\npublic enum {} {{\n", auto_gen_comment, package, enum_name); for value in values.iter() { result.push_str(" "); @@ -42,7 +52,6 @@ fn create_java_enum(name: &str, values: Vec<&str>) -> String { result.push('\n'); } - result.push('\n'); result.push_str(" ;\n"); result.push('\n'); @@ -57,5 +66,9 @@ fn create_java_enum(name: &str, values: Vec<&str>) -> String { result.push_str("}\n"); - result + fs::create_dir_all("./bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/") + .expect("Couldn't create directories"); + let file = + File::create(format!("./bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/{}.java", enum_name)); + file.expect("Error: File not found").write_all(result.as_ref()).expect("Error: Couldn't write to file"); } From 32865ee283699e4c6fce1157b105e53e3e9409c3 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Mon, 26 Aug 2024 23:11:54 +0200 Subject: [PATCH 15/32] enums: use autogenerated files --- bindings/java/.gitignore | 2 + .../dioxuslabs/taffy/enums/AlignContent.java | 26 ++++++++ .../dioxuslabs/taffy/enums/AlignItems.java | 24 +++++++ .../taffy/enums/BoxGenerationMode.java | 19 ++++++ .../com/dioxuslabs/taffy/enums/BoxSizing.java | 19 ++++++ .../com/dioxuslabs/taffy/enums/Display.java | 21 ++++++ .../dioxuslabs/taffy/enums/FlexDirection.java | 21 ++++++ .../com/dioxuslabs/taffy/enums/FlexWrap.java | 20 ++++++ .../dioxuslabs/taffy/enums/GridAutoFlow.java | 21 ++++++ .../com/dioxuslabs/taffy/enums/Overflow.java | 21 ++++++ .../com/dioxuslabs/taffy/enums/Position.java | 19 ++++++ .../com/dioxuslabs/taffy/enums/TextAlign.java | 21 ++++++ .../taffy/geom/grid/GenericGridPlacement.java | 2 +- .../dioxuslabs/taffy/style/AlignContent.java | 64 ------------------- .../dioxuslabs/taffy/style/AlignItems.java | 53 --------------- .../com/dioxuslabs/taffy/style/BoxSizing.java | 33 ---------- .../com/dioxuslabs/taffy/style/Display.java | 31 --------- .../dioxuslabs/taffy/style/FlexDirection.java | 47 -------------- .../com/dioxuslabs/taffy/style/FlexWrap.java | 29 --------- .../dioxuslabs/taffy/style/GridAutoFlow.java | 35 ---------- .../com/dioxuslabs/taffy/style/Overflow.java | 46 ------------- .../com/dioxuslabs/taffy/style/Position.java | 34 ---------- .../com/dioxuslabs/taffy/style/Style.java | 1 + .../com/dioxuslabs/taffy/style/TextAlign.java | 29 --------- .../com/dioxuslabs/taffy/RoundingTests.java | 2 +- bindings/scripts/src/main.rs | 5 ++ 26 files changed, 242 insertions(+), 403 deletions(-) create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AlignContent.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AlignItems.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/BoxGenerationMode.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/BoxSizing.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Display.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/FlexDirection.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/FlexWrap.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/GridAutoFlow.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Overflow.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Position.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/TextAlign.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignContent.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignItems.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/BoxSizing.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Display.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexDirection.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexWrap.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/GridAutoFlow.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Overflow.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Position.java delete mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TextAlign.java diff --git a/bindings/java/.gitignore b/bindings/java/.gitignore index a1d0ee321..8b2c3a1a1 100644 --- a/bindings/java/.gitignore +++ b/bindings/java/.gitignore @@ -2,3 +2,5 @@ *.iml out *.class +/target +/java/target \ No newline at end of file diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AlignContent.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AlignContent.java new file mode 100644 index 000000000..25e61c8af --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AlignContent.java @@ -0,0 +1,26 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum AlignContent { + START, + END, + FLEX_START, + FLEX_END, + CENTER, + STRETCH, + SPACE_BETWEEN, + SPACE_EVENLY, + SPACE_AROUND + ; + + private final int ordinal; + + AlignContent() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AlignItems.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AlignItems.java new file mode 100644 index 000000000..1e5f0478e --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AlignItems.java @@ -0,0 +1,24 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum AlignItems { + START, + END, + FLEX_START, + FLEX_END, + CENTER, + BASELINE, + STRETCH + ; + + private final int ordinal; + + AlignItems() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/BoxGenerationMode.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/BoxGenerationMode.java new file mode 100644 index 000000000..bbc5f06db --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/BoxGenerationMode.java @@ -0,0 +1,19 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum BoxGenerationMode { + NORMAL, + NONE + ; + + private final int ordinal; + + BoxGenerationMode() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/BoxSizing.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/BoxSizing.java new file mode 100644 index 000000000..752aa09c8 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/BoxSizing.java @@ -0,0 +1,19 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum BoxSizing { + BORDER_BOX, + CONTENT_BOX + ; + + private final int ordinal; + + BoxSizing() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Display.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Display.java new file mode 100644 index 000000000..5215a7d7b --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Display.java @@ -0,0 +1,21 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum Display { + BLOCK, + FLEX, + GRID, + NONE + ; + + private final int ordinal; + + Display() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/FlexDirection.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/FlexDirection.java new file mode 100644 index 000000000..f16357277 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/FlexDirection.java @@ -0,0 +1,21 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum FlexDirection { + ROW, + COLUMN, + ROW_REVERSE, + COLUMN_REVERSE + ; + + private final int ordinal; + + FlexDirection() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/FlexWrap.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/FlexWrap.java new file mode 100644 index 000000000..c5718b122 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/FlexWrap.java @@ -0,0 +1,20 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum FlexWrap { + NO_WRAP, + WRAP, + WRAP_REVERSE + ; + + private final int ordinal; + + FlexWrap() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/GridAutoFlow.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/GridAutoFlow.java new file mode 100644 index 000000000..0bf56bb3d --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/GridAutoFlow.java @@ -0,0 +1,21 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum GridAutoFlow { + ROW, + COLUMN, + ROW_DENSE, + COLUMN_DENSE + ; + + private final int ordinal; + + GridAutoFlow() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Overflow.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Overflow.java new file mode 100644 index 000000000..67ffe141a --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Overflow.java @@ -0,0 +1,21 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum Overflow { + VISIBLE, + CLIP, + HIDDEN, + SCROLL + ; + + private final int ordinal; + + Overflow() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Position.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Position.java new file mode 100644 index 000000000..369c1a973 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/Position.java @@ -0,0 +1,19 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum Position { + RELATIVE, + ABSOLUTE + ; + + private final int ordinal; + + Position() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/TextAlign.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/TextAlign.java new file mode 100644 index 000000000..9215004a5 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/TextAlign.java @@ -0,0 +1,21 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum TextAlign { + AUTO, + LEGACY_LEFT, + LEGACY_RIGHT, + LEGACY_CENTER + ; + + private final int ordinal; + + TextAlign() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GenericGridPlacement.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GenericGridPlacement.java index 2edad1667..3e4cb9787 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GenericGridPlacement.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/grid/GenericGridPlacement.java @@ -1,6 +1,6 @@ package com.dioxuslabs.taffy.geom.grid; -import com.dioxuslabs.taffy.style.GridAutoFlow; +import com.dioxuslabs.taffy.enums.GridAutoFlow; /** * A grid line placement specification which is generic over the coordinate system that it uses to define diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignContent.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignContent.java deleted file mode 100644 index 5873e826d..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignContent.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * Sets the distribution of space between and around content items - * For Flexbox it controls alignment in the cross axis - * For Grid it controls alignment in the block axis - *

- * MDN - */ -public enum AlignContent { - /** - * Items are packed toward the start of the axis - */ - START, - /** - * Items are packed toward the end of the axis - */ - END, - /** - * Items are packed towards the flex-relative start of the axis. - *

- * For flex containers with {@link FlexDirection#ROW_REVERSE} or {@link FlexDirection#COLUMN_REVERSE} - * this is equivalent to {@link AlignContent#END}. In all other cases it is equivalent to - * {@link AlignContent#START}. - */ - FLEX_START, - /** - * Items are packed towards the flex-relative end of the axis. - *

- * For flex containers with {@link FlexDirection#ROW_REVERSE} or {@link FlexDirection#COLUMN_REVERSE} - * this is equivalent to {@link AlignContent#START}. In all other cases it is equivalent to - * {@link AlignContent#END}. - */ - FLEX_END, - /** - * Items are centered along the middle of the cross axis - */ - CENTER, - /** - * Items are stretched to fill the container - */ - STRETCH, - /** - * The first and last items are aligned flush with the edges of the container (no gap) - * The gap between items is distributed evenly. - */ - SPACE_BETWEEN, - /** - * The gap between the first and last items is exactly THE SAME as the gap between items. - * The gaps are distributed evenly - */ - SPACE_EVENLY, - /** - * The gap between the first and last items is exactly HALF the gap between items. - * The gaps are distributed evenly in proportion to these ratios. - */ - SPACE_AROUND; - - private final int internal; - - AlignContent() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignItems.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignItems.java deleted file mode 100644 index c8c50d815..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/AlignItems.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * Used to control how child nodes are aligned. - * For Flexbox it controls alignment in the cross axis - * For Grid it controls alignment in the block axis - *

- * MDN - */ -public enum AlignItems { - /** - * Items are packed toward the start of the axis - */ - START, - /** - * Items are packed toward the end of the axis - */ - END, - /** - * Items are packed towards the flex-relative start of the axis. - *

- * For flex containers with {@link FlexDirection#ROW_REVERSE} or {@link FlexDirection#COLUMN_REVERSE} - * this is equivalent to {@link AlignItems#END}. In all other cases it is equivalent to - * {@link AlignItems#START}. - */ - FLEX_START, - /** - * Items are packed towards the flex-relative end of the axis. - *

- * For flex containers with {@link FlexDirection#ROW_REVERSE} or {@link FlexDirection#COLUMN_REVERSE} - * this is equivalent to {@link AlignItems#START}. In all other cases it is equivalent to - * {@link AlignItems#END}. - */ - FLEX_END, - /** - * Items are packed along the center of the cross axis - */ - CENTER, - /** - * Items are aligned such as their baselines align - */ - BASELINE, - /** - * Stretch to fill the container - */ - STRETCH; - - private final int internal; - - AlignItems() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/BoxSizing.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/BoxSizing.java deleted file mode 100644 index 06c4a8a55..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/BoxSizing.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * Specifies whether size styles for this node are assigned to the node's "content box" or "border box" - *

- * - The "content box" is the node's inner size excluding padding, border and margin - * - The "border box" is the node's outer size including padding and border (but still excluding margin) - *

- * This property modifies the application of the following styles: - *

- * - `size` - * - `min_size` - * - `max_size` - * - `flex_basis` - *

... - */ -public enum BoxSizing { - /** - * Size styles such size, min_size, max_size specify the box's "content box" (the size excluding padding/border/margin) - */ - BORDER_BOX, - /** - * Size styles such size, min_size, max_size specify the box's "border box" (the size excluding margin but including padding/border) - */ - CONTENT_BOX; - - private final int internal; - - BoxSizing() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Display.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Display.java deleted file mode 100644 index 655739378..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Display.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * Sets the layout used for the children of this node - *

- * The default values depends on on which feature flags are enabled. The order of precedence is: Flex, Grid, Block, None. - */ -public enum Display { - /** - * The children will follow the block layout algorithm - */ - BLOCK, - /** - * The children will follow the flexbox layout algorithm - */ - FLEX, - /** - * The children will follow the CSS Grid layout algorithm - */ - GRID, - /** - * The node is hidden, and it's children will also be hidden - */ - NONE; - - private final int internal; - - Display() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexDirection.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexDirection.java deleted file mode 100644 index 8d65fbec9..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexDirection.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * The direction of the flexbox layout main axis. - *

- * There are always two perpendicular layout axes: main (or primary) and cross (or secondary). - * Adding items will cause them to be positioned adjacent to each other along the main axis. - * By varying this value throughout your tree, you can create complex axis-aligned layouts. - *

- * Items are always aligned relative to the cross axis, and justified relative to the main axis. - *

- * The default behavior is {@link FlexDirection#ROW}. - *

- * Specification - */ -public enum FlexDirection { - /** - * Defines +x as the main axis - *

- * Items will be added from left to right in a row. - */ - ROW, - /** - * Defines +y as the main axis - *

- * Items will be added from top to bottom in a column. - */ - COLUMN, - /** - * Defines -x as the main axis - *

- * Items will be added from right to left in a row. - */ - ROW_REVERSE, - /** - * Defines -y as the main axis - *

- * Items will be added from bottom to top in a column. - */ - COLUMN_REVERSE; - - private final int internal; - - FlexDirection() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexWrap.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexWrap.java deleted file mode 100644 index f379c4768..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/FlexWrap.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * Controls whether flex items are forced onto one line or can wrap onto multiple lines. - *

- * Defaults to {@link FlexWrap#NO_WRAP} - *

- * Specification - */ -public enum FlexWrap { - /** - * Items will not wrap and stay on a single line - */ - NO_WRAP, - /** - * Items will wrap according to this item's {@link FlexDirection} - */ - WRAP, - /** - * Items will wrap in the opposite direction to this item's {@link FlexDirection} - */ - WRAP_REVERSE; - - private final int internal; - - FlexWrap() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/GridAutoFlow.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/GridAutoFlow.java deleted file mode 100644 index 34945502f..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/GridAutoFlow.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * Controls whether grid items are placed row-wise or column-wise. And whether the sparse or dense packing algorithm is used. - *

- * The "dense" packing algorithm attempts to fill in holes earlier in the grid, if smaller items come up later. This may cause items to appear out-of-order, when doing so would fill in holes left by larger items. - *

- * Defaults to [`GridAutoFlow::Row`] - *

- * MDN - */ -public enum GridAutoFlow { - /** - * Items are placed by filling each row in turn, adding new rows as necessary - */ - ROW, - /** - * Items are placed by filling each column in turn, adding new columns as necessary. - */ - COLUMN, - /** - * Combines ROW with the dense packing algorithm. - */ - ROW_DENSE, - /** - * Combines COLUMN with the dense packing algorithm. - */ - COLUMN_DENSE; - - private final int internal; - - GridAutoFlow() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Overflow.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Overflow.java deleted file mode 100644 index 8eef0a837..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Overflow.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * How children overflowing their container should affect layout - *

- * In CSS the primary effect of this property is to control whether contents of a parent container that overflow that container should - * be displayed anyway, be clipped, or trigger the container to become a scroll container. However it also has secondary effects on layout, - * the main ones being: - *

- * - The automatic minimum size Flexbox/CSS Grid items with non-`Visible` overflow is `0` rather than being content based - * - `Overflow::Scroll` nodes have space in the layout reserved for a scrollbar (width controlled by the `scrollbar_width` property) - *

- * In Taffy, we only implement the layout related secondary effects as we are not concerned with drawing/painting. The amount of space reserved for - * a scrollbar is controlled by the `scrollbar_width` property. If this is `0` then `Scroll` behaves identically to `Hidden`. - *

- * ... - */ -public enum Overflow { - /** - * The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content. - * Content that overflows this node *should* contribute to the scroll region of its parent. - */ - VISIBLE, - /** - * The automatic minimum size of this node as a flexbox/grid item should be based on the size of its content. - * Content that overflows this node should *not* contribute to the scroll region of its parent. - */ - CLIP, - /** - * The automatic minimum size of this node as a flexbox/grid item should be `0`. - * Content that overflows this node should *not* contribute to the scroll region of its parent. - */ - HIDDEN, - /** - * The automatic minimum size of this node as a flexbox/grid item should be `0`. Additionally, space should be reserved - * for a scrollbar. The amount of space reserved is controlled by the `scrollbar_width` property. - * Content that overflows this node should *not* contribute to the scroll region of its parent. - */ - SCROLL; - - private final int internal; - - Overflow() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Position.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Position.java deleted file mode 100644 index 62101110e..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Position.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * The positioning strategy for this item. - *

- * This controls both how the origin is determined for the [`Style::position`] field, - * and whether or not the item will be controlled by flexbox's layout algorithm. - *

- * WARNING: this enum follows the behavior of CSS's `position` property, - * which can be unintuitive. - *

- * {@link Position#RELATIVE} is the default value, in contrast to the default behavior in CSS. - */ -public enum Position { - /** - * The offset is computed relative to the final position given by the layout algorithm. - * Offsets do not affect the position of any other items; they are effectively a correction factor applied at the end. - */ - RELATIVE, - /** - * The offset is computed relative to this item's closest positioned ancestor, if any. - * Otherwise, it is placed relative to the origin. - * No space is created for the item in the page layout, and its size will not be altered. - *

- * WARNING: to opt-out of layouting entirely, you must use {@link Display#NONE} instead on your {@link Style} object. - */ - ABSOLUTE; - - private final int internal; - - Position() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java index c0ae7d8a5..cd0354cc1 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java @@ -1,5 +1,6 @@ package com.dioxuslabs.taffy.style; +import com.dioxuslabs.taffy.enums.*; import com.dioxuslabs.taffy.geom.Line; import com.dioxuslabs.taffy.geom.Point; import com.dioxuslabs.taffy.geom.Rect; diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TextAlign.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TextAlign.java deleted file mode 100644 index ab2b576db..000000000 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/TextAlign.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.dioxuslabs.taffy.style; - -/** - * Used by block layout to implement the legacy behaviour of `

` and `
` - */ -public enum TextAlign { - /** - * No special legacy text align behaviour. - */ - AUTO, - /** - * Corresponds to `-webkit-left` or `-moz-left` in browsers - */ - LEGACY_LEFT, - /** - * Corresponds to `-webkit-right` or `-moz-right` in browsers - */ - LEGACY_RIGHT, - /** - * Corresponds to `-webkit-center` or `-moz-center` in browsers - */ - LEGACY_CENTER; - - private final int internal; - - TextAlign() { - internal = ordinal(); - } -} diff --git a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java index 8a29b50c2..a880a7a99 100644 --- a/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java +++ b/bindings/java/java/src/test/java/com/dioxuslabs/taffy/RoundingTests.java @@ -1,8 +1,8 @@ package com.dioxuslabs.taffy; +import com.dioxuslabs.taffy.enums.AlignContent; import com.dioxuslabs.taffy.geom.Size; import com.dioxuslabs.taffy.geom.measure.Dimension; -import com.dioxuslabs.taffy.style.AlignContent; import com.dioxuslabs.taffy.style.Style; import com.dioxuslabs.taffy.tree.Layout; import org.junit.jupiter.api.Test; diff --git a/bindings/scripts/src/main.rs b/bindings/scripts/src/main.rs index 02d5b98cb..ec9f50b03 100644 --- a/bindings/scripts/src/main.rs +++ b/bindings/scripts/src/main.rs @@ -14,6 +14,11 @@ fn main() { enums.insert("GridAutoFlow", vec!["Row", "Column", "RowDense", "ColumnDense"]); enums.insert("FlexWrap", vec!["NoWrap", "Wrap", "WrapReverse"]); enums.insert("FlexDirection", vec!["Row", "Column", "RowReverse", "ColumnReverse"]); + enums.insert( + "AlignContent", + vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Stretch", "SpaceBetween", "SpaceEvenly", "SpaceAround"], + ); + enums.insert("AlignItems", vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Baseline", "Stretch"]); for (key, value) in enums.into_iter() { create_enum(key, value); From 1fd2c5b7e27c111c01f72abb2cbf32c3ce5d23df Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 27 Aug 2024 12:33:43 +0200 Subject: [PATCH 16/32] enums: update refs --- bindings/java/src/enums.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index fca834b14..14af15429 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -24,7 +24,7 @@ pub fn get_overflow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local> #[allow(dead_code)] pub fn f_get_overflow<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Overflow { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/Overflow;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/Overflow;"); get_overflow(env, obj) } @@ -44,7 +44,7 @@ pub fn get_position<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local> } pub fn f_get_position<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Position { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/Position;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/Position;"); get_position(env, obj) } @@ -70,7 +70,7 @@ pub fn get_text_align<'local>( } pub fn f_get_text_align<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> TextAlign { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/TextAlign;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/TextAlign;"); get_text_align(env, obj, TextAlign::default) } @@ -96,7 +96,7 @@ pub fn get_flex_direction<'local>( } pub fn f_get_flex_direction<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> FlexDirection { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/FlexDirection;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/FlexDirection;"); get_flex_direction(env, obj, FlexDirection::default) } @@ -117,7 +117,7 @@ pub fn get_flex_wrap<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local } pub fn f_get_flex_wrap<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> FlexWrap { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/FlexWrap;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/FlexWrap;"); get_flex_wrap(env, obj, FlexWrap::default) } @@ -143,7 +143,7 @@ pub fn get_grid_auto_flow<'local>( } pub fn f_get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> GridAutoFlow { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/GridAutoFlow;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"); get_grid_auto_flow(env, obj, GridAutoFlow::default) } @@ -168,7 +168,7 @@ pub fn get_align_items<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loc } pub fn f_get_align_items<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Option { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/AlignItems;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/AlignItems;"); get_align_items(env, obj) } @@ -199,7 +199,7 @@ pub fn f_get_align_content<'local>( base: &JObject<'local>, field: &str, ) -> Option { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/AlignContent;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/AlignContent;"); get_align_content(env, obj) } @@ -221,7 +221,7 @@ pub fn get_display<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) } pub fn f_get_display<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Display { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/Display;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/Display;"); get_display(env, obj) } @@ -241,7 +241,7 @@ pub fn get_box_sizing<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'loca } pub fn f_get_box_sizing<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> BoxSizing { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/style/BoxSizing;"); + let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/BoxSizing;"); get_box_sizing(env, obj) } From 8bf25f1a3317c457e4441421ae0e2528856d257f Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Thu, 29 Aug 2024 01:11:39 +0200 Subject: [PATCH 17/32] java: readded first example for reference --- .../java/com/dioxuslabs/taffy/Basic.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 bindings/java/java/examples/java/com/dioxuslabs/taffy/Basic.java diff --git a/bindings/java/java/examples/java/com/dioxuslabs/taffy/Basic.java b/bindings/java/java/examples/java/com/dioxuslabs/taffy/Basic.java new file mode 100644 index 000000000..24cf0d65f --- /dev/null +++ b/bindings/java/java/examples/java/com/dioxuslabs/taffy/Basic.java @@ -0,0 +1,40 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.enums.AlignContent; +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.Dimension; +import com.dioxuslabs.taffy.style.Style; + +import java.util.List; + +/** + * Equivalent of taffy/examples/basic.rs + */ +public class Basic { + public static void main(String[] args) { + TaffyTree taffy = new TaffyTree(); + + long child = taffy.newLeaf( + Style.builder().size(new Size<>(Dimension.percent(0.5f), Dimension.auto())) + ); + + long node = taffy.newWithChildren( + Style.builder().size(Size.length(Dimension.class, 100, 100)) + .justifyContent(AlignContent.CENTER), + List.of(child) + ); + + System.out.println("Compute layout with 100x100 viewport:"); + taffy.computeLayout( + node, + Size.definiteAvailableSize(100, 100) + ); + System.out.println("node: " + taffy.layout(node).toString()); + System.out.println("child: " + taffy.layout(child).toString()); + + System.out.println("Compute layout with undefined (infinite) viewport:"); + taffy.computeLayout(node, Size.maxContentAvailableSize()); + System.out.println("node: " + taffy.layout(node).toString()); + System.out.println("child: " + taffy.layout(child).toString()); + } +} From 5bae96d4905322f6a35ac2716a17730af33c77d6 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Thu, 29 Aug 2024 01:43:50 +0200 Subject: [PATCH 18/32] java: add nested and somewhat tweak toString's --- .../java/com/dioxuslabs/taffy/Nested.java | 53 +++++++++++++++++++ .../java/com/dioxuslabs/taffy/geom/Point.java | 10 ++++ .../java/com/dioxuslabs/taffy/geom/Rect.java | 16 +++++- .../java/com/dioxuslabs/taffy/geom/Size.java | 52 ++++++++++++------ .../com/dioxuslabs/taffy/tree/Layout.java | 40 +++++++++----- 5 files changed, 142 insertions(+), 29 deletions(-) create mode 100644 bindings/java/java/examples/java/com/dioxuslabs/taffy/Nested.java diff --git a/bindings/java/java/examples/java/com/dioxuslabs/taffy/Nested.java b/bindings/java/java/examples/java/com/dioxuslabs/taffy/Nested.java new file mode 100644 index 000000000..a0a04e2f3 --- /dev/null +++ b/bindings/java/java/examples/java/com/dioxuslabs/taffy/Nested.java @@ -0,0 +1,53 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.Dimension; +import com.dioxuslabs.taffy.style.Style; + +import java.util.List; + +/** + * Equivalent of taffy/examples/nested.rs + */ +public class Nested { + public static void main(String[] args) { + TaffyTree taffy = new TaffyTree(); + + // left + long childT1 = taffy.newLeaf( + Style.builder().size(Size.lengthDimension(5, 5)) + ); + + long div1 = taffy.newWithChildren( + Style.builder().size(new Size<>(Dimension.percent(0.5f), Dimension.percent(1f))), + List.of(childT1) + ); + + // right + long childT2 = taffy.newLeaf( + Style.builder().size(Size.lengthDimension(5, 5)) + ); + + long div2 = taffy.newWithChildren( + Style.builder().size(new Size<>(Dimension.percent(0.5f), Dimension.percent(1f))), + List.of(childT2) + ); + + long container = taffy.newWithChildren( + Style.builder().size(Size.percent(Dimension.class, 1, 1)), + List.of(div1, div2) + ); + + taffy.computeLayout( + container, + Size.definiteAvailableSize(100, 100) + ); + System.out.println("node: " + taffy.layout(container).toString()); + + System.out.println("div1: " + taffy.layout(div1).toString()); + System.out.println("div2: " + taffy.layout(div2).toString()); + + System.out.println("child1: " + taffy.layout(childT1).toString()); + System.out.println("child: " + taffy.layout(childT2).toString()); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Point.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Point.java index 027be6396..38ed05b5f 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Point.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Point.java @@ -35,4 +35,14 @@ public void x(T x) { public void y(T y) { this.y = y; } + + @Override + public String toString() { + return String.format(""" + Point { + x: %s, + y: %s, + }""", + x, y); + } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Rect.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Rect.java index 6fc287047..3106c1c0c 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Rect.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Rect.java @@ -3,10 +3,10 @@ /** * An axis-aligned UI rectangle */ -public class Rect{ +public class Rect { /** * This can represent either the x-coordinate of the starting edge, - * or the amount of padding on the starting side. + * or the amount of padding on the starting side. *

* The starting edge is the left edge when working with LTR text, * and the right edge when working with RTL text. @@ -66,4 +66,16 @@ public void top(T top) { public void bottom(T bottom) { this.bottom = bottom; } + + @Override + public String toString() { + return String.format(""" + Rect { + left: %s, + right: %s, + top: %s, + bottom: %s, + }""", + left, right, top, bottom); + } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java index 4c48dd23a..0baaa8a93 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java @@ -7,7 +7,8 @@ /** * The width and height of a {@link Rect} - * @param width The x extent of the rectangle + * + * @param width The x extent of the rectangle * @param height The y extent of the rectangle */ public record Size( @@ -17,11 +18,11 @@ public record Size( /** * Dynamic function to create a {@link Size} with a fixed width and height * - * @param clazz The type of the {@link Size} - * @param width The width + * @param clazz The type of the {@link Size} + * @param width The width * @param height The height + * @param The type between {@link Dimension}, {@link AvailableSpace}, {@link LengthPercentage} and {@link LengthPercentageAuto} * @return A new {@link Size} with the given width and height - * @param The type between {@link Dimension}, {@link AvailableSpace}, {@link LengthPercentage} and {@link LengthPercentageAuto} */ @SuppressWarnings("unchecked") public static Size length(Class clazz, float width, float height) { @@ -40,11 +41,11 @@ public static Size length(Class clazz, float width, float height) { /** * Dynamic function to create a {@link Size} with a percentage width and height * - * @param clazz The type of the {@link Size} - * @param width The width + * @param clazz The type of the {@link Size} + * @param width The width * @param height The height + * @param The type between {@link Dimension}, {@link LengthPercentage} and {@link LengthPercentageAuto} * @return A new {@link Size} with the given width and height - * @param The type between {@link Dimension}, {@link LengthPercentage} and {@link LengthPercentageAuto} */ @SuppressWarnings("unchecked") public static Size percent(Class clazz, float width, float height) { @@ -62,8 +63,8 @@ public static Size percent(Class clazz, float width, float height) { * Dynamic function to create a {@link Size} with auto as values * * @param clazz The type of the {@link Size} + * @param The type between {@link Dimension} and {@link LengthPercentageAuto} * @return A new {@link Size} with auto as width and height - * @param The type between {@link Dimension} and {@link LengthPercentageAuto} */ @SuppressWarnings("unchecked") public static Size auto(Class clazz) { @@ -77,7 +78,8 @@ public static Size auto(Class clazz) { /** * Creates a {@link LengthPercentageAuto} {@link Size} with a fixed width and height - * @param width The width + * + * @param width The width * @param height The height * @return A new {@link Size} with the given width and height */ @@ -87,7 +89,8 @@ public static Size lengthLengthPercentageAuto(float width, /** * Creates a {@link LengthPercentageAuto} {@link Size} with a percentage width and height - * @param width The width + * + * @param width The width * @param height The height * @return A new {@link Size} with the given width and height */ @@ -97,6 +100,7 @@ public static Size percentLengthPercentageAuto(float width /** * Creates a {@link LengthPercentageAuto} {@link Size} with auto as width and height + * * @return A new {@link Size} with auto as width and height */ public static Size autoLengthPercentageAuto() { @@ -105,7 +109,8 @@ public static Size autoLengthPercentageAuto() { /** * Creates a {@link LengthPercentage} {@link Size} with a fixed width and height - * @param width The width + * + * @param width The width * @param height The height * @return A new {@link Size} with the given width and height */ @@ -115,7 +120,8 @@ public static Size lengthLengthPercentage(float width, float h /** * Creates a {@link LengthPercentage} {@link Size} with a percentage width and height - * @param width The width + * + * @param width The width * @param height The height * @return A new {@link Size} with the given width and height */ @@ -125,7 +131,8 @@ public static Size percentLengthPercentage(float width, float /** * Creates a {@link Dimension} {@link Size} with a fixed width and height - * @param width The width + * + * @param width The width * @param height The height * @return A new {@link Size} with the given width and height */ @@ -135,7 +142,8 @@ public static Size lengthDimension(float width, float height) { /** * Creates a {@link Dimension} {@link Size} with a fixed width and height - * @param width The width + * + * @param width The width * @param height The height * @return A new {@link Size} with the given width and height */ @@ -145,6 +153,7 @@ public static Size percentDimension(float width, float height) { /** * Creates a {@link Dimension} {@link Size} with auto as width and height + * * @return A new {@link Size} with auto as width and height */ public static Size autoDimension() { @@ -153,7 +162,8 @@ public static Size autoDimension() { /** * Creates a {@link AvailableSpace} {@link Size} with a fixed width and height - * @param width The width + * + * @param width The width * @param height The height * @return A new {@link Size} with the given width and height */ @@ -163,6 +173,7 @@ public static Size definiteAvailableSize(float width, float heig /** * Creates a {@link Dimension} {@link Size} with MinContent as width and height + * * @return A new {@link Size} with MinContent as width and height */ public static Size minContentAvailableSize() { @@ -171,9 +182,20 @@ public static Size minContentAvailableSize() { /** * Creates a {@link Dimension} {@link Size} with MaxContent as width and height + * * @return A new {@link Size} with MaxContent as width and height */ public static Size maxContentAvailableSize() { return new Size<>(AvailableSpace.maxContent(), AvailableSpace.maxContent()); } + + @Override + public String toString() { + return String.format(""" + Size { + width: %s, + height: %s, + }""", + width, height); + } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/Layout.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/Layout.java index 4665722a6..9977ce2ab 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/Layout.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/tree/Layout.java @@ -6,20 +6,21 @@ /** * The final result of a layout algorithm for a single node. - * @param order The relative ordering of the node - *

- * Nodes with a higher order should be rendered on top of those with a lower order. This is - * effectively a topological sort of each tree. - * @param location The top-left corner of the node - * @param size The width and height of the node - * @param contentSize The width and height of the content inside the node. This may be larger than the size - * of the node in the case of overflowing content and is useful for computing a "scroll - * width/height" for scrollable nodes + * + * @param order The relative ordering of the node + *

+ * Nodes with a higher order should be rendered on top of those with a lower order. This is + * effectively a topological sort of each tree. + * @param location The top-left corner of the node + * @param size The width and height of the node + * @param contentSize The width and height of the content inside the node. This may be larger than the size + * of the node in the case of overflowing content and is useful for computing a "scroll + * width/height" for scrollable nodes * @param scrollbarSize The size of the scrollbars in each dimension. If there is no scrollbar then the * size will be zero. - * @param border The size of the borders of the node - * @param padding The size of the padding of the node - * @param margin The size of the margin of the node + * @param border The size of the borders of the node + * @param padding The size of the padding of the node + * @param margin The size of the margin of the node */ public record Layout( int order, @@ -31,4 +32,19 @@ public record Layout( Rect padding, Rect margin ) { + @Override + public String toString() { + return String.format(""" + Layout { + order: %s, + location: %s, + size: %s; + content_size: %s, + scrollbar_size: %s, + border: %s, + padding: %s, + margin: %s, + }""", + order, location, size, contentSize, scrollbarSize, border, padding, margin); + } } From 9b412c8e069d043e243a33abea451913fe2292d5 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Thu, 29 Aug 2024 01:55:32 +0200 Subject: [PATCH 19/32] java: gap & related util --- .../java/com/dioxuslabs/taffy/FlexboxGap.java | 32 +++++++++++++++++++ .../taffy/geom/measure/LengthPercentage.java | 4 +++ 2 files changed, 36 insertions(+) create mode 100644 bindings/java/java/examples/java/com/dioxuslabs/taffy/FlexboxGap.java diff --git a/bindings/java/java/examples/java/com/dioxuslabs/taffy/FlexboxGap.java b/bindings/java/java/examples/java/com/dioxuslabs/taffy/FlexboxGap.java new file mode 100644 index 000000000..afc146ca6 --- /dev/null +++ b/bindings/java/java/examples/java/com/dioxuslabs/taffy/FlexboxGap.java @@ -0,0 +1,32 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.LengthPercentage; +import com.dioxuslabs.taffy.style.Style; + +import java.util.List; + +/** + * Equivalent of taffy/examples/flexbox_gap.rs + */ +public class FlexboxGap { + public static void main(String[] args) { + TaffyTree taffy = new TaffyTree(); + + Style childStyle = Style.builder() + .size(Size.lengthDimension(20, 20)); + long child0 = taffy.newLeaf(childStyle); + long child1 = taffy.newLeaf(childStyle); + long child2 = taffy.newLeaf(childStyle); + + long root = taffy.newWithChildren( + Style.builder() + .gap(new Size<>(LengthPercentage.length(10), LengthPercentage.zero())), + List.of(child0, child1, child2) + ); + + // Compute layout and print result + taffy.computeLayout(root, Size.maxContentAvailableSize()); + taffy.printTree(root); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentage.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentage.java index b6504547a..b56469dac 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentage.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/LengthPercentage.java @@ -17,6 +17,10 @@ public static LengthPercentage percent(float value) { return new LengthPercentage((byte) 1, value); } + public static LengthPercentage zero() { + return length(0); + } + @Override public boolean equals(Object obj) { if (!(obj instanceof LengthPercentage lpo)) { From cbdb98ce0705f1b9b20ae7c6c7adc901bc17c1d4 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Thu, 29 Aug 2024 01:56:06 +0200 Subject: [PATCH 20/32] rust: fix enum field stuff --- bindings/java/src/enums.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index 14af15429..9d8ff7766 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -247,5 +247,5 @@ pub fn f_get_box_sizing<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local> } fn get_enum_value<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>) -> i32 { - f_i32_from_primitive(env, object, "internal", || 0) + f_i32_from_primitive(env, object, "ordinal", || 0) } From 114a279b35eaf455add5c0f4ac1e6a9effb4447f Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Thu, 29 Aug 2024 02:26:08 +0200 Subject: [PATCH 21/32] rust/ java: wip - more complex examples --- .../com/dioxuslabs/taffy/common/Image.java | 26 ++++++++++ .../com/dioxuslabs/taffy/common/Text.java | 52 +++++++++++++++++++ .../dioxuslabs/taffy/enums/AbsoluteAxis.java | 19 +++++++ .../java/com/dioxuslabs/taffy/geom/Size.java | 16 ++++++ bindings/scripts/src/main.rs | 1 + 5 files changed, 114 insertions(+) create mode 100644 bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Image.java create mode 100644 bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Text.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AbsoluteAxis.java diff --git a/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Image.java b/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Image.java new file mode 100644 index 000000000..a8e673c0c --- /dev/null +++ b/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Image.java @@ -0,0 +1,26 @@ +package com.dioxuslabs.taffy.common; + +import com.dioxuslabs.taffy.geom.Size; + +public class Image { + public record ImageContext( + float width, + float height + ) { + } + + public static Size imageMeasureFunction(Size knownDimensions, ImageContext imageContext) { + Float width = knownDimensions.width(); + Float height = knownDimensions.height(); + + if (width != null && height != null) { + return new Size<>(width, height); + } else if (width != null) { + return new Size<>(width, (width / imageContext.width) * imageContext.height); + } else if (height != null) { + return new Size<>((height / imageContext.height) * imageContext.width, height); + } else { + return new Size<>(imageContext.width, imageContext.height); + } + } +} diff --git a/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Text.java b/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Text.java new file mode 100644 index 000000000..321a1aeae --- /dev/null +++ b/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Text.java @@ -0,0 +1,52 @@ +package com.dioxuslabs.taffy.common; + +import com.dioxuslabs.taffy.enums.AbsoluteAxis; +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.AvailableSpace; + +import java.util.Arrays; +import java.util.concurrent.atomic.AtomicInteger; + +public class Text { + public static String LOREM_IPSUM = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."; + + public record FontMetrics( + float charWidth, + float charHeight + ) { + } + + public enum WritingMode { + HORIZONTAL, + VERTICAL + } + + public record TextContext( + String textContent, + WritingMode writingMode + ) { + } + + public static Size textMeasureFunction( + Size knownDimensions, + Size availableSpace, + TextContext textContext, + FontMetrics fontMetrics + ) { + AbsoluteAxis inlineAxis = textContext.writingMode == WritingMode.HORIZONTAL ? AbsoluteAxis.HORIZONTAL : AbsoluteAxis.VERTICAL; + AbsoluteAxis blockAxis = inlineAxis == AbsoluteAxis.HORIZONTAL ? AbsoluteAxis.VERTICAL : AbsoluteAxis.HORIZONTAL; + + String[] words = textContext.textContent.split(" "); + + if (words.length == 0) { + return Size.zero(Float.class); + } + + int minLineLength = Arrays.stream(words).map(String::length).max(Integer::compareTo).orElse(0); + AtomicInteger maxLineLength = new AtomicInteger(); + Arrays.stream(words).forEach(line -> maxLineLength.addAndGet(line.length())); + + float inlineSize = + knownDimensions.getAbs(inlineAxis) + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AbsoluteAxis.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AbsoluteAxis.java new file mode 100644 index 000000000..1da764d52 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/AbsoluteAxis.java @@ -0,0 +1,19 @@ +/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +package com.dioxuslabs.taffy.enums; + +public enum AbsoluteAxis { + HORIZONTAL, + VERTICAL + ; + + private final int ordinal; + + AbsoluteAxis() { + this.ordinal = ordinal(); + } +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java index 0baaa8a93..8dee0814b 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java @@ -1,5 +1,6 @@ package com.dioxuslabs.taffy.geom; +import com.dioxuslabs.taffy.enums.AbsoluteAxis; import com.dioxuslabs.taffy.geom.measure.AvailableSpace; import com.dioxuslabs.taffy.geom.measure.Dimension; import com.dioxuslabs.taffy.geom.measure.LengthPercentage; @@ -38,6 +39,17 @@ public static Size length(Class clazz, float width, float height) { return null; } + /** + * Dynamic function to create a {@link Size} with fixed zero width and height + * + * @param clazz The type of the {@link Size} + * @param The type between {@link Dimension}, {@link AvailableSpace}, {@link LengthPercentage} and {@link LengthPercentageAuto} + * @return A new {@link Size} with the given width and height + */ + public static Size zero(Class clazz) { + return length(clazz, 0, 0); + } + /** * Dynamic function to create a {@link Size} with a percentage width and height * @@ -198,4 +210,8 @@ public String toString() { }""", width, height); } + + public T getAbs(AbsoluteAxis axis) { + return axis == AbsoluteAxis.HORIZONTAL ? width : height; + } } diff --git a/bindings/scripts/src/main.rs b/bindings/scripts/src/main.rs index ec9f50b03..37dc6e0c4 100644 --- a/bindings/scripts/src/main.rs +++ b/bindings/scripts/src/main.rs @@ -19,6 +19,7 @@ fn main() { vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Stretch", "SpaceBetween", "SpaceEvenly", "SpaceAround"], ); enums.insert("AlignItems", vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Baseline", "Stretch"]); + enums.insert("AbsoluteAxis", vec!["Horizontal", "Vertical"]); for (key, value) in enums.into_iter() { create_enum(key, value); From 560fd19608362f6250d3f9bbe7686d87cfdab3ea Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Mon, 2 Sep 2024 23:29:23 +0200 Subject: [PATCH 22/32] genenums: move to to scripts folder --- Cargo.toml | 2 +- {bindings/scripts => scripts/genenums}/Cargo.toml | 2 +- {bindings/scripts => scripts/genenums}/src/main.rs | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename {bindings/scripts => scripts/genenums}/Cargo.toml (92%) rename {bindings/scripts => scripts/genenums}/src/main.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index e78b866fa..d51a3198a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -103,5 +103,5 @@ members = [ "benches", "taffy_stylo", "bindings/java", - "bindings/scripts" + "scripts/genenums" ] diff --git a/bindings/scripts/Cargo.toml b/scripts/genenums/Cargo.toml similarity index 92% rename from bindings/scripts/Cargo.toml rename to scripts/genenums/Cargo.toml index b5400c23b..85f2e4fe8 100644 --- a/bindings/scripts/Cargo.toml +++ b/scripts/genenums/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "bindings_scripts" +name = "genenums" version = "0.0.1" authors = [ "Arby Djabaev " diff --git a/bindings/scripts/src/main.rs b/scripts/genenums/src/main.rs similarity index 100% rename from bindings/scripts/src/main.rs rename to scripts/genenums/src/main.rs From 9db2b69796f40945918af91d38abb0c9dc97c723 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 00:46:42 +0200 Subject: [PATCH 23/32] genenums: auto generate "transformers" --- bindings/java/src/enums.rs | 341 +++++++++++++------------------- bindings/java/src/lib.rs | 1 + bindings/java/src/primitives.rs | 30 +++ bindings/java/src/traits.rs | 4 + scripts/genenums/src/main.rs | 61 +++++- 5 files changed, 226 insertions(+), 211 deletions(-) create mode 100644 bindings/java/src/traits.rs diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index 9d8ff7766..b576783ad 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -1,251 +1,180 @@ -use crate::conversions::f_get_value; -use crate::primitives::f_i32_from_primitive; -use jni::objects::{JObject, JValueOwned}; -use jni::JNIEnv; -use taffy::{ - AlignContent, AlignItems, BoxSizing, Display, FlexDirection, FlexWrap, GridAutoFlow, Overflow, Position, TextAlign, -}; - -pub fn get_overflow<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Overflow { - let obj = &value.l().unwrap(); - if obj.is_null() { - return Overflow::default(); - } +use taffy::TextAlign; +use taffy::BoxSizing; +use taffy::AlignContent; +use taffy::GridAutoFlow; +use taffy::FlexDirection; +use taffy::AbsoluteAxis; +use taffy::AlignItems; +use taffy::FlexWrap; +use taffy::BoxGenerationMode; +use taffy::Display; +use taffy::Overflow; +use taffy::Position; +use crate::traits::FromJavaEnum; - let internal = get_enum_value(env, obj); +impl FromJavaEnum for Position { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Position;"; - match internal { - 0 => Overflow::Visible, - 1 => Overflow::Clip, - 2 => Overflow::Hidden, - _ => Overflow::Scroll, + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => Position::Relative, + 1 => Position::Absolute, + _ => panic!("Invalid value: {internal}"), + }) } } -#[allow(dead_code)] -pub fn f_get_overflow<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Overflow { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/Overflow;"); - - get_overflow(env, obj) -} - -pub fn get_position<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Position { - let obj = &value.l().unwrap(); - if obj.is_null() { - return Position::default(); - } - - let internal = get_enum_value(env, obj); +impl FromJavaEnum for Overflow { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Overflow;"; - match internal { - 0 => Position::Relative, - _ => Position::Absolute, + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => Overflow::Visible, + 1 => Overflow::Clip, + 2 => Overflow::Hidden, + 3 => Overflow::Scroll, + _ => panic!("Invalid value: {internal}"), + }) } } -pub fn f_get_position<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Position { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/Position;"); +impl FromJavaEnum for Display { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Display;"; - get_position(env, obj) -} - -pub fn get_text_align<'local>( - env: &mut JNIEnv<'local>, - value: JValueOwned<'local>, - def: fn() -> TextAlign, -) -> TextAlign { - let obj = &value.l().unwrap(); - if obj.is_null() { - return def(); - } - - let internal = get_enum_value(env, obj); - - match internal { - 0 => TextAlign::Auto, - 1 => TextAlign::LegacyLeft, - 2 => TextAlign::LegacyRight, - _ => TextAlign::LegacyCenter, + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => Display::Block, + 1 => Display::Flex, + 2 => Display::Grid, + 3 => Display::None, + _ => panic!("Invalid value: {internal}"), + }) } } -pub fn f_get_text_align<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> TextAlign { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/TextAlign;"); - - get_text_align(env, obj, TextAlign::default) -} - -pub fn get_flex_direction<'local>( - env: &mut JNIEnv<'local>, - value: JValueOwned<'local>, - def: fn() -> FlexDirection, -) -> FlexDirection { - let obj = &value.l().unwrap(); - if obj.is_null() { - return def(); - } - - let internal = get_enum_value(env, obj); +impl FromJavaEnum for BoxGenerationMode { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; - match internal { - 0 => FlexDirection::Row, - 1 => FlexDirection::Column, - 2 => FlexDirection::RowReverse, - _ => FlexDirection::ColumnReverse, + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => BoxGenerationMode::Normal, + 1 => BoxGenerationMode::None, + _ => panic!("Invalid value: {internal}"), + }) } } -pub fn f_get_flex_direction<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> FlexDirection { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/FlexDirection;"); - - get_flex_direction(env, obj, FlexDirection::default) -} - -pub fn get_flex_wrap<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>, def: fn() -> FlexWrap) -> FlexWrap { - let obj = &value.l().unwrap(); - if obj.is_null() { - return def(); - } - - let internal = get_enum_value(env, obj); +impl FromJavaEnum for FlexWrap { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexWrap;"; - match internal { - 0 => FlexWrap::NoWrap, - 1 => FlexWrap::Wrap, - _ => FlexWrap::WrapReverse, + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => FlexWrap::NoWrap, + 1 => FlexWrap::Wrap, + 2 => FlexWrap::WrapReverse, + _ => panic!("Invalid value: {internal}"), + }) } } -pub fn f_get_flex_wrap<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> FlexWrap { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/FlexWrap;"); +impl FromJavaEnum for AlignItems { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignItems;"; - get_flex_wrap(env, obj, FlexWrap::default) -} - -pub fn get_grid_auto_flow<'local>( - env: &mut JNIEnv<'local>, - value: JValueOwned<'local>, - def: fn() -> GridAutoFlow, -) -> GridAutoFlow { - let obj = &value.l().unwrap(); - if obj.is_null() { - return def(); + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => AlignItems::Start, + 1 => AlignItems::End, + 2 => AlignItems::FlexStart, + 3 => AlignItems::FlexEnd, + 4 => AlignItems::Center, + 5 => AlignItems::Baseline, + 6 => AlignItems::Stretch, + _ => panic!("Invalid value: {internal}"), + }) } - - let internal = get_enum_value(env, obj); - - match internal { - 0 => GridAutoFlow::Row, - 1 => GridAutoFlow::Column, - 2 => GridAutoFlow::RowDense, - _ => GridAutoFlow::ColumnDense, - } -} - -pub fn f_get_grid_auto_flow<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> GridAutoFlow { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"); - - get_grid_auto_flow(env, obj, GridAutoFlow::default) } -pub fn get_align_items<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Option { - let obj = &value.l().unwrap(); - if obj.is_null() { - return None; - } - - let internal = get_enum_value(env, obj); +impl FromJavaEnum for AbsoluteAxis { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AbsoluteAxis;"; - match internal { - 0 => Some(AlignItems::Start), - 1 => Some(AlignItems::End), - 2 => Some(AlignItems::FlexStart), - 3 => Some(AlignItems::FlexEnd), - 4 => Some(AlignItems::Center), - 5 => Some(AlignItems::Baseline), - _ => Some(AlignItems::Stretch), + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => AbsoluteAxis::Horizontal, + 1 => AbsoluteAxis::Vertical, + _ => panic!("Invalid value: {internal}"), + }) } } -pub fn f_get_align_items<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Option { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/AlignItems;"); - - get_align_items(env, obj) -} - -pub fn get_align_content<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Option { - let obj = &value.l().unwrap(); - if obj.is_null() { - return None; - } +impl FromJavaEnum for FlexDirection { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexDirection;"; - let internal = get_enum_value(env, obj); - - match internal { - 0 => Some(AlignContent::Start), - 1 => Some(AlignContent::End), - 2 => Some(AlignContent::FlexStart), - 3 => Some(AlignContent::FlexEnd), - 4 => Some(AlignContent::Center), - 5 => Some(AlignContent::Stretch), - 6 => Some(AlignContent::SpaceBetween), - 7 => Some(AlignContent::SpaceEvenly), - _ => Some(AlignContent::SpaceAround), + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => FlexDirection::Row, + 1 => FlexDirection::Column, + 2 => FlexDirection::RowReverse, + 3 => FlexDirection::ColumnReverse, + _ => panic!("Invalid value: {internal}"), + }) } } -pub fn f_get_align_content<'local>( - env: &mut JNIEnv<'local>, - base: &JObject<'local>, - field: &str, -) -> Option { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/AlignContent;"); +impl FromJavaEnum for GridAutoFlow { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"; - get_align_content(env, obj) -} - -pub fn get_display<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> Display { - let obj = &value.l().unwrap(); - if obj.is_null() { - return Display::default(); - } - - let internal = get_enum_value(env, obj); - - match internal { - 0 => Display::Block, - 1 => Display::Flex, - 2 => Display::Grid, - _ => Display::None, + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => GridAutoFlow::Row, + 1 => GridAutoFlow::Column, + 2 => GridAutoFlow::RowDense, + 3 => GridAutoFlow::ColumnDense, + _ => panic!("Invalid value: {internal}"), + }) } } -pub fn f_get_display<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> Display { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/Display;"); - - get_display(env, obj) -} +impl FromJavaEnum for AlignContent { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; -pub fn get_box_sizing<'local>(env: &mut JNIEnv<'local>, value: JValueOwned<'local>) -> BoxSizing { - let obj = &value.l().unwrap(); - if obj.is_null() { - return BoxSizing::default(); + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => AlignContent::Start, + 1 => AlignContent::End, + 2 => AlignContent::FlexStart, + 3 => AlignContent::FlexEnd, + 4 => AlignContent::Center, + 5 => AlignContent::Stretch, + 6 => AlignContent::SpaceBetween, + 7 => AlignContent::SpaceEvenly, + 8 => AlignContent::SpaceAround, + _ => panic!("Invalid value: {internal}"), + }) } +} - let internal = get_enum_value(env, obj); +impl FromJavaEnum for BoxSizing { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; - match internal { - 0 => BoxSizing::BorderBox, - _ => BoxSizing::ContentBox, + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => BoxSizing::BorderBox, + 1 => BoxSizing::ContentBox, + _ => panic!("Invalid value: {internal}"), + }) } } -pub fn f_get_box_sizing<'local>(env: &mut JNIEnv<'local>, base: &JObject<'local>, field: &str) -> BoxSizing { - let obj = f_get_value(env, base, field, "Lcom/dioxuslabs/taffy/enums/BoxSizing;"); - - get_box_sizing(env, obj) -} +impl FromJavaEnum for TextAlign { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; -fn get_enum_value<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>) -> i32 { - f_i32_from_primitive(env, object, "ordinal", || 0) -} + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => TextAlign::Auto, + 1 => TextAlign::LegacyLeft, + 2 => TextAlign::LegacyRight, + 3 => TextAlign::LegacyCenter, + _ => panic!("Invalid value: {internal}"), + }) + } +} \ No newline at end of file diff --git a/bindings/java/src/lib.rs b/bindings/java/src/lib.rs index 932ef0588..b8d1c2c0c 100644 --- a/bindings/java/src/lib.rs +++ b/bindings/java/src/lib.rs @@ -5,6 +5,7 @@ mod geom; mod java; mod measure; mod primitives; +mod traits; use crate::collections::get_list; use crate::conversions::get_style; diff --git a/bindings/java/src/primitives.rs b/bindings/java/src/primitives.rs index 9f604a97a..926a325bd 100644 --- a/bindings/java/src/primitives.rs +++ b/bindings/java/src/primitives.rs @@ -1,4 +1,5 @@ use crate::conversions::f_get_value; +use crate::traits::FromJavaEnum; use jni::objects::{JObject, JValueOwned}; use jni::JNIEnv; use taffy::NodeId; @@ -254,3 +255,32 @@ pub fn f_bool_from_primitive<'local>( bool_from_primitive(env, value, def) } + +/// Enums are here as these are basically represented by integers which are primitives + +/// Get enum value T from a JValueOwned (as well as a JNIEnv which is required by JNI) +pub fn get_enum(env: &mut JNIEnv, value: JValueOwned) -> T { + let obj = &value.l().unwrap(); + if obj.is_null() { + return T::default(); + } + + let internal = get_enum_value(env, obj); + T::from_ordinal(internal) +} + +/// Get enum value T from a JObject and a field name (as well as a JNIEnv which is required by JNI) +pub fn f_get_enum<'local, T: FromJavaEnum + Default>( + env: &mut JNIEnv<'local>, + base: &JObject<'local>, + field: &str, +) -> T { + let obj = f_get_value(env, base, field, T::JAVA_CLASS); + + get_enum(env, obj) +} + +/// Internal method that gets the ordinal int +fn get_enum_value<'local>(env: &mut JNIEnv<'local>, object: &JObject<'local>) -> i32 { + f_i32_from_primitive(env, object, "ordinal", || 0) +} diff --git a/bindings/java/src/traits.rs b/bindings/java/src/traits.rs new file mode 100644 index 000000000..18da1d6a7 --- /dev/null +++ b/bindings/java/src/traits.rs @@ -0,0 +1,4 @@ +pub trait FromJavaEnum { + const JAVA_CLASS : &'static str; + fn from_ordinal(ordinal: i32) -> Option; +} \ No newline at end of file diff --git a/scripts/genenums/src/main.rs b/scripts/genenums/src/main.rs index 37dc6e0c4..c70ce003f 100644 --- a/scripts/genenums/src/main.rs +++ b/scripts/genenums/src/main.rs @@ -2,8 +2,12 @@ use std::collections::HashMap; use std::fs; use std::fs::File; use std::io::Write; +use std::path::Path; fn main() { + fs::remove_file("./bindings/java/src/enums.rs") + .expect("Error: Unable to remove java/src/enums.rs file"); + let mut enums: HashMap<&str, Vec<&str>> = HashMap::new(); enums.insert("Display", vec!["Block", "Flex", "Grid", "None"]); enums.insert("BoxGenerationMode", vec!["Normal", "None"]); @@ -22,15 +26,18 @@ fn main() { enums.insert("AbsoluteAxis", vec!["Horizontal", "Vertical"]); for (key, value) in enums.into_iter() { - create_enum(key, value); + create_enum(key, &value); + create_transformer(key, &value); } } -fn create_enum(name: &str, values: Vec<&str>) { +/// Enum generators + +fn create_enum(name: &str, values: &[&str]) { create_java_enum(name, values); } -fn create_java_enum(name: &str, values: Vec<&str>) { +fn create_java_enum(name: &str, values: &[&str]) { use convert_case::{Case, Casing}; let package = "com.dioxuslabs.taffy.enums"; @@ -41,9 +48,16 @@ fn create_java_enum(name: &str, values: Vec<&str>) { ** AUTOGENERATED CLASSES FOR TAFFY ** ** This code was automatically generated. Do not edit. ** ********************************************************** -*/\n"; +*/ +"; + + let mut result = format!( + r"{}package {}; - let mut result = format!("{}package {};\n\npublic enum {} {{\n", auto_gen_comment, package, enum_name); +public enum {} {{ +", + auto_gen_comment, package, enum_name + ); for value in values.iter() { result.push_str(" "); @@ -78,3 +92,40 @@ fn create_java_enum(name: &str, values: Vec<&str>) { File::create(format!("./bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/{}.java", enum_name)); file.expect("Error: File not found").write_all(result.as_ref()).expect("Error: Couldn't write to file"); } + +/// Transformer generators + +fn create_transformer(name: &str, values: &[&str]) { + create_java_tranformer(name, values); +} + +fn create_java_tranformer(name: &str, values: &[&str]) { + let mut file_content: String = "use crate::traits::FromJavaEnum;".to_string(); + + if Path::new("./bindings/java/src/enums.rs").exists() { + file_content = fs::read_to_string(Path::new("./bindings/java/src/enums.rs")).unwrap() + } + + let mut enum_values: String = "".to_string(); + for (index, value) in values.iter().enumerate() { + enum_values.push_str(format!("\n {index} => {name}::{value},").as_str()); + } + enum_values.push_str("\n _ => panic!(\"Invalid value: {internal}\"),"); + + file_content = format!( + "use taffy::{name}; +{file_content} + +impl FromJavaEnum for {name} {{ + const JAVA_CLASS: &'static str = \"Lcom/dioxuslabs/taffy/enums/{name};\"; + + fn from_ordinal(internal: i32) -> Option<{name}> {{ + Some(match internal {{{enum_values} + }}) + }} +}}" + ); + + let file = File::create("./bindings/java/src/enums.rs"); + file.expect("Error: File not found").write_all(file_content.as_ref()).expect("Error: Couldn't write to file"); +} From 1697115a1c8b6870b6e54ef4f61ac493f0c82b2a Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 00:50:43 +0200 Subject: [PATCH 24/32] genenums: fix generics --- bindings/java/src/enums.rs | 150 +++++++++++++++++------------------ bindings/java/src/traits.rs | 6 +- scripts/genenums/src/main.rs | 7 +- 3 files changed, 81 insertions(+), 82 deletions(-) diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index b576783ad..8fe65e8ed 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -1,30 +1,37 @@ +use taffy::FlexDirection; use taffy::TextAlign; +use taffy::Display; +use taffy::Position; use taffy::BoxSizing; -use taffy::AlignContent; -use taffy::GridAutoFlow; -use taffy::FlexDirection; -use taffy::AbsoluteAxis; +use taffy::BoxGenerationMode; use taffy::AlignItems; +use taffy::AbsoluteAxis; +use taffy::GridAutoFlow; use taffy::FlexWrap; -use taffy::BoxGenerationMode; -use taffy::Display; use taffy::Overflow; -use taffy::Position; +use taffy::AlignContent; use crate::traits::FromJavaEnum; -impl FromJavaEnum for Position { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Position;"; +impl FromJavaEnum for AlignContent { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => Position::Relative, - 1 => Position::Absolute, + 0 => AlignContent::Start, + 1 => AlignContent::End, + 2 => AlignContent::FlexStart, + 3 => AlignContent::FlexEnd, + 4 => AlignContent::Center, + 5 => AlignContent::Stretch, + 6 => AlignContent::SpaceBetween, + 7 => AlignContent::SpaceEvenly, + 8 => AlignContent::SpaceAround, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for Overflow { +impl FromJavaEnum for Overflow { const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Overflow;"; fn from_ordinal(internal: i32) -> Option { @@ -38,46 +45,46 @@ impl FromJavaEnum for Overflow { } } -impl FromJavaEnum for Display { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Display;"; +impl FromJavaEnum for FlexWrap { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexWrap;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => Display::Block, - 1 => Display::Flex, - 2 => Display::Grid, - 3 => Display::None, + 0 => FlexWrap::NoWrap, + 1 => FlexWrap::Wrap, + 2 => FlexWrap::WrapReverse, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for BoxGenerationMode { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; +impl FromJavaEnum for GridAutoFlow { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => BoxGenerationMode::Normal, - 1 => BoxGenerationMode::None, + 0 => GridAutoFlow::Row, + 1 => GridAutoFlow::Column, + 2 => GridAutoFlow::RowDense, + 3 => GridAutoFlow::ColumnDense, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for FlexWrap { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexWrap;"; +impl FromJavaEnum for AbsoluteAxis { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AbsoluteAxis;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => FlexWrap::NoWrap, - 1 => FlexWrap::Wrap, - 2 => FlexWrap::WrapReverse, + 0 => AbsoluteAxis::Horizontal, + 1 => AbsoluteAxis::Vertical, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for AlignItems { +impl FromJavaEnum for AlignItems { const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignItems;"; fn from_ordinal(internal: i32) -> Option { @@ -94,86 +101,79 @@ impl FromJavaEnum for AlignItems { } } -impl FromJavaEnum for AbsoluteAxis { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AbsoluteAxis;"; +impl FromJavaEnum for BoxGenerationMode { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AbsoluteAxis::Horizontal, - 1 => AbsoluteAxis::Vertical, + 0 => BoxGenerationMode::Normal, + 1 => BoxGenerationMode::None, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for FlexDirection { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexDirection;"; +impl FromJavaEnum for BoxSizing { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => FlexDirection::Row, - 1 => FlexDirection::Column, - 2 => FlexDirection::RowReverse, - 3 => FlexDirection::ColumnReverse, + 0 => BoxSizing::BorderBox, + 1 => BoxSizing::ContentBox, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for GridAutoFlow { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"; +impl FromJavaEnum for Position { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Position;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => GridAutoFlow::Row, - 1 => GridAutoFlow::Column, - 2 => GridAutoFlow::RowDense, - 3 => GridAutoFlow::ColumnDense, + 0 => Position::Relative, + 1 => Position::Absolute, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for AlignContent { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; +impl FromJavaEnum for Display { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Display;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AlignContent::Start, - 1 => AlignContent::End, - 2 => AlignContent::FlexStart, - 3 => AlignContent::FlexEnd, - 4 => AlignContent::Center, - 5 => AlignContent::Stretch, - 6 => AlignContent::SpaceBetween, - 7 => AlignContent::SpaceEvenly, - 8 => AlignContent::SpaceAround, + 0 => Display::Block, + 1 => Display::Flex, + 2 => Display::Grid, + 3 => Display::None, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for BoxSizing { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; +impl FromJavaEnum for TextAlign { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => BoxSizing::BorderBox, - 1 => BoxSizing::ContentBox, + 0 => TextAlign::Auto, + 1 => TextAlign::LegacyLeft, + 2 => TextAlign::LegacyRight, + 3 => TextAlign::LegacyCenter, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for TextAlign { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; +impl FromJavaEnum for FlexDirection { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexDirection;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => TextAlign::Auto, - 1 => TextAlign::LegacyLeft, - 2 => TextAlign::LegacyRight, - 3 => TextAlign::LegacyCenter, + 0 => FlexDirection::Row, + 1 => FlexDirection::Column, + 2 => FlexDirection::RowReverse, + 3 => FlexDirection::ColumnReverse, _ => panic!("Invalid value: {internal}"), }) } diff --git a/bindings/java/src/traits.rs b/bindings/java/src/traits.rs index 18da1d6a7..e94ca6568 100644 --- a/bindings/java/src/traits.rs +++ b/bindings/java/src/traits.rs @@ -1,4 +1,4 @@ -pub trait FromJavaEnum { - const JAVA_CLASS : &'static str; +pub trait FromJavaEnum { + const JAVA_CLASS: &'static str; fn from_ordinal(ordinal: i32) -> Option; -} \ No newline at end of file +} diff --git a/scripts/genenums/src/main.rs b/scripts/genenums/src/main.rs index c70ce003f..d2adc2b50 100644 --- a/scripts/genenums/src/main.rs +++ b/scripts/genenums/src/main.rs @@ -5,9 +5,8 @@ use std::io::Write; use std::path::Path; fn main() { - fs::remove_file("./bindings/java/src/enums.rs") - .expect("Error: Unable to remove java/src/enums.rs file"); - + fs::remove_file("./bindings/java/src/enums.rs").expect("Error: Unable to remove java/src/enums.rs file"); + let mut enums: HashMap<&str, Vec<&str>> = HashMap::new(); enums.insert("Display", vec!["Block", "Flex", "Grid", "None"]); enums.insert("BoxGenerationMode", vec!["Normal", "None"]); @@ -116,7 +115,7 @@ fn create_java_tranformer(name: &str, values: &[&str]) { "use taffy::{name}; {file_content} -impl FromJavaEnum for {name} {{ +impl FromJavaEnum<{name}> for {name} {{ const JAVA_CLASS: &'static str = \"Lcom/dioxuslabs/taffy/enums/{name};\"; fn from_ordinal(internal: i32) -> Option<{name}> {{ From efebe81805a3a3bf3e23d8f719f3f816e2bb33d0 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 00:58:37 +0200 Subject: [PATCH 25/32] genenums: add subfolders --- scripts/genenums/src/generators/java.rs | 60 +++++++++++++++ scripts/genenums/src/generators/mod.rs | 1 + scripts/genenums/src/main.rs | 93 ++--------------------- scripts/genenums/src/transformers/java.rs | 34 +++++++++ scripts/genenums/src/transformers/mod.rs | 1 + 5 files changed, 101 insertions(+), 88 deletions(-) create mode 100644 scripts/genenums/src/generators/java.rs create mode 100644 scripts/genenums/src/generators/mod.rs create mode 100644 scripts/genenums/src/transformers/java.rs create mode 100644 scripts/genenums/src/transformers/mod.rs diff --git a/scripts/genenums/src/generators/java.rs b/scripts/genenums/src/generators/java.rs new file mode 100644 index 000000000..b5f11fa37 --- /dev/null +++ b/scripts/genenums/src/generators/java.rs @@ -0,0 +1,60 @@ +use std::fs; +use std::fs::File; +use std::io::Write; +use std::path::Path; + +pub(crate) fn create_java_enum(name: &str, values: &[&str]) { + use convert_case::{Case, Casing}; + + let package = "com.dioxuslabs.taffy.enums"; + let enum_name = name.to_case(Case::Pascal); + + let auto_gen_comment = "/* +********************************************************** +** AUTOGENERATED CLASSES FOR TAFFY ** +** This code was automatically generated. Do not edit. ** +********************************************************** +*/ +"; + + let mut result = format!( + r"{}package {}; + +public enum {} {{ +", + auto_gen_comment, package, enum_name + ); + + for value in values.iter() { + result.push_str(" "); + result.push_str(&value.to_case(Case::UpperSnake)); + result.push_str(",\n"); + } + + // eliminate the last comma + if !values.is_empty() { + result.pop(); + result.pop(); + result.push('\n'); + } + + result.push_str(" ;\n"); + result.push('\n'); + + result.push_str(" private final int ordinal;\n"); + + result.push('\n'); + result.push_str(" "); + result.push_str(enum_name.as_str()); + result.push_str("() {\n"); + result.push_str(" this.ordinal = ordinal();\n"); + result.push_str(" }\n"); + + result.push_str("}\n"); + + fs::create_dir_all("./bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/") + .expect("Couldn't create directories"); + let file = + File::create(format!("./bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/{}.java", enum_name)); + file.expect("Error: File not found").write_all(result.as_ref()).expect("Error: Couldn't write to file"); +} diff --git a/scripts/genenums/src/generators/mod.rs b/scripts/genenums/src/generators/mod.rs new file mode 100644 index 000000000..8a6ae718a --- /dev/null +++ b/scripts/genenums/src/generators/mod.rs @@ -0,0 +1 @@ +pub(crate) mod java; diff --git a/scripts/genenums/src/main.rs b/scripts/genenums/src/main.rs index d2adc2b50..0a7210278 100644 --- a/scripts/genenums/src/main.rs +++ b/scripts/genenums/src/main.rs @@ -1,8 +1,10 @@ use std::collections::HashMap; use std::fs; -use std::fs::File; -use std::io::Write; -use std::path::Path; +use crate::generators::java::create_java_enum; +use crate::transformers::java::create_java_tranformer; + +mod generators; +mod transformers; fn main() { fs::remove_file("./bindings/java/src/enums.rs").expect("Error: Unable to remove java/src/enums.rs file"); @@ -36,95 +38,10 @@ fn create_enum(name: &str, values: &[&str]) { create_java_enum(name, values); } -fn create_java_enum(name: &str, values: &[&str]) { - use convert_case::{Case, Casing}; - - let package = "com.dioxuslabs.taffy.enums"; - let enum_name = name.to_case(Case::Pascal); - - let auto_gen_comment = "/* -********************************************************** -** AUTOGENERATED CLASSES FOR TAFFY ** -** This code was automatically generated. Do not edit. ** -********************************************************** -*/ -"; - - let mut result = format!( - r"{}package {}; - -public enum {} {{ -", - auto_gen_comment, package, enum_name - ); - - for value in values.iter() { - result.push_str(" "); - result.push_str(&value.to_case(Case::UpperSnake)); - result.push_str(",\n"); - } - - // eliminate the last comma - if !values.is_empty() { - result.pop(); - result.pop(); - result.push('\n'); - } - - result.push_str(" ;\n"); - result.push('\n'); - - result.push_str(" private final int ordinal;\n"); - - result.push('\n'); - result.push_str(" "); - result.push_str(enum_name.as_str()); - result.push_str("() {\n"); - result.push_str(" this.ordinal = ordinal();\n"); - result.push_str(" }\n"); - - result.push_str("}\n"); - - fs::create_dir_all("./bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/") - .expect("Couldn't create directories"); - let file = - File::create(format!("./bindings/java/java/src/main/java/com/dioxuslabs/taffy/enums/{}.java", enum_name)); - file.expect("Error: File not found").write_all(result.as_ref()).expect("Error: Couldn't write to file"); -} - /// Transformer generators fn create_transformer(name: &str, values: &[&str]) { create_java_tranformer(name, values); } -fn create_java_tranformer(name: &str, values: &[&str]) { - let mut file_content: String = "use crate::traits::FromJavaEnum;".to_string(); - if Path::new("./bindings/java/src/enums.rs").exists() { - file_content = fs::read_to_string(Path::new("./bindings/java/src/enums.rs")).unwrap() - } - - let mut enum_values: String = "".to_string(); - for (index, value) in values.iter().enumerate() { - enum_values.push_str(format!("\n {index} => {name}::{value},").as_str()); - } - enum_values.push_str("\n _ => panic!(\"Invalid value: {internal}\"),"); - - file_content = format!( - "use taffy::{name}; -{file_content} - -impl FromJavaEnum<{name}> for {name} {{ - const JAVA_CLASS: &'static str = \"Lcom/dioxuslabs/taffy/enums/{name};\"; - - fn from_ordinal(internal: i32) -> Option<{name}> {{ - Some(match internal {{{enum_values} - }}) - }} -}}" - ); - - let file = File::create("./bindings/java/src/enums.rs"); - file.expect("Error: File not found").write_all(file_content.as_ref()).expect("Error: Couldn't write to file"); -} diff --git a/scripts/genenums/src/transformers/java.rs b/scripts/genenums/src/transformers/java.rs new file mode 100644 index 000000000..19f792db0 --- /dev/null +++ b/scripts/genenums/src/transformers/java.rs @@ -0,0 +1,34 @@ +use std::fs; +use std::fs::File; +use std::path::Path; + +pub(crate) fn create_java_tranformer(name: &str, values: &[&str]) { + let mut file_content: String = "use crate::traits::FromJavaEnum;".to_string(); + + if Path::new("./bindings/java/src/enums.rs").exists() { + file_content = fs::read_to_string(Path::new("./bindings/java/src/enums.rs")).unwrap() + } + + let mut enum_values: String = "".to_string(); + for (index, value) in values.iter().enumerate() { + enum_values.push_str(format!("\n {index} => {name}::{value},").as_str()); + } + enum_values.push_str("\n _ => panic!(\"Invalid value: {internal}\"),"); + + file_content = format!( + "use taffy::{name}; +{file_content} + +impl FromJavaEnum<{name}> for {name} {{ + const JAVA_CLASS: &'static str = \"Lcom/dioxuslabs/taffy/enums/{name};\"; + + fn from_ordinal(internal: i32) -> Option<{name}> {{ + Some(match internal {{{enum_values} + }}) + }} +}}" + ); + + let file = File::create("./bindings/java/src/enums.rs"); + file.expect("Error: File not found").write_all(file_content.as_ref()).expect("Error: Couldn't write to file"); +} \ No newline at end of file diff --git a/scripts/genenums/src/transformers/mod.rs b/scripts/genenums/src/transformers/mod.rs new file mode 100644 index 000000000..8a6ae718a --- /dev/null +++ b/scripts/genenums/src/transformers/mod.rs @@ -0,0 +1 @@ +pub(crate) mod java; From 17358618c7db955172e28f6d29255c7e80ef89e2 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 01:17:35 +0200 Subject: [PATCH 26/32] genenums: support default & remove usage of HashMap as it doesn't keep insertion order and isn't consistent --- bindings/java/src/enums.rs | 200 +++++++++++----------- scripts/genenums/src/generators/java.rs | 1 - scripts/genenums/src/main.rs | 82 ++++++--- scripts/genenums/src/transformers/java.rs | 11 +- 4 files changed, 164 insertions(+), 130 deletions(-) diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index 8fe65e8ed..e61529d4a 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -1,179 +1,179 @@ +use taffy::AbsoluteAxis; +use taffy::AlignItems; +use taffy::AlignContent; use taffy::FlexDirection; +use taffy::FlexWrap; +use taffy::GridAutoFlow; use taffy::TextAlign; -use taffy::Display; -use taffy::Position; +use taffy::Overflow; use taffy::BoxSizing; +use taffy::Position; use taffy::BoxGenerationMode; -use taffy::AlignItems; -use taffy::AbsoluteAxis; -use taffy::GridAutoFlow; -use taffy::FlexWrap; -use taffy::Overflow; -use taffy::AlignContent; +use taffy::Display; use crate::traits::FromJavaEnum; -impl FromJavaEnum for AlignContent { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; +impl FromJavaEnum for Display { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Display;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AlignContent::Start, - 1 => AlignContent::End, - 2 => AlignContent::FlexStart, - 3 => AlignContent::FlexEnd, - 4 => AlignContent::Center, - 5 => AlignContent::Stretch, - 6 => AlignContent::SpaceBetween, - 7 => AlignContent::SpaceEvenly, - 8 => AlignContent::SpaceAround, - _ => panic!("Invalid value: {internal}"), + 0 => Display::Block, + 1 => Display::Flex, + 2 => Display::Grid, + 3 => Display::None, + _ => Display::default(), }) } } -impl FromJavaEnum for Overflow { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Overflow;"; +impl FromJavaEnum for BoxGenerationMode { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => Overflow::Visible, - 1 => Overflow::Clip, - 2 => Overflow::Hidden, - 3 => Overflow::Scroll, - _ => panic!("Invalid value: {internal}"), + 0 => BoxGenerationMode::Normal, + 1 => BoxGenerationMode::None, + _ => BoxGenerationMode::default(), }) } } -impl FromJavaEnum for FlexWrap { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexWrap;"; +impl FromJavaEnum for Position { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Position;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => FlexWrap::NoWrap, - 1 => FlexWrap::Wrap, - 2 => FlexWrap::WrapReverse, - _ => panic!("Invalid value: {internal}"), + 0 => Position::Relative, + 1 => Position::Absolute, + _ => Position::default(), }) } } -impl FromJavaEnum for GridAutoFlow { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"; +impl FromJavaEnum for BoxSizing { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => GridAutoFlow::Row, - 1 => GridAutoFlow::Column, - 2 => GridAutoFlow::RowDense, - 3 => GridAutoFlow::ColumnDense, - _ => panic!("Invalid value: {internal}"), + 0 => BoxSizing::BorderBox, + 1 => BoxSizing::ContentBox, + _ => BoxSizing::default(), }) } } -impl FromJavaEnum for AbsoluteAxis { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AbsoluteAxis;"; +impl FromJavaEnum for Overflow { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Overflow;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AbsoluteAxis::Horizontal, - 1 => AbsoluteAxis::Vertical, - _ => panic!("Invalid value: {internal}"), + 0 => Overflow::Visible, + 1 => Overflow::Clip, + 2 => Overflow::Hidden, + 3 => Overflow::Scroll, + _ => Overflow::default(), }) } } -impl FromJavaEnum for AlignItems { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignItems;"; +impl FromJavaEnum for TextAlign { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AlignItems::Start, - 1 => AlignItems::End, - 2 => AlignItems::FlexStart, - 3 => AlignItems::FlexEnd, - 4 => AlignItems::Center, - 5 => AlignItems::Baseline, - 6 => AlignItems::Stretch, - _ => panic!("Invalid value: {internal}"), + 0 => TextAlign::Auto, + 1 => TextAlign::LegacyLeft, + 2 => TextAlign::LegacyRight, + 3 => TextAlign::LegacyCenter, + _ => TextAlign::default(), }) } } -impl FromJavaEnum for BoxGenerationMode { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; +impl FromJavaEnum for GridAutoFlow { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => BoxGenerationMode::Normal, - 1 => BoxGenerationMode::None, - _ => panic!("Invalid value: {internal}"), + 0 => GridAutoFlow::Row, + 1 => GridAutoFlow::Column, + 2 => GridAutoFlow::RowDense, + 3 => GridAutoFlow::ColumnDense, + _ => GridAutoFlow::default(), }) } } -impl FromJavaEnum for BoxSizing { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; +impl FromJavaEnum for FlexWrap { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexWrap;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => BoxSizing::BorderBox, - 1 => BoxSizing::ContentBox, - _ => panic!("Invalid value: {internal}"), + 0 => FlexWrap::NoWrap, + 1 => FlexWrap::Wrap, + 2 => FlexWrap::WrapReverse, + _ => FlexWrap::default(), }) } } -impl FromJavaEnum for Position { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Position;"; +impl FromJavaEnum for FlexDirection { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexDirection;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => Position::Relative, - 1 => Position::Absolute, - _ => panic!("Invalid value: {internal}"), + 0 => FlexDirection::Row, + 1 => FlexDirection::Column, + 2 => FlexDirection::RowReverse, + 3 => FlexDirection::ColumnReverse, + _ => FlexDirection::default(), }) } } -impl FromJavaEnum for Display { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Display;"; +impl FromJavaEnum for AlignContent { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => Display::Block, - 1 => Display::Flex, - 2 => Display::Grid, - 3 => Display::None, + 0 => AlignContent::Start, + 1 => AlignContent::End, + 2 => AlignContent::FlexStart, + 3 => AlignContent::FlexEnd, + 4 => AlignContent::Center, + 5 => AlignContent::Stretch, + 6 => AlignContent::SpaceBetween, + 7 => AlignContent::SpaceEvenly, + 8 => AlignContent::SpaceAround, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for TextAlign { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; +impl FromJavaEnum for AlignItems { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignItems;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => TextAlign::Auto, - 1 => TextAlign::LegacyLeft, - 2 => TextAlign::LegacyRight, - 3 => TextAlign::LegacyCenter, + 0 => AlignItems::Start, + 1 => AlignItems::End, + 2 => AlignItems::FlexStart, + 3 => AlignItems::FlexEnd, + 4 => AlignItems::Center, + 5 => AlignItems::Baseline, + 6 => AlignItems::Stretch, _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for FlexDirection { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexDirection;"; +impl FromJavaEnum for AbsoluteAxis { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AbsoluteAxis;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => FlexDirection::Row, - 1 => FlexDirection::Column, - 2 => FlexDirection::RowReverse, - 3 => FlexDirection::ColumnReverse, + 0 => AbsoluteAxis::Horizontal, + 1 => AbsoluteAxis::Vertical, _ => panic!("Invalid value: {internal}"), }) } diff --git a/scripts/genenums/src/generators/java.rs b/scripts/genenums/src/generators/java.rs index b5f11fa37..d8de2b164 100644 --- a/scripts/genenums/src/generators/java.rs +++ b/scripts/genenums/src/generators/java.rs @@ -1,7 +1,6 @@ use std::fs; use std::fs::File; use std::io::Write; -use std::path::Path; pub(crate) fn create_java_enum(name: &str, values: &[&str]) { use convert_case::{Case, Casing}; diff --git a/scripts/genenums/src/main.rs b/scripts/genenums/src/main.rs index 0a7210278..4e3cd0c55 100644 --- a/scripts/genenums/src/main.rs +++ b/scripts/genenums/src/main.rs @@ -1,34 +1,66 @@ -use std::collections::HashMap; -use std::fs; use crate::generators::java::create_java_enum; use crate::transformers::java::create_java_tranformer; +use std::fs; mod generators; mod transformers; +struct RustEnum<'local> { + name: &'local str, + values: Vec<&'local str>, + default: bool, +} + fn main() { fs::remove_file("./bindings/java/src/enums.rs").expect("Error: Unable to remove java/src/enums.rs file"); - let mut enums: HashMap<&str, Vec<&str>> = HashMap::new(); - enums.insert("Display", vec!["Block", "Flex", "Grid", "None"]); - enums.insert("BoxGenerationMode", vec!["Normal", "None"]); - enums.insert("Position", vec!["Relative", "Absolute"]); - enums.insert("BoxSizing", vec!["BorderBox", "ContentBox"]); - enums.insert("Overflow", vec!["Visible", "Clip", "Hidden", "Scroll"]); - enums.insert("TextAlign", vec!["Auto", "LegacyLeft", "LegacyRight", "LegacyCenter"]); - enums.insert("GridAutoFlow", vec!["Row", "Column", "RowDense", "ColumnDense"]); - enums.insert("FlexWrap", vec!["NoWrap", "Wrap", "WrapReverse"]); - enums.insert("FlexDirection", vec!["Row", "Column", "RowReverse", "ColumnReverse"]); - enums.insert( - "AlignContent", - vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Stretch", "SpaceBetween", "SpaceEvenly", "SpaceAround"], - ); - enums.insert("AlignItems", vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Baseline", "Stretch"]); - enums.insert("AbsoluteAxis", vec!["Horizontal", "Vertical"]); - - for (key, value) in enums.into_iter() { - create_enum(key, &value); - create_transformer(key, &value); + let mut enums: Vec = Vec::new(); + enums.push(RustEnum { name: "Display", values: vec!["Block", "Flex", "Grid", "None"], default: true }); + enums.push(RustEnum { name: "BoxGenerationMode", values: vec!["Normal", "None"], default: true }); + enums.push(RustEnum { name: "Position", values: vec!["Relative", "Absolute"], default: true }); + enums.push(RustEnum { name: "BoxSizing", values: vec!["BorderBox", "ContentBox"], default: true }); + enums.push(RustEnum { name: "Overflow", values: vec!["Visible", "Clip", "Hidden", "Scroll"], default: true }); + enums.push(RustEnum { + name: "TextAlign", + values: vec!["Auto", "LegacyLeft", "LegacyRight", "LegacyCenter"], + default: true, + }); + enums.push(RustEnum { + name: "GridAutoFlow", + values: vec!["Row", "Column", "RowDense", "ColumnDense"], + default: true, + }); + enums.push(RustEnum { name: "FlexWrap", values: vec!["NoWrap", "Wrap", "WrapReverse"], default: true }); + enums.push(RustEnum { + name: "FlexDirection", + values: vec!["Row", "Column", "RowReverse", "ColumnReverse"], + default: true, + }); + enums.push(RustEnum { + name: "AlignContent", + values: vec![ + "Start", + "End", + "FlexStart", + "FlexEnd", + "Center", + "Stretch", + "SpaceBetween", + "SpaceEvenly", + "SpaceAround", + ], + default: false, + }); + enums.push(RustEnum { + name: "AlignItems", + values: vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Baseline", "Stretch"], + default: false, + }); + enums.push(RustEnum { name: "AbsoluteAxis", values: vec!["Horizontal", "Vertical"], default: false }); + + for value in enums.into_iter() { + create_enum(value.name, &value.values); + create_transformer(value.name, &value.values, value.default); } } @@ -40,8 +72,6 @@ fn create_enum(name: &str, values: &[&str]) { /// Transformer generators -fn create_transformer(name: &str, values: &[&str]) { - create_java_tranformer(name, values); +fn create_transformer(name: &str, values: &[&str], default: bool) { + create_java_tranformer(name, values, default); } - - diff --git a/scripts/genenums/src/transformers/java.rs b/scripts/genenums/src/transformers/java.rs index 19f792db0..9a7533281 100644 --- a/scripts/genenums/src/transformers/java.rs +++ b/scripts/genenums/src/transformers/java.rs @@ -1,8 +1,9 @@ use std::fs; use std::fs::File; +use std::io::Write; use std::path::Path; -pub(crate) fn create_java_tranformer(name: &str, values: &[&str]) { +pub(crate) fn create_java_tranformer(name: &str, values: &[&str], default: bool) { let mut file_content: String = "use crate::traits::FromJavaEnum;".to_string(); if Path::new("./bindings/java/src/enums.rs").exists() { @@ -13,7 +14,11 @@ pub(crate) fn create_java_tranformer(name: &str, values: &[&str]) { for (index, value) in values.iter().enumerate() { enum_values.push_str(format!("\n {index} => {name}::{value},").as_str()); } - enum_values.push_str("\n _ => panic!(\"Invalid value: {internal}\"),"); + if default { + enum_values.push_str(format!("\n _ => {name}::default(),").as_str()); + } else { + enum_values.push_str("\n _ => panic!(\"Invalid value: {internal}\"),"); + } file_content = format!( "use taffy::{name}; @@ -31,4 +36,4 @@ impl FromJavaEnum<{name}> for {name} {{ let file = File::create("./bindings/java/src/enums.rs"); file.expect("Error: File not found").write_all(file_content.as_ref()).expect("Error: Couldn't write to file"); -} \ No newline at end of file +} From a10e904281330481a058ba105e1db60f15769fc8 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 01:21:35 +0200 Subject: [PATCH 27/32] genenums: tweak file end line generation --- bindings/java/src/enums.rs | 2 +- scripts/genenums/src/transformers/java.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index e61529d4a..22c136ea3 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -177,4 +177,4 @@ impl FromJavaEnum for AbsoluteAxis { _ => panic!("Invalid value: {internal}"), }) } -} \ No newline at end of file +} diff --git a/scripts/genenums/src/transformers/java.rs b/scripts/genenums/src/transformers/java.rs index 9a7533281..5e9555615 100644 --- a/scripts/genenums/src/transformers/java.rs +++ b/scripts/genenums/src/transformers/java.rs @@ -4,7 +4,7 @@ use std::io::Write; use std::path::Path; pub(crate) fn create_java_tranformer(name: &str, values: &[&str], default: bool) { - let mut file_content: String = "use crate::traits::FromJavaEnum;".to_string(); + let mut file_content: String = "use crate::traits::FromJavaEnum;\n".to_string(); if Path::new("./bindings/java/src/enums.rs").exists() { file_content = fs::read_to_string(Path::new("./bindings/java/src/enums.rs")).unwrap() @@ -23,7 +23,6 @@ pub(crate) fn create_java_tranformer(name: &str, values: &[&str], default: bool) file_content = format!( "use taffy::{name}; {file_content} - impl FromJavaEnum<{name}> for {name} {{ const JAVA_CLASS: &'static str = \"Lcom/dioxuslabs/taffy/enums/{name};\"; @@ -31,7 +30,8 @@ impl FromJavaEnum<{name}> for {name} {{ Some(match internal {{{enum_values} }}) }} -}}" +}} +" ); let file = File::create("./bindings/java/src/enums.rs"); From 38b657642955e7468e48b08672c56cea6d6930a0 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 01:28:21 +0200 Subject: [PATCH 28/32] genenums: try to fix rustfmt --- bindings/java/src/enums.rs | 128 +++++++++++++++++------------------ scripts/genenums/src/main.rs | 83 +++++++++++------------ 2 files changed, 104 insertions(+), 107 deletions(-) diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index 22c136ea3..c6c9d67c6 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -1,39 +1,27 @@ use taffy::AbsoluteAxis; -use taffy::AlignItems; use taffy::AlignContent; +use taffy::AlignItems; +use taffy::BoxGenerationMode; +use taffy::BoxSizing; +use taffy::Display; use taffy::FlexDirection; use taffy::FlexWrap; use taffy::GridAutoFlow; -use taffy::TextAlign; use taffy::Overflow; -use taffy::BoxSizing; use taffy::Position; -use taffy::BoxGenerationMode; -use taffy::Display; +use taffy::TextAlign; use crate::traits::FromJavaEnum; -impl FromJavaEnum for Display { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Display;"; - - fn from_ordinal(internal: i32) -> Option { - Some(match internal { - 0 => Display::Block, - 1 => Display::Flex, - 2 => Display::Grid, - 3 => Display::None, - _ => Display::default(), - }) - } -} - -impl FromJavaEnum for BoxGenerationMode { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; +impl FromJavaEnum for TextAlign { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => BoxGenerationMode::Normal, - 1 => BoxGenerationMode::None, - _ => BoxGenerationMode::default(), + 0 => TextAlign::Auto, + 1 => TextAlign::LegacyLeft, + 2 => TextAlign::LegacyRight, + 3 => TextAlign::LegacyCenter, + _ => TextAlign::default(), }) } } @@ -50,18 +38,6 @@ impl FromJavaEnum for Position { } } -impl FromJavaEnum for BoxSizing { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; - - fn from_ordinal(internal: i32) -> Option { - Some(match internal { - 0 => BoxSizing::BorderBox, - 1 => BoxSizing::ContentBox, - _ => BoxSizing::default(), - }) - } -} - impl FromJavaEnum for Overflow { const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Overflow;"; @@ -76,20 +52,6 @@ impl FromJavaEnum for Overflow { } } -impl FromJavaEnum for TextAlign { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; - - fn from_ordinal(internal: i32) -> Option { - Some(match internal { - 0 => TextAlign::Auto, - 1 => TextAlign::LegacyLeft, - 2 => TextAlign::LegacyRight, - 3 => TextAlign::LegacyCenter, - _ => TextAlign::default(), - }) - } -} - impl FromJavaEnum for GridAutoFlow { const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"; @@ -131,21 +93,40 @@ impl FromJavaEnum for FlexDirection { } } -impl FromJavaEnum for AlignContent { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; +impl FromJavaEnum for Display { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Display;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AlignContent::Start, - 1 => AlignContent::End, - 2 => AlignContent::FlexStart, - 3 => AlignContent::FlexEnd, - 4 => AlignContent::Center, - 5 => AlignContent::Stretch, - 6 => AlignContent::SpaceBetween, - 7 => AlignContent::SpaceEvenly, - 8 => AlignContent::SpaceAround, - _ => panic!("Invalid value: {internal}"), + 0 => Display::Block, + 1 => Display::Flex, + 2 => Display::Grid, + 3 => Display::None, + _ => Display::default(), + }) + } +} + +impl FromJavaEnum for BoxSizing { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; + + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => BoxSizing::BorderBox, + 1 => BoxSizing::ContentBox, + _ => BoxSizing::default(), + }) + } +} + +impl FromJavaEnum for BoxGenerationMode { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; + + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => BoxGenerationMode::Normal, + 1 => BoxGenerationMode::None, + _ => BoxGenerationMode::default(), }) } } @@ -167,6 +148,25 @@ impl FromJavaEnum for AlignItems { } } +impl FromJavaEnum for AlignContent { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; + + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => AlignContent::Start, + 1 => AlignContent::End, + 2 => AlignContent::FlexStart, + 3 => AlignContent::FlexEnd, + 4 => AlignContent::Center, + 5 => AlignContent::Stretch, + 6 => AlignContent::SpaceBetween, + 7 => AlignContent::SpaceEvenly, + 8 => AlignContent::SpaceAround, + _ => panic!("Invalid value: {internal}"), + }) + } +} + impl FromJavaEnum for AbsoluteAxis { const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AbsoluteAxis;"; diff --git a/scripts/genenums/src/main.rs b/scripts/genenums/src/main.rs index 4e3cd0c55..3d30c23a7 100644 --- a/scripts/genenums/src/main.rs +++ b/scripts/genenums/src/main.rs @@ -14,49 +14,46 @@ struct RustEnum<'local> { fn main() { fs::remove_file("./bindings/java/src/enums.rs").expect("Error: Unable to remove java/src/enums.rs file"); - let mut enums: Vec = Vec::new(); - enums.push(RustEnum { name: "Display", values: vec!["Block", "Flex", "Grid", "None"], default: true }); - enums.push(RustEnum { name: "BoxGenerationMode", values: vec!["Normal", "None"], default: true }); - enums.push(RustEnum { name: "Position", values: vec!["Relative", "Absolute"], default: true }); - enums.push(RustEnum { name: "BoxSizing", values: vec!["BorderBox", "ContentBox"], default: true }); - enums.push(RustEnum { name: "Overflow", values: vec!["Visible", "Clip", "Hidden", "Scroll"], default: true }); - enums.push(RustEnum { - name: "TextAlign", - values: vec!["Auto", "LegacyLeft", "LegacyRight", "LegacyCenter"], - default: true, - }); - enums.push(RustEnum { - name: "GridAutoFlow", - values: vec!["Row", "Column", "RowDense", "ColumnDense"], - default: true, - }); - enums.push(RustEnum { name: "FlexWrap", values: vec!["NoWrap", "Wrap", "WrapReverse"], default: true }); - enums.push(RustEnum { - name: "FlexDirection", - values: vec!["Row", "Column", "RowReverse", "ColumnReverse"], - default: true, - }); - enums.push(RustEnum { - name: "AlignContent", - values: vec![ - "Start", - "End", - "FlexStart", - "FlexEnd", - "Center", - "Stretch", - "SpaceBetween", - "SpaceEvenly", - "SpaceAround", - ], - default: false, - }); - enums.push(RustEnum { - name: "AlignItems", - values: vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Baseline", "Stretch"], - default: false, - }); - enums.push(RustEnum { name: "AbsoluteAxis", values: vec!["Horizontal", "Vertical"], default: false }); + let mut enums: Vec = vec![ + RustEnum { name: "Display", values: vec!["Block", "Flex", "Grid", "None"], default: true }, + RustEnum { name: "BoxGenerationMode", values: vec!["Normal", "None"], default: true }, + RustEnum { name: "Position", values: vec!["Relative", "Absolute"], default: true }, + RustEnum { name: "BoxSizing", values: vec!["BorderBox", "ContentBox"], default: true }, + RustEnum { name: "Overflow", values: vec!["Visible", "Clip", "Hidden", "Scroll"], default: true }, + RustEnum { + name: "TextAlign", + values: vec!["Auto", "LegacyLeft", "LegacyRight", "LegacyCenter"], + default: true, + }, + RustEnum { name: "GridAutoFlow", values: vec!["Row", "Column", "RowDense", "ColumnDense"], default: true }, + RustEnum { name: "FlexWrap", values: vec!["NoWrap", "Wrap", "WrapReverse"], default: true }, + RustEnum { name: "FlexDirection", values: vec!["Row", "Column", "RowReverse", "ColumnReverse"], default: true }, + RustEnum { + name: "AlignContent", + values: vec![ + "Start", + "End", + "FlexStart", + "FlexEnd", + "Center", + "Stretch", + "SpaceBetween", + "SpaceEvenly", + "SpaceAround", + ], + default: false, + }, + RustEnum { + name: "AlignItems", + values: vec!["Start", "End", "FlexStart", "FlexEnd", "Center", "Baseline", "Stretch"], + default: false, + }, + RustEnum { name: "AbsoluteAxis", values: vec!["Horizontal", "Vertical"], default: false }, + ]; + + // Might look inverted at first, but it is needed for rustfmt to not throw formatting errors + // (also known as ensuring alphabetical order in imports) + enums.sort_by(|a, b| b.name.cmp(a.name)); for value in enums.into_iter() { create_enum(value.name, &value.values); From d7090fbbf948ac017536ff64714b1f7c1a9902d2 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 01:36:33 +0200 Subject: [PATCH 29/32] genenums: try to fix rustfmt --- bindings/java/src/enums.rs | 189 +++++++++++----------- scripts/genenums/src/main.rs | 5 +- scripts/genenums/src/transformers/java.rs | 15 +- 3 files changed, 108 insertions(+), 101 deletions(-) diff --git a/bindings/java/src/enums.rs b/bindings/java/src/enums.rs index c6c9d67c6..43c5e69fb 100644 --- a/bindings/java/src/enums.rs +++ b/bindings/java/src/enums.rs @@ -1,3 +1,4 @@ +use crate::traits::FromJavaEnum; use taffy::AbsoluteAxis; use taffy::AlignContent; use taffy::AlignItems; @@ -10,85 +11,76 @@ use taffy::GridAutoFlow; use taffy::Overflow; use taffy::Position; use taffy::TextAlign; -use crate::traits::FromJavaEnum; - -impl FromJavaEnum for TextAlign { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; - - fn from_ordinal(internal: i32) -> Option { - Some(match internal { - 0 => TextAlign::Auto, - 1 => TextAlign::LegacyLeft, - 2 => TextAlign::LegacyRight, - 3 => TextAlign::LegacyCenter, - _ => TextAlign::default(), - }) - } -} +/// FromJavaEnum implementations for Enums that need it -impl FromJavaEnum for Position { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Position;"; +impl FromJavaEnum for AbsoluteAxis { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AbsoluteAxis;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => Position::Relative, - 1 => Position::Absolute, - _ => Position::default(), + 0 => AbsoluteAxis::Horizontal, + 1 => AbsoluteAxis::Vertical, + _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for Overflow { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Overflow;"; +impl FromJavaEnum for AlignContent { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => Overflow::Visible, - 1 => Overflow::Clip, - 2 => Overflow::Hidden, - 3 => Overflow::Scroll, - _ => Overflow::default(), + 0 => AlignContent::Start, + 1 => AlignContent::End, + 2 => AlignContent::FlexStart, + 3 => AlignContent::FlexEnd, + 4 => AlignContent::Center, + 5 => AlignContent::Stretch, + 6 => AlignContent::SpaceBetween, + 7 => AlignContent::SpaceEvenly, + 8 => AlignContent::SpaceAround, + _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for GridAutoFlow { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"; +impl FromJavaEnum for AlignItems { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignItems;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => GridAutoFlow::Row, - 1 => GridAutoFlow::Column, - 2 => GridAutoFlow::RowDense, - 3 => GridAutoFlow::ColumnDense, - _ => GridAutoFlow::default(), + 0 => AlignItems::Start, + 1 => AlignItems::End, + 2 => AlignItems::FlexStart, + 3 => AlignItems::FlexEnd, + 4 => AlignItems::Center, + 5 => AlignItems::Baseline, + 6 => AlignItems::Stretch, + _ => panic!("Invalid value: {internal}"), }) } } -impl FromJavaEnum for FlexWrap { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexWrap;"; +impl FromJavaEnum for BoxGenerationMode { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => FlexWrap::NoWrap, - 1 => FlexWrap::Wrap, - 2 => FlexWrap::WrapReverse, - _ => FlexWrap::default(), + 0 => BoxGenerationMode::Normal, + 1 => BoxGenerationMode::None, + _ => BoxGenerationMode::default(), }) } } -impl FromJavaEnum for FlexDirection { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexDirection;"; +impl FromJavaEnum for BoxSizing { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => FlexDirection::Row, - 1 => FlexDirection::Column, - 2 => FlexDirection::RowReverse, - 3 => FlexDirection::ColumnReverse, - _ => FlexDirection::default(), + 0 => BoxSizing::BorderBox, + 1 => BoxSizing::ContentBox, + _ => BoxSizing::default(), }) } } @@ -107,74 +99,83 @@ impl FromJavaEnum for Display { } } -impl FromJavaEnum for BoxSizing { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxSizing;"; +impl FromJavaEnum for FlexDirection { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexDirection;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => BoxSizing::BorderBox, - 1 => BoxSizing::ContentBox, - _ => BoxSizing::default(), + 0 => FlexDirection::Row, + 1 => FlexDirection::Column, + 2 => FlexDirection::RowReverse, + 3 => FlexDirection::ColumnReverse, + _ => FlexDirection::default(), }) } } -impl FromJavaEnum for BoxGenerationMode { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/BoxGenerationMode;"; +impl FromJavaEnum for FlexWrap { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/FlexWrap;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => BoxGenerationMode::Normal, - 1 => BoxGenerationMode::None, - _ => BoxGenerationMode::default(), + 0 => FlexWrap::NoWrap, + 1 => FlexWrap::Wrap, + 2 => FlexWrap::WrapReverse, + _ => FlexWrap::default(), }) } } -impl FromJavaEnum for AlignItems { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignItems;"; +impl FromJavaEnum for GridAutoFlow { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/GridAutoFlow;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AlignItems::Start, - 1 => AlignItems::End, - 2 => AlignItems::FlexStart, - 3 => AlignItems::FlexEnd, - 4 => AlignItems::Center, - 5 => AlignItems::Baseline, - 6 => AlignItems::Stretch, - _ => panic!("Invalid value: {internal}"), + 0 => GridAutoFlow::Row, + 1 => GridAutoFlow::Column, + 2 => GridAutoFlow::RowDense, + 3 => GridAutoFlow::ColumnDense, + _ => GridAutoFlow::default(), }) } } -impl FromJavaEnum for AlignContent { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AlignContent;"; +impl FromJavaEnum for Overflow { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Overflow;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AlignContent::Start, - 1 => AlignContent::End, - 2 => AlignContent::FlexStart, - 3 => AlignContent::FlexEnd, - 4 => AlignContent::Center, - 5 => AlignContent::Stretch, - 6 => AlignContent::SpaceBetween, - 7 => AlignContent::SpaceEvenly, - 8 => AlignContent::SpaceAround, - _ => panic!("Invalid value: {internal}"), + 0 => Overflow::Visible, + 1 => Overflow::Clip, + 2 => Overflow::Hidden, + 3 => Overflow::Scroll, + _ => Overflow::default(), }) } } -impl FromJavaEnum for AbsoluteAxis { - const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/AbsoluteAxis;"; +impl FromJavaEnum for Position { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/Position;"; - fn from_ordinal(internal: i32) -> Option { + fn from_ordinal(internal: i32) -> Option { Some(match internal { - 0 => AbsoluteAxis::Horizontal, - 1 => AbsoluteAxis::Vertical, - _ => panic!("Invalid value: {internal}"), + 0 => Position::Relative, + 1 => Position::Absolute, + _ => Position::default(), + }) + } +} + +impl FromJavaEnum for TextAlign { + const JAVA_CLASS: &'static str = "Lcom/dioxuslabs/taffy/enums/TextAlign;"; + + fn from_ordinal(internal: i32) -> Option { + Some(match internal { + 0 => TextAlign::Auto, + 1 => TextAlign::LegacyLeft, + 2 => TextAlign::LegacyRight, + 3 => TextAlign::LegacyCenter, + _ => TextAlign::default(), }) } } diff --git a/scripts/genenums/src/main.rs b/scripts/genenums/src/main.rs index 3d30c23a7..f01febf0e 100644 --- a/scripts/genenums/src/main.rs +++ b/scripts/genenums/src/main.rs @@ -51,9 +51,8 @@ fn main() { RustEnum { name: "AbsoluteAxis", values: vec!["Horizontal", "Vertical"], default: false }, ]; - // Might look inverted at first, but it is needed for rustfmt to not throw formatting errors - // (also known as ensuring alphabetical order in imports) - enums.sort_by(|a, b| b.name.cmp(a.name)); + // Sort to ensure consistency + enums.sort_by(|a, b| a.name.cmp(b.name)); for value in enums.into_iter() { create_enum(value.name, &value.values); diff --git a/scripts/genenums/src/transformers/java.rs b/scripts/genenums/src/transformers/java.rs index 5e9555615..95ee636da 100644 --- a/scripts/genenums/src/transformers/java.rs +++ b/scripts/genenums/src/transformers/java.rs @@ -4,7 +4,7 @@ use std::io::Write; use std::path::Path; pub(crate) fn create_java_tranformer(name: &str, values: &[&str], default: bool) { - let mut file_content: String = "use crate::traits::FromJavaEnum;\n".to_string(); + let mut file_content: String = "use crate::traits::FromJavaEnum;\n/// FromJavaEnum implementations for Enums that need it\n".to_string(); if Path::new("./bindings/java/src/enums.rs").exists() { file_content = fs::read_to_string(Path::new("./bindings/java/src/enums.rs")).unwrap() @@ -20,9 +20,16 @@ pub(crate) fn create_java_tranformer(name: &str, values: &[&str], default: bool) enum_values.push_str("\n _ => panic!(\"Invalid value: {internal}\"),"); } - file_content = format!( - "use taffy::{name}; -{file_content} + file_content = file_content.replace( + "\n/// FromJavaEnum implementations for Enums that need it", + format!( + "\nuse taffy::{name}; +/// FromJavaEnum implementations for Enums that need it" + ) + .as_str(), + ); + + file_content = format!("{file_content} impl FromJavaEnum<{name}> for {name} {{ const JAVA_CLASS: &'static str = \"Lcom/dioxuslabs/taffy/enums/{name};\"; From 798a82ab3aba7c03441bb76e628cb0292c0998b0 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 01:37:57 +0200 Subject: [PATCH 30/32] genenums: rustfmt stuff --- scripts/genenums/src/transformers/java.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/scripts/genenums/src/transformers/java.rs b/scripts/genenums/src/transformers/java.rs index 95ee636da..b975fb37a 100644 --- a/scripts/genenums/src/transformers/java.rs +++ b/scripts/genenums/src/transformers/java.rs @@ -4,7 +4,8 @@ use std::io::Write; use std::path::Path; pub(crate) fn create_java_tranformer(name: &str, values: &[&str], default: bool) { - let mut file_content: String = "use crate::traits::FromJavaEnum;\n/// FromJavaEnum implementations for Enums that need it\n".to_string(); + let mut file_content: String = + "use crate::traits::FromJavaEnum;\n/// FromJavaEnum implementations for Enums that need it\n".to_string(); if Path::new("./bindings/java/src/enums.rs").exists() { file_content = fs::read_to_string(Path::new("./bindings/java/src/enums.rs")).unwrap() @@ -29,7 +30,8 @@ pub(crate) fn create_java_tranformer(name: &str, values: &[&str], default: bool) .as_str(), ); - file_content = format!("{file_content} + file_content = format!( + "{file_content} impl FromJavaEnum<{name}> for {name} {{ const JAVA_CLASS: &'static str = \"Lcom/dioxuslabs/taffy/enums/{name};\"; From 59a793f20ee0a4233300a6ce592ee0c2b34178e2 Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 02:02:49 +0200 Subject: [PATCH 31/32] genenums: add note for when intersperse is stabilised --- scripts/genenums/src/generators/java.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/genenums/src/generators/java.rs b/scripts/genenums/src/generators/java.rs index d8de2b164..bae302048 100644 --- a/scripts/genenums/src/generators/java.rs +++ b/scripts/genenums/src/generators/java.rs @@ -24,6 +24,7 @@ public enum {} {{ auto_gen_comment, package, enum_name ); + // Todo: Consider using https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.intersperse (ONCE STABLE) to insert the commas to avoid needing this special case. for value in values.iter() { result.push_str(" "); result.push_str(&value.to_case(Case::UpperSnake)); From 765ccd269c05bc558e79e479a8b964687d464bba Mon Sep 17 00:00:00 2001 From: Arby Djabaev Date: Tue, 3 Sep 2024 02:53:39 +0200 Subject: [PATCH 32/32] java: working on measure function stuff --- .../java/com/dioxuslabs/taffy/Measure.java | 75 +++++++++++++++++++ .../com/dioxuslabs/taffy/common/Text.java | 50 ++++++++++++- .../com/dioxuslabs/taffy/MeasureFunction.java | 15 ++++ .../java/com/dioxuslabs/taffy/TaffyTree.java | 6 ++ .../java/com/dioxuslabs/taffy/geom/Size.java | 6 ++ .../taffy/geom/measure/AvailableSpace.java | 16 ++++ .../com/dioxuslabs/taffy/style/Style.java | 4 + 7 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 bindings/java/java/examples/java/com/dioxuslabs/taffy/Measure.java create mode 100644 bindings/java/java/src/main/java/com/dioxuslabs/taffy/MeasureFunction.java diff --git a/bindings/java/java/examples/java/com/dioxuslabs/taffy/Measure.java b/bindings/java/java/examples/java/com/dioxuslabs/taffy/Measure.java new file mode 100644 index 000000000..0f31b9eab --- /dev/null +++ b/bindings/java/java/examples/java/com/dioxuslabs/taffy/Measure.java @@ -0,0 +1,75 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.common.Image.ImageContext; +import com.dioxuslabs.taffy.common.Text; +import com.dioxuslabs.taffy.common.Text.FontMetrics; +import com.dioxuslabs.taffy.common.Text.TextContext; +import com.dioxuslabs.taffy.enums.Display; +import com.dioxuslabs.taffy.enums.FlexDirection; +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.AvailableSpace; +import com.dioxuslabs.taffy.geom.measure.Dimension; +import com.dioxuslabs.taffy.style.Style; + +import java.util.List; + +import static com.dioxuslabs.taffy.common.Image.imageMeasureFunction; +import static com.dioxuslabs.taffy.common.Text.LOREM_IPSUM; +import static com.dioxuslabs.taffy.common.Text.textMeasureFunction; + +/** + * Equivalent of taffy/examples/measure.rs + */ +public class Measure { + public static Size measureFunction( + Size knownDimensions, + Size availableSpace, + Object nodeContext, + FontMetrics fontMetrics + ) { + if (knownDimensions.bothAxisDefined()) { + return new Size<>(knownDimensions.width(), knownDimensions.height()); + } + + if (nodeContext instanceof TextContext nc) { + return textMeasureFunction(knownDimensions, availableSpace, nc, fontMetrics); + } else if (nodeContext instanceof ImageContext nc) { + return imageMeasureFunction(knownDimensions, nc); + } else { + return Size.zero(Float.class); + } + } + + public static void main(String[] args) { + TaffyTree taffy = new TaffyTree(); + + FontMetrics fontMetrics = new FontMetrics(10f, 10f); + + long textNode = taffy.newLeafWithContext( + Style.def(), + new TextContext(LOREM_IPSUM, Text.WritingMode.HORIZONTAL) + ); + + long imageNode = taffy.newLeafWithContext( + Style.def(), + new ImageContext(400f, 300f) + ); + + long root = taffy.newWithChildren( + Style.builder() + .display(Display.FLEX) + .flexDirection(FlexDirection.COLUMN) + .size(new Size<>(Dimension.length(200), Dimension.auto())), + List.of(textNode, imageNode) + ); + + // Compute layout and print result + taffy.computeLayoutWithMeasure( + root, + Size.maxContentAvailableSize() + // todo measure func + ); + + taffy.printTree(root); + } +} diff --git a/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Text.java b/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Text.java index 321a1aeae..2fe5b4695 100644 --- a/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Text.java +++ b/bindings/java/java/examples/java/com/dioxuslabs/taffy/common/Text.java @@ -46,7 +46,53 @@ public static Size textMeasureFunction( AtomicInteger maxLineLength = new AtomicInteger(); Arrays.stream(words).forEach(line -> maxLineLength.addAndGet(line.length())); - float inlineSize = - knownDimensions.getAbs(inlineAxis) + Float abs = knownDimensions.getAbs(inlineAxis); + float inlineSize; + if (abs == null) { + AvailableSpace as = availableSpace.getAbs(inlineAxis); + if (as.isMinContent()) { + inlineSize = minLineLength * fontMetrics.charWidth; + } else if (as.isMaxContent()) { + inlineSize = maxLineLength.get() * fontMetrics.charWidth; + } else { + inlineSize = Math.max(Math.min(as.val(), maxLineLength.get() * fontMetrics.charWidth), minLineLength * fontMetrics.charWidth); + } + } else { + inlineSize = abs; + } + + abs = knownDimensions.getAbs(blockAxis); + float blockSize; + if (abs == null) { + int inlineLineLength = (int) Math.floor(inlineSize / fontMetrics.charWidth); + int lineCount = 1; + int currentLineLength = 0; + + for (String word : words) { + if (currentLineLength == 0) { + // first word + currentLineLength = word.length(); + } else if (currentLineLength + word.length() + 1 > inlineLineLength) { + // every word past the first needs to check for line length including the space between words + // note: a real implementation of this should handle whitespace characters other than ' ' + // and do something more sophisticated for long words + lineCount += 1; + currentLineLength = word.length(); + } else { + // add the word and a space + currentLineLength += word.length() + 1; + } + } + + blockSize = lineCount * fontMetrics.charHeight; + } else { + blockSize = abs; + } + + if (textContext.writingMode == WritingMode.HORIZONTAL) { + return new Size<>(inlineSize, blockSize); + } else { + return new Size<>(blockSize, inlineSize); + } } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/MeasureFunction.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/MeasureFunction.java new file mode 100644 index 000000000..d21e357b5 --- /dev/null +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/MeasureFunction.java @@ -0,0 +1,15 @@ +package com.dioxuslabs.taffy; + +import com.dioxuslabs.taffy.geom.Size; +import com.dioxuslabs.taffy.geom.measure.AvailableSpace; +import com.dioxuslabs.taffy.style.Style; + +public interface MeasureFunction { + Size measure( + Size knownDimensions, + Size availableSpace, + long nodeId, + Object nodeContext, + Style style + ); +} diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java index c76363fd4..8f215f1a7 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/TaffyTree.java @@ -10,6 +10,7 @@ public class TaffyTree { public final long ptr; + public Object context; public TaffyTree() { this.ptr = nvNewTaffyTree(); @@ -31,6 +32,11 @@ public long newLeaf(Style style) { return nvNewLeaf(this.ptr, style); } + public long newLeafWithContext(Style style, Object context) { + this.context = context; + return nvNewLeaf(this.ptr, style); + } + public long newWithChildren(Style style, List children) { return nvNewWithChildren(this.ptr, style, children); } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java index 8dee0814b..01582ab0d 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/Size.java @@ -35,6 +35,8 @@ public static Size length(Class clazz, float width, float height) { return (Size) lengthLengthPercentage(width, height); } else if (clazz == LengthPercentageAuto.class) { return (Size) lengthLengthPercentageAuto(width, height); + } else if (clazz == Float.class) { + return (Size) new Size<>(width, height); } return null; } @@ -214,4 +216,8 @@ public String toString() { public T getAbs(AbsoluteAxis axis) { return axis == AbsoluteAxis.HORIZONTAL ? width : height; } + + public boolean bothAxisDefined() { + return width != null && height != null; + } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/AvailableSpace.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/AvailableSpace.java index 4938787df..d99ee61ee 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/AvailableSpace.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/geom/measure/AvailableSpace.java @@ -28,4 +28,20 @@ public boolean equals(Object obj) { } return type == as.type && value == as.value; } + + public boolean isDefinite() { + return type == 0; + } + + public boolean isMinContent() { + return type == 1; + } + + public boolean isMaxContent() { + return type == 2; + } + + public float val() { + return value; + } } diff --git a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java index cd0354cc1..f93b4d963 100644 --- a/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java +++ b/bindings/java/java/src/main/java/com/dioxuslabs/taffy/style/Style.java @@ -65,6 +65,10 @@ public static Style builder() { return new Style(); } + public static Style def() { + return new Style(); + } + public Style display(Display display) { this.display = display; return this;