Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1058,14 +1058,20 @@ private fun ZoomDialog(pdfState: PdfState, onDismiss: () -> Unit) {
@Composable
private fun GoToPageDialog(pdfState: PdfState, onDismiss: () -> Unit) {
var pageNumber by remember { mutableStateOf("") }
val focusRequester = remember { FocusRequester() }
val keyboard = LocalSoftwareKeyboardController.current

val select: () -> Unit = {
pageNumber.toIntOrNull()?.let { pdfState.pdfViewer?.goToPage(it) }
keyboard?.hide()
onDismiss()
}

AlertDialog(
onDismissRequest = onDismiss,
onDismissRequest = {
keyboard?.hide()
onDismiss()
},
title = {
Text(
text = "Go to page",
Expand All @@ -1074,7 +1080,12 @@ private fun GoToPageDialog(pdfState: PdfState, onDismiss: () -> Unit) {
)
},
confirmButton = { TextButton(onClick = select) { Text("Go") } },
dismissButton = { TextButton(onClick = onDismiss) { Text(text = "Cancel") } },
dismissButton = {
TextButton(onClick = {
keyboard?.hide()
onDismiss()
}) { Text("Cancel") }
},
text = {
BasicTextField(
value = pageNumber,
Expand All @@ -1092,7 +1103,8 @@ private fun GoToPageDialog(pdfState: PdfState, onDismiss: () -> Unit) {
cursorBrush = SolidColor(MaterialTheme.colorScheme.onBackground),
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 6.dp),
.padding(horizontal = 6.dp)
.focusRequester(focusRequester),
decorationBox = { textField ->
Box(Modifier.fillMaxWidth()) {
textField()
Expand All @@ -1112,6 +1124,13 @@ private fun GoToPageDialog(pdfState: PdfState, onDismiss: () -> Unit) {
)
}
)

LaunchedEffect(focusRequester) {
// Add a small delay to ensure the text field is composed and ready for focus
delay(100)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is delay the only way to do, have you tried any other reliable ways?

focusRequester.requestFocus()
keyboard?.show()
}
}

@Composable
Expand Down