oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import SwiftUI
2#if os(macOS)
3import AppKit
4#endif
5
6struct ChangeDiffSheet: View {
7 let selection: ChangeDiffSelection
8 let session: EquipoSession
9 @Environment(\.dismiss) private var dismiss
10 @State private var model = ChangeDiffModel()
11
12 var body: some View {
13 NavigationStack {
14 VStack(alignment: .leading, spacing: 12) {
15 ScrollView(.vertical) {
16 VStack(alignment: .leading, spacing: 12) {
17 Text(verbatim: ChangedFilesPresentation.displayPath(selection.file)).font(.system(.subheadline, design: .monospaced))
18 .textSelection(.enabled).fixedSize(horizontal: false, vertical: true)
19 Text(ChangedFilesPresentation.status(selection.file)).font(Typo.caption).foregroundStyle(Palette.secondary)
20 if model.loading { ProgressView().accessibilityLabel(EquipoStrings.text("native.task.changes")) }
21 if let failure = model.failure {
22 Text(failure).foregroundStyle(Palette.negative)
23 Button(EquipoStrings.text("read.retry")) { Task { await load() } }
24 }
25 if let diff = model.diff {
26 if let reason = diff.reason { Text(EquipoStrings.text(reasonKey(reason))).fixedSize(horizontal: false, vertical: true) }
27 else {
28 if let notice = diff.hiddenCharactersNotice {
29 Text(notice).font(Typo.secondary).foregroundStyle(Palette.warning).fixedSize(horizontal: false, vertical: true)
30 }
31 if diff.truncated { Text(EquipoStrings.text("native.diff.truncated")).font(Typo.secondary) }
32 DiffText(diff: diff).padding(12)
33 .background(Palette.surface2, in: .rect(cornerRadius: 8))
34 }
35 }
36 }.frame(maxWidth: .infinity, alignment: .leading)
37 }.defaultScrollAnchor(.topLeading)
38 #if os(macOS)
39 if let diff = model.diff, !diff.copyText.isEmpty {
40 Button(EquipoStrings.text("chat.paste.copy")) {
41 NSPasteboard.general.clearContents()
42 NSPasteboard.general.setString(diff.copyText, forType: .string)
43 }.buttonStyle(.bordered)
44 }
45 #endif
46 Button(EquipoStrings.text("work.open")) {
47 if let url = ChangedFilesPresentation.fileURL(task: selection.task, facts: selection.facts, file: selection.file) {
48 session.settings.openChanges(origin: session.origin, url: url)
49 } else { session.settings.open(origin: session.origin, path: "/tasks/\(selection.task.id)") }
50 }.buttonStyle(.bordered).frame(minHeight: 44)
51 }.padding(16).background(Palette.background)
52 .navigationTitle(EquipoStrings.text("native.task.changes"))
53 .toolbar { ToolbarItem(placement: .cancellationAction) { Button(EquipoStrings.text("team.close")) { dismiss() } } }
54 }
55 #if os(macOS)
56 .frame(minWidth: 560, idealWidth: 800, minHeight: 420, idealHeight: 640)
57 #endif
58 .task(id: selection.id) { await load() }
59 .onDisappear { model.clear() }
60 }
61 private func load() async { await model.load(client: session.client, task: selection.task.id, candidate: selection.facts.sha, path: selection.file.path) }
62 private func reasonKey(_ reason: String) -> String.LocalizationValue {
63 switch reason {
64 case "binary": "native.changes.binary"
65 case "withheld": "native.diff.withheld"
66 case "too_large": "native.diff.too_large"
67 case "expired": "native.diff.expired"
68 default: "native.diff.unavailable"
69 }
70 }
71}
72
73private struct DiffText: View {
74 let diff: ChangeDiff
75 var body: some View {
76 WrappingDiffText(diff: diff).frame(maxWidth: .infinity, alignment: .leading)
77 }
78}
79
80#if os(iOS)
81import UIKit
82private struct WrappingDiffText: UIViewRepresentable {
83 let diff: ChangeDiff
84 func makeUIView(context: Context) -> UITextView {
85 let view = UITextView()
86 view.isEditable = false; view.isSelectable = true; view.isScrollEnabled = false
87 view.backgroundColor = .clear; view.textContainerInset = .zero
88 view.textContainer.lineFragmentPadding = 0; view.textContainer.lineBreakMode = .byCharWrapping
89 view.adjustsFontForContentSizeCategory = true
90 return view
91 }
92 func updateUIView(_ view: UITextView, context: Context) {
93 view.attributedText = DiffAttributedText.make(diff)
94 }
95 func sizeThatFits(_ proposal: ProposedViewSize, uiView: UITextView, context: Context) -> CGSize? {
96 guard let width = proposal.width else { return nil }
97 return uiView.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude))
98 }
99}
100#else
101struct WrappingDiffText: NSViewRepresentable {
102 let diff: ChangeDiff
103 func makeNSView(context: Context) -> NSTextView { Self.makeTextView() }
104 func updateNSView(_ view: NSTextView, context: Context) {
105 view.textStorage?.setAttributedString(DiffAttributedText.make(diff))
106 }
107 func sizeThatFits(_ proposal: ProposedViewSize, nsView: NSTextView, context: Context) -> CGSize? {
108 guard let width = proposal.width else { return nil }
109 return Self.measure(nsView, width: width)
110 }
111 static func makeTextView() -> NSTextView {
112 let view = NSTextView(frame: .zero)
113 view.isEditable = false; view.isSelectable = true; view.drawsBackground = false
114 view.isHorizontallyResizable = false; view.isVerticallyResizable = true
115 view.textContainerInset = .zero
116 view.textContainer?.lineFragmentPadding = 0
117 view.textContainer?.lineBreakMode = .byCharWrapping
118 view.textContainer?.widthTracksTextView = false
119 view.textContainer?.heightTracksTextView = false
120 return view
121 }
122 static func measure(_ view: NSTextView, width: CGFloat) -> CGSize {
123 guard let container = view.textContainer, let layout = view.layoutManager else { return .zero }
124 container.containerSize = CGSize(width: max(1, width), height: .greatestFiniteMagnitude)
125 layout.ensureLayout(for: container)
126 return CGSize(width: width, height: ceil(layout.usedRect(for: container).height))
127 }
128}
129#endif
130
131/// Shared visible text for rendering and Copy. Selection also copies only markers.
132@MainActor enum DiffAttributedText {
133 static func make(_ diff: ChangeDiff) -> NSAttributedString {
134 let text = NSMutableAttributedString(string: "")
135 #if os(iOS)
136 let font = UIFontMetrics(forTextStyle: .caption1).scaledFont(for: .monospacedSystemFont(ofSize: 12, weight: .regular))
137 typealias PlatformColor = UIColor
138 #else
139 let font = NSFont.monospacedSystemFont(ofSize: NSFont.preferredFont(forTextStyle: .caption1).pointSize, weight: .regular)
140 typealias PlatformColor = NSColor
141 #endif
142 let paragraph = NSMutableParagraphStyle()
143 paragraph.lineBreakMode = .byCharWrapping
144 #if os(macOS)
145 // Continuations belong to their original diff line, including its +/-.
146 // This is layout only: selected text and whole-file Copy stay identical.
147 paragraph.firstLineHeadIndent = 0
148 paragraph.headIndent = font.pointSize * 3
149 #endif
150 // Append actual separators only where they exist, so whole-file Copy is
151 // byte-identical to the displayed text, including the final newline.
152 let lines = diff.lines
153 for (index, line) in lines.enumerated() {
154 let color = line.kind == .added ? Palette.positive : line.kind == .removed ? Palette.negative : Palette.text
155 for segment in line.displaySegments() {
156 var attributes: [NSAttributedString.Key: Any] = [.font: font, .paragraphStyle: paragraph,
157 .foregroundColor: PlatformColor(segment.marker ? Palette.warning : color)]
158 if segment.marker { attributes[.backgroundColor] = PlatformColor(Palette.warningBackground) }
159 text.append(NSAttributedString(string: segment.text, attributes: attributes))
160 }
161 if index < lines.count - 1 || diff.patch?.unicodeScalars.last == "\n" {
162 text.append(NSAttributedString(string: "\n", attributes: [.font: font, .paragraphStyle: paragraph]))
163 }
164 }
165 return text
166 }
167}
168