Skip to content

Commit 5855f70

Browse files
Merge branch 'main' into wear-tiles-screen-size-update
2 parents ed67d5c + 9436a8d commit 5855f70

File tree

2 files changed

+81
-17
lines changed

2 files changed

+81
-17
lines changed

compose/snippets/src/main/java/com/example/compose/snippets/layouts/InsetsSnippets.kt

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ import androidx.compose.foundation.layout.PaddingValues
2828
import androidx.compose.foundation.layout.Spacer
2929
import androidx.compose.foundation.layout.WindowInsets
3030
import androidx.compose.foundation.layout.WindowInsetsSides
31+
import androidx.compose.foundation.layout.asPaddingValues
3132
import androidx.compose.foundation.layout.consumeWindowInsets
33+
import androidx.compose.foundation.layout.fillMaxSize
3234
import androidx.compose.foundation.layout.ime
3335
import androidx.compose.foundation.layout.imePadding
36+
import androidx.compose.foundation.layout.navigationBars
3437
import androidx.compose.foundation.layout.only
3538
import androidx.compose.foundation.layout.padding
3639
import androidx.compose.foundation.layout.safeDrawingPadding
@@ -44,10 +47,16 @@ import androidx.compose.material3.ExperimentalMaterial3Api
4447
import androidx.compose.material3.LargeTopAppBar
4548
import androidx.compose.material3.Scaffold
4649
import androidx.compose.material3.Text
50+
import androidx.compose.material3.TextField
4751
import androidx.compose.runtime.Composable
52+
import androidx.compose.ui.Alignment.Companion.BottomCenter
4853
import androidx.compose.ui.Modifier
54+
import androidx.compose.ui.layout.WindowInsetsRulers
55+
import androidx.compose.ui.layout.layout
4956
import androidx.compose.ui.tooling.preview.Preview
57+
import androidx.compose.ui.unit.Constraints
5058
import androidx.compose.ui.unit.dp
59+
import kotlin.math.roundToInt
5160

5261
class InsetSnippetActivity : ComponentActivity() {
5362

@@ -147,3 +156,55 @@ fun OverrideDefaultInsetsSnippet() {
147156
)
148157
// [END android_compose_insets_override_defaults]
149158
}
159+
160+
// [START android_compose_insets_rulers]
161+
@Composable
162+
fun WindowInsetsRulersDemo(modifier: Modifier) {
163+
Box(
164+
contentAlignment = BottomCenter,
165+
modifier = modifier
166+
.fillMaxSize()
167+
// The mistake that causes issues downstream, as .padding doesn't consume insets.
168+
// While it's correct to instead use .windowInsetsPadding(WindowInsets.navigationBars),
169+
// assume it's difficult to identify this issue to see how WindowInsetsRulers can help.
170+
.padding(WindowInsets.navigationBars.asPaddingValues())
171+
) {
172+
TextField(
173+
value = "Demo IME Insets",
174+
onValueChange = {},
175+
modifier = modifier
176+
// Use alignToSafeDrawing() instead of .imePadding() to precisely place this child
177+
// Composable without having to fix the parent upstream.
178+
.alignToSafeDrawing()
179+
180+
// .imePadding()
181+
// .fillMaxWidth()
182+
)
183+
}
184+
}
185+
186+
fun Modifier.alignToSafeDrawing(): Modifier {
187+
return layout { measurable, constraints ->
188+
if (constraints.hasBoundedWidth && constraints.hasBoundedHeight) {
189+
val placeable = measurable.measure(constraints)
190+
val width = placeable.width
191+
val height = placeable.height
192+
layout(width, height) {
193+
val bottom = WindowInsetsRulers.SafeDrawing.current.bottom
194+
.current(0f).roundToInt() - height
195+
val right = WindowInsetsRulers.SafeDrawing.current.right
196+
.current(0f).roundToInt()
197+
val left = WindowInsetsRulers.SafeDrawing.current.left
198+
.current(0f).roundToInt()
199+
measurable.measure(Constraints.fixed(right - left, height))
200+
.place(left, bottom)
201+
}
202+
} else {
203+
val placeable = measurable.measure(constraints)
204+
layout(placeable.width, placeable.height) {
205+
placeable.place(0, 0)
206+
}
207+
}
208+
}
209+
}
210+
// [END android_compose_insets_rulers]

