Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extension GhosttySurfaceRepresentable {
surfaceID: surfaceID,
store: store,
terminalPresentationIsActive: terminalPresentationIsActive,
handoffTerminalSizingWhenInactive: handoffTerminalSizingWhenInactive,
artifactFilesEnabled: artifactFilesEnabled,
terminalFolderTapEnabled: terminalFolderTapEnabled,
terminalFilesChipEnabled: terminalFilesChipEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable {
/// screens. The default uses the settled visible-height grid for
/// alternate-screen apps.
var useLegacyTerminalSizing: Bool = false
/// When enabled, backgrounding releases this phone's viewport so the Mac
/// can take the terminal grid. The default preserves the viewport lease
/// across a background/foreground transition to keep the host policy and
/// rendered grid stable.
var handoffTerminalSizingWhenInactive: Bool = false
var sessionArtifactCountEnabled: Bool = false
var visibleArtifactCount: Int = 0
/// SSH terminals: show the Files chip regardless of paths on screen; it
Expand Down Expand Up @@ -163,6 +168,9 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable {
// composer band. This is a UIKit-internal mutation, not a sibling-observed
// state write, so it is safe in `updateUIViewController`.
context.coordinator.setTerminalPresentationActive(terminalPresentationIsActive)
context.coordinator.setHandoffTerminalSizingWhenInactive(
handoffTerminalSizingWhenInactive
)
context.coordinator.attemptPendingOutputConsumerRecoveryPresentation()
guard let surfaceView = (uiView as? GhosttySurfaceHostView)?.surfaceView else { return }
surfaceView.terminalWorkPopulation = terminalWorkPopulation
Expand Down Expand Up @@ -317,6 +325,7 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable {
/// will never be emitted again.
var outputStartReady = false
var terminalPresentationIsActive: Bool
var handoffTerminalSizingWhenInactive: Bool
var outputStartContinuation: AsyncStream<Void>.Continuation?
var outputStartViewportTimeouts = 0
var outputStartMinimumViewportReportID: UInt64?
Expand Down Expand Up @@ -393,6 +402,7 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable {
surfaceID: String,
store: CMUXMobileShellStore,
terminalPresentationIsActive: Bool = true,
handoffTerminalSizingWhenInactive: Bool = false,
artifactFilesEnabled: Bool,
terminalFolderTapEnabled: Bool,
terminalFilesChipEnabled: Bool,
Expand All @@ -412,6 +422,7 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable {
self.surfaceID = surfaceID
self.store = store
self.terminalPresentationIsActive = terminalPresentationIsActive
self.handoffTerminalSizingWhenInactive = handoffTerminalSizingWhenInactive
self.artifactFilesEnabled = artifactFilesEnabled
self.terminalFolderTapEnabled = terminalFolderTapEnabled
self.artifactChipGate = TerminalArtifactChipFeatureGate(
Expand Down Expand Up @@ -1243,10 +1254,21 @@ struct GhosttySurfaceRepresentable: UIViewControllerRepresentable {
attemptPendingOutputConsumerRecoveryPresentation()
} else {
outputConsumerRecoveryAlertPending = outputConsumerRestartBlocked
stopMountedTasks(releaseViewport: true)
stopMountedTasks(releaseViewport: handoffTerminalSizingWhenInactive)
}
}

/// Updates the persisted handoff choice while keeping the current
/// inactive surface consistent. Enabling handoff while the terminal is
/// already backgrounded releases the lease immediately; disabling it
/// preserves any lease until the next foreground mount.
func setHandoffTerminalSizingWhenInactive(_ enabled: Bool) {
guard handoffTerminalSizingWhenInactive != enabled else { return }
handoffTerminalSizingWhenInactive = enabled
guard enabled, !terminalPresentationIsActive else { return }
stopMountedTasks(releaseViewport: true)
}

Comment thread
austinywang marked this conversation as resolved.
Resolved
func detach() {
outputConsumerRecoveryAlertPending = false
stopMountedTasks(releaseViewport: true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public final class MobileDisplaySettings {
private static let showMissingFilesKey = "cmux.mobile.showMissingFiles"
private static let terminalFolderTapEnabledKey = "cmux.mobile.terminalFolderTapEnabled"
private static let useLegacyTerminalSizingKey = "cmux.mobile.useLegacyTerminalSizing"
private static let handoffTerminalSizingWhenInactiveKey = "cmux.mobile.handoffTerminalSizingWhenInactive"
private static let workspacePreviewLineCountKey = "cmux.mobile.workspacePreviewLineCount"
private static let unreadIndicatorLeftShiftKey = "cmux.mobile.debug.unreadIndicatorLeftShift.v2"
private static let unreadBadgeDiameterKey = "cmux.mobile.debug.unreadBadgeDiameter.v1"
Expand Down Expand Up @@ -115,6 +116,18 @@ public final class MobileDisplaySettings {
didSet { defaults.set(useLegacyTerminalSizing, forKey: Self.useLegacyTerminalSizingKey) }
}

/// Whether an inactive iPhone releases its terminal viewport so a Mac can
/// take the grid. Defaults to `false`, preserving the host policy and the
/// last confirmed grid across a background/foreground transition.
public var handoffTerminalSizingWhenInactive: Bool {
didSet {
defaults.set(
handoffTerminalSizingWhenInactive,
forKey: Self.handoffTerminalSizingWhenInactiveKey
)
}
}

/// Whether cmux emits app-owned haptic feedback. Defaults to `true`.
/// This is the sole observed writer for the persisted preference; haptic
/// emitters read the same defaults store through ``haptics``.
Expand Down Expand Up @@ -238,6 +251,9 @@ public final class MobileDisplaySettings {
self.showMissingFiles = defaults.bool(forKey: Self.showMissingFilesKey)
self.terminalFolderTapEnabled = defaults.object(forKey: Self.terminalFolderTapEnabledKey) as? Bool ?? true
self.useLegacyTerminalSizing = defaults.object(forKey: Self.useLegacyTerminalSizingKey) as? Bool ?? false
self.handoffTerminalSizingWhenInactive = defaults.object(
forKey: Self.handoffTerminalSizingWhenInactiveKey
) as? Bool ?? false
self.hapticFeedbackEnabled = haptics.isEnabled
self.terminalScrollbackRows = MobileTerminalScrollbackPreference.resolve(from: defaults)
let storedPreviewLines = defaults.object(forKey: Self.workspacePreviewLineCountKey) as? Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,22 @@ struct MobileSettingsView: View {
}
.accessibilityIdentifier("MobileSettingsLegacyTerminalSizingToggle")

Toggle(isOn: $displaySettings.handoffTerminalSizingWhenInactive) {
VStack(alignment: .leading, spacing: 4) {
Text(L10n.string(
"mobile.settings.handoffTerminalSizing",
defaultValue: "Hand Off Terminal Sizing When Inactive"
))
Text(L10n.string(
"mobile.settings.handoffTerminalSizing.description",
defaultValue: "Let the Mac choose the terminal size while this app is in the background."
))
.font(.caption)
.foregroundStyle(.secondary)
}
}
.accessibilityIdentifier("MobileSettingsHandoffTerminalSizingToggle")

Button {
showingShortcuts = true
} label: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22580,6 +22580,34 @@
}
}
},
"mobile.settings.handoffTerminalSizing": {
"extractionState": "manual",
"localizations": {
"en": {"stringUnit": {"state": "translated", "value": "Hand Off Terminal Sizing When Inactive"}},
"ja": {"stringUnit": {"state": "translated", "value": "非アクティブ時にターミナルサイズを引き継ぐ"}},
"de": {"stringUnit": {"state": "translated", "value": "Terminalgröße bei Inaktivität übergeben"}},
"fr": {"stringUnit": {"state": "translated", "value": "Transférer le dimensionnement du terminal en cas d’inactivité"}},
"ar": {"stringUnit": {"state": "translated", "value": "تسليم تحجيم الطرفية عند عدم النشاط"}},
"es": {"stringUnit": {"state": "translated", "value": "Ceder el dimensionamiento del terminal cuando está inactivo"}},
"zh-Hant": {"stringUnit": {"state": "translated", "value": "非使用中時交接終端機大小"}},
"zh-Hans": {"stringUnit": {"state": "translated", "value": "不活跃时移交终端大小"}},
"ko": {"stringUnit": {"state": "translated", "value": "비활성 상태일 때 터미널 크기 넘기기"}}
}
},
"mobile.settings.handoffTerminalSizing.description": {
"extractionState": "manual",
"localizations": {
"en": {"stringUnit": {"state": "translated", "value": "Let the Mac choose the terminal size while this app is in the background."}},
"ja": {"stringUnit": {"state": "translated", "value": "このアプリがバックグラウンドにある間は、Macにターミナルサイズを任せます。"}},
"de": {"stringUnit": {"state": "translated", "value": "Der Mac wählt die Terminalgröße, während diese App im Hintergrund ist."}},
"fr": {"stringUnit": {"state": "translated", "value": "Laisser le Mac choisir la taille du terminal lorsque l’app est en arrière-plan."}},
"ar": {"stringUnit": {"state": "translated", "value": "السماح لجهاز Mac باختيار حجم الطرفية عندما يكون هذا التطبيق في الخلفية."}},
"es": {"stringUnit": {"state": "translated", "value": "Dejar que el Mac elija el tamaño del terminal mientras esta app está en segundo plano."}},
"zh-Hant": {"stringUnit": {"state": "translated", "value": "此 App 在背景執行時,讓 Mac 選擇終端機大小。"}},
"zh-Hans": {"stringUnit": {"state": "translated", "value": "此 App 在后台运行时,让 Mac 选择终端大小。"}},
"ko": {"stringUnit": {"state": "translated", "value": "이 앱이 백그라운드에 있을 때 Mac이 터미널 크기를 선택하도록 합니다."}}
}
},
"mobile.ios106PairingUpdate.releaseLabel": {
"extractionState": "manual",
"localizations": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extension WorkspaceDetailView {
terminalFilesChipEnabled: isTerminalFilesChipEnabled,
showMissingFiles: showMissingFiles,
useLegacyTerminalSizing: displaySettings.useLegacyTerminalSizing,
handoffTerminalSizingWhenInactive: displaySettings.handoffTerminalSizingWhenInactive,
sessionArtifactCountEnabled: !isSSH && store.supportsChatArtifactGallery,
visibleArtifactCount: visibleArtifactCount,
sshFilesChipEnabled: isSSH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,22 @@ import Testing
#expect(!MobileDisplaySettings(defaults: defaults).useLegacyTerminalSizing)
}

@Test func handoffTerminalSizingDefaultsToOffWithoutAWrite() throws {
let defaults = try makeDefaults("handoffTerminalSizingDefaults")
let settings = MobileDisplaySettings(defaults: defaults)
#expect(!settings.handoffTerminalSizingWhenInactive)
#expect(defaults.object(forKey: "cmux.mobile.handoffTerminalSizingWhenInactive") == nil)
}

@Test func handoffTerminalSizingPersistsAcrossInstances() throws {
let defaults = try makeDefaults("handoffTerminalSizingPersists")
let settings = MobileDisplaySettings(defaults: defaults)
settings.handoffTerminalSizingWhenInactive = true
#expect(MobileDisplaySettings(defaults: defaults).handoffTerminalSizingWhenInactive)
settings.handoffTerminalSizingWhenInactive = false
#expect(!MobileDisplaySettings(defaults: defaults).handoffTerminalSizingWhenInactive)
}

@Test func hapticFeedbackDefaultsToEnabledWithoutAWrite() throws {
let defaults = try makeDefaults("hapticFeedbackDefaults")
let settings = MobileDisplaySettings(defaults: defaults)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ struct TerminalSurfaceMountOwnershipTests {
workspaceID: workspace.id.rawValue,
surfaceID: surfaceID,
store: store,
handoffTerminalSizingWhenInactive: true,
artifactFilesEnabled: false,
terminalFolderTapEnabled: false,
terminalFilesChipEnabled: false,
Expand Down Expand Up @@ -150,14 +151,84 @@ struct TerminalSurfaceMountOwnershipTests {
}
#expect(remounted)

// Presentation ownership, unlike temporary window attachment, releases
// the sticky viewport lease and its generation-fenced Mac report.
// Presentation ownership releases the sticky viewport lease only when
// the explicit handoff preference is enabled.
coordinator.setTerminalPresentationActive(false)
#expect(!store.reportedViewportSizesByTerminalKey.values.contains(
MobileTerminalViewportSize(columns: 72, rows: 61)
))
}

@MainActor
@Test("backgrounding preserves the viewport lease by default")
func backgroundingPreservesViewportLeaseByDefault() async throws {
let store = MobileShellComposite.preview()
let workspace = try #require(store.workspaces.first { !$0.terminals.isEmpty })
let terminal = try #require(workspace.terminals.first)
let surfaceID = terminal.id.rawValue
let coordinator = GhosttySurfaceRepresentable.Coordinator(
workspaceID: workspace.id.rawValue,
surfaceID: surfaceID,
store: store,
artifactFilesEnabled: false,
terminalFolderTapEnabled: false,
terminalFilesChipEnabled: false,
sessionArtifactCountEnabled: false,
visibleArtifactCount: 0,
onArtifactFilesRequested: { _ in },
onArtifactPathTapped: { _ in },
onVisibleArtifactCountChanged: { _ in },
onArtifactGalleryRefreshSignal: { _ in }
)
let surfaceView = GhosttySurfaceView(
runtime: try GhosttyRuntime.shared(),
delegate: coordinator
)
surfaceView.stopDisplayLink()
let host = UIViewController()
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844))
window.rootViewController = host
window.makeKeyAndVisible()
coordinator.attach(surfaceView: surfaceView)
defer {
surfaceView.removeFromSuperview()
coordinator.detach()
surfaceView.prepareForDismantle()
window.isHidden = true
}
surfaceView.frame = host.view.bounds
host.view.addSubview(surfaceView)
coordinator.ghosttySurfaceView(
surfaceView,
didResize: TerminalGridSize(
columns: 72,
rows: 61,
pixelWidth: 1_296,
pixelHeight: 2_135
),
reportID: 1
)
#expect(await waitUntil {
store.terminalOutputStreamTokensBySurfaceID[surfaceID] != nil
})

coordinator.setTerminalPresentationActive(false)

#expect(store.reportedViewportSizesByTerminalKey.values.contains(
MobileTerminalViewportSize(columns: 72, rows: 61)
))
#expect(store.terminalViewportGeneration(for: surfaceID) == 1)

coordinator.setTerminalPresentationActive(true)
#expect(await waitUntil {
store.terminalOutputStreamTokensBySurfaceID[surfaceID] != nil
})
#expect(store.reportedViewportSizesByTerminalKey.values.contains(
MobileTerminalViewportSize(columns: 72, rows: 61)
))
#expect(store.terminalViewportGeneration(for: surfaceID) == 1)
}

@MainActor
@Test("a terminated output stream is reclaimed while the surface remains mounted")
func terminatedOutputStreamIsReclaimedWhileSurfaceRemainsMounted() async throws {
Expand Down
Loading