Skip to content

Commit e22120c

Browse files
add size param to ZoomState
1 parent 51fa045 commit e22120c

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

image/src/main/java/com/smarttoolfactory/image/beforeafter/BeforeAfterImageImpl.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,14 @@ internal fun BeforeAfterImageImpl(
165165
val coroutineScope = rememberCoroutineScope()
166166

167167
val transformModifier = Modifier.pointerInput(Unit) {
168+
// Pass size of this Composable this Modifier is attached for constraining operations
169+
// inside this bounds
170+
zoomState.size = this.size
168171
detectTransformGestures(
169172
onGesture = { centroid: Offset, panChange: Offset, zoomChange: Float, _, _, _ ->
170173

171174
coroutineScope.launch {
172175
zoomState.updateZoomState(
173-
size,
174176
centroid = centroid,
175177
panChange = panChange,
176178
zoomChange = zoomChange
@@ -181,6 +183,9 @@ internal fun BeforeAfterImageImpl(
181183
}
182184

183185
val touchModifier = Modifier.pointerInput(Unit) {
186+
// Pass size of this Composable this Modifier is attached for constraining operations
187+
// inside this bounds
188+
zoomState.size = this.size
184189
detectMotionEvents(
185190
onDown = {
186191
val position = it.position
@@ -208,11 +213,7 @@ internal fun BeforeAfterImageImpl(
208213
detectTapGestures(
209214
onDoubleTap = {
210215
coroutineScope.launch {
211-
zoomState.animatePanTo(Offset.Zero)
212-
}
213-
214-
coroutineScope.launch {
215-
zoomState.animateZoomTo(1f)
216+
zoomState.resetWithAnimation()
216217
}
217218
}
218219
)

image/src/main/java/com/smarttoolfactory/image/beforeafter/BeforeAfterLayoutImpl.kt

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ internal fun Layout(
8585
val coroutineScope = rememberCoroutineScope()
8686

8787
val transformModifier = Modifier.pointerInput(Unit) {
88+
// Pass size of this Composable this Modifier is attached for constraining operations
89+
// inside this bounds
90+
zoomState.size = this.size
8891
detectTransformGestures(
8992
onGesture = { centroid: Offset, panChange: Offset, zoomChange: Float, _, _, _ ->
9093

9194
coroutineScope.launch {
9295
zoomState.updateZoomState(
93-
size,
9496
centroid = centroid,
9597
panChange = panChange,
9698
zoomChange = zoomChange
@@ -101,6 +103,9 @@ internal fun Layout(
101103
}
102104

103105
val touchModifier = Modifier.pointerInput(Unit) {
106+
// Pass size of this Composable this Modifier is attached for constraining operations
107+
// inside this bounds
108+
zoomState.size = this.size
104109
detectMotionEvents(
105110
onDown = {
106111
val position = it.position
@@ -125,14 +130,13 @@ internal fun Layout(
125130
}
126131

127132
val tapModifier = Modifier.pointerInput(Unit) {
133+
// Pass size of this Composable this Modifier is attached for constraining operations
134+
// inside this bounds
135+
zoomState.size = this.size
128136
detectTapGestures(
129137
onDoubleTap = {
130138
coroutineScope.launch {
131-
zoomState.animatePanTo(Offset.Zero)
132-
}
133-
134-
coroutineScope.launch {
135-
zoomState.animateZoomTo(1f)
139+
zoomState.resetWithAnimation()
136140
}
137141
}
138142
)

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ data class ZoomData(
1717

1818
/**
1919
* Class that contains current zoom, pan and rotation, and rectangle of zoomed and panned area
20-
* information
2120
*/
2221
@Immutable
2322
data class EnhancedZoomData(
2423
val zoom: Float = 1f,
2524
val pan: Offset = Offset.Zero,
2625
val rotation: Float = 0f,
27-
val drawRect: Rect,
28-
val cropRect: Rect
26+
val imageRegion: Rect,
27+
val visibleRegion: Rect
2928
)

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import kotlinx.coroutines.launch
3131
* of this modifier
3232
*/
3333
fun Modifier.zoom(
34-
key: Any?,
34+
key: Any? = Unit,
3535
consume: Boolean = true,
3636
clip: Boolean = true,
3737
zoomState: ZoomState,
@@ -48,6 +48,11 @@ fun Modifier.zoom(
4848
var zoomLevel by remember { mutableStateOf(ZoomLevel.Min) }
4949

5050
val transformModifier = Modifier.pointerInput(key) {
51+
52+
// Pass size of this Composable this Modifier is attached for constraining operations
53+
// inside this bounds
54+
zoomState.size = this.size
55+
5156
detectTransformGestures(
5257
consume = consume,
5358
onGestureStart = {
@@ -60,7 +65,6 @@ fun Modifier.zoom(
6065

6166
coroutineScope.launch {
6267
zoomState.updateZoomState(
63-
size = size,
6468
centroid = centroid,
6569
panChange = pan,
6670
zoomChange = zoom,
@@ -74,6 +78,9 @@ fun Modifier.zoom(
7478
}
7579

7680
val tapModifier = Modifier.pointerInput(key) {
81+
// Pass size of this Composable this Modifier is attached for constraining operations
82+
// inside this bounds
83+
zoomState.size = this.size
7784
detectTapGestures(
7885
onDoubleTap = {
7986

@@ -157,6 +164,9 @@ fun Modifier.zoom(
157164
var zoomLevel by remember { mutableStateOf(ZoomLevel.Min) }
158165

159166
val transformModifier = Modifier.pointerInput(key1, key2) {
167+
// Pass size of this Composable this Modifier is attached for constraining operations
168+
// inside this bounds
169+
zoomState.size = this.size
160170
detectTransformGestures(
161171
consume = consume,
162172
onGestureStart = {
@@ -169,7 +179,6 @@ fun Modifier.zoom(
169179

170180
coroutineScope.launch {
171181
zoomState.updateZoomState(
172-
size = size,
173182
centroid = centroid,
174183
panChange = pan,
175184
zoomChange = zoom,
@@ -183,6 +192,9 @@ fun Modifier.zoom(
183192
}
184193

185194
val tapModifier = Modifier.pointerInput(key1, key2) {
195+
// Pass size of this Composable this Modifier is attached for constraining operations
196+
// inside this bounds
197+
zoomState.size = this.size
186198
detectTapGestures(
187199
onDoubleTap = {
188200

@@ -266,6 +278,9 @@ fun Modifier.zoom(
266278
var zoomLevel by remember { mutableStateOf(ZoomLevel.Min) }
267279

268280
val transformModifier = Modifier.pointerInput(keys) {
281+
// Pass size of this Composable this Modifier is attached for constraining operations
282+
// inside this bounds
283+
zoomState.size = this.size
269284
detectTransformGestures(
270285
consume = consume,
271286
onGestureStart = {
@@ -278,7 +293,6 @@ fun Modifier.zoom(
278293

279294
coroutineScope.launch {
280295
zoomState.updateZoomState(
281-
size = size,
282296
centroid = centroid,
283297
panChange = pan,
284298
zoomChange = zoom,
@@ -292,6 +306,9 @@ fun Modifier.zoom(
292306
}
293307

294308
val tapModifier = Modifier.pointerInput(keys) {
309+
// Pass size of this Composable this Modifier is attached for constraining operations
310+
// inside this bounds
311+
zoomState.size = this.size
295312
detectTapGestures(
296313
onDoubleTap = {
297314

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

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

33
import androidx.compose.animation.core.Animatable
44
import androidx.compose.animation.core.VectorConverter
5-
import androidx.compose.runtime.Immutable
5+
import androidx.compose.runtime.Stable
66
import androidx.compose.ui.Modifier
77
import androidx.compose.ui.geometry.Offset
88
import androidx.compose.ui.unit.IntSize
@@ -21,7 +21,7 @@ import kotlinx.coroutines.launch
2121
* @param panEnabled when set to true pan is enabled
2222
* @param rotationEnabled when set to true rotation is enabled
2323
*/
24-
@Immutable
24+
@Stable
2525
open class ZoomState(
2626
initialZoom: Float = 1f,
2727
initialRotation: Float = 0f,
@@ -42,6 +42,8 @@ open class ZoomState(
4242
internal val animatableZoom = Animatable(zoomInitial)
4343
internal val animatableRotation = Animatable(rotationInitial)
4444

45+
internal var size: IntSize = IntSize.Zero
46+
4547
init {
4648
require(zoomMax >= zoomMin)
4749
}
@@ -81,8 +83,14 @@ open class ZoomState(
8183
return Offset(maxX, maxY)
8284
}
8385

86+
/**
87+
* Get bounds of Composables that can be panned based on zoom level using [size]
88+
*/
89+
protected fun getBounds(): Offset {
90+
return getBounds(size)
91+
}
92+
8493
open suspend fun updateZoomState(
85-
size: IntSize,
8694
centroid: Offset,
8795
panChange: Offset,
8896
zoomChange: Float,

0 commit comments

Comments
 (0)