Skip to content

Commit a56dfe5

Browse files
add content scale menu
1 parent fa52405 commit a56dfe5

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.smarttoolfactory.composeimage
2+
3+
import androidx.compose.foundation.layout.Row
4+
import androidx.compose.foundation.layout.fillMaxWidth
5+
import androidx.compose.foundation.layout.padding
6+
import androidx.compose.material3.*
7+
import androidx.compose.runtime.*
8+
import androidx.compose.ui.Alignment
9+
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.graphics.Color
11+
import androidx.compose.ui.layout.ContentScale
12+
import androidx.compose.ui.text.TextStyle
13+
import androidx.compose.ui.text.font.FontWeight
14+
import androidx.compose.ui.unit.dp
15+
import androidx.compose.ui.unit.sp
16+
17+
val contentScaleOptions =
18+
listOf("None", "Fit", "Crop", "FillBounds", "FillWidth", "FillHeight", "Inside")
19+
20+
@Composable
21+
fun ContentScaleSelectionMenu(
22+
contentScale: ContentScale,
23+
onContentScaleChanged: (ContentScale) -> Unit
24+
) {
25+
var index = when (contentScale) {
26+
ContentScale.None -> 0
27+
ContentScale.Fit -> 1
28+
ContentScale.Crop -> 2
29+
ContentScale.FillBounds -> 3
30+
ContentScale.FillWidth -> 4
31+
ContentScale.FillHeight -> 5
32+
else -> 6
33+
}
34+
35+
Row(
36+
verticalAlignment = Alignment.CenterVertically,
37+
modifier = Modifier
38+
.fillMaxWidth()
39+
.padding(2.dp)
40+
) {
41+
ExposedSelectionMenu(
42+
modifier = Modifier.fillMaxWidth(),
43+
index = index,
44+
title = "ContentScale",
45+
options = contentScaleOptions,
46+
onSelected = {
47+
index = it
48+
val scale = when (index) {
49+
0 -> ContentScale.None
50+
1 -> ContentScale.Fit
51+
2 -> ContentScale.Crop
52+
3 -> ContentScale.FillBounds
53+
4 -> ContentScale.FillWidth
54+
5 -> ContentScale.FillHeight
55+
else -> ContentScale.Inside
56+
}
57+
58+
onContentScaleChanged(scale)
59+
}
60+
)
61+
}
62+
}
63+
64+
@OptIn(ExperimentalMaterial3Api::class)
65+
@Composable
66+
fun ExposedSelectionMenu(
67+
modifier: Modifier = Modifier,
68+
index: Int,
69+
title: String? = null,
70+
textStyle: TextStyle = TextStyle(
71+
fontWeight = FontWeight.W600,
72+
fontSize = 14.sp
73+
),
74+
colors: TextFieldColors = ExposedDropdownMenuDefaults.textFieldColors(
75+
containerColor = Color.Transparent,
76+
focusedIndicatorColor = Color.Transparent,
77+
unfocusedIndicatorColor = Color.Transparent,
78+
disabledIndicatorColor = Color.Transparent
79+
),
80+
options: List<String>,
81+
onSelected: (Int) -> Unit
82+
) {
83+
84+
var expanded by remember { mutableStateOf(false) }
85+
var selectedOptionText by remember { mutableStateOf(options[index]) }
86+
87+
ExposedDropdownMenuBox(
88+
modifier = modifier,
89+
expanded = expanded,
90+
onExpandedChange = {
91+
expanded = !expanded
92+
}
93+
) {
94+
TextField(
95+
modifier = modifier,
96+
readOnly = true,
97+
value = selectedOptionText,
98+
onValueChange = { },
99+
label = {
100+
title?.let {
101+
Text(it)
102+
}
103+
},
104+
trailingIcon = {
105+
ExposedDropdownMenuDefaults.TrailingIcon(
106+
expanded = expanded
107+
)
108+
},
109+
colors = colors,
110+
textStyle = textStyle
111+
)
112+
ExposedDropdownMenu(
113+
expanded = expanded,
114+
onDismissRequest = {
115+
expanded = false
116+
117+
}
118+
) {
119+
options.forEachIndexed { index: Int, selectionOption: String ->
120+
DropdownMenuItem(
121+
onClick = {
122+
selectedOptionText = selectionOption
123+
expanded = false
124+
onSelected(index)
125+
},
126+
text = {
127+
Text(text = selectionOption)
128+
}
129+
)
130+
}
131+
}
132+
}
133+
}

0 commit comments

Comments
 (0)