import SwiftUI #if os(macOS) import AppKit #endif struct ChangeDiffSheet: View { let selection: ChangeDiffSelection let session: EquipoSession @Environment(\.dismiss) private var dismiss @State private var model = ChangeDiffModel() var body: some View { NavigationStack { VStack(alignment: .leading, spacing: 12) { ScrollView(.vertical) { VStack(alignment: .leading, spacing: 12) { Text(verbatim: ChangedFilesPresentation.displayPath(selection.file)).font(.system(.subheadline, design: .monospaced)) .textSelection(.enabled).fixedSize(horizontal: false, vertical: true) Text(ChangedFilesPresentation.status(selection.file)).font(Typo.caption).foregroundStyle(Palette.secondary) if model.loading { ProgressView().accessibilityLabel(EquipoStrings.text("native.task.changes")) } if let failure = model.failure { Text(failure).foregroundStyle(Palette.negative) Button(EquipoStrings.text("read.retry")) { Task { await load() } } } if let diff = model.diff { if let reason = diff.reason { Text(EquipoStrings.text(reasonKey(reason))).fixedSize(horizontal: false, vertical: true) } else { if let notice = diff.hiddenCharactersNotice { Text(notice).font(Typo.secondary).foregroundStyle(Palette.warning).fixedSize(horizontal: false, vertical: true) } if diff.truncated { Text(EquipoStrings.text("native.diff.truncated")).font(Typo.secondary) } DiffText(diff: diff).padding(12) .background(Palette.surface2, in: .rect(cornerRadius: 8)) } } }.frame(maxWidth: .infinity, alignment: .leading) }.defaultScrollAnchor(.topLeading) #if os(macOS) if let diff = model.diff, !diff.copyText.isEmpty { Button(EquipoStrings.text("chat.paste.copy")) { NSPasteboard.general.clearContents() NSPasteboard.general.setString(diff.copyText, forType: .string) }.buttonStyle(.bordered) } #endif Button(EquipoStrings.text("work.open")) { if let url = ChangedFilesPresentation.fileURL(task: selection.task, facts: selection.facts, file: selection.file) { session.settings.openChanges(origin: session.origin, url: url) } else { session.settings.open(origin: session.origin, path: "/tasks/\(selection.task.id)") } }.buttonStyle(.bordered).frame(minHeight: 44) }.padding(16).background(Palette.background) .navigationTitle(EquipoStrings.text("native.task.changes")) .toolbar { ToolbarItem(placement: .cancellationAction) { Button(EquipoStrings.text("team.close")) { dismiss() } } } } #if os(macOS) .frame(minWidth: 560, idealWidth: 800, minHeight: 420, idealHeight: 640) #endif .task(id: selection.id) { await load() } .onDisappear { model.clear() } } private func load() async { await model.load(client: session.client, task: selection.task.id, candidate: selection.facts.sha, path: selection.file.path) } private func reasonKey(_ reason: String) -> String.LocalizationValue { switch reason { case "binary": "native.changes.binary" case "withheld": "native.diff.withheld" case "too_large": "native.diff.too_large" case "expired": "native.diff.expired" default: "native.diff.unavailable" } } } private struct DiffText: View { let diff: ChangeDiff var body: some View { WrappingDiffText(diff: diff).frame(maxWidth: .infinity, alignment: .leading) } } #if os(iOS) import UIKit private struct WrappingDiffText: UIViewRepresentable { let diff: ChangeDiff func makeUIView(context: Context) -> UITextView { let view = UITextView() view.isEditable = false; view.isSelectable = true; view.isScrollEnabled = false view.backgroundColor = .clear; view.textContainerInset = .zero view.textContainer.lineFragmentPadding = 0; view.textContainer.lineBreakMode = .byCharWrapping view.adjustsFontForContentSizeCategory = true return view } func updateUIView(_ view: UITextView, context: Context) { view.attributedText = DiffAttributedText.make(diff) } func sizeThatFits(_ proposal: ProposedViewSize, uiView: UITextView, context: Context) -> CGSize? { guard let width = proposal.width else { return nil } return uiView.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude)) } } #else struct WrappingDiffText: NSViewRepresentable { let diff: ChangeDiff func makeNSView(context: Context) -> NSTextView { Self.makeTextView() } func updateNSView(_ view: NSTextView, context: Context) { view.textStorage?.setAttributedString(DiffAttributedText.make(diff)) } func sizeThatFits(_ proposal: ProposedViewSize, nsView: NSTextView, context: Context) -> CGSize? { guard let width = proposal.width else { return nil } return Self.measure(nsView, width: width) } static func makeTextView() -> NSTextView { let view = NSTextView(frame: .zero) view.isEditable = false; view.isSelectable = true; view.drawsBackground = false view.isHorizontallyResizable = false; view.isVerticallyResizable = true view.textContainerInset = .zero view.textContainer?.lineFragmentPadding = 0 view.textContainer?.lineBreakMode = .byCharWrapping view.textContainer?.widthTracksTextView = false view.textContainer?.heightTracksTextView = false return view } static func measure(_ view: NSTextView, width: CGFloat) -> CGSize { guard let container = view.textContainer, let layout = view.layoutManager else { return .zero } container.containerSize = CGSize(width: max(1, width), height: .greatestFiniteMagnitude) layout.ensureLayout(for: container) return CGSize(width: width, height: ceil(layout.usedRect(for: container).height)) } } #endif /// Shared visible text for rendering and Copy. Selection also copies only markers. @MainActor enum DiffAttributedText { static func make(_ diff: ChangeDiff) -> NSAttributedString { let text = NSMutableAttributedString(string: "") #if os(iOS) let font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .monospacedSystemFont(ofSize: 12, weight: .regular)) typealias PlatformColor = UIColor #else let font = NSFont.monospacedSystemFont(ofSize: NSFont.preferredFont(forTextStyle: .caption1).pointSize, weight: .regular) typealias PlatformColor = NSColor #endif let paragraph = NSMutableParagraphStyle() paragraph.lineBreakMode = .byCharWrapping #if os(macOS) // Continuations belong to their original diff line, including its +/-. // This is layout only: selected text and whole-file Copy stay identical. paragraph.firstLineHeadIndent = 0 paragraph.headIndent = font.pointSize * 3 #endif // Append actual separators only where they exist, so whole-file Copy is // byte-identical to the displayed text, including the final newline. let lines = diff.lines for (index, line) in lines.enumerated() { let color = line.kind == .added ? Palette.positive : line.kind == .removed ? Palette.negative : Palette.text for segment in line.displaySegments() { var attributes: [NSAttributedString.Key: Any] = [.font: font, .paragraphStyle: paragraph, .foregroundColor: PlatformColor(segment.marker ? Palette.warning : color)] if segment.marker { attributes[.backgroundColor] = PlatformColor(Palette.warningBackground) } text.append(NSAttributedString(string: segment.text, attributes: attributes)) } if index < lines.count - 1 || diff.patch?.unicodeScalars.last == "\n" { text.append(NSAttributedString(string: "\n", attributes: [.font: font, .paragraphStyle: paragraph])) } } return text } }