Skip to content

Commit

Permalink
Resolve various runtime errors. (#1948)
Browse files Browse the repository at this point in the history
<!-- start git-machete generated -->

# Based on PR #1947

## Chain of upstream PRs as of 2024-12-26

* PR #1886:
  `main` ← `feature/protoconv`

  * PR #1941:
    `feature/protoconv` ← `wb/froeht/protolize-dcf`

    * PR #1943:
      `wb/froeht/protolize-dcf` ← `wb/froeht/delete-proto-utils`

      * PR #1946:
`wb/froeht/delete-proto-utils` ←
`wb/froeht/temp-disable-variant-animation-test`

        * PR #1947:
`wb/froeht/temp-disable-variant-animation-test` ←
`wb/froeht/remove-serde-from-kotlin`

          * **PR #1948 (THIS ONE)**:
`wb/froeht/remove-serde-from-kotlin` ←
`wb/froeht/resolve-runtime-exceptions`

<!-- end git-machete generated -->

Protobuf maps are immutable and need to be modified by copying them.
  • Loading branch information
timothyfroehlich authored Dec 26, 2024
1 parent 1cfd0c4 commit b7e4597
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 51 deletions.
Binary file modified crates/figma_import/tests/layout-unit-tests.dcf
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.android.designcompose.definition.element.NumOrVar
import com.android.designcompose.definition.element.Variable
import com.android.designcompose.definition.element.VariableMap
import com.android.designcompose.definition.element.VariableValue
import com.android.designcompose.definition.element.copy
import com.android.designcompose.utils.toColor

// A variable mode, e.g. "light" or "dark"
Expand Down Expand Up @@ -170,20 +171,21 @@ internal object VariableManager {

internal fun init(docId: DesignDocId, map: VariableMap) {

// Remove old entries for docId
val oldVarMap = docVarMap[docId.id]
oldVarMap?.collectionsMap?.forEach { varMap.collectionsMap.remove(it.key) }
oldVarMap?.collectionNameMapMap?.forEach { varMap.collectionNameMapMap.remove(it.key) }
oldVarMap?.variablesMap?.forEach { varMap.variablesMap.remove(it.key) }
oldVarMap?.variableNameMapMap?.forEach { varMap.variableNameMapMap.remove(it.key) }

// Add new entries for docId
docVarMap[docId.id] = map
varMap.collectionsMap.putAll(map.collectionsMap)
varMap.collectionNameMapMap.putAll(map.collectionNameMapMap)
varMap.variablesMap.putAll(map.variablesMap)
varMap.variableNameMapMap.putAll(map.variableNameMapMap)

varMap = varMap.copy {
// Remove old entries for docId
val oldVarMap = docVarMap[docId.id]
oldVarMap?.collectionsMap?.forEach { this.collections.remove(it.key) }
oldVarMap?.collectionNameMapMap?.forEach { this.collectionNameMap.remove(it.key) }
oldVarMap?.variablesMap?.forEach { this.variables.remove(it.key) }
oldVarMap?.variableNameMapMap?.forEach { this.variableNameMap.remove(it.key) }

// Add new entries for docId
docVarMap[docId.id] = map
this.collections.putAll(map.collectionsMap)
this.collectionNameMap.putAll(map.collectionNameMapMap)
this.variables.putAll(map.variablesMap)
this.variableNameMap.putAll(map.variableNameMapMap)
}
currentDocId = docId
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ private fun updateLayoutTree(

layoutNodes.add(
layoutNode {
layoutId
parentLayoutId
-1 // not childIdx!
resolvedNode.style.layoutStyle
resolvedNode.view.name
useMeasureFunc
null
null
this.layoutId = layoutId
this.parentLayoutId = parentLayoutId
this.childIndex = -1 // not childIdx!
this.style = resolvedNode.style.layoutStyle
this.name = resolvedNode.view.name
this.useMeasureFunc = useMeasureFunc
clearFixedWidth()
clearFixedHeight()
}
)
layoutCache[layoutId] = layoutCacheKey
Expand Down Expand Up @@ -296,8 +296,8 @@ internal fun layoutTree(
val layoutParentChildren = arrayListOf<LayoutParentChildren>()
updateLayoutTree(manager, root, layoutCache, layoutNodes, layoutParentChildren)
val layoutNodeList = layoutNodeList {
layoutNodes
layoutParentChildren
this.layoutNodes.addAll(layoutNodes)
this.parentChildren.addAll(layoutParentChildren)
}

// Now we can give the new layoutNodeList to the Rust JNI layout implementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import com.android.designcompose.definition.view.View
import com.android.designcompose.definition.view.ViewDataKt.container
import com.android.designcompose.definition.view.ViewStyle
import com.android.designcompose.definition.view.containerOrNull
import com.android.designcompose.definition.view.copy
import com.android.designcompose.definition.view.frameExtrasOrNull
import com.android.designcompose.definition.view.overridesOrNull
import com.android.designcompose.definition.view.styleOrNull
Expand Down Expand Up @@ -566,59 +567,61 @@ private fun generateOverlayNode(
}
}

val nodeStyleBuilder = node.style.nodeStyle.toBuilder()
nodeStyleBuilder.overflow = Overflow.OVERFLOW_VISIBLE
nodeStyleBuilder.backgroundsList.clear()

overlay.overlayBackgroundOrNull?.colorOrNull?.let {
val bgColor =
com.android.designcompose.definition.element.color {
r = (it.r * 255.0).toInt()
g = (it.g * 255.0).toInt()
b = (it.b * 255.0).toInt()
a = (it.a * 255.0).toInt()
}
nodeStyleBuilder.backgroundsList.add(background { solid = colorOrVar { color = bgColor } })
val newNodeStyle = node.style.nodeStyle.copy {
this.overflow = Overflow.OVERFLOW_VISIBLE
this.backgrounds.clear()

overlay.overlayBackgroundOrNull?.colorOrNull?.let {
val bgColor =
com.android.designcompose.definition.element.color {
r = (it.r * 255.0).toInt()
g = (it.g * 255.0).toInt()
b = (it.b * 255.0).toInt()
a = (it.a * 255.0).toInt()
}
this.backgrounds.add(background { solid = colorOrVar { color = bgColor } })
}
}


val overlayStyle = viewStyle {
layoutStyle = layoutStyleBuilder.build()
nodeStyle = nodeStyleBuilder.build()
this.layoutStyle = layoutStyleBuilder.build()
this.nodeStyle = newNodeStyle
}

// Now synthesize a view.
val overlayViewData = container {
shape = viewShape { rect = box { isMask = false } }
children.add(node.view)
this.shape = viewShape { rect = box { isMask = false } }
this.children.add(node.view)
}

val overlayScrollInfo = scrollInfo {
pagedScrolling = false
overflow = OverflowDirection.OVERFLOW_DIRECTION_NONE
this.pagedScrolling = false
this.overflow = OverflowDirection.OVERFLOW_DIRECTION_NONE
}

if (node.view.uniqueId !in 0..0xFFFF) {
throw RuntimeException("View's unique ID must be in the range 0..0xFFFF")
}
val overlayView = view {
uniqueId = (node.view.uniqueId + 0x2000)
id = "overlay-${node.view.id}"
name = "Overlay ${node.view.name}"
this.uniqueId = (node.view.uniqueId + 0x2000)
this.id = "overlay-${node.view.id}"
this.name = "Overlay ${node.view.name}"
if (
overlay.overlayBackgroundInteraction ==
OverlayBackgroundInteraction.OVERLAY_BACKGROUND_INTERACTION_CLOSE_ON_CLICK_OUTSIDE
) {
reactions.add(
this.reactions.add(
reaction {
trigger = trigger { click = empty {} }
action = action { close = empty {} }
}
)
}
scrollInfo = overlayScrollInfo
style = overlayStyle
data = viewData { container = overlayViewData }
renderMethod = View.RenderMethod.RENDER_METHOD_NONE
this.scrollInfo = overlayScrollInfo
this.style = overlayStyle
this.data = viewData { container = overlayViewData }
this.renderMethod = View.RenderMethod.RENDER_METHOD_NONE
}
val overlayLayoutId = layoutIdAllocator.listLayoutId(node.layoutId)
val layoutId =
Expand Down
Binary file not shown.

0 comments on commit b7e4597

Please sign in to comment.