Change state propagation and rectangle selection

This commit is contained in:
Alexander Nozik 2022-09-13 19:01:23 +03:00
parent 6b0a2566bd
commit fa51fc03db
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
3 changed files with 11 additions and 11 deletions

View File

@ -96,8 +96,8 @@ fun App() {
centerCoordinates?.let { centerCoordinates?.let {
group(id = "center") { group(id = "center") {
circle(center = it, color = Color.Blue, size = 1f) circle(center = it, color = Color.Blue, id = "circle", size = 1f)
text(position = it, it.toShortString(), color = Color.Blue) text(position = it, it.toShortString(), id = "text", color = Color.Blue)
} }
} }

View File

@ -30,7 +30,7 @@ public interface MapFeatureBuilder {
public val features: MutableMap<FeatureId, MapFeature> public val features: MutableMap<FeatureId, MapFeature>
public fun attributes(): Map<FeatureId, MapFeatureAttributeSet> public val attributes: Map<FeatureId, MapFeatureAttributeSet>
//TODO use context receiver for that //TODO use context receiver for that
public fun FeatureId.draggable(enabled: Boolean = true) { public fun FeatureId.draggable(enabled: Boolean = true) {
@ -42,7 +42,7 @@ internal class MapFeatureBuilderImpl(
override val features: SnapshotStateMap<FeatureId, MapFeature>, override val features: SnapshotStateMap<FeatureId, MapFeature>,
) : MapFeatureBuilder { ) : MapFeatureBuilder {
private val attributes = SnapshotStateMap<FeatureId, SnapshotStateMap<MapFeatureAttributeKey<out Any?>, in Any?>>() private val _attributes = SnapshotStateMap<FeatureId, SnapshotStateMap<MapFeatureAttributeKey<out Any?>, in Any?>>()
private fun generateID(feature: MapFeature): FeatureId = "@feature[${feature.hashCode().toUInt()}]" private fun generateID(feature: MapFeature): FeatureId = "@feature[${feature.hashCode().toUInt()}]"
@ -54,11 +54,11 @@ internal class MapFeatureBuilderImpl(
} }
override fun <T> setAttribute(id: FeatureId, key: MapFeatureAttributeKey<T>, value: T) { override fun <T> setAttribute(id: FeatureId, key: MapFeatureAttributeKey<T>, value: T) {
attributes.getOrPut(id) { SnapshotStateMap() }[key] = value _attributes.getOrPut(id) { SnapshotStateMap() }[key] = value
} }
override fun attributes(): Map<FeatureId, MapFeatureAttributeSet> = override val attributes: Map<FeatureId, MapFeatureAttributeSet>
attributes.mapValues { MapFeatureAttributeSet(it.value) } get() = _attributes.mapValues { MapFeatureAttributeSet(it.value) }
} }

View File

@ -104,15 +104,15 @@ public fun MapView(
buildFeatures: @Composable (MapFeatureBuilder.() -> Unit) = {}, buildFeatures: @Composable (MapFeatureBuilder.() -> Unit) = {},
) { ) {
var viewPointOverride by remember { mutableStateOf(initialViewPoint ?: MapViewPoint.globe) } var viewPointOverride by remember(initialViewPoint, initialRectangle) { mutableStateOf(initialViewPoint ?: MapViewPoint.globe) }
val featuresBuilder = MapFeatureBuilderImpl(mutableStateMapOf()).apply { buildFeatures() } val featuresBuilder = MapFeatureBuilderImpl(mutableStateMapOf()).apply { buildFeatures() }
val features: SnapshotStateMap<FeatureId, MapFeature> = remember { featuresBuilder.features } val features: SnapshotStateMap<FeatureId, MapFeature> = remember(buildFeatures) { featuresBuilder.features }
val attributes = remember { featuresBuilder.attributes() } val attributes = remember(buildFeatures) { featuresBuilder.attributes }
val featureDrag = remember { val featureDrag by derivedStateOf {
DragHandle.withPrimaryButton { _, start, end -> DragHandle.withPrimaryButton { _, start, end ->
val zoom = start.zoom val zoom = start.zoom
attributes.filterValues { attributes.filterValues {