Skip to content

Commit f68e66d

Browse files
fix #3
Adds background change option to style settings
1 parent 191729e commit f68e66d

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ internal fun CropStyleSelectionMenu(
3131
val overlayStrokeWidth = cropStyle.strokeWidth
3232
val overlayColor = cropStyle.overlayColor
3333
val handleColor = cropStyle.handleColor
34+
val backgroundColor = cropStyle.backgroundColor
3435
val drawGridEnabled = cropStyle.drawGrid
3536
val theme = cropStyle.cropTheme
3637

@@ -73,6 +74,7 @@ internal fun CropStyleSelectionMenu(
7374
upperBound = 3.dp
7475
)
7576

77+
7678
ColorSelection(
7779
title = "Overlay Color",
7880
color = overlayColor,
@@ -94,6 +96,17 @@ internal fun CropStyleSelectionMenu(
9496
}
9597
)
9698

99+
Spacer(modifier = Modifier.height(20.dp))
100+
ColorSelection(
101+
title = "Background Color",
102+
color = backgroundColor,
103+
onColorChange = { color: Color ->
104+
onCropStyleChange(
105+
cropStyle.copy(backgroundColor = color)
106+
)
107+
}
108+
)
109+
97110
Title("Grid")
98111
FullRowSwitch(
99112
label = "Draw grid",

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.smarttoolfactory.cropper
32

43
import androidx.compose.animation.AnimatedVisibility
@@ -37,8 +36,6 @@ import com.smarttoolfactory.cropper.settings.CropStyle
3736
import com.smarttoolfactory.cropper.settings.CropType
3837
import com.smarttoolfactory.cropper.state.DynamicCropState
3938
import com.smarttoolfactory.cropper.state.rememberCropState
40-
import com.smarttoolfactory.cropper.ui.theme.BackgroundIdle
41-
import com.smarttoolfactory.cropper.ui.theme.BackgroundPressed
4239
import kotlinx.coroutines.Dispatchers
4340
import kotlinx.coroutines.delay
4441
import kotlinx.coroutines.flow.*
@@ -115,15 +112,20 @@ fun ImageCropper(
115112
keys = resetKeys
116113
)
117114

118-
val isTouched by remember(cropState) {
115+
val isHandleTouched by remember(cropState) {
119116
derivedStateOf {
120117
cropState is DynamicCropState && handlesTouched(cropState.touchRegion)
121118
}
122119
}
123120

121+
val pressedStateColor = remember(cropStyle.backgroundColor){
122+
cropStyle.backgroundColor
123+
.copy(cropStyle.backgroundColor.alpha * .7f)
124+
}
125+
124126
val transparentColor by animateColorAsState(
125127
animationSpec = tween(300, easing = LinearEasing),
126-
targetValue = if (isTouched) BackgroundPressed else BackgroundIdle
128+
targetValue = if (isHandleTouched) pressedStateColor else cropStyle.backgroundColor
127129
)
128130

129131
// Crops image when user invokes crop operation

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import com.smarttoolfactory.cropper.model.CropOutline
1313
import com.smarttoolfactory.cropper.model.OutlineType
1414
import com.smarttoolfactory.cropper.model.aspectRatios
1515
import com.smarttoolfactory.cropper.state.CropState
16+
import com.smarttoolfactory.cropper.ui.theme.DefaultBackgroundColor
17+
import com.smarttoolfactory.cropper.ui.theme.DefaultHandleColor
18+
import com.smarttoolfactory.cropper.ui.theme.DefaultOverlayColor
1619

1720
/**
1821
* Contains the default values used by [ImageCropper]
@@ -49,15 +52,17 @@ object CropDefaults {
4952
drawOverlay: Boolean = true,
5053
drawGrid: Boolean = true,
5154
strokeWidth: Dp = 1.dp,
52-
overlayColor: Color = Color.Gray,
53-
handleColor: Color = Color.White
55+
overlayColor: Color = DefaultOverlayColor,
56+
handleColor: Color = DefaultHandleColor,
57+
backgroundColor: Color = DefaultBackgroundColor
5458
): CropStyle {
5559
return CropStyle(
5660
drawOverlay = drawOverlay,
5761
drawGrid = drawGrid,
5862
strokeWidth = strokeWidth,
5963
overlayColor = overlayColor,
60-
handleColor = handleColor
64+
handleColor = handleColor,
65+
backgroundColor = backgroundColor
6166
)
6267
}
6368
}
@@ -92,6 +97,7 @@ data class CropStyle internal constructor(
9297
val strokeWidth: Dp,
9398
val overlayColor: Color,
9499
val handleColor: Color,
100+
val backgroundColor: Color,
95101
val cropTheme: CropTheme = CropTheme.Dark
96102
)
97103

cropper/src/main/java/com/smarttoolfactory/cropper/ui/theme/Color.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ package com.smarttoolfactory.cropper.ui.theme
22

33
import androidx.compose.ui.graphics.Color
44

5-
val BackgroundPressed = Color(0x66000000)
6-
val BackgroundIdle = Color(0x99000000)
5+
val DefaultBackgroundColor = Color(0x99000000)
6+
val DefaultOverlayColor = Color.Gray
7+
val DefaultHandleColor = Color.White

0 commit comments

Comments
 (0)