Repository navigation
Add native iOS diff navigation around shared renderer #7925
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 5 commits
0c5a8f1
15cde9a
af85615
d513385
a776492
63ca18c
a65dc92
bb2e596
03f48a7
55d4e16
3c17509
32fd7c4
fb02179
7fa2c43
0bb8cda
a1b885c
2d78eba
5e587f7
1f5dd8c
943222c
54fae40
ebba145
8fc9773
d652cd5
ea531f4
c5635a8
868bf80
aded6a9
88d1708
957d7d4
d7e4def
93645dd
729bbcc
f46f794
e17656a
2801d10
422188e
6bcf11d
924dd57
e978e14
f9e2461
a76a14e
b0c72bd
ba956a4
daff0a4
2aa24fb
498c0e1
4c1fb1a
7a1c6c8
664b10f
9d68f4c
c118f89
61a63a3
5f8562f
fa9ca3e
31706d9
4e1eecb
1a8c720
ed65834
764e637
8d6c955
2485dd9
4abffbf
36687ad
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 |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| #if canImport(UIKit) | ||
| public import SwiftUI | ||
| import CmuxMobileSupport | ||
| import Foundation | ||
|
|
||
| /// Native iOS chrome around the shared web diff renderer. | ||
| public struct MobileDiffPane: View { | ||
| @State private var state: MobileDiffState | ||
| @State private var showsFiles = false | ||
| private let onClose: () -> Void | ||
| private let onReload: () -> Void | ||
|
|
||
| public init( | ||
| state: MobileDiffState, | ||
| onClose: @escaping () -> Void, | ||
| onReload: @escaping () -> Void | ||
| ) { | ||
| _state = State(initialValue: state) | ||
| self.onClose = onClose | ||
| self.onReload = onReload | ||
| } | ||
|
|
||
| public var body: some View { | ||
| VStack(spacing: 0) { | ||
| MobileDiffNavigationBar( | ||
| selectedFile: state.selectedFile, | ||
| fileCount: state.files.count, | ||
| canSelectPrevious: state.canSelectPrevious, | ||
| canSelectNext: state.canSelectNext, | ||
| showFiles: { showsFiles = true }, | ||
| selectPrevious: state.selectPrevious, | ||
| selectNext: state.selectNext, | ||
| reload: onReload, | ||
| close: onClose | ||
| ) | ||
| Divider() | ||
| MobileDiffContent(state: state, reload: onReload) | ||
| } | ||
| .background(Color(.systemBackground)) | ||
| .sheet(isPresented: $showsFiles) { | ||
| MobileDiffFileList(state: state, dismiss: { showsFiles = false }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private struct MobileDiffNavigationBar: View { | ||
| let selectedFile: MobileDiffFile? | ||
| let fileCount: Int | ||
| let canSelectPrevious: Bool | ||
| let canSelectNext: Bool | ||
| let showFiles: () -> Void | ||
| let selectPrevious: () -> Void | ||
| let selectNext: () -> Void | ||
| let reload: () -> Void | ||
| let close: () -> Void | ||
|
|
||
| var body: some View { | ||
| HStack(spacing: 10) { | ||
| Button(action: showFiles) { | ||
| HStack(spacing: 7) { | ||
| Image(systemName: "list.bullet") | ||
| VStack(alignment: .leading, spacing: 1) { | ||
| Text(selectedFile?.name ?? L10n.string("mobile.diff.files", defaultValue: "Changed Files")) | ||
| .font(.subheadline.weight(.semibold)) | ||
| .lineLimit(1) | ||
| Text(String( | ||
| format: L10n.string("mobile.diff.fileCount", defaultValue: "%lld files"), | ||
| locale: Locale.current, | ||
| fileCount | ||
| )) | ||
| .font(.caption2) | ||
| .foregroundStyle(.secondary) | ||
| } | ||
| } | ||
| .frame(maxWidth: .infinity, alignment: .leading) | ||
| .contentShape(.rect) | ||
| } | ||
| .buttonStyle(.plain) | ||
| .accessibilityIdentifier("MobileDiffFileListButton") | ||
|
|
||
| Button(action: selectPrevious) { Image(systemName: "chevron.up") } | ||
| .disabled(!canSelectPrevious) | ||
| .accessibilityLabel(L10n.string("mobile.diff.previousFile", defaultValue: "Previous File")) | ||
| Button(action: selectNext) { Image(systemName: "chevron.down") } | ||
| .disabled(!canSelectNext) | ||
| .accessibilityLabel(L10n.string("mobile.diff.nextFile", defaultValue: "Next File")) | ||
| Button(action: reload) { Image(systemName: "arrow.clockwise") } | ||
| .accessibilityLabel(L10n.string("mobile.diff.refresh", defaultValue: "Refresh Diff")) | ||
| Button(action: close) { Image(systemName: "xmark") } | ||
| .accessibilityLabel(L10n.string("mobile.diff.close", defaultValue: "Close Diff")) | ||
| } | ||
| .padding(.horizontal, 12) | ||
| .padding(.vertical, 8) | ||
| .background(.bar) | ||
| } | ||
| } | ||
|
|
||
| private struct MobileDiffContent: View { | ||
| let state: MobileDiffState | ||
| let reload: () -> Void | ||
|
|
||
| var body: some View { | ||
| if state.isLoading, state.document == nil { | ||
| ProgressView(L10n.string("mobile.diff.loading", defaultValue: "Loading changes…")) | ||
| .frame(maxWidth: .infinity, maxHeight: .infinity) | ||
| } else if let errorMessage = state.errorMessage, state.document == nil { | ||
|
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 a refresh fails after a previous successful load (or WebKit reports an asset/navigation error after Useful? React with 👍 / 👎.
Contributor
Author
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. Fixed in 63ca18c: error UI takes precedence even when a prior document exists, with localized Retry. — Claude Code |
||
| ContentUnavailableView { | ||
| Label(L10n.string("mobile.diff.loadFailed", defaultValue: "Couldn’t Load Diff"), systemImage: "exclamationmark.triangle") | ||
| } description: { | ||
| Text(errorMessage) | ||
| } actions: { | ||
| Button(L10n.string("mobile.common.retry", defaultValue: "Retry"), action: reload) | ||
| } | ||
| } else if state.document != nil { | ||
|
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.
Contributor
Author
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. Fixed in 63ca18c: refresh errors replace stale content with a localized retry screen. — Claude Code
Contributor
Author
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. Fixed in 63ca18c: refresh errors replace stale content with a localized retry screen. — Claude Code |
||
| MobileDiffWebView(state: state) | ||
| .frame(maxWidth: .infinity, maxHeight: .infinity) | ||
| .overlay(alignment: .top) { | ||
| if state.isLoading { | ||
| ProgressView().controlSize(.small).padding(8) | ||
| } | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } else { | ||
| Color.clear | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private struct MobileDiffFileList: View { | ||
| let state: MobileDiffState | ||
|
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.
AGENTS.md's “Snapshot boundary for list subtrees” rule says a view with Useful? React with 👍 / 👎. |
||
| let dismiss: () -> Void | ||
|
|
||
| var body: some View { | ||
| NavigationStack { | ||
| List(state.files) { file in | ||
| Button { | ||
| state.selectFile(id: file.id) | ||
| dismiss() | ||
| } label: { | ||
| HStack(spacing: 10) { | ||
| Image(systemName: file.id == state.selectedFileID ? "checkmark.circle.fill" : "doc.text") | ||
| .foregroundStyle(file.id == state.selectedFileID ? Color.accentColor : Color.secondary) | ||
| VStack(alignment: .leading, spacing: 3) { | ||
| Text(file.name).foregroundStyle(.primary) | ||
| Text(file.path).font(.caption).foregroundStyle(.secondary).lineLimit(1) | ||
| } | ||
| Spacer(minLength: 8) | ||
| HStack(spacing: 5) { | ||
| Text(verbatim: "+\(file.added)").foregroundStyle(.green) | ||
| Text(verbatim: "−\(file.deleted)").foregroundStyle(.red) | ||
| } | ||
| .font(.caption.monospacedDigit()) | ||
| } | ||
| } | ||
| .accessibilityIdentifier("MobileDiffFile-\(file.id)") | ||
| } | ||
| .navigationTitle(L10n.string("mobile.diff.files", defaultValue: "Changed Files")) | ||
| .navigationBarTitleDisplayMode(.inline) | ||
| .toolbar { | ||
| ToolbarItem(placement: .confirmationAction) { | ||
| Button(L10n.string("mobile.common.done", defaultValue: "Done"), action: dismiss) | ||
| } | ||
| } | ||
| } | ||
| .presentationDetents([.medium, .large]) | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import Foundation | ||
| public import CmuxMobileShellModel | ||
| public import Observation | ||
|
|
||
| /// Native metadata for one file rendered by the shared web diff viewer. | ||
| public struct MobileDiffFile: Decodable, Equatable, Identifiable, Sendable { | ||
| public let id: String | ||
| public let path: String | ||
| public let added: Int | ||
| public let deleted: Int | ||
|
|
||
| public init(id: String, path: String, added: Int, deleted: Int) { | ||
| self.id = id | ||
| self.path = path | ||
| self.added = added | ||
| self.deleted = deleted | ||
| } | ||
|
|
||
| public var name: String { | ||
| URL(fileURLWithPath: path).lastPathComponent | ||
| } | ||
| } | ||
|
|
||
| /// Main-actor state shared by the native diff chrome and its `WKWebView`. | ||
| @MainActor | ||
| @Observable | ||
| public final class MobileDiffState { | ||
| public private(set) var document: MobileDiffDocument? | ||
| public private(set) var files: [MobileDiffFile] = [] | ||
| public private(set) var selectedFileID: String? | ||
| public private(set) var errorMessage: String? | ||
| public private(set) var isLoading = false | ||
| public private(set) var generation = 0 | ||
|
|
||
| public init() {} | ||
|
|
||
| public var selectedFile: MobileDiffFile? { | ||
| guard let selectedFileID else { return files.first } | ||
| return files.first { $0.id == selectedFileID } | ||
| } | ||
|
|
||
| public var selectedFileIndex: Int? { | ||
| guard let selectedFile else { return nil } | ||
| return files.firstIndex { $0.id == selectedFile.id } | ||
| } | ||
|
|
||
| public var canSelectPrevious: Bool { | ||
| guard let selectedFileIndex else { return false } | ||
| return selectedFileIndex > files.startIndex | ||
| } | ||
|
|
||
| public var canSelectNext: Bool { | ||
| guard let selectedFileIndex else { return false } | ||
| return selectedFileIndex + 1 < files.endIndex | ||
| } | ||
|
|
||
| public func beginLoading() { | ||
| isLoading = true | ||
| errorMessage = nil | ||
| } | ||
|
|
||
| public func load(_ document: MobileDiffDocument) { | ||
| self.document = document | ||
| files = [] | ||
| selectedFileID = nil | ||
|
lawrencecchen marked this conversation as resolved.
Resolved
|
||
| errorMessage = nil | ||
| isLoading = false | ||
| generation &+= 1 | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| public func fail(message: String) { | ||
| isLoading = false | ||
| errorMessage = message | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| public func updateFiles(_ files: [MobileDiffFile], selectedFileID: String?) { | ||
| self.files = files | ||
| if let selectedFileID, files.contains(where: { $0.id == selectedFileID }) { | ||
| self.selectedFileID = selectedFileID | ||
| } else if !files.contains(where: { $0.id == self.selectedFileID }) { | ||
| self.selectedFileID = files.first?.id | ||
| } | ||
|
cursor[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| public func selectFile(id: String) { | ||
| guard files.contains(where: { $0.id == id }) else { return } | ||
| selectedFileID = id | ||
| } | ||
|
|
||
| public func selectPrevious() { | ||
| guard let selectedFileIndex, selectedFileIndex > files.startIndex else { return } | ||
| selectedFileID = files[files.index(before: selectedFileIndex)].id | ||
| } | ||
|
|
||
| public func selectNext() { | ||
| guard let selectedFileIndex, selectedFileIndex + 1 < files.endIndex else { return } | ||
| selectedFileID = files[files.index(after: selectedFileIndex)].id | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.