gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 492HTML 238TOML 3CSS 11131 Definiciones

Hold weak reference from rust to measure func

313f2910ecab3fac2aaee55e751d579ee46aa459 · por Emil Sjölander · (hace 7 años) · padre 40d32f4a0 · ver el árbol en este commit

Delta semántico

Ninguna definición cambió: el cambio es de texto, documentación o datos.

3 archivos · +18 −10

diff --git a/bindings/kotlin/.idea/misc.xml b/bindings/kotlin/.idea/misc.xml
index af0bbdde1..c990ec869 100644
--- a/bindings/kotlin/.idea/misc.xml
+++ b/bindings/kotlin/.idea/misc.xml
@@ -5,10 +5,19 @@
       <configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
     </configurations>
   </component>
-  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
+  <component name="CargoProjects">
+    <cargoProject FILE="$PROJECT_DIR$/stretch/src/main/rust/Cargo.toml" />
+  </component>
+  <component name="MacroExpansionManager">
+    <option name="directoryName" value="BoHZVWHY" />
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
     <output url="file://$PROJECT_DIR$/build/classes" />
   </component>
   <component name="ProjectType">
     <option name="id" value="Android" />
   </component>
+  <component name="RustProjectSettings">
+    <option name="toolchainHomeDirectory" value="$USER_HOME$/.cargo/bin" />
+  </component>
 </project>
\ No newline at end of file
diff --git a/bindings/kotlin/stretch/src/main/java/app/visly/stretch/Node.kt b/bindings/kotlin/stretch/src/main/java/app/visly/stretch/Node.kt
index 32494f754..75a982fd1 100644
--- a/bindings/kotlin/stretch/src/main/java/app/visly/stretch/Node.kt
+++ b/bindings/kotlin/stretch/src/main/java/app/visly/stretch/Node.kt
@@ -1,13 +1,14 @@
 package app.visly.stretch
 import androidx.annotation.Keep
+import java.lang.ref.WeakReference
 
 interface MeasureFunc {
     fun measure(constraints: Size<Float?>): Size<Float>
 }
 
-private class MeasureFuncImpl(val measureFunc: MeasureFunc) {
+private class MeasureFuncImpl(val measureFunc: WeakReference<MeasureFunc>) {
     @Keep fun measure(width: Float, height: Float): FloatArray {
-        val result = measureFunc.measure(Size(
+        val result = measureFunc.get()!!.measure(Size(
             if (width.isNaN()) null else width,
             if (height.isNaN()) null else height
         ))
@@ -25,11 +26,13 @@ class Node {
     private val rustptr: Long
     private var style: Style
     private var children: MutableList<Node>
+    private var measure: MeasureFunc? = null
 
     constructor(style: Style, measure: MeasureFunc) {
-        this.rustptr = nConstructLeaf(style.rustptr, MeasureFuncImpl(measure))
+        this.rustptr = nConstructLeaf(style.rustptr, MeasureFuncImpl(WeakReference(measure)))
         this.style = style
         this.children = mutableListOf()
+        this.measure = measure
     }
 
     constructor(style: Style, children: List<Node>) {
@@ -43,7 +46,8 @@ class Node {
     }
 
     fun setMeasure(measure: MeasureFunc) {
-        nSetMeasure(rustptr, MeasureFuncImpl(measure))
+        nSetMeasure(rustptr, MeasureFuncImpl(WeakReference(measure)))
+        this.measure = measure
     }
 
     fun getChildren(): List<Node> {
diff --git a/bindings/kotlin/stretch/src/main/java/app/visly/stretch/Stretch.kt b/bindings/kotlin/stretch/src/main/java/app/visly/stretch/Stretch.kt
index 149c6c82f..b1338a148 100644
--- a/bindings/kotlin/stretch/src/main/java/app/visly/stretch/Stretch.kt
+++ b/bindings/kotlin/stretch/src/main/java/app/visly/stretch/Stretch.kt
@@ -1,10 +1,5 @@
 package app.visly.stretch
 
-// TODO
-// - Change measure to be a weak ref to node instance which in turn calls passed in measure function implementation from java.
-//   Problem with this is jni library currently does not support weak refs. This can be worked around by creating weak ref on java side.
-// - Integration tests to ensure bindings don't break
-
 class Stretch {
     companion object {
         private var didInit = false