Skip to content

Commit

Permalink
Fix Ctrl+Backspace in TextField (JetBrains#329)
Browse files Browse the repository at this point in the history
Issue JetBrains#2466

Co-Authored-By: Silas <s.develop@4-dc.de>

Co-authored-by: Silas <s.develop@4-dc.de>
  • Loading branch information
eymar and silenium-dev committed Jan 13, 2023
1 parent 81629af commit 65768ed
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
Expand Up @@ -344,6 +344,7 @@ internal abstract class BaseTextPreparedSelection<T : BaseTextPreparedSelection<

private fun charOffset(offset: Int) =
offset.coerceAtMost(text.length - 1)
.coerceAtLeast(0)

private fun getParagraphStart() = text.findParagraphStart(selection.min)

Expand Down Expand Up @@ -429,4 +430,4 @@ internal class TextFieldPreparedSelection(
value.getOffsetForPosition(Offset(x, y))
)
}
}
}
Expand Up @@ -153,4 +153,60 @@ class SelectionTests {
rule.awaitIdle()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(1, 0))
}

@OptIn(ExperimentalTestApi::class, ExperimentalComposeUiApi::class)
@Test
fun `Ctrl + Backspace on an empty line with DesktopPlatform-Windows`() = runBlocking {
setPlatformDefaultKeyMapping(createPlatformDefaultKeyMapping(DesktopPlatform.Windows))
val state = mutableStateOf(TextFieldValue(""))

rule.setContent {
BasicTextField(
value = state.value,
onValueChange = { state.value = it },
modifier = Modifier.testTag("textField")
)
}
rule.awaitIdle()
rule.onNodeWithTag("textField").performMouseInput {
click(Offset(0f, 0f))
}
rule.awaitIdle()
rule.onNodeWithTag("textField").assertIsFocused()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(0, 0))

rule.onNodeWithTag("textField").performKeyInput {
keyDown(Key.CtrlLeft)
keyDown(Key.Backspace)
}
rule.awaitIdle()
}

@OptIn(ExperimentalTestApi::class, ExperimentalComposeUiApi::class)
@Test
fun `Ctrl + Backspace on an empty line with DesktopPlatform-Macos`() = runBlocking {
setPlatformDefaultKeyMapping(createPlatformDefaultKeyMapping(DesktopPlatform.MacOS))
val state = mutableStateOf(TextFieldValue(""))

rule.setContent {
BasicTextField(
value = state.value,
onValueChange = { state.value = it },
modifier = Modifier.testTag("textField")
)
}
rule.awaitIdle()
rule.onNodeWithTag("textField").performMouseInput {
click(Offset(0f, 0f))
}
rule.awaitIdle()
rule.onNodeWithTag("textField").assertIsFocused()
Truth.assertThat(state.value.selection).isEqualTo(TextRange(0, 0))

rule.onNodeWithTag("textField").performKeyInput {
keyDown(Key.AltLeft)
keyDown(Key.Backspace)
}
rule.awaitIdle()
}
}

0 comments on commit 65768ed

Please sign in to comment.