From 1f86825151ebc907c48e558b257ef77d38ffc019 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 23 Oct 2026 05:41:55 +0000 Subject: [PATCH] app: redraw terminal after resizing The terminal emulator lives in the ViewModel and survives Activity recreation, but a recreated TerminalScreen draws its first frame before LaunchedEffect resizes the emulator to the new viewport. Nothing invalidated the drawing after the resize, so after rotating, the screen kept showing rows laid out for the old geometry instead of the end of the output, and the scrollbars used stale values. Refresh the snapshot state and redraw after every resize, through the same path used for new output, and keep a scrolled-up position within the transcript. Assisted-by: Claude Opus 5.5 --- .../magisk/ui/terminal/TerminalScreen.kt | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/app/apk/src/main/java/com/topjohnwu/magisk/ui/terminal/TerminalScreen.kt b/app/apk/src/main/java/com/topjohnwu/magisk/ui/terminal/TerminalScreen.kt index 50b8bb0c3..e27bf9646 100644 --- a/app/apk/src/main/java/com/topjohnwu/magisk/ui/terminal/TerminalScreen.kt +++ b/app/apk/src/main/java/com/topjohnwu/magisk/ui/terminal/TerminalScreen.kt @@ -57,17 +57,17 @@ fun TerminalScreen( var transcriptRows by remember { mutableIntStateOf(0) } var maxUsedColumn by remember { mutableIntStateOf(0) } + // Re-read emulator state after new output or a resize, and redraw + fun refresh() { + transcriptRows = emulator.screen.activeTranscriptRows + maxUsedColumn = emulator.maxUsedColumn + topRow = if (scrolledToBottom) 0 else topRow.coerceAtLeast(-transcriptRows) + drawTrigger++ + } + DisposableEffect(emulator) { - fun syncState() { - transcriptRows = emulator.screen.activeTranscriptRows - maxUsedColumn = emulator.maxUsedColumn - } - syncState() - emulator.onScreenUpdate = { - if (scrolledToBottom) topRow = 0 - syncState() - drawTrigger++ - } + refresh() + emulator.onScreenUpdate = { refresh() } onDispose { emulator.onScreenUpdate = null } @@ -88,6 +88,9 @@ fun TerminalScreen( LaunchedEffect(cols, rows) { emulator.resize(cols, rows, renderer.fontWidth.toInt(), renderer.fontLineSpacing) + // The emulator outlives this screen (e.g. across rotation), and the first frame was drawn + // before this resize, so redraw with the new geometry. + refresh() currentOnResized() }