-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Fix iOS terminal picker menu flicker and blocked scrolling #7959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/TerminalPickerMenu.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import CmuxMobileSupport | ||
| import SwiftUI | ||
|
|
||
| /// Snapshot-isolated native menu for switching the active workspace surface. | ||
| struct TerminalPickerMenu: View, Equatable { | ||
| let value: TerminalPickerMenuValue | ||
| let actions: TerminalPickerMenuActions | ||
| #if DEBUG | ||
| private let diagnostics = TerminalPickerMenuDiagnostics() | ||
| #endif | ||
|
|
||
| nonisolated static func == (lhs: Self, rhs: Self) -> Bool { | ||
| lhs.value == rhs.value | ||
| } | ||
|
|
||
| var body: some View { | ||
| Menu { | ||
| instrumentedMenuContent | ||
| } label: { | ||
| Label( | ||
| value.selectedName ?? L10n.string("mobile.terminal.select", defaultValue: "Terminal"), | ||
| systemImage: "rectangle.stack" | ||
| ) | ||
| .labelStyle(.iconOnly) | ||
| } | ||
| .foregroundStyle(TerminalPalette.foreground) | ||
| .accessibilityLabel(L10n.string("mobile.terminal.picker.title", defaultValue: "Terminals")) | ||
| .accessibilityIdentifier("MobileTerminalDropdown") | ||
| .accessibilityValue(value.selectedName ?? "") | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var instrumentedMenuContent: some View { | ||
| #if DEBUG | ||
| let _ = diagnostics.recordContentBuilderEvaluation(rowCount: value.rows.count) | ||
| #endif | ||
| menuContent | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private var menuContent: some View { | ||
| Section(L10n.string("mobile.terminal.picker.title", defaultValue: "Terminals")) { | ||
| ForEach(value.rows) { terminal in | ||
| Button { | ||
| actions.selectTerminal(terminal.id) | ||
| } label: { | ||
| Label( | ||
| terminal.name, | ||
| systemImage: terminal.id == value.selectedID && !value.hasActiveBrowser | ||
| ? "checkmark.circle.fill" | ||
| : "terminal" | ||
| ) | ||
| } | ||
| .accessibilityIdentifier("MobileTerminalMenuItem-\(terminal.id.rawValue)") | ||
| } | ||
| } | ||
|
|
||
| Section { | ||
| Button(action: actions.createWorkspace) { | ||
| Label( | ||
| L10n.string("mobile.workspace.new", defaultValue: "New Workspace"), | ||
| systemImage: "plus.square.on.square" | ||
| ) | ||
| } | ||
| .disabled(!value.canCreateWorkspace) | ||
| .accessibilityIdentifier("MobileNewWorkspaceMenuItem") | ||
|
|
||
| Button(action: actions.createTerminal) { | ||
| Label(L10n.string("mobile.terminal.new", defaultValue: "New Terminal"), systemImage: "plus") | ||
| } | ||
| .accessibilityIdentifier("MobileNewTerminalMenuItem") | ||
|
|
||
| Button(action: actions.openBrowser) { | ||
| Label( | ||
| L10n.string("mobile.browser.new", defaultValue: "New Browser"), | ||
| systemImage: value.hasActiveBrowser ? "checkmark.circle.fill" : "globe" | ||
| ) | ||
| } | ||
| .accessibilityIdentifier("MobileNewBrowserMenuItem") | ||
| } | ||
|
|
||
| #if canImport(UIKit) | ||
| Section { | ||
| if !value.hasActiveBrowser && !value.isChatMode { | ||
| Button(action: actions.openTextSheet) { | ||
| Label( | ||
| L10n.string("mobile.terminal.viewAsText", defaultValue: "View as Text"), | ||
| systemImage: "doc.plaintext" | ||
| ) | ||
| } | ||
| .accessibilityIdentifier("MobileViewAsTextMenuItem") | ||
| } | ||
|
|
||
| #if DEBUG | ||
| Button(action: actions.copyDebugLogs) { | ||
| Label( | ||
| L10n.string("mobile.debug.copyLogs", defaultValue: "Copy Debug Logs"), | ||
| systemImage: "doc.on.clipboard" | ||
| ) | ||
| } | ||
| .accessibilityIdentifier("MobileCopyDebugLogsMenuItem") | ||
| #endif | ||
|
|
||
| Button(action: actions.sendFeedback) { | ||
| Label( | ||
| L10n.string("mobile.feedback.send", defaultValue: "Send Feedback"), | ||
| systemImage: "paperplane" | ||
| ) | ||
| } | ||
| .accessibilityIdentifier("MobileSendFeedbackMenuItem") | ||
| } | ||
| #endif | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/TerminalPickerMenuActions.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import CmuxMobileShellModel | ||
|
|
||
| /// User actions emitted by ``TerminalPickerMenu`` without exposing mutable stores to its row subtree. | ||
| struct TerminalPickerMenuActions { | ||
| let selectTerminal: (MobileTerminalPreview.ID) -> Void | ||
| let createWorkspace: () -> Void | ||
| let createTerminal: () -> Void | ||
| let openBrowser: () -> Void | ||
| let openTextSheet: () -> Void | ||
| let copyDebugLogs: () -> Void | ||
| let sendFeedback: () -> Void | ||
| } |
41 changes: 41 additions & 0 deletions
41
Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/TerminalPickerMenuDiagnostics.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #if DEBUG | ||
| import Foundation | ||
| import OSLog | ||
|
|
||
| /// DEBUG-only events for counting terminal-picker menu evaluation and snapshot writes. | ||
| struct TerminalPickerMenuDiagnostics { | ||
| private let logger = Logger( | ||
| subsystem: Bundle.main.bundleIdentifier ?? "com.cmuxterm.app", | ||
| category: "TerminalPickerMenu" | ||
| ) | ||
| private let signpostLog = OSLog( | ||
| subsystem: Bundle.main.bundleIdentifier ?? "com.cmuxterm.app", | ||
| category: "TerminalPickerMenu" | ||
| ) | ||
|
|
||
| func recordContentBuilderEvaluation(rowCount: Int) { | ||
| logger.debug("content-builder evaluated rows=\(rowCount, privacy: .public)") | ||
| os_signpost( | ||
| .event, | ||
| log: signpostLog, | ||
| name: "ContentBuilderEvaluation", | ||
| "rows=%{public}d", | ||
| rowCount | ||
| ) | ||
| } | ||
|
|
||
| func recordRowsWrite(rowCount: Int, includesTitleChanges: Bool) { | ||
| logger.debug( | ||
| "snapshot rows write rows=\(rowCount, privacy: .public) includeTitles=\(includesTitleChanges, privacy: .public)" | ||
| ) | ||
| os_signpost( | ||
| .event, | ||
| log: signpostLog, | ||
| name: "SnapshotRowsWrite", | ||
| "rows=%{public}d includeTitles=%{public}d", | ||
| rowCount, | ||
| includesTitleChanges ? 1 : 0 | ||
| ) | ||
| } | ||
| } | ||
| #endif |
30 changes: 30 additions & 0 deletions
30
Packages/iOS/CmuxMobileShellUI/Sources/CmuxMobileShellUI/TerminalPickerMenuValue.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import CmuxMobileShellModel | ||
|
|
||
| /// Immutable state that determines the native terminal picker's presented menu. | ||
| struct TerminalPickerMenuValue: Equatable { | ||
| let rows: [TerminalPickerMenuRow] | ||
| let selectedID: MobileTerminalPreview.ID? | ||
| let selectedName: String? | ||
| let canCreateWorkspace: Bool | ||
| let hasActiveBrowser: Bool | ||
| let isChatMode: Bool | ||
|
|
||
| init( | ||
| liveTerminals: [MobileTerminalPreview], | ||
| snapshotRows: [TerminalPickerMenuRow], | ||
| selectedID: MobileTerminalPreview.ID?, | ||
| canCreateWorkspace: Bool, | ||
| hasActiveBrowser: Bool, | ||
| isChatMode: Bool | ||
| ) { | ||
| rows = snapshotRows.isEmpty | ||
| ? liveTerminals.map(TerminalPickerMenuRow.init) | ||
| : snapshotRows | ||
| let selection = rows.resolvedTerminalPickerSelection(selectedID: selectedID) | ||
| self.selectedID = selection?.id | ||
| selectedName = selection?.name | ||
| self.canCreateWorkspace = canCreateWorkspace | ||
| self.hasActiveBrowser = hasActiveBrowser | ||
| self.isChatMode = isChatMode | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The equality check ignores
actions, although the rendered menu invokes those closures. If SwiftUI preserves this view identity while the workspace changes to one with an equal menu snapshot,.equatable()can retain closures from the previousWorkspaceDetailView; selecting a terminal can then close the browser or perform another action for the previous workspace. Include the action owner, such as the workspace ID, in the compared value.Rule Used: Flag SwiftUI changes that can cause stale state, b... (source)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not reachable: terminal ids are globally unique surface ids, so a workspace change always changes the row ids and therefore the menu value; equal snapshots across workspaces cannot occur. The existing .onChange(of: terminalPickerLiveMembership) also resyncs the snapshot on any membership change, installing fresh closures. Within the single-render window the closures act via the live store reference keyed by unique terminal id, so they cannot target the wrong workspace.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a sound argument. Globally unique terminal IDs ensure workspace switches always produce unequal
TerminalPickerMenuValues,.equatable()never suppresses those updates, and the.onChangeresync installs fresh closures before any interaction is possible. The stale-closure path I described isn't reachable given those invariants.