diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/SessionsScreen.kt b/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/SessionsScreen.kt index 7258323..d67aea8 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/SessionsScreen.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/SessionsScreen.kt @@ -2,6 +2,7 @@ package io.github.lordofpolls.shellwave.feature.session import android.content.Intent import android.net.Uri +import android.view.KeyEvent import androidx.activity.compose.LocalActivity import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement @@ -35,6 +36,7 @@ import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.rememberUpdatedState import androidx.compose.runtime.setValue import androidx.compose.runtime.snapshotFlow import androidx.compose.runtime.withFrameNanos @@ -43,7 +45,9 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color +import androidx.compose.ui.input.key.KeyEventType import androidx.compose.ui.input.key.onPreviewKeyEvent +import androidx.compose.ui.input.key.type import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.platform.LocalView @@ -69,6 +73,7 @@ import io.github.lordofpolls.shellwave.ssh.SessionManager import io.github.lordofpolls.shellwave.ssh.SessionStatus import io.github.lordofpolls.shellwave.ssh.SshConnection import io.github.lordofpolls.shellwave.ssh.TerminalSize +import io.github.lordofpolls.shellwave.terminal.ClipboardShortcut import io.github.lordofpolls.shellwave.terminal.DEFAULT_KEY_BAR_KEYS import io.github.lordofpolls.shellwave.terminal.DEFAULT_TERMINAL_PROFILE import io.github.lordofpolls.shellwave.terminal.DEFAULT_TERMINAL_TEXT_SIZE_SP @@ -81,6 +86,8 @@ import io.github.lordofpolls.shellwave.terminal.TerminalInputCapture import io.github.lordofpolls.shellwave.terminal.TerminalLink import io.github.lordofpolls.shellwave.terminal.TerminalMatch import io.github.lordofpolls.shellwave.terminal.TerminalSelectionOverlay +import io.github.lordofpolls.shellwave.terminal.WheelAction +import io.github.lordofpolls.shellwave.terminal.clipboardShortcut import io.github.lordofpolls.shellwave.terminal.collectTerminalRows import io.github.lordofpolls.shellwave.terminal.ctrlCode import io.github.lordofpolls.shellwave.terminal.decodeKeyBarKeys @@ -93,7 +100,9 @@ import io.github.lordofpolls.shellwave.terminal.resolveTerminalTypeface import io.github.lordofpolls.shellwave.terminal.selectionHighlightColor import io.github.lordofpolls.shellwave.terminal.dragRows import io.github.lordofpolls.shellwave.terminal.terminalGestures +import io.github.lordofpolls.shellwave.terminal.wheelAction import io.github.lordofpolls.shellwave.terminal.wheelButton +import io.github.lordofpolls.shellwave.terminal.wheelNotches import io.github.lordofpolls.shellwave.ui.design.EmptyState import io.github.lordofpolls.shellwave.ui.design.MachineText import io.github.lordofpolls.shellwave.ui.design.SessionChipModel @@ -405,6 +414,9 @@ internal fun SessionTabBody( var terminalEmulator by remember { mutableStateOf(null) } var measuredSize by remember { mutableStateOf(null) } + // TerminalSize is the pty's integer geometry; hit-testing needs the renderer's fractional + // width. + var cellWidth by remember { mutableStateOf(0f) } var ctrlLatched by remember { mutableStateOf(false) } var altLatched by remember { mutableStateOf(false) } @@ -414,6 +426,10 @@ internal fun SessionTabBody( var topRow by remember { mutableStateOf(0) } var scrollDragRemainderPx by remember { mutableStateOf(0f) } + // Wheel scrolling carries its remainder in notches, not pixels like the drag above, so a + // touchpad's fractional scroll events add up to a notch instead of each rounding to zero. + var wheelRemainderNotches by remember { mutableStateOf(0f) } + var searchQuery by remember(sessionId) { mutableStateOf("") } var searchMatches by remember(sessionId) { mutableStateOf(emptyList()) } var searchMatchIndex by remember(sessionId) { mutableIntStateOf(0) } @@ -613,7 +629,7 @@ internal fun SessionTabBody( // this only converts a pixel offset to the 1-based column and row it expects. fun mouseCell(offset: Offset): Pair? { val size = measuredSize ?: return null - return (offset.x / size.cellWidthPx).toInt() + 1 to (offset.y / size.cellHeightPx).toInt() + 1 + return (offset.x / cellWidth).toInt() + 1 to (offset.y / size.cellHeightPx).toInt() + 1 } fun sendMouseClick(offset: Offset) { @@ -632,6 +648,54 @@ internal fun SessionTabBody( repeat(abs(notches)) { emulator.sendMouseEvent(wheelButton(notches), column, row, true) } } + // `terminalGestures` captures its callbacks once, at the first composition, so anything they + // reach that reads `summary` directly would keep talking to the connection of that first + // composition. This state always holds the current one. + val connection by rememberUpdatedState(summary.connection) + + fun copySelection() { + val emulator = terminalEmulator ?: return + val text = selectionState.selectedText(emulator) ?: return + connection.onCopyTextToClipboard(text) + selectionState.clear() + } + + // Shift bypasses mouse tracking for the wheel as it does for a drag, so a full-screen app that + // grabbed the mouse can still be scrolled locally. + fun handleWheelScroll(offset: Offset, dy: Float, shift: Boolean) { + val emulator = terminalEmulator ?: return + val (notches, remainder) = wheelNotches(wheelRemainderNotches, dy) + wheelRemainderNotches = remainder + val action = + wheelAction( + notches = notches, + mouseTracking = emulator.isMouseTrackingActive() && !shift, + alternateBuffer = emulator.isAlternateBufferActive(), + ) ?: return + when (action) { + is WheelAction.MouseReport -> { + val (column, row) = mouseCell(offset) ?: return + repeat(action.count) { emulator.sendMouseEvent(action.button, column, row, true) } + } + + is WheelAction.Arrows -> { + val code = + KeyHandler.getCode( + if (action.down) KeyEvent.KEYCODE_DPAD_DOWN else KeyEvent.KEYCODE_DPAD_UP, + 0, + emulator.isCursorKeysApplicationMode, + emulator.isKeypadApplicationMode, + ) ?: return + connection.write(code.repeat(action.count)) + } + + is WheelAction.Scrollback -> { + val maxScrollback = emulator.screen.activeTranscriptRows + topRow = (topRow + action.rows).coerceIn(-maxScrollback, 0) + } + } + } + val localContext = LocalContext.current fun shareSelection(text: String) { @@ -683,9 +747,21 @@ internal fun SessionTabBody( .fillMaxSize() .background(Color.Black) // Preview rather than onKeyEvent: claims the event before - // TerminalInputCapture's BasicTextField, so Ctrl+C isn't first eaten by the - // field's own copy shortcut. + // TerminalInputCapture's BasicTextField. Without it Shift+Insert would paste + // into the invisible field and go out as typed text, with no bracketed paste, + // and Ctrl+C would be eaten by the field's own copy shortcut. .onPreviewKeyEvent { event -> + val shortcut = clipboardShortcut(event) + if (shortcut != null) { + if (event.type == KeyEventType.KeyDown) { + when (shortcut) { + ClipboardShortcut.PASTE -> + summary.connection.onPasteTextFromClipboard() + ClipboardShortcut.COPY -> copySelection() + } + } + return@onPreviewKeyEvent true + } val emulator = terminalEmulator val bytes = hardwareKeyCode( @@ -719,7 +795,7 @@ internal fun SessionTabBody( val size = measuredSize val link = if (emulator != null && size != null) { - val col = (offset.x / size.cellWidthPx).toInt() + val col = (offset.x / cellWidth).toInt() .coerceIn(0, emulator.mColumns - 1) val screenRow = (offset.y / size.cellHeightPx).toInt() .coerceIn(0, emulator.mRows - 1) @@ -740,7 +816,7 @@ internal fun SessionTabBody( if (emulator != null && size != null) { selectionState.selectWord( emulator, - size.cellWidthPx, + cellWidth, size.cellHeightPx, topRow, offset @@ -752,13 +828,52 @@ internal fun SessionTabBody( }, onMouseClick = ::sendMouseClick, onMouseWheel = ::sendMouseWheel, + onMouseSelect = { anchor, current -> + val emulator = terminalEmulator + val size = measuredSize + if (emulator != null && size != null) { + selectionState.selectRange( + emulator, + cellWidth, + size.cellHeightPx, + topRow, + anchor, + current + ) + } + }, + onMouseSelectEnd = { selectionState.endRangeSelection() }, + onMouseDoubleClick = { offset -> + val emulator = terminalEmulator + val size = measuredSize + if (emulator != null && size != null) { + selectionState.selectWord( + emulator, + cellWidth, + size.cellHeightPx, + topRow, + offset + ) + } + }, + onSecondaryClick = { + if (selectionState.selection != null) { + copySelection() + } else { + connection.onPasteTextFromClipboard() + } + }, + // A lambda, not ::handleWheelScroll: see the note on the key bar's + // `onText` below about memoised local function references. + onWheelScroll = { offset, dy, shift -> handleWheelScroll(offset, dy, shift) }, ), ) { TerminalCanvas( emulator = terminalEmulator, frame = frame, - onMeasured = { cols, rows, cellW, cellH -> - measuredSize = TerminalSize(cols, rows, cellW, cellH) + onMeasured = { cols, rows, cellWidthPx, cellHeightPx, measuredCellWidth -> + measuredSize = TerminalSize(cols, rows, cellWidthPx, cellHeightPx) + cellWidth = measuredCellWidth }, modifier = Modifier.matchParentSize(), fontSizeSp = fontSizeSp, @@ -789,7 +904,7 @@ internal fun SessionTabBody( TerminalSelectionOverlay( state = selectionState, emulator = terminalEmulator, - cellWidthPx = measuredSize?.cellWidthPx ?: 0, + cellWidth = cellWidth, cellHeightPx = measuredSize?.cellHeightPx ?: 0, topRow = topRow, onCopy = { text -> summary.connection.onCopyTextToClipboard(text) }, diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt index 0e1d03f..119804a 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/ssh/SshConnection.kt @@ -444,15 +444,17 @@ open class SshConnection( override fun titleChanged(oldTitle: String?, newTitle: String?) {} - // Two callers: TerminalEmulator for an OSC 52 request from the remote app (tmux's - // `set-clipboard`), and the selection overlay's Copy action. One clipboard write between them. + // Four callers: TerminalEmulator for an OSC 52 request from the remote app (tmux's + // `set-clipboard`), the selection overlay's Copy action, Ctrl+Shift+C and the link dialog's + // Copy. One clipboard write between them. override fun onCopyTextToClipboard(text: String?) { if (text.isNullOrEmpty()) return val clipboard = context.getSystemService(ClipboardManager::class.java) ?: return clipboard.setPrimaryClip(ClipData.newPlainText("terminal", text)) } - // Only the overlay's Paste action calls this; no OSC sequence triggers it from the remote side. + // Called by the selection overlay's Paste action and the hardware paste shortcuts (Ctrl+Shift+V, + // Shift+Insert); no OSC sequence triggers it from the remote side. // The clipboard read lives here so there is one place that knows how to read it, mirroring // onCopyTextToClipboard. emulator.paste() goes down the same write path as typed input. override fun onPasteTextFromClipboard() { diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalCanvas.kt b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalCanvas.kt index f3ded5f..518326c 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalCanvas.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalCanvas.kt @@ -34,14 +34,24 @@ val DEFAULT_TERMINAL_TEXT_SIZE_SP = 14.sp * scopes Compose's invalidation to this draw. The session screen coalesces those to ~16 ms. * * [topRow] is the external buffer row at the viewport's top (`<= 0`; 0 is the live screen bottom). - * [onMeasured] fires with the new (cols, rows, cellWidthPx, cellHeightPx) whenever the pixel size - * or font metrics change, undebounced. + * [onMeasured] fires with the new (cols, rows, cellWidthPx, cellHeightPx, cellWidth) whenever the + * pixel size or font metrics change, undebounced. `cellWidth` is the renderer's fractional advance + * and is what the grid is laid out and drawn with, so the column count and every pixel-to-column + * conversion must use it: dividing by a truncated width puts the last columns past the right edge + * and makes taps land further right the further across they are. `cellWidthPx` is that width + * truncated, kept for the emulator's integer per-cell pixel size. */ @Composable fun TerminalCanvas( emulator: TerminalEmulator?, frame: IntState, - onMeasured: (cols: Int, rows: Int, cellWidthPx: Int, cellHeightPx: Int) -> Unit, + onMeasured: ( + cols: Int, + rows: Int, + cellWidthPx: Int, + cellHeightPx: Int, + cellWidth: Float, + ) -> Unit, modifier: Modifier = Modifier, fontSizeSp: TextUnit = DEFAULT_TERMINAL_TEXT_SIZE_SP, typeface: Typeface = Typeface.MONOSPACE, @@ -62,11 +72,12 @@ fun TerminalCanvas( var boxSize by remember { mutableStateOf(IntSize.Zero) } LaunchedEffect(renderer, boxSize) { if (boxSize.width == 0 || boxSize.height == 0) return@LaunchedEffect - val cellWidthPx = renderer.fontWidth.toInt().coerceAtLeast(1) + val cellWidth = renderer.fontWidth.coerceAtLeast(1f) + val cellWidthPx = cellWidth.toInt() val cellHeightPx = renderer.fontLineSpacing.coerceAtLeast(1) - val cols = max(1, boxSize.width / cellWidthPx) + val cols = terminalColumns(boxSize.width, cellWidth) val rows = max(1, boxSize.height / cellHeightPx) - onMeasured(cols, rows, cellWidthPx, cellHeightPx) + onMeasured(cols, rows, cellWidthPx, cellHeightPx, cellWidth) } Canvas( @@ -83,6 +94,12 @@ fun TerminalCanvas( } } +/** + * Whole columns that fit in [widthPx] at the renderer's fractional [cellWidth]. Dividing by the + * truncated width would count a column that [TerminalRenderer] draws past the right edge. + */ +fun terminalColumns(widthPx: Int, cellWidth: Float): Int = max(1, (widthPx / cellWidth).toInt()) + private fun DrawScope.drawTerminal( emulator: TerminalEmulator?, renderer: TerminalRenderer, diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalGestures.kt b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalGestures.kt index 0caabaa..26d09e9 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalGestures.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalGestures.kt @@ -5,10 +5,15 @@ import androidx.compose.foundation.gestures.awaitFirstDown import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.input.pointer.PointerEventPass +import androidx.compose.ui.input.pointer.PointerEventType import androidx.compose.ui.input.pointer.PointerId +import androidx.compose.ui.input.pointer.PointerType +import androidx.compose.ui.input.pointer.isSecondaryPressed +import androidx.compose.ui.input.pointer.isShiftPressed import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.positionChange import com.termux.terminal.TerminalEmulator +import kotlin.math.abs import kotlin.math.hypot private const val LONG_PRESS_TIMEOUT_MS = 500L @@ -16,6 +21,14 @@ private const val LONG_PRESS_TIMEOUT_MS = 500L /** Only needs to tell a stationary long-press from a drag. */ private const val TOUCH_SLOP_PX = 24f +/** + * A physical mouse is exact, so far less travel than [TOUCH_SLOP_PX] separates a click from a drag. + * Also the radius within which a second click counts as a double-click. + */ +private const val MOUSE_DRAG_SLOP_PX = 8f + +private const val WHEEL_ROWS_PER_NOTCH = 3 + private enum class GestureMode { UNDECIDED, DRAG, PINCH, LONG_PRESS } /** @@ -28,6 +41,12 @@ private enum class GestureMode { UNDECIDED, DRAG, PINCH, LONG_PRESS } * drag, since it reads as a pan to the transform detector too, so telling scrolling from pinching * needs one source of truth for how many fingers are down. * + * A physical mouse takes its own path ahead of that machine: a primary press-drag selects, a + * double-click selects the word, a right-click copies or pastes, and the wheel scrolls. There is + * no long-press to select, since a held button is how a mouse starts a drag. While the remote app + * has mouse tracking on, a mouse press is forwarded to it as touch is, unless Shift is held, which + * bypasses tracking for local selection as xterm does. + * * Selection handles need no special case: each lives in its own `Popup`, a separate Android window, * so the platform routes a finger on one straight there and this never sees it. * @@ -45,12 +64,73 @@ fun Modifier.terminalGestures( isMouseReportingActive: () -> Boolean = { false }, onMouseClick: (Offset) -> Unit = {}, onMouseWheel: (Offset, dy: Float) -> Unit = { _, _ -> }, + onMouseSelect: (anchor: Offset, current: Offset) -> Unit = { _, _ -> }, + onMouseSelectEnd: () -> Unit = {}, + onMouseDoubleClick: (Offset) -> Unit = {}, + onSecondaryClick: (Offset) -> Unit = {}, + onWheelScroll: (Offset, dy: Float, shift: Boolean) -> Unit = { _, _, _ -> }, ): Modifier = pointerInput(Unit) { + // Outside awaitEachGesture because a double-click spans two gestures. + var lastClickMillis = 0L + var lastClickPos = Offset.Zero awaitEachGesture { val down = awaitFirstDown(requireUnconsumed = false, pass = PointerEventPass.Initial) down.consume() val pointer1 = down.id + + if (down.type == PointerType.Mouse) { + // Checked before the tracking test below: right-click is always the local + // copy/paste, never a forwarded left click. + if (currentEvent.buttons.isSecondaryPressed) { + onSecondaryClick(down.position) + while (true) { + val event = awaitPointerEvent() + event.changes.forEach { it.consume() } + if (event.changes.none { it.pressed }) return@awaitEachGesture + } + } + if (!isMouseReportingActive() || currentEvent.keyboardModifiers.isShiftPressed) { + var selecting = false + try { + while (true) { + val event = awaitPointerEvent() + val change1 = event.changes.firstOrNull { it.id == pointer1 } + ?: return@awaitEachGesture + if (!change1.pressed) { + change1.consume() + if (!selecting) { + val now = System.currentTimeMillis() + val isDoubleClick = + now - lastClickMillis <= + viewConfiguration.doubleTapTimeoutMillis && + distanceBetween(lastClickPos, change1.position) <= + MOUSE_DRAG_SLOP_PX + if (isDoubleClick) { + lastClickMillis = 0L + onMouseDoubleClick(change1.position) + } else { + lastClickMillis = now + lastClickPos = change1.position + onTap(change1.position) + } + } + return@awaitEachGesture + } + if (change1.positionChange() != Offset.Zero) { + selecting = selecting || + distanceBetween(down.position, change1.position) > + MOUSE_DRAG_SLOP_PX + if (selecting) onMouseSelect(down.position, change1.position) + change1.consume() + } + } + } finally { + if (selecting) onMouseSelectEnd() + } + } + } + var pointer2: PointerId? = null var lastPos1 = down.position var pinchStartDistance = 0f @@ -153,6 +233,20 @@ fun Modifier.terminalGestures( } } } + }.pointerInput(Unit) { + // Separate from the loop above: a scroll event has no press, so `awaitFirstDown` never + // returns for it. Initial pass for the same reason as the down: the invisible field would + // otherwise see it first. + awaitPointerEventScope { + while (true) { + val event = awaitPointerEvent(PointerEventPass.Initial) + if (event.type != PointerEventType.Scroll) continue + val change = event.changes.first() + val shift = event.keyboardModifiers.isShiftPressed + onWheelScroll(change.position, change.scrollDelta.y, shift) + event.changes.forEach { it.consume() } + } + } } private fun distanceBetween(a: Offset, b: Offset): Float = hypot(a.x - b.x, a.y - b.y) @@ -166,3 +260,39 @@ fun dragRows(remainderPx: Float, cellHeightPx: Int): Pair { /** Finger down (positive rows) shows older content, which is wheel up, as with the local scrollback. */ fun wheelButton(rows: Int): Int = if (rows > 0) TerminalEmulator.MOUSE_WHEELUP_BUTTON else TerminalEmulator.MOUSE_WHEELDOWN_BUTTON + +/** + * Whole wheel notches in [remainder] plus [dy] and what is left over to carry into the next scroll + * event. A touchpad reports fractions of a notch, which would otherwise each round to zero. + * Positive is wheel toward the user. + */ +fun wheelNotches(remainder: Float, dy: Float): Pair { + val total = remainder + dy + val notches = total.toInt() + return notches to total - notches +} + +/** What one wheel scroll does, by what the remote screen can make of it. */ +sealed interface WheelAction { + data class MouseReport(val button: Int, val count: Int) : WheelAction + data class Arrows(val down: Boolean, val count: Int) : WheelAction + + /** Positive [rows] moves toward newer content. */ + data class Scrollback(val rows: Int) : WheelAction +} + +/** + * Picks the wheel's effect from the remote's mode, most specific first: an app that asked for mouse + * tracking gets wheel events, one per notch; a full-screen app without it (`less`, `man`, plain + * `vim`) has no scrollback to move, so arrow keys stand in as xterm's `alternateScroll` does; the + * main screen scrolls the local scrollback. [notches] is positive for wheel toward the user, which + * is down to newer content. Pure so the policy is testable without an emulator. + */ +fun wheelAction(notches: Int, mouseTracking: Boolean, alternateBuffer: Boolean): WheelAction? = + when { + notches == 0 -> null + mouseTracking -> WheelAction.MouseReport(wheelButton(-notches), abs(notches)) + alternateBuffer -> + WheelAction.Arrows(down = notches > 0, count = abs(notches) * WHEEL_ROWS_PER_NOTCH) + else -> WheelAction.Scrollback(notches * WHEEL_ROWS_PER_NOTCH) + } diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt index 888e5b6..9a0c3a6 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt @@ -211,3 +211,35 @@ private fun ctrlChordCode(codePoint: Int): Char? { else -> null } } + +enum class ClipboardShortcut { COPY, PASTE } + +/** + * The desktop-terminal chords, since plain Ctrl+C and Ctrl+V are owned by the remote (SIGINT and + * literal-next) and keep going out as control bytes. Checked before [hardwareKeyCode], which would + * otherwise claim both: [KeyHandler.getCode] turns Shift+Insert into `\033[2;2~`, and the Ctrl + * chord table turns Ctrl+Shift+V into 0x16. Alt is excluded so AltGr layouts still reach the + * Alt+ path. + */ +fun clipboardShortcut(event: ComposeKeyEvent): ClipboardShortcut? = + clipboardShortcut( + keyCode = event.nativeKeyEvent.keyCode, + ctrl = event.isCtrlPressed, + shift = event.isShiftPressed, + alt = event.isAltPressed, + ) + +internal fun clipboardShortcut( + keyCode: Int, + ctrl: Boolean, + shift: Boolean, + alt: Boolean, +): ClipboardShortcut? { + if (alt) return null + return when { + ctrl && shift && keyCode == android.view.KeyEvent.KEYCODE_V -> ClipboardShortcut.PASTE + ctrl && shift && keyCode == android.view.KeyEvent.KEYCODE_C -> ClipboardShortcut.COPY + shift && !ctrl && keyCode == android.view.KeyEvent.KEYCODE_INSERT -> ClipboardShortcut.PASTE + else -> null + } +} diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalSelectionOverlay.kt b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalSelectionOverlay.kt index b94d610..a05a762 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalSelectionOverlay.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalSelectionOverlay.kt @@ -62,7 +62,7 @@ private const val RIGHT_HANDLE_HOTSPOT_FRACTION = 0.25f fun TerminalSelectionOverlay( state: TerminalSelectionState, emulator: TerminalEmulator?, - cellWidthPx: Int, + cellWidth: Float, cellHeightPx: Int, topRow: Int, onCopy: (String) -> Unit, @@ -90,7 +90,7 @@ fun TerminalSelectionOverlay( active = sel != null && emulator != null, emulator = emulator, selection = sel, - cellWidthPx = cellWidthPx, + cellWidth = cellWidth, cellHeightPx = cellHeightPx, topRow = topRow, boxOffsetInRoot = boxOffsetInRoot, @@ -100,16 +100,16 @@ fun TerminalSelectionOverlay( onShare = onShare, ) - if (sel != null && emulator != null && cellWidthPx > 0 && cellHeightPx > 0) { + if (sel != null && emulator != null && cellWidth > 0f && cellHeightPx > 0) { SelectionHandlePopup( handleAttr = android.R.attr.textSelectHandleLeft, hotspotFraction = LEFT_HANDLE_HOTSPOT_FRACTION, - targetXPx = sel.startCol * cellWidthPx, + targetXPx = (sel.startCol * cellWidth).toInt(), targetYPx = (sel.startRow - topRow + 1) * cellHeightPx, onDrag = { dx, dy -> state.dragHandle( emulator, - cellWidthPx, + cellWidth, cellHeightPx, isStart = true, dx = dx, @@ -120,12 +120,12 @@ fun TerminalSelectionOverlay( SelectionHandlePopup( handleAttr = android.R.attr.textSelectHandleRight, hotspotFraction = RIGHT_HANDLE_HOTSPOT_FRACTION, - targetXPx = (sel.endCol + 1) * cellWidthPx, + targetXPx = ((sel.endCol + 1) * cellWidth).toInt(), targetYPx = (sel.endRow - topRow + 1) * cellHeightPx, onDrag = { dx, dy -> state.dragHandle( emulator, - cellWidthPx, + cellWidth, cellHeightPx, isStart = false, dx = dx, @@ -218,7 +218,7 @@ private fun SelectionHandlePopup( private class SelectionMenuContext( val emulator: TerminalEmulator?, val selection: TerminalSelection?, - val cellWidthPx: Int, + val cellWidth: Float, val cellHeightPx: Int, val topRow: Int, val boxOffsetInRoot: Offset, @@ -238,7 +238,7 @@ private fun TerminalSelectionActionMode( active: Boolean, emulator: TerminalEmulator?, selection: TerminalSelection?, - cellWidthPx: Int, + cellWidth: Float, cellHeightPx: Int, topRow: Int, boxOffsetInRoot: Offset, @@ -254,7 +254,7 @@ private fun TerminalSelectionActionMode( SelectionMenuContext( emulator, selection, - cellWidthPx, + cellWidth, cellHeightPx, topRow, boxOffsetInRoot, @@ -333,9 +333,9 @@ private fun TerminalSelectionActionMode( override fun onGetContentRect(mode: ActionMode, view: View, outRect: Rect) { val ctx = current val sel = ctx.selection - if (sel == null || ctx.cellWidthPx <= 0 || ctx.cellHeightPx <= 0) return - val left = min(sel.startCol, sel.endCol) * ctx.cellWidthPx - val right = (max(sel.startCol, sel.endCol) + 1) * ctx.cellWidthPx + if (sel == null || ctx.cellWidth <= 0f || ctx.cellHeightPx <= 0) return + val left = min(sel.startCol, sel.endCol) * ctx.cellWidth + val right = (max(sel.startCol, sel.endCol) + 1) * ctx.cellWidth val top = ((sel.startRow - ctx.topRow) * ctx.cellHeightPx).coerceAtLeast(0) val bottom = (sel.endRow - ctx.topRow + 1) * ctx.cellHeightPx @@ -357,7 +357,7 @@ private fun TerminalSelectionActionMode( // The mode object survives the whole selection; only its position and, after Select All, its // content change. Nudge it to re-query onGetContentRect instead of recreating it per frame. - LaunchedEffect(selection, cellWidthPx, cellHeightPx, topRow, boxOffsetInRoot) { + LaunchedEffect(selection, cellWidth, cellHeightPx, topRow, boxOffsetInRoot) { actionMode?.invalidateContentRect() } } diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalSelectionState.kt b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalSelectionState.kt index 9acbcd9..fc7873c 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalSelectionState.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalSelectionState.kt @@ -19,24 +19,58 @@ class TerminalSelectionState { private var startRemainderPx = Offset.Zero private var endRemainderPx = Offset.Zero + // Where a mouse drag began, as (col, row) in external rows. Resolved once so wheel scrolling or + // new output mid-drag cannot move it: the anchor offset is a screen position, and the row under + // it changes with `topRow`. + private var dragAnchor: Pair? = null + fun clear() { selection = null + dragAnchor = null } fun selectWord( emulator: TerminalEmulator, - cellWidthPx: Int, + cellWidth: Float, cellHeightPx: Int, topRow: Int, offset: Offset ) { - val col = (offset.x / cellWidthPx).toInt().coerceIn(0, emulator.mColumns - 1) + val col = (offset.x / cellWidth).toInt().coerceIn(0, emulator.mColumns - 1) val screenRow = (offset.y / cellHeightPx).toInt().coerceIn(0, emulator.mRows - 1) startRemainderPx = Offset.Zero endRemainderPx = Offset.Zero selection = wordSelectionAt(emulator, col, screenRow + topRow) } + /** Extends a mouse drag from [anchor], where it began, to [current], both screen offsets. */ + fun selectRange( + emulator: TerminalEmulator, + cellWidth: Float, + cellHeightPx: Int, + topRow: Int, + anchor: Offset, + current: Offset + ) { + fun col(o: Offset) = (o.x / cellWidth).toInt().coerceIn(0, emulator.mColumns - 1) + fun row(o: Offset) = (o.y / cellHeightPx).toInt().coerceIn(0, emulator.mRows - 1) + topRow + val start = dragAnchor ?: (col(anchor) to row(anchor)).also { dragAnchor = it } + val (startCol, startRow) = start + val endRow = row(current) + startRemainderPx = Offset.Zero + endRemainderPx = Offset.Zero + selection = TerminalSelection( + validColumn(emulator, startRow, startCol), + startRow, + validColumn(emulator, endRow, col(current)), + endRow + ).normalized() + } + + fun endRangeSelection() { + dragAnchor = null + } + fun selectAll(emulator: TerminalEmulator) { startRemainderPx = Offset.Zero endRemainderPx = Offset.Zero @@ -49,6 +83,7 @@ class TerminalSelectionState { } fun shiftForScroll(scrolled: Int) { + dragAnchor = dragAnchor?.let { (col, row) -> col to row - scrolled } selection = selection?.let { it.copy( startRow = it.startRow - scrolled, @@ -69,7 +104,7 @@ class TerminalSelectionState { */ fun dragHandle( emulator: TerminalEmulator, - cellWidthPx: Int, + cellWidth: Float, cellHeightPx: Int, isStart: Boolean, dx: Float, @@ -78,14 +113,14 @@ class TerminalSelectionState { val sel = selection ?: return val remainder = if (isStart) startRemainderPx else endRemainderPx val newRemainder = Offset(remainder.x + dx, remainder.y + dy) - val colDelta = (newRemainder.x / cellWidthPx).toInt() + val colDelta = (newRemainder.x / cellWidth).toInt() val rowDelta = (newRemainder.y / cellHeightPx).toInt() if (colDelta == 0 && rowDelta == 0) { if (isStart) startRemainderPx = newRemainder else endRemainderPx = newRemainder return } val settled = Offset( - newRemainder.x - colDelta * cellWidthPx, + newRemainder.x - colDelta * cellWidth, newRemainder.y - rowDelta * cellHeightPx ) if (isStart) startRemainderPx = settled else endRemainderPx = settled diff --git a/app/src/test/java/io/github/lordofpolls/shellwave/terminal/ClipboardShortcutTest.kt b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/ClipboardShortcutTest.kt new file mode 100644 index 0000000..195f83b --- /dev/null +++ b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/ClipboardShortcutTest.kt @@ -0,0 +1,51 @@ +package io.github.lordofpolls.shellwave.terminal + +import android.view.KeyEvent +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class ClipboardShortcutTest { + private fun shortcut( + keyCode: Int, + ctrl: Boolean = false, + shift: Boolean = false, + alt: Boolean = false, + ) = clipboardShortcut(keyCode, ctrl, shift, alt) + + @Test + fun `plain Ctrl+V stays a control byte for the remote`() { + assertNull(shortcut(KeyEvent.KEYCODE_V, ctrl = true)) + } + + @Test + fun `Ctrl+Shift+V pastes`() { + assertEquals( + ClipboardShortcut.PASTE, + shortcut(KeyEvent.KEYCODE_V, ctrl = true, shift = true), + ) + } + + @Test + fun `Ctrl+Shift+C copies`() { + assertEquals( + ClipboardShortcut.COPY, + shortcut(KeyEvent.KEYCODE_C, ctrl = true, shift = true), + ) + } + + @Test + fun `Shift+Insert pastes`() { + assertEquals(ClipboardShortcut.PASTE, shortcut(KeyEvent.KEYCODE_INSERT, shift = true)) + } + + @Test + fun `Ctrl+Insert is not a shortcut`() { + assertNull(shortcut(KeyEvent.KEYCODE_INSERT, ctrl = true)) + } + + @Test + fun `Alt+Shift+V is left to the Alt path`() { + assertNull(shortcut(KeyEvent.KEYCODE_V, shift = true, alt = true)) + } +} diff --git a/app/src/test/java/io/github/lordofpolls/shellwave/terminal/TerminalCanvasTest.kt b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/TerminalCanvasTest.kt new file mode 100644 index 0000000..9b7a99d --- /dev/null +++ b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/TerminalCanvasTest.kt @@ -0,0 +1,19 @@ +package io.github.lordofpolls.shellwave.terminal + +import org.junit.Assert.assertEquals +import org.junit.Test + +class TerminalCanvasTest { + @Test + fun `columns use the fractional cell width so the last one stays on screen`() { + // The truncated width 21 would give 51 columns, two more than the renderer draws. + assertEquals(49, terminalColumns(1080, 21.7f)) + // The truncated width 16 would give 67. + assertEquals(66, terminalColumns(1080, 16.2f)) + } + + @Test + fun `a box narrower than one cell still has a column`() { + assertEquals(1, terminalColumns(5, 21.7f)) + } +} diff --git a/app/src/test/java/io/github/lordofpolls/shellwave/terminal/TerminalGesturesTest.kt b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/TerminalGesturesTest.kt index dbaa1ee..66367e2 100644 --- a/app/src/test/java/io/github/lordofpolls/shellwave/terminal/TerminalGesturesTest.kt +++ b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/TerminalGesturesTest.kt @@ -2,6 +2,7 @@ package io.github.lordofpolls.shellwave.terminal import com.termux.terminal.TerminalEmulator import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull import org.junit.Test class TerminalGesturesTest { @@ -17,4 +18,61 @@ class TerminalGesturesTest { assertEquals(TerminalEmulator.MOUSE_WHEELUP_BUTTON, wheelButton(1)) assertEquals(TerminalEmulator.MOUSE_WHEELDOWN_BUTTON, wheelButton(-1)) } + + @Test + fun `fractional wheel scroll carries until it adds up to a notch`() { + val (first, carry) = wheelNotches(0f, 0.4f) + assertEquals(0, first) + val (second, left) = wheelNotches(carry, 0.7f) + assertEquals(1, second) + assertEquals(0.1f, left, 0.001f) + } + + @Test + fun `wheel notches keep their sign and leave the fraction`() { + assertEquals(1, wheelNotches(0f, 1.5f).first) + assertEquals(0.5f, wheelNotches(0f, 1.5f).second, 0.001f) + assertEquals(-2 to 0f, wheelNotches(0f, -2f)) + } + + @Test + fun `no notches does nothing`() { + assertNull(wheelAction(0, mouseTracking = true, alternateBuffer = true)) + } + + @Test + fun `mouse tracking wins and sends one event per notch`() { + assertEquals( + WheelAction.MouseReport(TerminalEmulator.MOUSE_WHEELDOWN_BUTTON, 2), + wheelAction(2, mouseTracking = true, alternateBuffer = true), + ) + assertEquals( + WheelAction.MouseReport(TerminalEmulator.MOUSE_WHEELUP_BUTTON, 1), + wheelAction(-1, mouseTracking = true, alternateBuffer = false), + ) + } + + @Test + fun `alternate buffer sends three arrows per notch`() { + assertEquals( + WheelAction.Arrows(down = true, count = 6), + wheelAction(2, mouseTracking = false, alternateBuffer = true), + ) + assertEquals( + WheelAction.Arrows(down = false, count = 3), + wheelAction(-1, mouseTracking = false, alternateBuffer = true), + ) + } + + @Test + fun `main screen scrolls three rows per notch, positive toward newer content`() { + assertEquals( + WheelAction.Scrollback(3), + wheelAction(1, mouseTracking = false, alternateBuffer = false), + ) + assertEquals( + WheelAction.Scrollback(-6), + wheelAction(-2, mouseTracking = false, alternateBuffer = false), + ) + } }