Skip to content

Commit 31b1757

Browse files
authored
Merge pull request #36 from maiatoday/work/better-safe-area
Allow SafeArea to Capture the Bitmap
2 parents ff1e426 + cec7393 commit 31b1757

9 files changed

Lines changed: 213 additions & 156 deletions

File tree

app/src/main/java/de/berlindroid/zepatch/Holzmesser.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ import de.berlindroid.zepatch.generated.PatchRegistry
1010
*/
1111

1212
// Generated via @Patch + KSP
13+
1314
val patchables = PatchRegistry.patchables

app/src/main/java/de/berlindroid/zepatch/MainActivity.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private fun PatchableDetail(
195195
modifier: Modifier = Modifier,
196196
name: String,
197197
onBackClick: () -> Unit,
198-
patchable: @Composable () -> Unit,
198+
patchable: @Composable (Boolean, (ImageBitmap) -> Unit) -> Unit,
199199
) {
200200
var imageBitmap by remember { mutableStateOf<ImageBitmap?>(null) }
201201
var reducedImageBitmap by remember { mutableStateOf<ImageBitmap?>(null) }
@@ -245,12 +245,24 @@ private fun PatchableDetail(
245245
name,
246246
onBitmapUpdated = { imageBitmap = it },
247247
onColorCountUpdated = { colorCount = it },
248-
onReducedUpdated = { img, histo -> reducedImageBitmap = img;reducedHistogram = histo },
249-
onEmbroideryUpdated = { data, preview -> embroideryData = data; embroideryPreviewImage = preview },
248+
onReducedUpdated = { img, histo ->
249+
reducedImageBitmap = img;reducedHistogram = histo
250+
},
251+
onEmbroideryUpdated = { data, preview ->
252+
embroideryData = data
253+
embroideryPreviewImage = preview
254+
},
250255
patchable
251256
)
252257

253-
WizardButtons(currentMode, imageBitmap, reducedImageBitmap, embroideryData, name, launcher) {
258+
WizardButtons(
259+
currentMode,
260+
imageBitmap,
261+
reducedImageBitmap,
262+
embroideryData,
263+
name,
264+
launcher
265+
) {
254266
currentMode = it
255267
}
256268
}
@@ -269,7 +281,7 @@ private fun WizardContent(
269281
onColorCountUpdated: (Int) -> Unit,
270282
onReducedUpdated: (ImageBitmap, Histogram) -> Unit,
271283
onEmbroideryUpdated: (ByteArray, ImageBitmap) -> Unit,
272-
patchable: @Composable (() -> Unit),
284+
patchable: @Composable (Boolean, (ImageBitmap) -> Unit) -> Unit,
273285
) {
274286
Card(
275287
modifier = Modifier

app/src/main/java/de/berlindroid/zepatch/patchable/Demo.kt

Lines changed: 126 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@ import androidx.compose.foundation.background
55
import androidx.compose.foundation.border
66
import androidx.compose.foundation.layout.Column
77
import androidx.compose.foundation.layout.Spacer
8-
import androidx.compose.foundation.layout.fillMaxSize
98
import androidx.compose.foundation.layout.fillMaxWidth
109
import androidx.compose.foundation.layout.size
1110
import androidx.compose.foundation.shape.RoundedCornerShape
1211
import androidx.compose.material3.ExperimentalMaterial3Api
1312
import androidx.compose.material3.Text
13+
import androidx.compose.material3.TextField
1414
import androidx.compose.runtime.Composable
15+
import androidx.compose.runtime.getValue
16+
import androidx.compose.runtime.mutableStateOf
17+
import androidx.compose.runtime.remember
18+
import androidx.compose.runtime.setValue
1519
import androidx.compose.ui.Modifier
1620
import androidx.compose.ui.graphics.Brush
1721
import androidx.compose.ui.graphics.Color
22+
import androidx.compose.ui.graphics.ImageBitmap
1823
import androidx.compose.ui.graphics.Shadow
1924
import androidx.compose.ui.res.painterResource
2025
import androidx.compose.ui.text.TextStyle
@@ -29,12 +34,17 @@ import de.berlindroid.zepatch.R
2934
import de.berlindroid.zepatch.annotations.Patch
3035
import de.berlindroid.zepatch.ui.SafeArea
3136

32-
3337
@OptIn(ExperimentalMaterial3Api::class)
3438
@Patch("AppLogo")
3539
@Composable
36-
fun AppLogo() {
37-
SafeArea {
40+
fun AppLogo(
41+
shouldCapture: Boolean = false,
42+
onBitmap: (ImageBitmap) -> Unit = {},
43+
) {
44+
SafeArea(
45+
shouldCapture = shouldCapture,
46+
onBitmap = onBitmap,
47+
) {
3848
Image(
3949
modifier = Modifier.size(200.dp),
4050
painter = painterResource(R.drawable.ai_logo),
@@ -47,8 +57,14 @@ fun AppLogo() {
4757
@OptIn(ExperimentalMaterial3Api::class)
4858
@Patch("BerlindroidLogo")
4959
@Composable
50-
fun BerlindroidLogo() {
51-
SafeArea {
60+
fun BerlindroidLogo(
61+
shouldCapture: Boolean = false,
62+
onBitmap: (ImageBitmap) -> Unit = {},
63+
) {
64+
SafeArea(
65+
shouldCapture = shouldCapture,
66+
onBitmap = onBitmap,
67+
) {
5268
Image(
5369
modifier = Modifier.size(200.dp),
5470
painter = painterResource(R.drawable.voltron_nosign),
@@ -61,8 +77,14 @@ fun BerlindroidLogo() {
6177
@OptIn(ExperimentalMaterial3Api::class)
6278
@Patch("GoogleLogo")
6379
@Composable
64-
fun GoogleLogo() {
65-
SafeArea {
80+
fun GoogleLogo(
81+
shouldCapture: Boolean = false,
82+
onBitmap: (ImageBitmap) -> Unit = {},
83+
) {
84+
SafeArea(
85+
shouldCapture = shouldCapture,
86+
onBitmap = onBitmap,
87+
) {
6688
Image(
6789
modifier = Modifier.size(200.dp),
6890
painter = painterResource(R.drawable.g),
@@ -75,8 +97,14 @@ fun GoogleLogo() {
7597
@OptIn(ExperimentalMaterial3Api::class)
7698
@Patch("Android")
7799
@Composable
78-
fun AndroidLogo() {
79-
SafeArea {
100+
fun AndroidLogo(
101+
shouldCapture: Boolean = false,
102+
onBitmap: (ImageBitmap) -> Unit = {},
103+
) {
104+
SafeArea(
105+
shouldCapture = shouldCapture,
106+
onBitmap = onBitmap,
107+
) {
80108
Image(
81109
modifier = Modifier.size(200.dp),
82110
painter = painterResource(R.drawable.andriod),
@@ -88,69 +116,85 @@ fun AndroidLogo() {
88116

89117
@Patch("AndyA")
90118
@Composable
91-
fun AndyA() {
92-
SafeArea(
93-
modifier = Modifier
94-
.size(200.dp)
95-
.background(
96-
brush = Brush.linearGradient(
97-
colors = listOf(
98-
Color.Red,
99-
Color.Yellow,
100-
Color.Green,
101-
Color.Blue,
102-
Color.Cyan
103-
)
104-
),
105-
shape = RoundedCornerShape(
106-
topStartPercent = 50,
107-
topEndPercent = 50,
108-
bottomStartPercent = 50,
109-
bottomEndPercent = 0
119+
fun AndyA(
120+
shouldCapture: Boolean = false,
121+
onBitmap: (ImageBitmap) -> Unit = {},
122+
) {
123+
var name by remember { mutableStateOf("Andy") }
124+
Column {
125+
SafeArea(
126+
shouldCapture = shouldCapture,
127+
onBitmap = onBitmap,
128+
) {
129+
Column(
130+
modifier = Modifier
131+
.size(200.dp)
132+
.background(
133+
brush = Brush.linearGradient(
134+
colors = listOf(
135+
Color.Red,
136+
Color.Yellow,
137+
Color.Green,
138+
Color.Blue,
139+
Color.Cyan
140+
)
141+
),
142+
shape = RoundedCornerShape(
143+
topStartPercent = 50,
144+
topEndPercent = 50,
145+
bottomStartPercent = 50,
146+
bottomEndPercent = 0
147+
)
148+
),
149+
) {
150+
Spacer(modifier = Modifier.weight(1f))
151+
Text(
152+
modifier = Modifier.fillMaxWidth(),
153+
fontFamily = FontFamily.Cursive,
154+
textAlign = TextAlign.Center,
155+
fontStyle = FontStyle.Italic,
156+
fontWeight = FontWeight.ExtraBold,
157+
fontSize = 70.sp,
158+
text = name,
110159
)
111-
),
112-
) {
113-
Column(modifier = Modifier.fillMaxSize()) {
114-
Spacer(modifier = Modifier.weight(1f))
115-
Text(
116-
modifier = Modifier.fillMaxWidth(),
117-
fontFamily = FontFamily.Cursive,
118-
textAlign = TextAlign.Center,
119-
fontStyle = FontStyle.Italic,
120-
fontWeight = FontWeight.ExtraBold,
121-
fontSize = 70.sp,
122-
text = "Andy",
123-
)
124-
Spacer(modifier = Modifier.weight(1f))
160+
Spacer(modifier = Modifier.weight(1f))
161+
}
125162
}
163+
TextField(value = name, onValueChange = { name = it })
126164
}
127165
}
128166

129167
@Patch("AndyB")
130168
@Composable
131-
fun AndyB() {
169+
fun AndyB(
170+
shouldCapture: Boolean = false,
171+
onBitmap: (ImageBitmap) -> Unit = {},
172+
) {
132173
SafeArea(
133-
modifier = Modifier
134-
.size(200.dp)
135-
.background(
136-
color = Color.White,
137-
shape = RoundedCornerShape(
138-
topStartPercent = 0,
139-
topEndPercent = 50,
140-
bottomStartPercent = 50,
141-
bottomEndPercent = 50
142-
)
143-
)
144-
.border(
145-
12.dp, Color.Black, RoundedCornerShape(
146-
topStartPercent = 0,
147-
topEndPercent = 50,
148-
bottomStartPercent = 50,
149-
bottomEndPercent = 50
150-
)
151-
),
174+
shouldCapture = shouldCapture,
175+
onBitmap = onBitmap,
152176
) {
153-
Column(modifier = Modifier.fillMaxSize()) {
177+
Column(
178+
modifier = Modifier
179+
.size(200.dp)
180+
.background(
181+
color = Color.White,
182+
shape = RoundedCornerShape(
183+
topStartPercent = 0,
184+
topEndPercent = 50,
185+
bottomStartPercent = 50,
186+
bottomEndPercent = 50
187+
)
188+
)
189+
.border(
190+
12.dp, Color.Black, RoundedCornerShape(
191+
topStartPercent = 0,
192+
topEndPercent = 50,
193+
bottomStartPercent = 50,
194+
bottomEndPercent = 50
195+
)
196+
),
197+
) {
154198
Spacer(modifier = Modifier.weight(1f))
155199
Text(
156200
modifier = Modifier.fillMaxWidth(),
@@ -161,7 +205,7 @@ fun AndyB() {
161205
style = TextStyle.Default.copy(shadow = Shadow(blurRadius = 12f)),
162206
color = Color.Black,
163207
text = "Andy",
164-
letterSpacing = -1.sp
208+
letterSpacing = (-1).sp
165209
)
166210
Spacer(modifier = Modifier.weight(1f))
167211
}
@@ -170,22 +214,34 @@ fun AndyB() {
170214

171215
@Patch("flex")
172216
@Composable
173-
fun Emoji() {
174-
SafeArea {
217+
fun Emoji(
218+
shouldCapture: Boolean = false,
219+
onBitmap: (ImageBitmap) -> Unit = {},
220+
) {
221+
SafeArea(
222+
shouldCapture = shouldCapture,
223+
onBitmap = onBitmap,
224+
) {
175225
Text("💪", fontSize = 48.sp)
176226
}
177227
}
178228

179229
@Patch("ROBOT")
180230
@Composable
181-
fun Emoji2() {
182-
SafeArea {
231+
fun Emoji2(
232+
shouldCapture: Boolean = false,
233+
onBitmap: (ImageBitmap) -> Unit = {},
234+
) {
235+
SafeArea(
236+
shouldCapture = shouldCapture,
237+
onBitmap = onBitmap,
238+
) {
183239
Text("🦾🤖\nHereWeGo", fontSize = 34.sp)
184240
}
185241
}
186242

187243
@Preview
188244
@Composable
189245
fun PreviewDemo() {
190-
AndyB()
246+
AndyA()
191247
}

app/src/main/java/de/berlindroid/zepatch/ui/PatchableToBitmap.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import androidx.compose.runtime.remember
1313
import androidx.compose.runtime.setValue
1414
import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.graphics.ImageBitmap
16-
import de.berlindroid.zepatch.utils.CaptureToBitmap
1716

1817
/**
1918
* Renders the provided [patchable] into a Bitmap-like [ImageBitmap] and displays it via [Image].
@@ -23,7 +22,7 @@ import de.berlindroid.zepatch.utils.CaptureToBitmap
2322
fun PatchableToBitmap(
2423
modifier: Modifier = Modifier,
2524
onBitmap: (ImageBitmap) -> Unit = {},
26-
patchable: @Composable () -> Unit,
25+
patchable: @Composable (Boolean, (ImageBitmap) -> Unit) -> Unit,
2726
) {
2827
var image by remember { mutableStateOf<ImageBitmap?>(null) }
2928
var shouldCapture by remember { mutableStateOf(false) }
@@ -33,16 +32,12 @@ fun PatchableToBitmap(
3332
image = null
3433
shouldCapture = true
3534
}) { Text("Do it") }
36-
// Compose the content through the composable utility; it will invoke the callback when ready.
37-
CaptureToBitmap(
38-
modifier = Modifier.fillMaxWidth(),
39-
shouldCapture = shouldCapture,
40-
onBitmap = { img ->
41-
image = img
42-
shouldCapture = false
43-
},
44-
content = patchable
45-
)
35+
36+
// Render the patchable; it will capture via SafeArea when shouldCapture is true
37+
patchable(shouldCapture) { img ->
38+
image = img
39+
shouldCapture = false
40+
}
4641

4742
image?.let {
4843
Image(
@@ -52,7 +47,6 @@ fun PatchableToBitmap(
5247
)
5348
onBitmap(it)
5449
} ?: CircularProgressIndicator()
55-
5650
}
5751
}
5852

0 commit comments

Comments
 (0)