-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BasicTextField selection collapses to a zero span at the cursor location when focus is lost #2569
Comments
Reproduced. To be precise, this is the change that causing such a behaviour:
It was intentional, but apparently it didn't consider the use case like you described. Thank you! We'll need to come up with some better logic. |
I checked this code, changing the focus programmatically, not pressing the button:
In Compose 1.1.1 and Compose 1.2.2 it works the same - we lose selection in the text field. So it is not a regression. In Compose 1.2.0 It also not a bug, because it programmed intentionally to clear selection when we lose focus. Maybe it is not right to clear selection from UX perspective or platform guidelines perspective, but we need to investigate that. If we decide to not clear selection - we probably should change the color of the selection (usually it is gray). Also it is not obvious how will 2 text fields behave. Should they have its own selection? Maybe we need to introduce something like focus groups, that can have their own independent focus inside the group. |
It looks like those are icon buttons in a toolbar, and we need to not switch focus to them, when we click on them 🤔. I.e. they shouldn't be focusable. @eymar, I think, we either need to rethink our approach to make all clickable's focusable (make only some widgets), or provide a way to disable focusability. |
One of the current workarounds - don't use |
From the UX perspective, I don't think it would be the right move to disable focus on such toolbar buttons or comboboxes. I'd still want them to be fully traversible. I would rather prefer to explicitly mark the text field (read only or not) to not lose selection when it loses focus. |
As an example, see IntelliJ. You can select a block in the editor tab, then start Search / Search & Replace and use tab to traverse the text fields and buttons in that bar without losing the selection in the editor. |
One more report for this issue: #2687 |
It's been a year since this issue has been reported. Any progress ? |
Waiting for a fix, I found the folowing workaround. var value: TextFieldValue by remember { mutableStateOf(TextFieldValue()) }
var previousSelection by remember { mutableStateOf(TextRange(0)) }
BasicTextField(
value = value,
onValueChange = {
previousSelection = value.selection
value = it
},
modifier = Modifier
.onFocusChanged {
if (!previousSelection.collapsed && state.value.selection.max == previousSelection.max) {
value = value.copy(selection = previousSelection)
}
}
) |
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks. |
Here is the sample to reproduce this on 1.2.0 and 1.3.0-rc01:
Run the app. Select a range in the text - it will print the selected range in the console. Now click the button at the top of the window and look at the updated range printed in the console.
JetBrains/compose-multiplatform-core@ca869b1#diff-42592036468eac046f04d9f25ac21f134ed22d3b55e602da3c56bb0c9f206a7c may be the change that caused this behavior change. Note that version 1.1.0 and the Android variant do not lose the selection on focus loss.
In Swing, by the way, selection is not lost on focus loss. It is not drawn by default, and there is a workaround to keep the selection visible:
The target use case is a simple text editor that allows toggling bold, italic, underline, strikethrough, font size, font family on the selected span. Think a text pane with a strip of buttons on the side to initiate those actions.
The text was updated successfully, but these errors were encountered: