Skip to content

Commit 5c57c87

Browse files
update Modifier.zoom and ZoomState
1 parent 3d24434 commit 5c57c87

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

image/src/main/java/com/smarttoolfactory/image/zoom/ZoomModifier.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.smarttoolfactory.image.zoom
22

33
import androidx.compose.foundation.gestures.detectTapGestures
4-
import androidx.compose.runtime.rememberCoroutineScope
4+
import androidx.compose.runtime.*
55
import androidx.compose.ui.Modifier
66
import androidx.compose.ui.composed
77
import androidx.compose.ui.draw.clipToBounds
@@ -50,6 +50,8 @@ fun Modifier.zoom(
5050
val boundPan = limitPan && !rotationEnabled
5151
val clipToBounds = (clip || boundPan)
5252

53+
var zoomLevel by remember { mutableStateOf(ZoomLevel.Min) }
54+
5355
val transformModifier = Modifier.pointerInput(keys) {
5456
detectTransformGestures(
5557
consume = consume,
@@ -118,13 +120,13 @@ fun Modifier.zoom(
118120
onDoubleTap = {
119121

120122
val (newZoomLevel, newZoom) = calculateZoom(
121-
zoomLevel = zoomState.zoomLevel,
123+
zoomLevel = zoomLevel,
122124
initial = zoomState.zoomInitial,
123125
min = zoomState.zoomMin,
124126
max = zoomState.zoomMax
125127
)
126128

127-
zoomState.zoomLevel = newZoomLevel
129+
zoomLevel = newZoomLevel
128130

129131
coroutineScope.launch {
130132
zoomState.animatePanTo(Offset.Zero)

image/src/main/java/com/smarttoolfactory/image/zoom/ZoomState.kt

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ package com.smarttoolfactory.image.zoom
33
import androidx.compose.animation.core.Animatable
44
import androidx.compose.animation.core.VectorConverter
55
import androidx.compose.runtime.Composable
6+
import androidx.compose.runtime.Immutable
67
import androidx.compose.runtime.remember
8+
import androidx.compose.ui.Modifier
79
import androidx.compose.ui.geometry.Offset
810
import kotlinx.coroutines.coroutineScope
911

12+
/**
13+
* * Create and [remember] the [ZoomState] based on the currently appropriate transform
14+
* configuration to allow changing pan, zoom, and rotation.
15+
*
16+
*/
1017
@Composable
1118
fun rememberZoomState(
1219
initialZoom: Float = 1f,
@@ -24,13 +31,18 @@ fun rememberZoomState(
2431
}
2532
}
2633

27-
class ZoomState internal constructor(
34+
/**
35+
* * State of the zoom. Allows the developer to change zoom, pan, translate,
36+
* or get current state by
37+
* calling methods on this object. To be hosted and passed to [Modifier.zoom]
38+
*/
39+
@Immutable
40+
open class ZoomState internal constructor(
2841
initialZoom: Float = 1f,
2942
initialRotation: Float = 0f,
3043
minZoom: Float = 1f,
3144
maxZoom: Float = 5f
3245
) {
33-
internal var zoomLevel = ZoomLevel.Min
3446

3547
internal val zoomMin = minZoom.coerceAtLeast(.5f)
3648
internal val zoomMax = maxZoom.coerceAtLeast(1f)
@@ -69,27 +81,27 @@ class ZoomState internal constructor(
6981
)
7082
}
7183

72-
suspend fun animatePanTo(pan: Offset) = coroutineScope {
84+
internal suspend fun animatePanTo(pan: Offset) = coroutineScope {
7385
animatablePan.animateTo(pan)
7486
}
7587

76-
suspend fun animateZoomTo(zoom: Float) = coroutineScope {
88+
internal suspend fun animateZoomTo(zoom: Float) = coroutineScope {
7789
animatableZoom.animateTo(zoom)
7890
}
7991

80-
suspend fun animateRotationTo(rotation: Float) = coroutineScope {
92+
internal suspend fun animateRotationTo(rotation: Float) = coroutineScope {
8193
animatableRotation.animateTo(rotation)
8294
}
8395

84-
suspend fun snapPanTo(offset: Offset) = coroutineScope {
96+
internal suspend fun snapPanTo(offset: Offset) = coroutineScope {
8597
animatablePan.snapTo(offset)
8698
}
8799

88-
suspend fun snapZoomTo(zoom: Float) = coroutineScope {
100+
internal suspend fun snapZoomTo(zoom: Float) = coroutineScope {
89101
animatableZoom.snapTo(zoom)
90102
}
91103

92-
suspend fun snapRotationTo(rotation: Float) = coroutineScope {
104+
internal suspend fun snapRotationTo(rotation: Float) = coroutineScope {
93105
animatableRotation.snapTo(rotation)
94106
}
95107
}

0 commit comments

Comments
 (0)