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..9e8b445 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 @@ -336,10 +336,31 @@ fun SessionsScreen( SessionLoggingEffects(loggingController) } +/** + * State that outlives the per-session `key` around [SessionTabBody]: whether a body has been + * composed since the Sessions screen was entered, and the pinch-zoomed font size, which all tabs + * share. [fontProfileId] is the terminal profile [fontSizeSp] was last taken from. + */ +internal class SessionBodyShared { + var shown = false + var fontSizeSp by mutableStateOf(DEFAULT_TERMINAL_TEXT_SIZE_SP) + var fontProfileId: Long? = null +} + /** * One session's terminal and key bar. Reads live state from [sessionManager]; owns nothing that * would need saving across rotation. * + * Callers wrap it in `key(sessionId)`. Everything here is plain `remember` state about one + * session - the emulator, scrollback position, selection, latched modifiers, font size. Without + * the key, switching tabs keeps that state and the new tab draws the previous session's emulator + * until it connects, or for good if it never does, and copy or selection then act on the wrong + * buffer. + * + * [shared] outlives the key. It tells the first body composed after the Sessions screen is entered + * from one composed by a tab switch (only the first raises the keyboard unprompted), and keeps the + * pinch-zoomed font size across tabs. + * * [terminalHeight] and [preKeyBarGap] are tabletop posture's hook: the terminal is sized exactly * instead of sharing space via `weight(1f)`, and the gap reserves the hinge's occluded height. The * defaults reproduce the ordinary layout. @@ -355,6 +376,7 @@ internal fun SessionTabBody( keyBarLayoutDao: KeyBarLayoutDao, fileTransferController: FileTransferController, searchController: TerminalSearchController, + shared: SessionBodyShared, modifier: Modifier = Modifier, terminalProfile: TerminalProfileEntity? = null, terminalHeight: Dp? = null, @@ -370,8 +392,8 @@ internal fun SessionTabBody( // The keyboard button's two halves live here, not in KeyBar, because this is what owns the // focus: TerminalInputCapture's shim is the only focusable thing on the screen, and an IME with // nothing focused has nowhere to send what it types. Showing the keyboard is focus then show, - // in that order - requestFocus() on an already-focused field is a no-op, which is why the - // button cannot be that call alone. + // in that order - the shim does not show it on focus, and requestFocus() on an already-focused + // field is a no-op, so neither alone is enough. A tap on the terminal does the same. val keyboardController = LocalSoftwareKeyboardController.current val imeVisible = WindowInsets.isImeVisible @@ -428,10 +450,14 @@ internal fun SessionTabBody( topRow = externalRowAt(emulator, matches[clamped].row).coerceIn(-maxScrollback, 0) } - var fontSizeSp by remember { mutableStateOf(DEFAULT_TERMINAL_TEXT_SIZE_SP) } - // Keyed on the row's id alone, so a later settings edit doesn't stomp an in-progress - // pinch-to-resize. - LaunchedEffect(profileOrDefault.id) { fontSizeSp = profileOrDefault.fontSizeSp.sp } + // Applied only when the profile row's id changes, so neither a later settings edit nor a tab + // switch to a session on the same profile stomps an in-progress pinch-to-resize. + LaunchedEffect(profileOrDefault.id) { + if (shared.fontProfileId != profileOrDefault.id) { + shared.fontProfileId = profileOrDefault.id + shared.fontSizeSp = profileOrDefault.fontSizeSp.sp + } + } // Coordinates are the external row system, same as topRow. val selectionState = rememberTerminalSelectionState() @@ -519,14 +545,19 @@ internal fun SessionTabBody( } LaunchedEffect(summary.connection, summary.status) { - if (summary.status == SessionStatus.CONNECTED) { - terminalEmulator = summary.connection.emulatorOrNull - redrawRequests.trySend(Unit) - } else if (summary.status == SessionStatus.CONNECTING) { + if (summary.status == SessionStatus.CONNECTING) { // Fresh connection and emulator: drop the old buffer and anything pointing into it. terminalEmulator = null topRow = 0 selectionState.clear() + } else { + // Not only for CONNECTED: this body is composed fresh on every tab switch, and a + // dropped or reconnecting session still holds the emulator whose last screen it should + // keep showing. Null until the first connect, so nothing is drawn before one exists. + summary.connection.emulatorOrNull?.let { + terminalEmulator = it + redrawRequests.trySend(Unit) + } } } @@ -709,7 +740,7 @@ internal fun SessionTabBody( if (rows != 0) topRow = (topRow - rows).coerceIn(-maxScrollback, 0) }, onPinch = { scale -> - fontSizeSp = (fontSizeSp.value * scale).coerceIn( + shared.fontSizeSp = (shared.fontSizeSp.value * scale).coerceIn( FONT_SIZE_SP_RANGE.start, FONT_SIZE_SP_RANGE.endInclusive ).sp @@ -731,6 +762,7 @@ internal fun SessionTabBody( detectedLink = link } else { focusRequester.requestFocus() + keyboardController?.show() selectionState.clear() // tapping outside a selection dismisses it, same as Android's own text selection } }, @@ -761,7 +793,7 @@ internal fun SessionTabBody( measuredSize = TerminalSize(cols, rows, cellW, cellH) }, modifier = Modifier.matchParentSize(), - fontSizeSp = fontSizeSp, + fontSizeSp = shared.fontSizeSp, typeface = terminalTypeface, lineHeightMultiplier = lineHeightMultiplier, topRow = topRow, @@ -870,7 +902,14 @@ internal fun SessionTabBody( ) } - LaunchedEffect(sessionId) { focusRequester.requestFocus() } + // The field no longer raises the keyboard when it gains focus, and a tab switch composes a new + // one. Entering the screen raises it, as before; a switch only keeps it up if it was up. + LaunchedEffect(sessionId) { + focusRequester.requestFocus() + val firstOpen = !shared.shown + shared.shown = true + if (firstOpen || imeVisible) keyboardController?.show() + } } /** Scrollback position and jump-to-bottom in one: shown only while scrolled back. */ diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/SessionsSplitLayout.kt b/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/SessionsSplitLayout.kt index b01fb82..64f1063 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/SessionsSplitLayout.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/feature/session/SessionsSplitLayout.kt @@ -15,6 +15,7 @@ import androidx.compose.material3.adaptive.layout.ListDetailPaneScaffold import androidx.compose.material3.adaptive.navigation.ThreePaneScaffoldNavigator import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue +import androidx.compose.runtime.key import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue @@ -61,6 +62,7 @@ internal fun SessionsSplitLayout( searchController: TerminalSearchController, modifier: Modifier = Modifier, ) { + val shared = remember { SessionBodyShared() } Box(modifier = modifier) { val tabletop = posture as? FoldPosture.Tabletop if (tabletop != null) { @@ -74,25 +76,30 @@ internal fun SessionsSplitLayout( keyBarLayoutDao = keyBarLayoutDao, fileTransferController = fileTransferController, searchController = searchController, + shared = shared, modifier = Modifier.fillMaxSize(), ) } else if (isWide && fullWidthTerminal) { // Bypasses the scaffold rather than talking it into a Detail-only pane while // `isWide` stays true. Reachable only while `isWide`, so a narrow window cannot get // stuck. - SessionTabBody( - sessionId = selectedId, - sessionManager = sessionManager, - terminalProfile = terminalProfile, - terminalProfileDao = terminalProfileDao, - keyBarLayoutDao = keyBarLayoutDao, - fileTransferController = fileTransferController, - searchController = searchController, - showLayoutToggle = true, - fullWidthTerminal = fullWidthTerminal, - onToggleFullWidth = onToggleFullWidth, - modifier = Modifier.fillMaxSize() - ) + // Fresh state per session: see SessionTabBody. + key(selectedId) { + SessionTabBody( + sessionId = selectedId, + sessionManager = sessionManager, + terminalProfile = terminalProfile, + terminalProfileDao = terminalProfileDao, + keyBarLayoutDao = keyBarLayoutDao, + fileTransferController = fileTransferController, + searchController = searchController, + shared = shared, + showLayoutToggle = true, + fullWidthTerminal = fullWidthTerminal, + onToggleFullWidth = onToggleFullWidth, + modifier = Modifier.fillMaxSize() + ) + } } else { ListDetailPaneScaffold( directive = navigator.scaffoldDirective, @@ -112,19 +119,23 @@ internal fun SessionsSplitLayout( }, detailPane = { AnimatedPane { - SessionTabBody( - sessionId = selectedId, - sessionManager = sessionManager, - terminalProfile = terminalProfile, - terminalProfileDao = terminalProfileDao, - keyBarLayoutDao = keyBarLayoutDao, - fileTransferController = fileTransferController, - searchController = searchController, - showLayoutToggle = isWide, - fullWidthTerminal = fullWidthTerminal, - onToggleFullWidth = onToggleFullWidth, - modifier = Modifier.fillMaxSize() - ) + // Fresh state per session: see SessionTabBody. + key(selectedId) { + SessionTabBody( + sessionId = selectedId, + sessionManager = sessionManager, + terminalProfile = terminalProfile, + terminalProfileDao = terminalProfileDao, + keyBarLayoutDao = keyBarLayoutDao, + fileTransferController = fileTransferController, + searchController = searchController, + shared = shared, + showLayoutToggle = isWide, + fullWidthTerminal = fullWidthTerminal, + onToggleFullWidth = onToggleFullWidth, + modifier = Modifier.fillMaxSize() + ) + } } }, ) @@ -211,6 +222,7 @@ private fun TabletopSessionLayout( keyBarLayoutDao: KeyBarLayoutDao, fileTransferController: FileTransferController, searchController: TerminalSearchController, + shared: SessionBodyShared, modifier: Modifier = Modifier, ) { val density = LocalDensity.current @@ -226,32 +238,40 @@ private fun TabletopSessionLayout( val hingeGap = with(density) { (localHingeBottomPx - localHingeTopPx).toDp() } if (terminalHeight > 0.dp) { - SessionTabBody( - sessionId = sessionId, - sessionManager = sessionManager, - terminalProfile = terminalProfile, - terminalProfileDao = terminalProfileDao, - keyBarLayoutDao = keyBarLayoutDao, - fileTransferController = fileTransferController, - searchController = searchController, - terminalHeight = terminalHeight, - preKeyBarGap = hingeGap, - modifier = Modifier.fillMaxSize(), - ) + // Fresh state per session: see SessionTabBody. + key(sessionId) { + SessionTabBody( + sessionId = sessionId, + sessionManager = sessionManager, + terminalProfile = terminalProfile, + terminalProfileDao = terminalProfileDao, + keyBarLayoutDao = keyBarLayoutDao, + fileTransferController = fileTransferController, + searchController = searchController, + shared = shared, + terminalHeight = terminalHeight, + preKeyBarGap = hingeGap, + modifier = Modifier.fillMaxSize(), + ) + } } else { // Hinge at or above this layout's own top edge, e.g. mid fold-transition before the // first onGloballyPositioned lands. Fall back to the weighted split rather than hand // SessionTabBody a zero or negative height. - SessionTabBody( - sessionId = sessionId, - sessionManager = sessionManager, - terminalProfile = terminalProfile, - terminalProfileDao = terminalProfileDao, - keyBarLayoutDao = keyBarLayoutDao, - fileTransferController = fileTransferController, - searchController = searchController, - modifier = Modifier.fillMaxSize(), - ) + // Fresh state per session: see SessionTabBody. + key(sessionId) { + SessionTabBody( + sessionId = sessionId, + sessionManager = sessionManager, + terminalProfile = terminalProfile, + terminalProfileDao = terminalProfileDao, + keyBarLayoutDao = keyBarLayoutDao, + fileTransferController = fileTransferController, + searchController = searchController, + shared = shared, + modifier = Modifier.fillMaxSize(), + ) + } } } } diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/KeyBar.kt b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/KeyBar.kt index f3c54b5..72ca9e2 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/KeyBar.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/KeyBar.kt @@ -173,11 +173,11 @@ private const val MIN_BUTTONS_FOR_FREE_SECOND_ROW = 4 /** * Shows the soft keyboard, or hides it when it is already up. * - * This exists because focus is not visibility. The terminal's IME shim ([TerminalInputCapture]) is - * focused for the whole life of a session, so once the keyboard has been dismissed - by the back - * gesture, or by the system - tapping the terminal calls `requestFocus()` on a field that is - * *already* focused, which is a no-op and brings nothing back. The bar is the only chrome that - * survives an IME dismissal, so this is where the way back belongs. + * This exists because focus is not visibility. The terminal's IME shim ([TerminalInputCapture]) does + * not show the keyboard when it gains focus, and it stays focused while the keyboard is dismissed - + * by the back gesture, or by the system - so focus alone never brings it back. Tapping the terminal + * shows it explicitly, but the bar is the one control that is always on screen, so the keyboard's + * toggle belongs here too. * * Icon-only: it is app chrome rather than a key, and nothing it does reaches the far end, so * dressing it as one more `CTRL`-style label would be a claim about the wrong thing. The glyph 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..c08046f 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 @@ -115,6 +115,9 @@ fun TerminalInputCapture( capitalization = KeyboardCapitalization.None, // Prevent keyboard from autocorrect/suggestions keyboardType = KeyboardType.PasswordVisible, + // The caller decides when the keyboard comes up. A field gaining focus on its + // own, as a freshly composed one does on every tab switch, must not pop it up. + showKeyboardOnFocus = false, ), ) }