GeezerVison is showing github.com — fix(terminal): keep shared terminal grids in sync (#24352) · pingdotgg/t3code@49a9e80
File treeExpand file treeCollapse file tree Open diff view settings android/src/main/java/expo/modules/t3terminal
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalModule.kt +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -66,8 +66,16 @@ class T3TerminalModule : Module() { | 66 | 66 | Prop("captureRequest") { view: T3TerminalView, request: Double -> | 67 | 67 | view.captureRequest = request | 68 | 68 | } | | 69 | + | | 70 | + Prop("sharedGrid") { view: T3TerminalView, sharedGrid: List<Int> -> | | 71 | + view.sharedGrid = sharedGrid | | 72 | + } | 69 | 73 | Events("onInput", "onResize", "onCapture") | 70 | 74 |
| | 75 | + OnViewDidUpdateProps { view: T3TerminalView -> | | 76 | + view.applyPendingBuffer() | | 77 | + } | | 78 | + | 71 | 79 | OnViewDestroys { view: T3TerminalView -> | 72 | 80 | view.cleanup() | 73 | 81 | } | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt +39-4Lines changed: 39 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -58,6 +58,8 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex | 58 | 58 | private var streamedOutput: TerminalOutputBuffer? = null | 59 | 59 | private var cols = 0 | 60 | 60 | private var rows = 0 | | 61 | + private var measuredCols = 0 | | 62 | + private var measuredRows = 0 | 61 | 63 | private var clearingInput = false | 62 | 64 | private var isCleanedUp = false | 63 | 65 | private var backgroundColorValue = Color.parseColor("#24292E") | @@ -74,12 +76,16 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex | 74 | 76 | recreateTerminal() | 75 | 77 | } | 76 | 78 |
| | 79 | + // Expo sets a batch's props in no fixed order, so the buffer waits for the whole batch: | | 80 | + // output fed before the same batch's sharedGrid lands would be drawn, and clipped, at the | | 81 | + // previous grid. | 77 | 82 | var initialBuffer: String = "" | 78 | 83 | set(value) { | 79 | 84 | if (field == value) return | 80 | 85 | field = value | 81 | | - feedPendingBuffer() | | 86 | + hasPendingBuffer = true | 82 | 87 | } | | 88 | + private var hasPendingBuffer = false | 83 | 89 |
| 84 | 90 | var fontSize: Float = 10f | 85 | 91 | set(value) { | @@ -91,6 +97,17 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex | 91 | 97 |
| 92 | 98 | var appearanceScheme: String = "dark" | 93 | 99 |
| | 100 | + // The PTY grid shared by every attached client, or empty before the server reports one. | | 101 | + // The terminal draws this grid, clamped to the view's own fit (the server's grid lags a | | 102 | + // local resize, and read-only observers never constrain it), so its wrapping and cursor | | 103 | + // match the PTY; the view's own fit still goes out through onResize. | | 104 | + var sharedGrid: List<Int> = emptyList() | | 105 | + set(value) { | | 106 | + if (field == value) return | | 107 | + field = value | | 108 | + applyGrid() | | 109 | + } | | 110 | + | 94 | 111 | var themeConfig: String = "" | 95 | 112 | set(value) { | 96 | 113 | field = value | @@ -327,6 +344,19 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex | 327 | 344 | val nextRows = (terminalCanvas.usableHeight() / terminalCanvas.cellHeightPx) | 328 | 345 | .toInt() | 329 | 346 | .coerceIn(2, 200) | | 347 | + if (nextCols != measuredCols || nextRows != measuredRows) { | | 348 | + measuredCols = nextCols | | 349 | + measuredRows = nextRows | | 350 | + onResize(mapOf("cols" to measuredCols, "rows" to measuredRows)) | | 351 | + } | | 352 | + applyGrid() | | 353 | + } | | 354 | + | | 355 | + private fun applyGrid() { | | 356 | + if (measuredCols <= 0 || measuredRows <= 0 || isCleanedUp) return | | 357 | + val shared = sharedGrid.takeIf { it.size == 2 && it[0] > 0 && it[1] > 0 } | | 358 | + val nextCols = shared?.get(0)?.coerceAtMost(measuredCols) ?: measuredCols | | 359 | + val nextRows = shared?.get(1)?.coerceAtMost(measuredRows) ?: measuredRows | 330 | 360 | if (nextCols == cols && nextRows == rows && terminalHandle != 0L) return | 331 | 361 | cols = nextCols | 332 | 362 | rows = nextRows | @@ -343,8 +373,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex | 343 | 373 | ) | 344 | 374 | } | 345 | 375 | emitResponse(response) | 346 | | - onResize(mapOf("cols" to cols, "rows" to rows)) | 347 | | - feedPendingBuffer() | | 376 | + if (!hasPendingBuffer) feedPendingBuffer() | 348 | 377 | renderSnapshot() | 349 | 378 | } | 350 | 379 |
| @@ -369,7 +398,7 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex | 369 | 398 | if (terminalHandle == 0L) return | 370 | 399 | destroyTerminal() | 371 | 400 | createTerminal() | 372 | | - feedPendingBuffer() | | 401 | + if (!hasPendingBuffer) feedPendingBuffer() | 373 | 402 | renderSnapshot() | 374 | 403 | } | 375 | 404 |
| @@ -381,6 +410,12 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex | 381 | 410 | terminalCanvas.resetSelectionState() | 382 | 411 | } | 383 | 412 |
| | 413 | + fun applyPendingBuffer() { | | 414 | + if (!hasPendingBuffer) return | | 415 | + hasPendingBuffer = false | | 416 | + feedPendingBuffer() | | 417 | + } | | 418 | + | 384 | 419 | private fun feedPendingBuffer() { | 385 | 420 | if (streamedOutput != null || terminalHandle == 0L || initialBuffer == fedBuffer) return | 386 | 421 | if (!initialBuffer.startsWith(fedBuffer)) { | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/modules/t3-terminal/ios/T3TerminalModule.swift +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -63,7 +63,15 @@ public class T3TerminalModule: Module { | 63 | 63 | Prop("captureRequest") { (view: T3TerminalView, request: Double) in | 64 | 64 | view.captureRequest = request | 65 | 65 | } | | 66 | + | | 67 | + Prop("sharedGrid") { (view: T3TerminalView, sharedGrid: [Int]) in | | 68 | + view.sharedGrid = sharedGrid | | 69 | + } | 66 | 70 | Events("onInput", "onResize", "onCapture") | | 71 | + | | 72 | + OnViewDidUpdateProps { (view: T3TerminalView) in | | 73 | + view.applyPendingBuffer() | | 74 | + } | 67 | 75 | } | 68 | 76 | } | 69 | 77 | } |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift +48-19Lines changed: 48 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -243,9 +243,23 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate { | 243 | 243 | } | 244 | 244 | } | 245 | 245 |
| | 246 | + // Expo sets a batch's props in no fixed order, so the buffer waits for the whole batch: | | 247 | + // output fed before the same batch's sharedGrid lands would be drawn, and clipped, at the | | 248 | + // previous grid. | 246 | 249 | var initialBuffer: String = "" { | 247 | 250 | didSet { | 248 | | - applyRemoteBuffer(initialBuffer) | | 251 | + hasPendingBuffer = true | | 252 | + } | | 253 | + } | | 254 | + private var hasPendingBuffer = false | | 255 | + | | 256 | + // The PTY grid shared by every attached client, or empty before the server reports one. | | 257 | + // The surface draws this grid so its wrapping and cursor match the PTY; the view's own | | 258 | + // fit still goes out through onResize. | | 259 | + var sharedGrid: [Int] = [] { | | 260 | + didSet { | | 261 | + guard oldValue != sharedGrid else { return } | | 262 | + resizeSurface() | 249 | 263 | } | 250 | 264 | } | 251 | 265 |
| @@ -529,7 +543,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate { | 529 | 543 | resizeSurface() | 530 | 544 | if let streamedOutput { | 531 | 545 | feedData(streamedOutput.data) | 532 | | - } else { | | 546 | + } else if !hasPendingBuffer { | 533 | 547 | feedBuffer(initialBuffer) | 534 | 548 | } | 535 | 549 | } | @@ -559,6 +573,19 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate { | 559 | 573 | } | 560 | 574 | surface = nil | 561 | 575 | app = nil | | 576 | + // Ghostty adds its IOSurface layer to the viewport on iOS but never removes it on | | 577 | + // free, leaving the last frame on screen under the next surface's layer. The | | 578 | + // viewport has no subviews, so every sublayer belongs to a freed surface. | | 579 | + CATransaction.begin() | | 580 | + CATransaction.setDisableActions(true) | | 581 | + terminalViewport.layer.sublayers?.forEach { $0.removeFromSuperlayer() } | | 582 | + CATransaction.commit() | | 583 | + } | | 584 | + | | 585 | + func applyPendingBuffer() { | | 586 | + guard hasPendingBuffer else { return } | | 587 | + hasPendingBuffer = false | | 588 | + applyRemoteBuffer(initialBuffer) | 562 | 589 | } | 563 | 590 |
| 564 | 591 | private func applyRemoteBuffer(_ buffer: String) { | @@ -647,30 +674,32 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate { | 647 | 674 | terminalViewport.contentScaleFactor = scale | 648 | 675 | ghostty_surface_set_content_scale(surface, Double(scale), Double(scale)) | 649 | 676 | ghostty_surface_set_size(surface, width, height) | | 677 | + let fitted = ghostty_surface_size(surface) | | 678 | + let fittedCols = max(1, Int(fitted.columns)) | | 679 | + let fittedRows = max(1, Int(fitted.rows)) | | 680 | + // Trimming whole cells keeps ghostty's padding, and the surface can only shrink | | 681 | + // within the view, so a stale larger grid falls back to the fitted one. | | 682 | + var surfaceWidth = width | | 683 | + var surfaceHeight = height | | 684 | + if sharedGrid.count == 2, sharedGrid[0] > 0, sharedGrid[1] > 0 { | | 685 | + surfaceWidth -= UInt32(max(fittedCols - sharedGrid[0], 0)) * fitted.cell_width_px | | 686 | + surfaceHeight -= UInt32(max(fittedRows - sharedGrid[1], 0)) * fitted.cell_height_px | | 687 | + if surfaceWidth != width || surfaceHeight != height { | | 688 | + ghostty_surface_set_size(surface, surfaceWidth, surfaceHeight) | | 689 | + } | | 690 | + } | 650 | 691 | ghostty_surface_set_occlusion(surface, window != nil) | 651 | | - configureIOSurfaceLayers() | | 692 | + configureIOSurfaceLayers( | | 693 | + size: CGSize(width: CGFloat(surfaceWidth) / scale, height: CGFloat(surfaceHeight) / scale)) | 652 | 694 | redrawSurface() | 653 | | - emitGhosttyResize() | | 695 | + emitResize(cols: fittedCols, rows: fittedRows) | 654 | 696 | } | 655 | 697 |
| 656 | 698 | private func redrawSurface() { | 657 | 699 | guard let surface else { return } | 658 | 700 | ghostty_surface_refresh(surface) | 659 | 701 | ghostty_surface_draw(surface) | 660 | 702 | markIOSurfaceLayersForDisplay() | 661 | | - emitGhosttyResize() | 662 | | - } | 663 | | - | 664 | | - private func emitGhosttyResize() { | 665 | | - guard let surface else { | 666 | | - emitEstimatedResize() | 667 | | - return | 668 | | - } | 669 | | - | 670 | | - let size = ghostty_surface_size(surface) | 671 | | - let cols = max(1, Int(size.columns)) | 672 | | - let rows = max(1, Int(size.rows)) | 673 | | - emitResize(cols: cols, rows: rows) | 674 | 703 | } | 675 | 704 |
| 676 | 705 | private func emitEstimatedResize() { | @@ -721,8 +750,8 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate { | 721 | 750 | ghostty_app_keyboard_changed(app) | 722 | 751 | } | 723 | 752 |
| 724 | | - private func configureIOSurfaceLayers() { | 725 | | - let targetBounds = CGRect(origin: .zero, size: terminalViewport.bounds.size) | | 753 | + private func configureIOSurfaceLayers(size: CGSize) { | | 754 | + let targetBounds = CGRect(origin: .zero, size: size) | 726 | 755 | CATransaction.begin() | 727 | 756 | CATransaction.setDisableActions(true) | 728 | 757 | terminalViewport.layer.sublayers?.forEach { sublayer in | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/terminal/NativeTerminalSurface.tsx +10-2Lines changed: 10 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -import type { TerminalOutputState } from "@t3tools/client-runtime/state/terminal"; | 2 | | -import { memo, useEffect, useImperativeHandle, useRef, useState, type Ref } from "react"; | | 1 | +import type { TerminalGrid, TerminalOutputState } from "@t3tools/client-runtime/state/terminal"; | | 2 | +import { memo, useEffect, useImperativeHandle, useMemo, useRef, useState, type Ref } from "react"; | 3 | 3 | import { | 4 | 4 | Pressable, | 5 | 5 | ScrollView, | @@ -47,6 +47,7 @@ interface TerminalSurfaceProps extends ViewProps { | 47 | 47 | readonly buffer: string | null; | 48 | 48 | readonly output: TerminalOutputState | null; | 49 | 49 | readonly fontSize?: number; | | 50 | + readonly grid?: TerminalGrid | null; | 50 | 51 | readonly isRunning: boolean; | 51 | 52 | readonly readOnly?: boolean; | 52 | 53 | readonly autoFocus?: boolean; | @@ -204,6 +205,12 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm | 204 | 205 | const theme = props.theme ?? getMobileTerminalTheme(themeId, themeAppearance); | 205 | 206 | const { onInput, onResize } = props; | 206 | 207 | const NativeTerminalSurfaceView = resolveNativeTerminalSurfaceView(); | | 208 | + const gridCols = props.grid?.cols; | | 209 | + const gridRows = props.grid?.rows; | | 210 | + const sharedGrid = useMemo( | | 211 | + () => (gridCols !== undefined && gridRows !== undefined ? [gridCols, gridRows] : []), | | 212 | + [gridCols, gridRows], | | 213 | + ); | 207 | 214 | const streaming = supportsNativeTerminalOutput(); | 208 | 215 | const nativeRef = useRef<NativeTerminalSurfaceHandle>(null); | 209 | 216 | const writerRef = useRef<ReturnType<typeof createTerminalOutputWriter> | null>(null); | @@ -260,6 +267,7 @@ const ReadyTerminalSurface = memo(function ReadyTerminalSurface(props: ReadyTerm | 260 | 267 | terminalKey={props.terminalKey} | 261 | 268 | initialBuffer={streaming ? "" : props.buffer} | 262 | 269 | fontSize={fontSize} | | 270 | + sharedGrid={sharedGrid} | 263 | 271 | style={{ flex: 1 }} | 264 | 272 | themeConfig={buildGhosttyThemeConfig(theme)} | 265 | 273 | onInput={handleNativeInput} | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -939,6 +939,7 @@ function ThreadTerminal(props: { | 939 | 939 | buffer={terminal.version === 0 ? null : terminal.buffer} | 940 | 940 | output={terminal.version === 0 ? null : terminal.output} | 941 | 941 | fontSize={fontSize} | | 942 | + grid={terminal.grid} | 942 | 943 | isRunning={isRunning} | 943 | 944 | keyboardFocusRequest={keyboardFocusRequest} | 944 | 945 | captureRequest={captureRequest} | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/terminal/nativeTerminalModule.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,8 @@ export interface NativeTerminalSurfaceProps extends ViewProps { | 36 | 36 | readonly terminalKey: string; | 37 | 37 | readonly initialBuffer: string; | 38 | 38 | readonly fontSize: number; | | 39 | + /** `[cols, rows]` of the shared PTY, or empty before the server reports one. */ | | 40 | + readonly sharedGrid?: ReadonlyArray<number>; | 39 | 41 | readonly onInput?: (event: NativeSyntheticEvent<TerminalInputEvent>) => void; | 40 | 42 | readonly onResize?: (event: NativeSyntheticEvent<TerminalResizeEvent>) => void; | 41 | 43 | } | |
|