Skip to content

Commit 3a99ea4

Browse files
add theme selection option
1 parent 5b3f711 commit 3a99ea4

4 files changed

Lines changed: 148 additions & 42 deletions

File tree

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

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.smarttoolfactory.composecropper.demo
44

55
import androidx.compose.foundation.Image
66
import androidx.compose.foundation.background
7+
import androidx.compose.foundation.isSystemInDarkTheme
78
import androidx.compose.foundation.layout.*
89
import androidx.compose.foundation.shape.RoundedCornerShape
910
import androidx.compose.material.*
@@ -30,6 +31,7 @@ import com.smarttoolfactory.composecropper.ImageSelectionButton
3031
import com.smarttoolfactory.composecropper.R
3132
import com.smarttoolfactory.composecropper.preferences.CropStyleSelectionMenu
3233
import com.smarttoolfactory.composecropper.preferences.PropertySelectionSheet
34+
import com.smarttoolfactory.composecropper.ui.theme.ComposeCropperTheme
3335
import com.smarttoolfactory.cropper.ImageCropper
3436
import com.smarttoolfactory.cropper.model.OutlineType
3537
import com.smarttoolfactory.cropper.model.RectCropShape
@@ -78,51 +80,66 @@ fun ImageCropDemo() {
7880

7981
var selectionPage by remember { mutableStateOf(SelectionPage.Properties) }
8082

81-
BottomSheetScaffold(
82-
scaffoldState = bottomSheetScaffoldState,
83-
sheetElevation = 16.dp,
84-
sheetShape = RoundedCornerShape(
85-
bottomStart = 0.dp,
86-
bottomEnd = 0.dp,
87-
topStart = 28.dp,
88-
topEnd = 28.dp
89-
),
90-
91-
sheetGesturesEnabled = true,
92-
sheetContent = {
93-
94-
if (selectionPage == SelectionPage.Properties) {
95-
PropertySelectionSheet(
96-
cropFrameFactory = cropFrameFactory,
97-
cropProperties = cropProperties,
98-
onCropPropertiesChange = {
99-
cropProperties = it
100-
}
101-
)
102-
} else {
103-
CropStyleSelectionMenu(
104-
cropStyle = cropStyle,
105-
onCropStyleChange = {
106-
cropStyle = it
107-
}
108-
)
109-
}
110-
},
11183

112-
// This is the height in collapsed state
113-
sheetPeekHeight = 0.dp
84+
val theme by remember {
85+
derivedStateOf {
86+
cropStyle.cropTheme
87+
}
88+
}
89+
90+
ComposeCropperTheme(
91+
darkTheme = when(theme){
92+
CropTheme.Dark ->true
93+
CropTheme.Light->false
94+
else -> isSystemInDarkTheme()
95+
}
11496
) {
115-
MainContent(
116-
cropProperties,
117-
cropStyle,
118-
) {
119-
selectionPage = it
97+
BottomSheetScaffold(
98+
scaffoldState = bottomSheetScaffoldState,
99+
sheetElevation = 16.dp,
100+
sheetShape = RoundedCornerShape(
101+
bottomStart = 0.dp,
102+
bottomEnd = 0.dp,
103+
topStart = 28.dp,
104+
topEnd = 28.dp
105+
),
120106

121-
coroutineScope.launch {
122-
if (bottomSheetScaffoldState.bottomSheetState.isExpanded) {
123-
bottomSheetScaffoldState.bottomSheetState.collapse()
107+
sheetGesturesEnabled = true,
108+
sheetContent = {
109+
110+
if (selectionPage == SelectionPage.Properties) {
111+
PropertySelectionSheet(
112+
cropFrameFactory = cropFrameFactory,
113+
cropProperties = cropProperties,
114+
onCropPropertiesChange = {
115+
cropProperties = it
116+
}
117+
)
124118
} else {
125-
bottomSheetScaffoldState.bottomSheetState.expand()
119+
CropStyleSelectionMenu(
120+
cropStyle = cropStyle,
121+
onCropStyleChange = {
122+
cropStyle = it
123+
}
124+
)
125+
}
126+
},
127+
128+
// This is the height in collapsed state
129+
sheetPeekHeight = 0.dp
130+
) {
131+
MainContent(
132+
cropProperties,
133+
cropStyle,
134+
) {
135+
selectionPage = it
136+
137+
coroutineScope.launch {
138+
if (bottomSheetScaffoldState.bottomSheetState.isExpanded) {
139+
bottomSheetScaffoldState.bottomSheetState.collapse()
140+
} else {
141+
bottomSheetScaffoldState.bottomSheetState.expand()
142+
}
126143
}
127144
}
128145
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ internal fun CropStyleSelectionMenu(
3232
val overlayColor = cropStyle.overlayColor
3333
val handleColor = cropStyle.handleColor
3434
val drawGridEnabled = cropStyle.drawGrid
35+
val theme = cropStyle.cropTheme
36+
37+
Title("Theme")
38+
CropThemeSelection(
39+
cropTheme = theme,
40+
onThemeChange = {
41+
onCropStyleChange(
42+
cropStyle.copy(cropTheme = it)
43+
)
44+
}
45+
)
46+
3547

3648
Title("Overlay")
3749
FullRowSwitch(
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.smarttoolfactory.composecropper.preferences
2+
3+
import androidx.compose.foundation.clickable
4+
import androidx.compose.foundation.layout.fillMaxWidth
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.material3.Text
7+
import androidx.compose.runtime.*
8+
import androidx.compose.ui.Modifier
9+
import androidx.compose.ui.unit.dp
10+
import androidx.compose.ui.unit.sp
11+
import com.smarttoolfactory.cropper.settings.CropTheme
12+
13+
@Composable
14+
fun CropThemeSelection(
15+
cropTheme: CropTheme,
16+
onThemeChange: (CropTheme) -> Unit
17+
) {
18+
19+
val cropThemeOptions =
20+
remember {
21+
listOf(
22+
CropTheme.Light.toString(),
23+
CropTheme.Dark.toString(),
24+
CropTheme.System.toString()
25+
)
26+
}
27+
28+
var showDialog by remember { mutableStateOf(false) }
29+
30+
val index = when (cropTheme) {
31+
CropTheme.Light -> 0
32+
CropTheme.Dark -> 1
33+
else -> 2
34+
}
35+
36+
Text(
37+
text = cropThemeOptions[index],
38+
fontSize = 18.sp,
39+
modifier = Modifier
40+
.fillMaxWidth()
41+
.clickable {
42+
showDialog = true
43+
}
44+
.padding(8.dp)
45+
46+
)
47+
48+
if (showDialog) {
49+
DialogWithMultipleSelection(
50+
title = "Theme",
51+
options = cropThemeOptions,
52+
value = index,
53+
onDismiss = { showDialog = false },
54+
onConfirm = {
55+
56+
val cropThemeChange = when (it) {
57+
0 -> CropTheme.Light
58+
1 -> CropTheme.Dark
59+
else -> CropTheme.System
60+
}
61+
onThemeChange(cropThemeChange)
62+
showDialog = false
63+
}
64+
)
65+
}
66+
67+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ data class CropStyle internal constructor(
9191
val drawGrid: Boolean,
9292
val strokeWidth: Dp,
9393
val overlayColor: Color,
94-
val handleColor: Color
94+
val handleColor: Color,
95+
val cropTheme: CropTheme = CropTheme.Dark
9596
)
9697

9798
/**
@@ -102,3 +103,12 @@ data class CropOutlineProperty(
102103
val outlineType: OutlineType,
103104
val cropOutline: CropOutline
104105
)
106+
107+
/**
108+
* Light, Dark or system controlled theme
109+
*/
110+
enum class CropTheme{
111+
Light,
112+
Dark,
113+
System
114+
}

0 commit comments

Comments
 (0)