Repository navigation
iOS: multi-Mac host switcher #5513
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
Changes from 1 commit
0ad3b78
853b512
4a3441d
662ec39
861ac47
499b673
27b7020
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -506,6 +506,60 @@ public final class MobileShellComposite: MobileTerminalOutputSinking { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return connectionState == .connected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // MARK: - Paired Mac switching | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Every Mac paired with this device, for the host switcher. Refreshed via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// ``loadPairedMacs()`` and after switch/forget. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public private(set) var pairedMacs: [MobilePairedMac] = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// The device id of the Mac the live connection currently targets, if any. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Used by the switcher to mark the active row. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public var activeMacDeviceID: String? { activeTicket?.macDeviceID } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Reload ``pairedMacs`` from the store, scoped to the signed-in user. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public func loadPairedMacs() async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| guard let pairedMacStore else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pairedMacs = [] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pairedMacs = try await pairedMacStore.loadAll(stackUserID: identityProvider?.currentUserID) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mobileShellLog.error("paired mac store loadAll failed: \(String(describing: error), privacy: .public)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Make `macDeviceID` the active pairing and reconnect to it. A no-op when it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// is already the active Mac. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// - Parameter macDeviceID: The stored Mac to switch to. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public func switchToMac(macDeviceID: String) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| guard let pairedMacStore, macDeviceID != activeMacDeviceID else { return } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try await pairedMacStore.setActive(macDeviceID: macDeviceID) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mobileShellLog.error("paired mac store setActive failed mac=\(macDeviceID, privacy: .public) error=\(String(describing: error), privacy: .public)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ = await reconnectActiveMacIfAvailable(stackUserID: identityProvider?.currentUserID) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await loadPairedMacs() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
cursor[bot] marked this conversation as resolved.
Resolved
Comment on lines
+621
to
+655
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The doc comment directly above promises the opposite: "a failed switch…leaves the previously-working Mac active and reachable." That is true of the SQLite record (
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// Forget `macDeviceID`. If it is the active Mac the live connection is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// dropped too; otherwise only the stored entry is removed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// - Parameter macDeviceID: The stored Mac to forget. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public func forgetMac(macDeviceID: String) async { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if macDeviceID == activeMacDeviceID { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| disconnectAndForgetActiveMac() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try await pairedMacStore?.remove(macDeviceID: macDeviceID) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mobileShellLog.error("paired mac store remove failed mac=\(macDeviceID, privacy: .public) error=\(String(describing: error), privacy: .public)") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| await loadPairedMacs() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
cursor[bot] marked this conversation as resolved.
Resolved
cursor[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+663
to
+674
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When The fix is to
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static func firstReconnectHostPortRoute( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| _ routes: [CmxAttachRoute], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| supportedKinds: [CmxAttachTransportKind] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #if os(iOS) | ||
| import CmuxMobilePairedMac | ||
| import CmuxMobileShell | ||
| import CmuxMobileSupport | ||
| import SwiftUI | ||
|
|
||
| /// Lets the user switch which paired Mac this device controls, and pair another. | ||
| /// | ||
| /// Lists every Mac paired with this device (from the on-device store), marks the | ||
| /// one the live connection targets, switches on tap, forgets on swipe, and pairs | ||
| /// a new Mac by scanning its QR code without dropping the others. | ||
| struct MobileHostPickerView: View { | ||
| @Bindable var store: CMUXMobileShellStore | ||
| @Environment(\.dismiss) private var dismiss | ||
| @State private var showingScanner = false | ||
|
|
||
| var body: some View { | ||
| NavigationStack { | ||
| List { | ||
| Section { | ||
| if store.pairedMacs.isEmpty { | ||
| Text(L10n.string("mobile.hostPicker.empty", defaultValue: "No paired Macs yet.")) | ||
| .foregroundStyle(.secondary) | ||
| } | ||
| ForEach(store.pairedMacs) { mac in | ||
| macRow(mac) | ||
| } | ||
| } header: { | ||
| Text(L10n.string("mobile.hostPicker.header", defaultValue: "Paired Macs")) | ||
| } footer: { | ||
| Text(L10n.string( | ||
| "mobile.hostPicker.footer", | ||
| defaultValue: "Switch which Mac this device controls. Pairing another Mac keeps the others, so you can hop between them." | ||
| )) | ||
| } | ||
|
|
||
| Section { | ||
| Button { | ||
| showingScanner = true | ||
| } label: { | ||
| Label( | ||
| L10n.string("mobile.hostPicker.addMac", defaultValue: "Pair Another Mac"), | ||
| systemImage: "plus" | ||
| ) | ||
| } | ||
| .accessibilityIdentifier("MobileHostPickerAddMac") | ||
| } | ||
| } | ||
| .navigationTitle(L10n.string("mobile.hostPicker.title", defaultValue: "Switch Mac")) | ||
| .navigationBarTitleDisplayMode(.inline) | ||
| .toolbar { | ||
| ToolbarItem(placement: .confirmationAction) { | ||
| Button(L10n.string("mobile.common.done", defaultValue: "Done")) { | ||
| dismiss() | ||
| } | ||
| .accessibilityIdentifier("MobileHostPickerDone") | ||
| } | ||
| } | ||
| .task { await store.loadPairedMacs() } | ||
| .sheet(isPresented: $showingScanner) { | ||
| MobilePairingScannerSheet { code in | ||
| showingScanner = false | ||
| Task { | ||
| _ = await store.connectPairingURL(code) | ||
| await store.loadPairedMacs() | ||
| dismiss() | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
Comment on lines
+61
to
+68
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The fix is to check the return value of |
||
| } | ||
| } | ||
| .accessibilityIdentifier("MobileHostPicker") | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private func macRow(_ mac: MobilePairedMac) -> some View { | ||
| let isActive = mac.macDeviceID == store.activeMacDeviceID | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| Button { | ||
| Task { await store.switchToMac(macDeviceID: mac.macDeviceID) } | ||
| } label: { | ||
| HStack(spacing: 12) { | ||
| Image(systemName: "desktopcomputer") | ||
| .foregroundStyle(.secondary) | ||
| VStack(alignment: .leading, spacing: 2) { | ||
| Text(mac.displayName ?? mac.macDeviceID) | ||
| .foregroundStyle(.primary) | ||
| Text(mac.lastSeenAt, format: .relative(presentation: .named)) | ||
| .font(.caption) | ||
| .foregroundStyle(.secondary) | ||
| } | ||
| Spacer(minLength: 8) | ||
| if isActive { | ||
| Image(systemName: "checkmark") | ||
| .foregroundStyle(Color.accentColor) | ||
| .accessibilityLabel(L10n.string("mobile.hostPicker.active", defaultValue: "Active")) | ||
| } | ||
| } | ||
| .contentShape(Rectangle()) | ||
| } | ||
| .buttonStyle(.plain) | ||
| .accessibilityIdentifier("MobileHostPickerRow-\(mac.macDeviceID)") | ||
| .swipeActions(edge: .trailing) { | ||
| Button(role: .destructive) { | ||
| Task { await store.forgetMac(macDeviceID: mac.macDeviceID) } | ||
| } label: { | ||
| Label(L10n.string("mobile.hostPicker.forget", defaultValue: "Forget"), systemImage: "trash") | ||
| } | ||
| .accessibilityIdentifier("MobileHostPickerForget-\(mac.macDeviceID)") | ||
| } | ||
|
Comment on lines
+77
to
+108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Both Rule Used: Flag new legacy async patterns in cmux-owned Swift... (source) |
||
| } | ||
| } | ||
| #endif | ||
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.
Clear
pairedMacswhen reload fails to avoid stale/cross-account host exposure.If
loadAllthrows, the previous in-memory list is kept. After account/scope changes, the UI can continue showing old pairings until a later successful reload.Suggested fix
public func loadPairedMacs() async { guard let pairedMacStore else { pairedMacs = [] return } do { pairedMacs = try await pairedMacStore.loadAll(stackUserID: identityProvider?.currentUserID) } catch { + pairedMacs = [] mobileShellLog.error("paired mac store loadAll failed: \(String(describing: error), privacy: .public)") } }📝 Committable suggestion
🤖 Prompt for AI Agents