Skip to content

Commit 5604a7e

Browse files
fix handle size not changing actual size
1 parent 7c6c9b0 commit 5604a7e

File tree

9 files changed

+58
-40
lines changed

9 files changed

+58
-40
lines changed

app/src/main/java/com/smarttoolfactory/composecropper/demo/ImageCropDemo.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import androidx.compose.ui.graphics.Color
2424
import androidx.compose.ui.graphics.ImageBitmap
2525
import androidx.compose.ui.layout.ContentScale
2626
import androidx.compose.ui.platform.LocalContext
27+
import androidx.compose.ui.platform.LocalDensity
2728
import androidx.compose.ui.res.imageResource
2829
import androidx.compose.ui.unit.dp
2930
import com.smarttoolfactory.colorpicker.widget.drawChecker
@@ -46,7 +47,6 @@ internal enum class SelectionPage {
4647
@Composable
4748
fun ImageCropDemo() {
4849

49-
5050
val bottomSheetScaffoldState: BottomSheetScaffoldState = rememberBottomSheetScaffoldState(
5151
bottomSheetState = BottomSheetState(BottomSheetValue.Collapsed)
5252
)
@@ -65,13 +65,16 @@ fun ImageCropDemo() {
6565
)
6666
}
6767

68+
val handleSize: Float = LocalDensity.current.run { 20.dp.toPx() }
69+
6870
var cropProperties by remember {
6971
mutableStateOf(
7072
CropDefaults.properties(
7173
cropOutlineProperty = CropOutlineProperty(
7274
OutlineType.Rect,
73-
RectCropShape(0, "")
74-
)
75+
RectCropShape(0, "Rect")
76+
),
77+
handleSize = handleSize
7578
)
7679
)
7780
}
@@ -117,6 +120,7 @@ fun ImageCropDemo() {
117120
)
118121
} else {
119122
CropStyleSelectionMenu(
123+
cropType = cropProperties.cropType,
120124
cropStyle = cropStyle,
121125
onCropStyleChange = {
122126
cropStyle = it

app/src/main/java/com/smarttoolfactory/composecropper/demo/ImageCropDemoSimple.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import androidx.compose.ui.Modifier
1414
import androidx.compose.ui.graphics.Color
1515
import androidx.compose.ui.graphics.ImageBitmap
1616
import androidx.compose.ui.platform.LocalContext
17+
import androidx.compose.ui.platform.LocalDensity
1718
import androidx.compose.ui.res.imageResource
19+
import androidx.compose.ui.unit.dp
1820
import com.smarttoolfactory.composecropper.R
1921
import com.smarttoolfactory.cropper.ImageCropper
2022
import com.smarttoolfactory.cropper.model.OutlineType
@@ -25,13 +27,16 @@ import com.smarttoolfactory.cropper.settings.CropOutlineProperty
2527
@Composable
2628
fun ImageCropDemoSimple() {
2729

30+
val handleSize: Float = LocalDensity.current.run { 20.dp.toPx() }
31+
2832
val cropProperties by remember {
2933
mutableStateOf(
3034
CropDefaults.properties(
3135
cropOutlineProperty = CropOutlineProperty(
3236
OutlineType.Rect,
3337
RectCropShape(0, "Rect")
34-
)
38+
),
39+
handleSize = handleSize
3540
)
3641
)
3742
}

app/src/main/java/com/smarttoolfactory/composecropper/preferences/CropPropertySelection.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
66
import androidx.compose.runtime.Composable
77
import androidx.compose.runtime.remember
88
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.platform.LocalDensity
910
import androidx.compose.ui.unit.dp
1011
import androidx.compose.ui.unit.sp
1112
import com.smarttoolfactory.cropper.model.AspectRatio
@@ -23,11 +24,14 @@ internal fun CropPropertySelectionMenu(
2324
cropProperties: CropProperties,
2425
onCropPropertiesChange: (CropProperties) -> Unit
2526
) {
27+
28+
val density = LocalDensity.current
29+
2630
// Crop properties
2731
val cropType = cropProperties.cropType
2832
val aspectRatio = cropProperties.aspectRatio
2933

30-
val handleSize = cropProperties.handleSize
34+
val handleSize = density.run { cropProperties.handleSize.toDp() }
3135
val contentScale = cropProperties.contentScale
3236
val cropOutlineProperty = cropProperties.cropOutlineProperty
3337
val overlayRatio = (cropProperties.overlayRatio * 100)
@@ -87,11 +91,11 @@ internal fun CropPropertySelectionMenu(
8791
value = handleSize,
8892
onValueChange = {
8993
onCropPropertiesChange(
90-
cropProperties.copy(handleSize = it)
94+
cropProperties.copy(handleSize = density.run { it.toPx() })
9195
)
9296
},
9397
lowerBound = 10.dp,
94-
upperBound = 50.dp
98+
upperBound = 40.dp
9599
)
96100
}
97101
}
@@ -154,7 +158,6 @@ internal fun CropGestureSelectionMenu(
154158
)
155159
}
156160
}
157-
158161
}
159162

