Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Android: type into the PC like a normal keyboard

Beee2ab25d67b80
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0180323..e58c6b8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -73,6 +73,10 @@ change things that need a step from you, and its notes say so.
- **Mirroring with Elevated starts without asking on the phone.** Starting it
failed on the phone (it opened the helper's stream on the main thread,
which Android refuses), so it fell back to asking you to share the screen.
+- **Typing on the PC from the phone works like a normal keyboard.** In
+ Control PC, Backspace deletes, the symbols page stays open for more than
+ one symbol, autocorrect and suggestions work, and letters no longer come
+ out jumbled.
- Android: Phone as webcam no longer crashes the app when its preview
comes back (the same fix covers the QR scanner and Continuity Camera).
- Android: on Hive, the category chips at the top of Settings and the
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/remote/RemoteScreen.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/remote/RemoteScreen.kt
index 32a42ac..45f524f 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/ui/remote/RemoteScreen.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/remote/RemoteScreen.kt
@@ -79,6 +79,11 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.geometry.Offset
+import androidx.compose.ui.input.key.Key
+import androidx.compose.ui.input.key.KeyEventType
+import androidx.compose.ui.input.key.key
+import androidx.compose.ui.input.key.onPreviewKeyEvent
+import androidx.compose.ui.input.key.type
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.positionChange
import androidx.compose.ui.platform.LocalConfiguration
@@ -93,7 +98,9 @@ import androidx.compose.ui.semantics.onClick
import androidx.compose.ui.semantics.role
import androidx.compose.ui.semantics.selected
import androidx.compose.ui.semantics.semantics
+import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.ImeAction
+import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
@@ -103,9 +110,9 @@ import app.nectarlink.android.core.Core
import app.nectarlink.android.core.Device
import app.nectarlink.android.core.WakeState
import app.nectarlink.android.ui.theme.LocalReducedMotion
-import kotlinx.coroutines.delay
import kotlin.math.abs
import kotlin.math.hypot
+import kotlinx.coroutines.delay
enum class RemoteMode { Touchpad, AirMouse, Presentation, Screen }
@@ -422,7 +429,9 @@ private fun TouchpadView(
modifier: Modifier = Modifier,
) {
var showKeyboard by rememberSaveable { mutableStateOf(false) }
- var textBuffer by remember { mutableStateOf("") }
+ // What's been typed since the last Enter: a real field, so the keyboard
+ // keeps its page, autocorrects, and has something to delete.
+ var typed by remember { mutableStateOf(TextFieldValue("")) }
val activeMods = remember { mutableStateListOf<String>() }
var dragging by remember { mutableStateOf(false) }
var touchPoint by remember { mutableStateOf<Offset?>(null) }
@@ -681,21 +690,28 @@ private fun TouchpadView(
if (showKeyboard) {
val typePlaceholder = stringResource(R.string.remote_type_placeholder, pcName)
OutlinedTextField(
- value = textBuffer,
+ value = typed,
onValueChange = { next ->
- if (next.length > textBuffer.length) {
- val added = next.substring(textBuffer.length)
- if (activeMods.isNotEmpty() && added.length == 1 && added[0].isLetterOrDigit()) {
- core.remoteKey(pcId, added.lowercase(), activeMods.toList(), onStatus)
- activeMods.clear()
- } else {
- core.remoteText(pcId, added, onStatus)
- }
- } else if (next.length < textBuffer.length) {
- core.remoteKey(pcId, "backspace", activeMods.toList(), onStatus)
+ val diff = typingDiff(typed.text, next.text)
+ if (activeMods.isNotEmpty() && diff.backspaces == 0 && diff.added.length == 1 &&
+ diff.added[0].isLetterOrDigit()
+ ) {
+ // A shortcut (Ctrl+C...), not text: it stays out of the field.
+ core.remoteKey(pcId, diff.added.lowercase(), activeMods.toList(), onStatus)
activeMods.clear()
+ return@OutlinedTextField
+ }
+ repeat(diff.backspaces) { i ->
+ core.remoteKey(pcId, "backspace", if (i == 0) activeMods.toList() else emptyList(), onStatus)
+ }
+ if (diff.added.isNotEmpty()) core.remoteText(pcId, diff.added, onStatus)
+ if (diff.backspaces > 0 || diff.added.isNotEmpty()) activeMods.clear()
+ // The PC's cursor is at the end of what was typed: keep this one there too.
+ typed = if (next.selection == TextRange(next.text.length)) {
+ next
+ } else {
+ next.copy(selection = TextRange(next.text.length))
}
- textBuffer = " "
},
placeholder = { Text(typePlaceholder) },
singleLine = true,
@@ -704,11 +720,22 @@ private fun TouchpadView(
onSend = {
core.remoteKey(pcId, "enter", activeMods.toList(), onStatus)
activeMods.clear()
+ typed = TextFieldValue("")
},
),
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
+ // Backspace with nothing typed here still deletes on the PC.
+ .onPreviewKeyEvent { event ->
+ if (event.type == KeyEventType.KeyDown && event.key == Key.Backspace && typed.text.isEmpty()) {
+ core.remoteKey(pcId, "backspace", activeMods.toList(), onStatus)
+ activeMods.clear()
+ true
+ } else {
+ false
+ }
+ }
.semantics { contentDescription = typePlaceholder },
)
}
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/ui/remote/TypingDiff.kt b/android/app/src/main/kotlin/app/nectarlink/android/ui/remote/TypingDiff.kt
new file mode 100644
index 0000000..062df42
--- /dev/null
+++ b/android/app/src/main/kotlin/app/nectarlink/android/ui/remote/TypingDiff.kt
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.ui.remote
+
+/**
+ * What changed in the phone's typing field, as keys for the PC: delete
+ * [backspaces] characters at the cursor, then type [added]. The field keeps
+ * what was typed so the keyboard can autocorrect and stay on its symbols
+ * page; a word it corrects ("teh" to "the") comes out as backspaces and the
+ * new word.
+ */
+internal data class TypingDiff(val backspaces: Int, val added: String)
+
+internal fun typingDiff(old: String, new: String): TypingDiff {
+ var same = 0
+ val shorter = minOf(old.length, new.length)
+ while (same < shorter && old[same] == new[same]) same++
+ // Never split an emoji (or any character outside the BMP) in two.
+ if (same > 0 && Character.isHighSurrogate(old[same - 1]) && same < old.length) same--
+ val removed = old.substring(same)
+ return TypingDiff(
+ backspaces = removed.codePointCount(0, removed.length),
+ added = new.substring(same),
+ )
+}
diff --git a/android/app/src/test/kotlin/app/nectarlink/android/ui/remote/TypingDiffTest.kt b/android/app/src/test/kotlin/app/nectarlink/android/ui/remote/TypingDiffTest.kt
new file mode 100644
index 0000000..daa7d6a
--- /dev/null
+++ b/android/app/src/test/kotlin/app/nectarlink/android/ui/remote/TypingDiffTest.kt
@@ -0,0 +1,49 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+package app.nectarlink.android.ui.remote
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+class TypingDiffTest {
+ @Test
+ fun aKeyTypedIsSentAsItself() {
+ assertEquals(TypingDiff(0, "a"), typingDiff("", "a"))
+ assertEquals(TypingDiff(0, "%"), typingDiff("50", "50%"))
+ }
+
+ @Test
+ fun symbolsInARowEachArrive() {
+ assertEquals(TypingDiff(0, "%"), typingDiff("5%", "5%%"))
+ }
+
+ @Test
+ fun backspaceDeletesOneCharacter() {
+ assertEquals(TypingDiff(1, ""), typingDiff("hello", "hell"))
+ }
+
+ @Test
+ fun anAutocorrectedWordIsDeletedAndRetyped() {
+ assertEquals(TypingDiff(2, "he "), typingDiff("teh", "the "))
+ // Only what differs goes: "anyt" stays, "ing" becomes "hing ".
+ assertEquals(TypingDiff(3, "hing "), typingDiff("is anyting", "is anything "))
+ }
+
+ @Test
+ fun aPastedOrSuggestedChunkIsSentWhole() {
+ assertEquals(TypingDiff(0, "world"), typingDiff("hello ", "hello world"))
+ }
+
+ @Test
+ fun anEmojiIsOneCharacterToDeleteAndIsNeverSplit() {
+ val grin = "😀"
+ val cry = "😢"
+ assertEquals(TypingDiff(1, ""), typingDiff("hi$grin", "hi"))
+ // Emoji that share their first half: replace the whole emoji.
+ assertEquals(TypingDiff(1, cry), typingDiff("hi$grin", "hi$cry"))
+ }
+
+ @Test
+ fun noChangeSendsNothing() {
+ assertEquals(TypingDiff(0, ""), typingDiff("same", "same"))
+ }
+}