Skip to content

Commit aba0f19

Browse files
fix: Correct system message TextField behavior
Addresses your feedback on the system message TextField in PhotoReasoningScreen: - Sets focused height with keyboard to 450dp (was 600dp). - Dynamically adjusts minLines and maxLines of the OutlinedTextField to ensure the text input area expands with the component's height. - Modifies the BackHandler to explicitly clear focus from the TextField (in addition to collapsing it) when it's focused without the keyboard and back is pressed. This ensures it can be re-expanded correctly on subsequent focus. These changes improve the usability and appearance of the system message input field.
1 parent a0075d8 commit aba0f19

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

app/src/main/kotlin/com/google/ai/sample/feature/multimodal/PhotoReasoningScreen.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import androidx.compose.ui.focus.onFocusChanged
5454
import androidx.compose.ui.draw.drawBehind
5555
import androidx.compose.ui.graphics.Color
5656
import androidx.compose.ui.platform.LocalContext
57+
import androidx.compose.ui.platform.LocalFocusManager
5758
import androidx.compose.ui.res.stringResource
5859
import androidx.compose.ui.tooling.preview.Preview
5960
import androidx.compose.ui.unit.dp
@@ -195,9 +196,11 @@ fun PhotoReasoningScreen(
195196
var isSystemMessageFocused by rememberSaveable { mutableStateOf(false) }
196197
val listState = rememberLazyListState()
197198
val context = LocalContext.current // Get context for Toast
199+
val focusManager = LocalFocusManager.current
198200

199201
BackHandler(enabled = isSystemMessageFocused && !isKeyboardOpen) {
200-
isSystemMessageFocused = false
202+
focusManager.clearFocus() // Clear focus first
203+
isSystemMessageFocused = false // Then update the state that controls height
201204
}
202205

203206
val pickMedia = rememberLauncherForActivityResult(
@@ -234,10 +237,12 @@ fun PhotoReasoningScreen(
234237
)
235238
Spacer(modifier = Modifier.height(8.dp))
236239
val systemMessageHeight = when {
237-
isSystemMessageFocused && isKeyboardOpen -> 600.dp
240+
isSystemMessageFocused && isKeyboardOpen -> 450.dp // Changed from 600.dp
238241
isSystemMessageFocused && !isKeyboardOpen -> 1000.dp
239242
else -> 120.dp
240243
}
244+
val currentMinLines = if (systemMessageHeight == 120.dp) 3 else 1
245+
val currentMaxLines = if (systemMessageHeight == 120.dp) 5 else Int.MAX_VALUE
241246
OutlinedTextField(
242247
value = systemMessage,
243248
onValueChange = onSystemMessageChanged,
@@ -246,8 +251,8 @@ fun PhotoReasoningScreen(
246251
.fillMaxWidth()
247252
.height(systemMessageHeight)
248253
.onFocusChanged { focusState -> isSystemMessageFocused = focusState.isFocused },
249-
maxLines = 5,
250-
minLines = 3
254+
minLines = currentMinLines,
255+
maxLines = currentMaxLines
251256
)
252257
}
253258
}

0 commit comments

Comments
 (0)