compose/snippets/src/main/java/com/example/compose/snippets/text/StateBasedText.kt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ import androidx.compose.material.TextField
3838
//noinspection UsingMaterialAndMaterial3Libraries
3939
import androidx.compose.material3.Text
4040
import androidx.compose.runtime.Composable
41-
import androidx.compose.runtime.LaunchedEffect
4241
import androidx.compose.runtime.remember
4342
import androidx.compose.ui.Modifier
4443
import androidx.compose.ui.graphics.Brush
4544
import androidx.compose.ui.graphics.Color
4645
import androidx.compose.ui.text.TextStyle
4746
import androidx.compose.ui.text.font.FontWeight
4847
import androidx.compose.ui.text.input.ImeAction
48+
import androidx.compose.ui.text.input.KeyboardType
4949
import androidx.compose.ui.tooling.preview.Preview
5050
import androidx.compose.ui.unit.dp
5151
import androidx.core.text.isDigitsOnly
@@ -144,21 +144,19 @@ fun TextFieldInitialState() {
144144
// [END android_compose_state_text_7]
145145
}
146146

147+
@Preview(showBackground = true)
147148
@Composable
148149
fun TextFieldBuffer() {
149150
// [START android_compose_state_text_8]
150-
val phoneNumberState = rememberTextFieldState()
151-
152-
LaunchedEffect(phoneNumberState) {
153-
phoneNumberState.edit { // TextFieldBuffer scope
154-
append("123456789")
155-
}
156-
}
151+
val phoneNumberState = rememberTextFieldState("1234567890")
157152

158153
TextField(
159154
state = phoneNumberState,
160-
inputTransformation = InputTransformation { // TextFieldBuffer scope
161-
if (asCharSequence().isDigitsOnly()) {
155+
keyboardOptions = KeyboardOptions(
156+
keyboardType = KeyboardType.Phone
157+
),
158+
inputTransformation = InputTransformation.maxLength(10).then {
159+
if (!asCharSequence().isDigitsOnly()) {
162160
revertAllChanges()
163161
}
164162
},
@@ -174,32 +172,37 @@ fun TextFieldBuffer() {
174172
@Preview
175173
@Composable
176174
fun EditTextFieldState() {
177-
// [START android_compose_state_text_9]
178175
val usernameState = rememberTextFieldState("I love Android")
176+
editTFState(usernameState)
177+
}
178+
179+
fun editTFState(textFieldState: TextFieldState) {
180+
// [START android_compose_state_text_9]
181+
// Initial textFieldState text passed in is "I love Android"
179182
// textFieldState.text : I love Android
180183
// textFieldState.selection: TextRange(14, 14)
181-
usernameState.edit { insert(14, "!") }
184+
textFieldState.edit { insert(14, "!") }
182185
// textFieldState.text : I love Android!
183186
// textFieldState.selection: TextRange(15, 15)
184-
usernameState.edit { replace(7, 14, "Compose") }
187+
textFieldState.edit { replace(7, 14, "Compose") }
185188
// textFieldState.text : I love Compose!
186189
// textFieldState.selection: TextRange(15, 15)
187-
usernameState.edit { append("!!!") }
190+
textFieldState.edit { append("!!!") }
188191
// textFieldState.text : I love Compose!!!!
189192
// textFieldState.selection: TextRange(18, 18)
190-
usernameState.edit { selectAll() }
193+
textFieldState.edit { selectAll() }
191194
// textFieldState.text : I love Compose!!!!
192195
// textFieldState.selection: TextRange(0, 18)
193196
// [END android_compose_state_text_9]
194197

195198
// [START android_compose_state_text_10]
196-
usernameState.setTextAndPlaceCursorAtEnd("I really love Android")
199+
textFieldState.setTextAndPlaceCursorAtEnd("I really love Android")
197200
// textFieldState.text : I really love Android
198201
// textFieldState.selection : TextRange(21, 21)
199202
// [END android_compose_state_text_10]
200203

201204
// [START android_compose_state_text_11]
202-
usernameState.clearText()
205+
textFieldState.clearText()
203206
// textFieldState.text :
204207
// textFieldState.selection : TextRange(0, 0)
205208
// [END android_compose_state_text_11]

0 commit comments

Comments
 (0)