160163
@Composable
@@ -225,4 +228,4 @@ internal fun MaxZoomSelection(
225228
onValueChange = onMaxZoomChange,
226229
valueRange = valueRange
227230
)
228-
}
231+
}

app/src/main/java/com/smarttoolfactory/composecropper/preferences/CropStyleSelection.kt

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ import androidx.compose.ui.unit.dp
1616
import androidx.compose.ui.unit.sp
1717
import com.smarttoolfactory.colorpicker.dialog.ColorPickerRingDiamondHSLDialog
1818
import com.smarttoolfactory.cropper.settings.CropStyle
19+
import com.smarttoolfactory.cropper.settings.CropType
1920

2021
/**
2122
* Crop style selection menu
2223
*/
2324
@Composable
2425
internal fun CropStyleSelectionMenu(
26+
cropType: CropType,
2527
cropStyle: CropStyle,
2628
onCropStyleChange: (CropStyle) -> Unit
2729
) {
@@ -85,16 +87,18 @@ internal fun CropStyleSelectionMenu(
8587
}
8688
)
8789

88-
Spacer(modifier = Modifier.height(20.dp))
89-
ColorSelection(
90-
title = "Handle Color",
91-
color = handleColor,
92-
onColorChange = { color: Color ->
93-
onCropStyleChange(
94-
cropStyle.copy(handleColor = color)
95-
)
96-
}
97-
)
90+
if(cropType== CropType.Dynamic){
91+
Spacer(modifier = Modifier.height(20.dp))
92+
ColorSelection(
93+
title = "Handle Color",
94+
color = handleColor,
95+
onColorChange = { color: Color ->
96+
onCropStyleChange(
97+
cropStyle.copy(handleColor = color)
98+
)
99+
}
100+
)
101+
}
98102

