Skip to content

Commit 7879c5e

Browse files
shivamhwpclaude
andcommitted
fix(mobile): iOS terminal font and theme changes no longer replay output
Changing the font or theme recreated the native surface and replayed retained output, so historical status queries were answered again on the PTY. Font, appearance and theme now update the existing surface in place, so nothing is replayed. Co-Authored-By: Claude Haiku 5.5 <[email protected]>
1 parent b2375ad commit 7879c5e

1 file changed

Lines changed: 26 additions & 20 deletions

File tree

‎apps/mobile/modules/t3-terminal/ios/T3TerminalView.swift‎

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInte
219219
private var lastReportedGrid: (cols: Int, rows: Int)?
220220
private var lastAppliedBuffer = ""
221221
private var streamedOutput: TerminalOutputBuffer?
222-
private var unfedOutputCount = 0
223222
private var pendingVerticalScrollPoints: CGFloat = 0
224223
private var menuText: String?
225224
private var sentInput = ""
@@ -286,7 +285,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInte
286285
didSet {
287286
guard oldValue != fontSize else { return }
288287
inputField.font = UIFont.monospacedSystemFont(ofSize: max(fontSize, 13), weight: .regular)
289-
resetSurface()
288+
applyConfigInPlace()
290289
}
291290
}
292291

@@ -322,14 +321,14 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInte
322321
didSet {
323322
guard oldValue != appearanceScheme else { return }
324323
appearance = TerminalAppearanceScheme(value: appearanceScheme)
325-
resetSurface()
324+
applyConfigInPlace()
326325
}
327326
}
328327

329328
var themeConfig: String = "" {
330329
didSet {
331330
guard oldValue != themeConfig else { return }
332-
resetSurface()
331+
applyConfigInPlace()
333332
}
334333
}
335334

@@ -692,21 +691,13 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInte
692691
surface = createdSurface
693692
ghostty_app_set_color_scheme(createdApp, appearance.ghosttyColorScheme)
694693
ghostty_surface_set_color_scheme(createdSurface, appearance.ghosttyColorScheme)
694+
setupWriteCallback()
695695
resizeSurface()
696696
if let streamedOutput {
697-
let data = streamedOutput.data
698-
let live = min(unfedOutputCount, data.count)
699-
// Replay history before attaching the writer, or its queries get answered to the PTY again.
700-
feedData(data.dropLast(live))
701-
setupWriteCallback()
702-
feedData(data.suffix(live))
703-
} else {
704-
if !hasPendingBuffer {
705-
feedBuffer(initialBuffer)
706-
}
707-
setupWriteCallback()
697+
feedData(streamedOutput.data)
698+
} else if !hasPendingBuffer {
699+
feedBuffer(initialBuffer)
708700
}
709-
unfedOutputCount = 0
710701
}
711702

712703
private func resetSurface() {
@@ -719,6 +710,24 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInte
719710
setNeedsLayout()
720711
}
721712

713+
// Font and theme change in place: a new surface would replay output and re-answer its queries.
714+
private func applyConfigInPlace() {
715+
guard let app, let surface else {
716+
resetSurface()
717+
return
718+
}
719+
guard let config = ghostty_config_new() else { return }
720+
defer { ghostty_config_free(config) }
721+
loadThemeConfig(into: config)
722+
ghostty_config_finalize(config)
723+
ghostty_app_set_color_scheme(app, appearance.ghosttyColorScheme)
724+
ghostty_surface_set_color_scheme(surface, appearance.ghosttyColorScheme)
725+
tickApp()
726+
ghostty_surface_update_config(surface, config)
727+
resizeSurface()
728+
setNeedsLayout()
729+
}
730+
722731
private func destroySurface() {
723732
// Unblock the io thread before ghostty_surface_free joins it.
724733
tickApp()
@@ -781,10 +790,8 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInte
781790
output.write(bytes, reset: reset)
782791
streamedOutput = output
783792
if reset {
784-
unfedOutputCount = bytes.count
785793
resetSurface()
786794
} else if surface == nil {
787-
unfedOutputCount += bytes.count
788795
createSurfaceIfPossible()
789796
} else {
790797
feedData(bytes)
@@ -952,8 +959,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate, UIEditMenuInte
952959
}
953960

954961
private func writeThemeConfigFile() -> String? {
955-
guard !themeConfig.isEmpty else { return nil }
956-
let configContents = themeConfig
962+
let configContents = "\(themeConfig)\nfont-size = \(String(format: "%g", Double(fontSize)))\n"
957963
let url = URL(fileURLWithPath: NSTemporaryDirectory())
958964
.appendingPathComponent("t3-terminal-theme-\(appearance.rawValue).ghostty")
959965

0 commit comments

Comments
 (0)