Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ internal fun rememberConstraintLayoutMeasurePolicy(
}
}
}

override fun override(name: String, value: Float) : ConstraintSet {
// nothing here yet
return this
}
}
val layoutSize = measurer.performMeasure(
constraints,
Expand Down Expand Up @@ -1231,14 +1236,32 @@ interface ConstraintSet {
* Applies the [ConstraintSet] to a state.
*/
fun applyTo(state: State, measurables: List<Measurable>)

fun override(name: String, value: Float) : ConstraintSet
}

fun ConstraintSet(@Language("json5") content : String) = object : ConstraintSet {
private val overridedVariables = HashMap<String, Float>()

override fun applyTo(state: State, measurables: List<Measurable>) {
measurables.forEach { measurable ->
state.map((measurable.layoutId ?: createId()), measurable)
var layoutId = measurable.layoutId ?: measurable.constraintLayoutId ?: createId()
state.map(layoutId, measurable)
var tag = measurable.constraintLayoutTag
if (tag != null && tag is String && layoutId is String) {
state.setTag(layoutId, tag)
}
}
parseJSON(content, state)
val layoutVariables = LayoutVariables()
for (name in overridedVariables.keys) {
layoutVariables.putOverride(name, overridedVariables[name]!!)
}
parseJSON(content, state, layoutVariables)
}

override fun override(name: String, value: Float) : ConstraintSet {
overridedVariables[name] = value
return this
}
}

Expand All @@ -1254,6 +1277,11 @@ fun ConstraintSet(description: ConstraintSetScope.() -> Unit) = object : Constra
scope.description()
scope.applyTo(state)
}

override fun override(name: String, value: Float) : ConstraintSet {
// nothing yet
return this
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.constraintlayout.compose

import androidx.compose.runtime.Immutable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Measurable
import androidx.compose.ui.layout.ParentDataModifier
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.InspectorInfo
import androidx.compose.ui.platform.InspectorValueInfo
import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.unit.Density

fun Modifier.layoutId(layoutId: String, tag: String) = this.then(
ConstraintLayoutTag(
constraintLayoutId = layoutId,
constraintLayoutTag = tag,
inspectorInfo = debugInspectorInfo {
name = "constraintLayoutId"
value = layoutId
}
)
)

@Immutable
private class ConstraintLayoutTag(
override val constraintLayoutTag: String,
override val constraintLayoutId: String,
inspectorInfo: InspectorInfo.() -> Unit
) : ParentDataModifier, ConstraintLayoutTagParentData, InspectorValueInfo(inspectorInfo) {
override fun Density.modifyParentData(parentData: Any?): Any? {
return this@ConstraintLayoutTag
}

override fun hashCode(): Int =
constraintLayoutTag.hashCode()

override fun equals(other: Any?): Boolean {
if (this === other) return true
val otherModifier = other as? ConstraintLayoutTag ?: return false
return constraintLayoutTag == otherModifier.constraintLayoutTag
}

override fun toString(): String =
"ConstraintLayoutTag(id=$constraintLayoutTag)"
}

interface ConstraintLayoutTagParentData {
val constraintLayoutId: String
val constraintLayoutTag: String
}

val Measurable.constraintLayoutTag: Any?
get() = (parentData as? ConstraintLayoutTagParentData)?.constraintLayoutTag

val Measurable.constraintLayoutId: Any?
get() = (parentData as? ConstraintLayoutTagParentData)?.constraintLayoutId
Loading