Skip to content

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

feat(android): the Screen tab has a touchpad under the PC's screen (#104)

averagec46649116a61
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 a1a8f3c..39f01bd 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
@@ -331,14 +331,29 @@ fun RemoteScreen(
onStatus = onStatus,
modifier = Modifier.fillMaxSize(),
)
- RemoteMode.Screen -> PcMirrorView(
- pcId = device.id,
- pcName = device.name,
- core = core,
- isFullScreen = screenFullScreen,
- onFullScreenChange = { screenFullScreen = it },
+ RemoteMode.Screen -> Column(
modifier = Modifier.fillMaxSize(),
- )
+ verticalArrangement = Arrangement.spacedBy(10.dp),
+ ) {
+ PcMirrorView(
+ pcId = device.id,
+ pcName = device.name,
+ core = core,
+ isFullScreen = screenFullScreen,
+ onFullScreenChange = { screenFullScreen = it },
+ modifier = Modifier.weight(1f).fillMaxWidth(),
+ )
+ // The touchpad under the screen: watch the PC and move its pointer together.
+ if (!screenFullScreen) {
+ TouchpadSurface(
+ pcId = device.id,
+ core = core,
+ sensitivity = sensitivity,
+ onStatus = onStatus,
+ modifier = Modifier.fillMaxWidth().height(180.dp),
+ )
+ }
+ }
}
}
}
@@ -424,6 +439,171 @@ private fun StatusBanner(title: String?, text: String) {
}
}
+/** The touchpad surface: moves, clicks, drags and scrolls the PC pointer. */
+@Composable
+private fun TouchpadSurface(
+ pcId: String,
+ core: Core,
+ sensitivity: Float,
+ onStatus: (Core.RemoteAccess) -> Unit,
+ modifier: Modifier = Modifier,
+) {
+ var dragging by remember { mutableStateOf(false) }
+ var touchPoint by remember { mutableStateOf<Offset?>(null) }
+ val primaryColor = MaterialTheme.colorScheme.primary
+ val outlineColor = MaterialTheme.colorScheme.outlineVariant
+ val touchpadDesc = stringResource(R.string.remote_mode_touchpad)
+ Surface(
+ shape = MaterialTheme.shapes.extraLarge,
+ color = MaterialTheme.colorScheme.surfaceContainerHigh,
+ border = BorderStroke(
+ width = if (dragging) 2.dp else 1.dp,
+ color = if (dragging) primaryColor else outlineColor,
+ ),
+ modifier = modifier
+ .semantics { contentDescription = touchpadDesc }
+ .pointerInput(pcId, sensitivity) {
+ val tapSlop = viewConfiguration.touchSlop
+ // Movement in reference-density pixels, so the same physical swipe moves
+ // the PC pointer and scrolls the same amount on every phone's screen.
+ val toReference = TOUCHPAD_REFERENCE_DENSITY / density
+ val tapTimeoutMs = 240L
+ val doubleTapWindowMs = 260L
+ var lastSingleTapTime = 0L
+
+ awaitEachGesture {
+ val firstDown = awaitFirstDown(requireUnconsumed = false)
+ val downTime = System.currentTimeMillis()
+ var maxPointers = 1
+ var totalMoved = 0f
+ var scrollAccumX = 0f
+ var scrollAccumY = 0f
+ var isDragHold = (downTime - lastSingleTapTime) <= doubleTapWindowMs
+ var dragStarted = false
+
+ touchPoint = firstDown.position
+
+ while (true) {
+ val event = awaitPointerEvent()
+ val changes = event.changes
+ var pressedCount = 0
+ var firstPressed: androidx.compose.ui.input.pointer.PointerInputChange? = null
+ for (i in 0 until changes.size) {
+ val c = changes[i]
+ if (c.pressed) {
+ if (firstPressed == null) firstPressed = c
+ pressedCount++
+ }
+ }
+ if (pressedCount == 0 || firstPressed == null) break
+
+ if (pressedCount > maxPointers) {
+ maxPointers = pressedCount
+ if (dragStarted) {
+ core.remoteButton(pcId, "left", "up", onStatus)
+ dragStarted = false
+ dragging = false
+ }
+ isDragHold = false
+ }
+
+ if (pressedCount == 1 && maxPointers == 1) {
+ val change = firstPressed
+ touchPoint = change.position
+ val delta = change.positionChange()
+ val dist = hypot(delta.x, delta.y)
+ totalMoved += dist
+
+ if (isDragHold && !dragStarted && totalMoved > tapSlop * 0.6f) {
+ dragStarted = true
+ dragging = true
+ core.remoteButton(pcId, "left", "down", onStatus)
+ }
+
+ if (dist > 0.2f) {
+ val scaledDx = delta.x * toReference * sensitivity
+ val scaledDy = delta.y * toReference * sensitivity
+ core.remoteMove(pcId, scaledDx, scaledDy)
+ change.consume()
+ }
+ } else if (pressedCount >= 2) {
+ touchPoint = firstPressed.position
+ var avgDx = 0f
+ var avgDy = 0f
+ for (i in 0 until changes.size) {
+ val c = changes[i]
+ if (c.pressed) {
+ val d = c.positionChange()
+ avgDx += d.x
+ avgDy += d.y
+ c.consume()
+ }
+ }
+ avgDx /= pressedCount
+ avgDy /= pressedCount
+ totalMoved += hypot(avgDx, avgDy)
+
+ scrollAccumX += avgDx * toReference
+ scrollAccumY += avgDy * toReference
+ val stepPx = 18f
+ if (abs(scrollAccumX) >= stepPx || abs(scrollAccumY) >= stepPx) {
+ val notchesX = -(scrollAccumX / 48f)
+ val notchesY = -(scrollAccumY / 36f)
+ scrollAccumX = 0f
+ scrollAccumY = 0f
+ core.remoteScroll(pcId, notchesX, notchesY, fast = true)
+ }
+ }
+ }
+
+ touchPoint = null
+ val elapsed = System.currentTimeMillis() - downTime
+ if (dragStarted) {
+ core.remoteButton(pcId, "left", "up", onStatus)
+ dragging = false
+ lastSingleTapTime = 0L
+ } else if (elapsed <= tapTimeoutMs && totalMoved <= tapSlop * 1.5f) {
+ if (maxPointers == 1) {
+ lastSingleTapTime = System.currentTimeMillis()
+ core.remoteButton(pcId, "left", "click", onStatus)
+ } else if (maxPointers == 2) {
+ lastSingleTapTime = 0L
+ core.remoteButton(pcId, "right", "click", onStatus)
+ }
+ } else {
+ lastSingleTapTime = 0L
+ }
+ }
+ },
+ ) {
+ Box(Modifier.fillMaxSize()) {
+ Canvas(Modifier.fillMaxSize()) {
+ touchPoint?.let { pt ->
+ drawCircle(
+ color = primaryColor.copy(alpha = 0.22f),
+ radius = 28.dp.toPx(),
+ center = pt,
+ )
+ drawCircle(
+ color = primaryColor.copy(alpha = 0.65f),
+ radius = 6.dp.toPx(),
+ center = pt,
+ )
+ }
+ }
+ Text(
+ stringResource(R.string.remote_touchpad_hint),
+ style = MaterialTheme.typography.bodySmall,
+ color = MaterialTheme.colorScheme.onSurfaceVariant,
+ textAlign = TextAlign.Center,
+ modifier = Modifier
+ .align(Alignment.BottomCenter)
+ .padding(horizontal = 20.dp, vertical = 14.dp),
+ )
+ }
+ }
+}
+
@OptIn(ExperimentalLayoutApi::class)
@Composable
private fun TouchpadView(
@@ -440,8 +620,6 @@ private fun TouchpadView(
// 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) }
val focusRequester = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current
@@ -465,161 +643,13 @@ private fun TouchpadView(
// While typing, the keyboard leaves too little room for the touchpad,
// and the field below must keep its full height.
if (!imeVisible) {
- // Big touchpad surface
- val primaryColor = MaterialTheme.colorScheme.primary
- val outlineColor = MaterialTheme.colorScheme.outlineVariant
- val touchpadDesc = stringResource(R.string.remote_mode_touchpad)
- Surface(
- shape = MaterialTheme.shapes.extraLarge,
- color = MaterialTheme.colorScheme.surfaceContainerHigh,
- border = BorderStroke(
- width = if (dragging) 2.dp else 1.dp,
- color = if (dragging) primaryColor else outlineColor,
- ),
- modifier = Modifier
- .weight(1f)
- .fillMaxWidth()
- .semantics { contentDescription = touchpadDesc }
- .pointerInput(pcId, sensitivity) {
- val tapSlop = viewConfiguration.touchSlop
- // Movement in reference-density pixels, so the same physical swipe moves
- // the PC pointer and scrolls the same amount on every phone's screen.
- val toReference = TOUCHPAD_REFERENCE_DENSITY / density
- val tapTimeoutMs = 240L
- val doubleTapWindowMs = 260L
- var lastSingleTapTime = 0L
-
- awaitEachGesture {
- val firstDown = awaitFirstDown(requireUnconsumed = false)
- val downTime = System.currentTimeMillis()
- var maxPointers = 1
- var totalMoved = 0f
- var scrollAccumX = 0f
- var scrollAccumY = 0f
- var isDragHold = (downTime - lastSingleTapTime) <= doubleTapWindowMs
- var dragStarted = false
-
- touchPoint = firstDown.position
-
- while (true) {
- val event = awaitPointerEvent()
- val changes = event.changes
- var pressedCount = 0
- var firstPressed: androidx.compose.ui.input.pointer.PointerInputChange? = null
- for (i in 0 until changes.size) {
- val c = changes[i]
- if (c.pressed) {
- if (firstPressed == null) firstPressed = c
- pressedCount++
- }
- }
- if (pressedCount == 0 || firstPressed == null) break
-
- if (pressedCount > maxPointers) {
- maxPointers = pressedCount
- if (dragStarted) {
- core.remoteButton(pcId, "left", "up", onStatus)
- dragStarted = false
- dragging = false
- }
- isDragHold = false
- }
-
- if (pressedCount == 1 && maxPointers == 1) {
- val change = firstPressed
- touchPoint = change.position
- val delta = change.positionChange()
- val dist = hypot(delta.x, delta.y)
- totalMoved += dist
-
- if (isDragHold && !dragStarted && totalMoved > tapSlop * 0.6f) {
- dragStarted = true
- dragging = true
- core.remoteButton(pcId, "left", "down", onStatus)
- }
-
- if (dist > 0.2f) {
- val scaledDx = delta.x * toReference * sensitivity
- val scaledDy = delta.y * toReference * sensitivity
- core.remoteMove(pcId, scaledDx, scaledDy)
- change.consume()
- }
- } else if (pressedCount >= 2) {
- touchPoint = firstPressed.position
- var avgDx = 0f
- var avgDy = 0f
- for (i in 0 until changes.size) {
- val c = changes[i]
- if (c.pressed) {
- val d = c.positionChange()
- avgDx += d.x
- avgDy += d.y
- c.consume()
- }
- }
- avgDx /= pressedCount
- avgDy /= pressedCount
- totalMoved += hypot(avgDx, avgDy)
-
- scrollAccumX += avgDx * toReference
- scrollAccumY += avgDy * toReference
- val stepPx = 18f
- if (abs(scrollAccumX) >= stepPx || abs(scrollAccumY) >= stepPx) {
- val notchesX = -(scrollAccumX / 48f)
- val notchesY = -(scrollAccumY / 36f)
- scrollAccumX = 0f
- scrollAccumY = 0f
- core.remoteScroll(pcId, notchesX, notchesY, fast = true)
- }
- }
- }
-
- touchPoint = null
- val elapsed = System.currentTimeMillis() - downTime
- if (dragStarted) {
- core.remoteButton(pcId, "left", "up", onStatus)
- dragging = false
- lastSingleTapTime = 0L
- } else if (elapsed <= tapTimeoutMs && totalMoved <= tapSlop * 1.5f) {
- if (maxPointers == 1) {
- lastSingleTapTime = System.currentTimeMillis()
- core.remoteButton(pcId, "left", "click", onStatus)
- } else if (maxPointers == 2) {
- lastSingleTapTime = 0L
- core.remoteButton(pcId, "right", "click", onStatus)
- }
- } else {
- lastSingleTapTime = 0L
- }
- }
- },
- ) {
- Box(Modifier.fillMaxSize()) {
- Canvas(Modifier.fillMaxSize()) {
- touchPoint?.let { pt ->
- drawCircle(
- color = primaryColor.copy(alpha = 0.22f),
- radius = 28.dp.toPx(),
- center = pt,
- )
- drawCircle(
- color = primaryColor.copy(alpha = 0.65f),
- radius = 6.dp.toPx(),
- center = pt,
- )
- }
- }
- Text(
- stringResource(R.string.remote_touchpad_hint),
- style = MaterialTheme.typography.bodySmall,
- color = MaterialTheme.colorScheme.onSurfaceVariant,
- textAlign = TextAlign.Center,
- modifier = Modifier
- .align(Alignment.BottomCenter)
- .padding(horizontal = 20.dp, vertical = 14.dp),
- )
- }
- }
+ TouchpadSurface(
+ pcId = pcId,
+ core = core,
+ sensitivity = sensitivity,
+ onStatus = onStatus,
+ modifier = Modifier.weight(1f).fillMaxWidth(),
+ )
}
// Mouse buttons + Keyboard toggle row