99103
Spacer(modifier = Modifier.height(20.dp))
100104
ColorSelection(

cropper/src/main/java/com/smarttoolfactory/cropper/ImageCropper.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private fun ImageCropper(
185185
containerHeight: Dp,
186186
imageWidthPx: Int,
187187
imageHeightPx: Int,
188-
handleSize: Dp,
188+
handleSize: Float,
189189
cropType: CropType,
190190
cropOutline: CropOutline,
191191
cropStyle: CropStyle,
@@ -248,7 +248,7 @@ private fun ImageCropperImpl(
248248
imageHeightPx: Int,
249249
cropType: CropType,
250250
cropOutline: CropOutline,
251-
handleSize: Dp,
251+
handleSize: Float,
252252
cropStyle: CropStyle,
253253
transparentColor: Color,
254254
rectOverlay: Rect
@@ -271,9 +271,7 @@ private fun ImageCropperImpl(
271271
val handleColor = cropStyle.handleColor
272272
val drawHandles = cropType == CropType.Dynamic
273273
val strokeWidth = cropStyle.strokeWidth
274-
275-
val handleSizeInPx = LocalDensity.current.run { handleSize.toPx() }
276-
274+
277275
DrawingOverlay(
278276
modifier = Modifier.size(containerWidth, containerHeight),
279277
drawOverlay = drawOverlay,
@@ -284,7 +282,7 @@ private fun ImageCropperImpl(
284282
handleColor = handleColor,
285283
strokeWidth = strokeWidth,
286284
drawHandles = drawHandles,
287-
handleSize = handleSizeInPx,
285+
handleSize = handleSize,
288286
transparentColor = transparentColor,
289287
)
290288

cropper/src/main/java/com/smarttoolfactory/cropper/settings/CropDefaults.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ object CropDefaults {
2727
*/
2828
fun properties(
2929
cropType: CropType = CropType.Dynamic,
30-
handleSize: Dp = 20.dp,
30+
handleSize: Float,
3131
maxZoom: Float = 10f,
3232
contentScale: ContentScale = ContentScale.Fit,
3333
cropOutlineProperty: CropOutlineProperty,
@@ -84,7 +84,7 @@ object CropDefaults {
8484
@Immutable
8585
data class CropProperties internal constructor(
8686
val cropType: CropType,
87-
val handleSize: Dp,
87+
val handleSize: Float,
8888
val contentScale: ContentScale,
8989
val cropOutlineProperty: CropOutlineProperty,
9090
val aspectRatio: AspectRatio,

cropper/src/main/java/com/smarttoolfactory/cropper/state/CropState.kt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.smarttoolfactory.cropper.state
22

33
import androidx.compose.runtime.Composable
44
import androidx.compose.runtime.remember
5-
import androidx.compose.ui.platform.LocalDensity
65
import androidx.compose.ui.unit.IntSize
76
import com.smarttoolfactory.cropper.settings.CropProperties
87
import com.smarttoolfactory.cropper.settings.CropType
@@ -40,13 +39,6 @@ fun rememberCropState(
4039
val pannable = cropProperties.pannable
4140
val rotatable = cropProperties.rotatable
4241

43-
val handleSizeInPx: Float
44-
val minOverlaySizePx: Float
45-
46-
with(LocalDensity.current) {
47-
handleSizeInPx = handleSize.toPx()
48-
minOverlaySizePx = minOverlaySize.toPx()
49-
}
5042

5143
return remember(*keys) {
5244
when (cropType) {
@@ -74,8 +66,8 @@ fun rememberCropState(
7466
aspectRatio = aspectRatio,
7567
overlayRatio = overlayRatio,
7668
maxZoom = maxZoom,
77-
handleSize = handleSizeInPx,
78-
minOverlaySize = minOverlaySizePx,
69+
handleSize = handleSize,
70+
minOverlaySize = minOverlaySize,
7971
fling = fling,
8072
zoomable = zoomable,
8173
pannable = pannable,

cropper/src/main/java/com/smarttoolfactory/cropper/state/CropStateImpl.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,18 @@ abstract class CropState internal constructor(
103103

104104
internal suspend fun init() {
105105
// When initial aspect ratio doesn't match drawable area
106-
// overlay gets updated so update draw area as well
106+
// overlay gets updated so updates draw area as well
107107
animateTransformationToOverlayBounds(overlayRect, animate = true)
108108
initialized = true
109109
}
110110

111111
/**
112112
* Update properties of [CropState] and animate to valid intervals if required
113113
*/
114-
internal open suspend fun updateProperties(cropProperties: CropProperties) {
114+
internal open suspend fun updateProperties(
115+
cropProperties: CropProperties,
116+
forceUpdate: Boolean = false
117+
) {
115118

116119
if (!initialized) return
117120

@@ -131,7 +134,8 @@ abstract class CropState internal constructor(
131134
if (
132135
this.aspectRatio.value != aspectRatio.value ||
133136
maxZoom != zoomMax ||
134-
this.overlayRatio != overlayRatio
137+
this.overlayRatio != overlayRatio ||
138+
forceUpdate
135139
) {
136140
this.aspectRatio = aspectRatio
137141
this.overlayRatio = overlayRatio

cropper/src/main/java/com/smarttoolfactory/cropper/state/DynamicCropState.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.compose.ui.input.pointer.positionChangeIgnoreConsumed
88
import androidx.compose.ui.unit.IntSize
99
import com.smarttoolfactory.cropper.TouchRegion
1010
import com.smarttoolfactory.cropper.model.AspectRatio
11+
import com.smarttoolfactory.cropper.settings.CropProperties
1112
import kotlinx.coroutines.coroutineScope
1213

1314
/**
@@ -82,6 +83,13 @@ class DynamicCropState internal constructor(
8283
// inside overlay but with multiple pointers to zoom
8384
private var gestureInvoked = false
8485

86+
override suspend fun updateProperties(cropProperties: CropProperties, forceUpdate: Boolean) {
87+
handleSize = cropProperties.handleSize
88+
minOverlaySize = handleSize * 2
89+
90+
super.updateProperties(cropProperties, forceUpdate)
91+
}
92+
8593
override suspend fun onDown(change: PointerInputChange) {
8694

8795
rectTemp = overlayRect.copy()

0 commit comments

Comments
 (0)