Skip to content

Commit 7c6c9b0

Browse files
add option to change overlay ratio to screen #4
* Adds option to change overlay ratio to screen dimension with smaller size. If width is smaller than height percentage of width is used to scale overlay * Adjusts style of TextFiled and Sliders for better settings UI
1 parent f68e66d commit 7c6c9b0

File tree

13 files changed

+97
-54
lines changed

13 files changed

+97
-54
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ internal fun CropPropertySelectionMenu(
2626
// Crop properties
2727
val cropType = cropProperties.cropType
2828
val aspectRatio = cropProperties.aspectRatio
29+
2930
val handleSize = cropProperties.handleSize
3031
val contentScale = cropProperties.contentScale
3132
val cropOutlineProperty = cropProperties.cropOutlineProperty
33+
val overlayRatio = (cropProperties.overlayRatio * 100)
3234

3335
Title("Crop Type")
3436
CropTypeDialogSelection(
@@ -69,6 +71,15 @@ internal fun CropPropertySelectionMenu(
6971
}
7072
)
7173

74+
Title("Overlay Ratio ${overlayRatio.toInt()}%")
75+
SliderSelection(
76+
value = overlayRatio, valueRange = 50f..100f
77+
) {
78+
onCropPropertiesChange(
79+
cropProperties.copy(overlayRatio = (it.toInt() / 100f))
80+
)
81+
}
82+
7283
// Handle size and overlay size applies only to Dynamic crop
7384
if (cropType == CropType.Dynamic) {
7485
Title("Handle Size")
@@ -96,15 +107,6 @@ internal fun CropGestureSelectionMenu(
96107
val zoomable = cropProperties.zoomable
97108
val maxZoom = cropProperties.maxZoom
98109

99-
Title("Fling")
100-
FlingEnableSelection(
101-
flingEnabled = flingEnabled,
102-
onFlingEnabledChange = {
103-
onCropPropertiesChange(
104-
cropProperties.copy(fling = it)
105-
)
106-
}
107-
)
108110
Title("Pan Enabled")
109111
PanEnableSelection(
110112
panEnabled = pannable,
@@ -115,6 +117,16 @@ internal fun CropGestureSelectionMenu(
115117
}
116118
)
117119

120+
Title("Fling")
121+
FlingEnableSelection(
122+
flingEnabled = flingEnabled,
123+
onFlingEnabledChange = {
124+
onCropPropertiesChange(
125+
cropProperties.copy(fling = it)
126+
)
127+
}
128+
)
129+
118130
Title("Zoom Enabled")
119131
ZoomEnableSelection(
120132
zoomEnabled = zoomable,

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,15 @@ internal fun SliderWithValueSelection(
7676
modifier = modifier,
7777
verticalAlignment = Alignment.CenterVertically
7878
) {
79-
8079
ColorfulSlider(
8180
modifier = Modifier.weight(1f),
8281
value = value,
8382
onValueChange = onValueChange,
8483
valueRange = valueRange,
8584
colors = colors,
86-
trackHeight = 11.dp,
87-
thumbRadius = 14.dp
85+
trackHeight = 10.dp,
86+
thumbRadius = 12.dp
8887
)
89-
90-
9188
}
9289
}
9390
}
@@ -96,12 +93,13 @@ internal fun SliderWithValueSelection(
9693
internal fun SliderSelection(
9794
modifier: Modifier = Modifier,
9895
value: Float,
99-
onValueChange: (Float) -> Unit,
10096
valueRange: ClosedFloatingPointRange<Float>,
10197
colors: MaterialSliderColors = MaterialSliderDefaults.materialColors(
10298
activeTrackColor = SliderBrushColor(MaterialTheme.colorScheme.primary),
10399
inactiveTrackColor = SliderBrushColor(Color.Transparent)
104-
)
100+
),
101+
onValueChangeFinished: (() -> Unit)? = null,
102+
onValueChange: (Float) -> Unit,
105103
) {
106104
ColorfulSlider(
107105
modifier = modifier,
@@ -110,8 +108,9 @@ internal fun SliderSelection(
110108
valueRange = valueRange,
111109
colors = colors,
112110
borderStroke = BorderStroke(2.dp, MaterialTheme.colorScheme.primary),
113-
trackHeight = 11.dp,
114-
thumbRadius = 14.dp
111+
trackHeight = 10.dp,
112+
thumbRadius = 12.dp,
113+
onValueChangeFinished = onValueChangeFinished
115114
)
116115
}
117116

@@ -161,6 +160,21 @@ internal fun FullRowSwitch(
161160
}
162161
}
163162

163+
@OptIn(ExperimentalMaterial3Api::class)
164+
@Composable
165+
internal fun CropTextField(value: String, onValueChange: (String) -> Unit) {
166+
TextField(
167+
value = value,
168+
onValueChange = onValueChange,
169+
colors = TextFieldDefaults.textFieldColors(
170+
containerColor = Color.Transparent,
171+
focusedIndicatorColor = Color.Transparent,
172+
unfocusedIndicatorColor = Color.Transparent,
173+
disabledIndicatorColor = Color.Transparent
174+
)
175+
)
176+
}
177+
164178
@Composable
165179
internal fun DialogWithMultipleSelection(
166180
title: String = "",

app/src/main/java/com/smarttoolfactory/composecropper/preferences/frames/edit/CustomPathEdit.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@ import androidx.compose.foundation.layout.Box
44
import androidx.compose.foundation.layout.Column
55
import androidx.compose.foundation.layout.aspectRatio
66
import androidx.compose.foundation.layout.fillMaxWidth
7-
import androidx.compose.material3.ExperimentalMaterial3Api
8-
import androidx.compose.material3.TextField
97
import androidx.compose.runtime.*
108
import androidx.compose.ui.Modifier
119
import androidx.compose.ui.draw.clipToBounds
1210
import androidx.compose.ui.draw.drawWithCache
1311
import androidx.compose.ui.graphics.Color
1412
import androidx.compose.ui.graphics.ImageBitmap
1513
import androidx.compose.ui.graphics.Path
14+
import com.smarttoolfactory.composecropper.preferences.CropTextField
1615
import com.smarttoolfactory.cropper.model.AspectRatio
1716
import com.smarttoolfactory.cropper.model.CustomPathOutline
1817
import com.smarttoolfactory.cropper.util.calculateSizeAndOffsetFromAspectRatio
1918
import com.smarttoolfactory.cropper.util.drawBlockWithCheckerAndLayer
2019
import com.smarttoolfactory.cropper.util.scaleAndTranslatePath
2120

22-
@OptIn(ExperimentalMaterial3Api::class)
2321
@Composable
2422
internal fun CustomPathEdit(
2523
aspectRatio: AspectRatio,
@@ -62,7 +60,7 @@ internal fun CustomPathEdit(
6260

6361
)
6462

65-
TextField(
63+
CropTextField(
6664
value = newTitle,
6765
onValueChange = {
6866
newTitle = it

app/src/main/java/com/smarttoolfactory/composecropper/preferences/frames/edit/CutCornerCropShapeEdit.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@ package com.smarttoolfactory.composecropper.preferences.frames.edit
22

33
import androidx.compose.foundation.layout.*
44
import androidx.compose.foundation.shape.CutCornerShape
5-
import androidx.compose.material3.ExperimentalMaterial3Api
6-
import androidx.compose.material3.TextField
75
import androidx.compose.runtime.*
86
import androidx.compose.ui.Modifier
97
import androidx.compose.ui.draw.clipToBounds
108
import androidx.compose.ui.graphics.ImageBitmap
119
import androidx.compose.ui.platform.LocalDensity
1210
import androidx.compose.ui.unit.dp
11+
import com.smarttoolfactory.composecropper.preferences.CropTextField
1312
import com.smarttoolfactory.composecropper.preferences.SliderWithValueSelection
1413
import com.smarttoolfactory.cropper.model.AspectRatio
1514
import com.smarttoolfactory.cropper.model.CornerRadiusProperties
1615
import com.smarttoolfactory.cropper.model.CutCornerCropShape
1716
import com.smarttoolfactory.cropper.util.drawOutlineWithBlendModeAndChecker
1817
import kotlin.math.roundToInt
1918

20-
@OptIn(ExperimentalMaterial3Api::class)
2119
@Composable
2220
internal fun CutCornerCropShapeEdit(
2321
aspectRatio: AspectRatio,
@@ -99,12 +97,12 @@ internal fun CutCornerCropShapeEdit(
9997
)
10098
)
10199

102-
TextField(
100+
CropTextField(
103101
value = newTitle,
104102
onValueChange = { newTitle = it }
105103
)
106104

107-
Spacer(modifier=Modifier.height(10.dp))
105+
Spacer(modifier = Modifier.height(10.dp))
108106

109107
SliderWithValueSelection(
110108
value = topStartPercent,

app/src/main/java/com/smarttoolfactory/composecropper/preferences/frames/edit/ImageMaskEdit.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@ import androidx.compose.foundation.Image
44
import androidx.compose.foundation.layout.Box
55
import androidx.compose.foundation.layout.Column
66
import androidx.compose.foundation.layout.fillMaxWidth
7-
import androidx.compose.material3.ExperimentalMaterial3Api
8-
import androidx.compose.material3.TextField
97
import androidx.compose.runtime.*
108
import androidx.compose.ui.Alignment
119
import androidx.compose.ui.Modifier
10+
import com.smarttoolfactory.composecropper.preferences.CropTextField
1211
import com.smarttoolfactory.cropper.model.ImageMaskOutline
1312

14-
@OptIn(ExperimentalMaterial3Api::class)
1513
@Composable
1614
internal fun ImageMaskEdit(
1715
imageMaskOutline: ImageMaskOutline,
@@ -31,7 +29,7 @@ internal fun ImageMaskEdit(
3129
Image(bitmap = imageMaskOutline.image, contentDescription = "ImageMask")
3230

3331
}
34-
TextField(
32+
CropTextField(
3533
value = newTitle,
3634
onValueChange = {
3735
newTitle = it

app/src/main/java/com/smarttoolfactory/composecropper/preferences/frames/edit/OvalCropShapeEdit.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package com.smarttoolfactory.composecropper.preferences.frames.edit
22

33
import androidx.compose.foundation.layout.*
44
import androidx.compose.foundation.shape.GenericShape
5-
import androidx.compose.material3.ExperimentalMaterial3Api
6-
import androidx.compose.material3.TextField
75
import androidx.compose.runtime.*
86
import androidx.compose.ui.Modifier
97
import androidx.compose.ui.draw.clipToBounds
@@ -14,13 +12,13 @@ import androidx.compose.ui.graphics.ImageBitmap
1412
import androidx.compose.ui.platform.LocalDensity
1513
import androidx.compose.ui.unit.LayoutDirection
1614
import androidx.compose.ui.unit.dp
15+
import com.smarttoolfactory.composecropper.preferences.CropTextField
1716
import com.smarttoolfactory.composecropper.preferences.SliderWithValueSelection
1817
import com.smarttoolfactory.cropper.model.AspectRatio
1918
import com.smarttoolfactory.cropper.model.OvalCropShape
2019
import com.smarttoolfactory.cropper.util.drawOutlineWithBlendModeAndChecker
2120

2221

23-
@OptIn(ExperimentalMaterial3Api::class)
2422
@Composable
2523
internal fun OvalCropShapeEdit(
2624
aspectRatio: AspectRatio,
@@ -113,7 +111,7 @@ internal fun OvalCropShapeEdit(
113111
)
114112
)
115113

116-
TextField(
114+
CropTextField(
117115
value = newTitle,
118116
onValueChange = { newTitle = it }
119117
)

app/src/main/java/com/smarttoolfactory/composecropper/preferences/frames/edit/PolygonCropShapeEdit.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package com.smarttoolfactory.composecropper.preferences.frames.edit
22

33
import androidx.compose.foundation.layout.*
4-
import androidx.compose.material3.ExperimentalMaterial3Api
5-
import androidx.compose.material3.TextField
64
import androidx.compose.runtime.*
75
import androidx.compose.ui.Modifier
86
import androidx.compose.ui.draw.clipToBounds
97
import androidx.compose.ui.graphics.ImageBitmap
108
import androidx.compose.ui.platform.LocalDensity
119
import androidx.compose.ui.unit.dp
10+
import com.smarttoolfactory.composecropper.preferences.CropTextField
1211
import com.smarttoolfactory.composecropper.preferences.SliderWithValueSelection
1312
import com.smarttoolfactory.cropper.model.AspectRatio
1413
import com.smarttoolfactory.cropper.model.PolygonCropShape
1514
import com.smarttoolfactory.cropper.util.createPolygonShape
1615
import com.smarttoolfactory.cropper.util.drawOutlineWithBlendModeAndChecker
1716

18-
@OptIn(ExperimentalMaterial3Api::class)
1917
@Composable
2018
internal fun PolygonCropShapeEdit(
2119
aspectRatio: AspectRatio,
@@ -78,7 +76,7 @@ internal fun PolygonCropShapeEdit(
7876
)
7977
)
8078

81-
TextField(
79+
CropTextField(
8280
value = newTitle,
8381
onValueChange = { newTitle = it }
8482
)

app/src/main/java/com/smarttoolfactory/composecropper/preferences/frames/edit/RoundedCornerCropShapeEdit.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@ package com.smarttoolfactory.composecropper.preferences.frames.edit
22

33
import androidx.compose.foundation.layout.*
44
import androidx.compose.foundation.shape.RoundedCornerShape
5-
import androidx.compose.material3.ExperimentalMaterial3Api
6-
import androidx.compose.material3.TextField
75
import androidx.compose.runtime.*
86
import androidx.compose.ui.Modifier
97
import androidx.compose.ui.draw.clipToBounds
108
import androidx.compose.ui.graphics.ImageBitmap
119
import androidx.compose.ui.platform.LocalDensity
1210
import androidx.compose.ui.unit.dp
11+
import com.smarttoolfactory.composecropper.preferences.CropTextField
1312
import com.smarttoolfactory.composecropper.preferences.SliderWithValueSelection
1413
import com.smarttoolfactory.cropper.model.AspectRatio
1514
import com.smarttoolfactory.cropper.model.CornerRadiusProperties
1615
import com.smarttoolfactory.cropper.model.RoundedCornerCropShape
1716
import com.smarttoolfactory.cropper.util.drawOutlineWithBlendModeAndChecker
1817
import kotlin.math.roundToInt
1918

20-
@OptIn(ExperimentalMaterial3Api::class)
2119
@Composable
2220
internal fun RoundedCornerCropShapeEdit(
2321
aspectRatio: AspectRatio,
@@ -99,7 +97,7 @@ internal fun RoundedCornerCropShapeEdit(
9997
)
10098
)
10199

102-
TextField(
100+
CropTextField(
103101
value = newTitle,
104102
onValueChange = { newTitle = it }
105103
)

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ import com.smarttoolfactory.cropper.ui.theme.DefaultOverlayColor
2222
*/
2323
object CropDefaults {
2424

25+
/**
26+
* Properties effect crop behavior that should be passed to [CropState]
27+
*/
2528
fun properties(
2629
cropType: CropType = CropType.Dynamic,
2730
handleSize: Dp = 20.dp,
2831
maxZoom: Float = 10f,
29-
aspectRatio: AspectRatio = aspectRatios[2].aspectRatio,
3032
contentScale: ContentScale = ContentScale.Fit,
3133
cropOutlineProperty: CropOutlineProperty,
34+
aspectRatio: AspectRatio = aspectRatios[2].aspectRatio,
35+
overlayRatio:Float = .9f,
3236
pannable: Boolean = true,
3337
fling: Boolean = false,
3438
zoomable: Boolean = true,
@@ -41,13 +45,18 @@ object CropDefaults {
4145
cropOutlineProperty = cropOutlineProperty,
4246
maxZoom = maxZoom,
4347
aspectRatio = aspectRatio,
48+
overlayRatio = overlayRatio,
4449
pannable = pannable,
4550
fling = fling,
4651
zoomable = zoomable,
4752
rotatable = rotatable
4853
)
4954
}
5055

56+
/**
57+
* Style is cosmetic changes that don't effect how [CropState] behaves because of that
58+
* none of these properties are passed to [CropState]
59+
*/
5160
fun style(
5261
drawOverlay: Boolean = true,
5362
drawGrid: Boolean = true,
@@ -76,9 +85,10 @@ object CropDefaults {
7685
data class CropProperties internal constructor(
7786
val cropType: CropType,
7887
val handleSize: Dp,
79-
val aspectRatio: AspectRatio,
8088
val contentScale: ContentScale,
8189
val cropOutlineProperty: CropOutlineProperty,
90+
val aspectRatio: AspectRatio,
91+
val overlayRatio: Float,
8292
val pannable: Boolean,
8393
val fling: Boolean,
8494
val rotatable: Boolean,

0 commit comments

Comments
 (0)