1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
import Splash
import SwiftUI
import UIKit
private typealias ViewColor = SwiftUI.Color
struct FileTreeView: View {
let repository: RepositorySummary
let client: SRHTClient
@State private var viewModel: FileTreeViewModel?
var body: some View {
Group {
if let viewModel {
FileTreeContentView(viewModel: viewModel)
} else {
SRHTLoadingStateView(message: "Loading files…")
}
}
.task {
if viewModel == nil {
let vm = FileTreeViewModel(
repositoryRid: repository.rid,
service: repository.service,
client: client
)
viewModel = vm
async let loadTree: () = vm.loadRootTree()
async let loadRefs: () = vm.loadReferences()
_ = await (loadTree, loadRefs)
}
}
}
}
// MARK: - Content View
private struct FileTreeContentView: View {
let viewModel: FileTreeViewModel
@AppStorage(AppStorageKeys.wrapRepositoryFileLines) private var wrapRepositoryFileLines = false
@State private var showRefPicker = false
@State private var showFileShareSheet = false
@State private var showShareUnavailableAlert = false
@State private var didCopyFileContents = false
@State private var copyResetTask: Task<Void, Never>?
var body: some View {
VStack(spacing: 0) {
breadcrumbBar
Divider()
contentArea
}
.toolbar {
ToolbarItem(placement: .topBarTrailing) {
Button {
showRefPicker = true
} label: {
Label(
revspecLabel,
systemImage: "arrow.triangle.branch"
)
.font(.subheadline)
}
}
}
.sheet(isPresented: $showRefPicker) {
RefPickerSheet(viewModel: viewModel, isPresented: $showRefPicker)
}
.srhtErrorBanner(error: Binding(
get: { viewModel.error },
set: { viewModel.error = $0 }
))
.onChange(of: viewModel.viewingEntry?.name) { _, _ in
resetCopyConfirmation()
}
}
private var revspecLabel: String {
let revspec = viewModel.revspec
if revspec == "HEAD" {
return "HEAD"
}
if revspec.hasPrefix("refs/heads/") {
return String(revspec.dropFirst("refs/heads/".count))
} else if revspec.hasPrefix("refs/tags/") {
return String(revspec.dropFirst("refs/tags/".count))
}
return revspec
}
// MARK: - Breadcrumb Bar
private var breadcrumbBar: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 12) {
HStack(spacing: 4) {
ForEach(Array(viewModel.navStack.enumerated()), id: \.offset) { index, navEntry in
if index > 0 {
Image(systemName: "chevron.right")
.font(.caption2)
.foregroundStyle(.tertiary)
}
Button {
Task {
await viewModel.navigateToBreadcrumb(at: index)
}
} label: {
Text(navEntry.name)
.font(.subheadline.monospaced())
.foregroundStyle(
index == viewModel.navStack.count - 1 && viewModel.viewingEntry == nil
? .primary : .secondary
)
}
.buttonStyle(.plain)
}
if let viewing = viewModel.viewingEntry {
Image(systemName: "chevron.right")
.font(.caption2)
.foregroundStyle(.tertiary)
Text(viewing.name)
.font(.subheadline.monospaced())
.foregroundStyle(.primary)
}
}
Spacer(minLength: 0)
}
.padding(.horizontal)
.padding(.vertical, 8)
}
.background(.bar)
}
// MARK: - Content Area
@ViewBuilder
private var contentArea: some View {
if viewModel.isLoading, viewModel.entries.isEmpty, viewModel.viewingEntry == nil {
SRHTLoadingStateView(message: "Loading files…")
} else if let entry = viewModel.viewingEntry, let object = viewModel.viewingObject {
// Viewing a file
fileContentView(entry: entry, object: object)
} else if let error = viewModel.error, viewModel.entries.isEmpty {
SRHTErrorStateView(
title: "Couldn't Load Files",
message: error,
retryAction: { await viewModel.loadRootTree() }
)
} else if !viewModel.entries.isEmpty {
// Viewing a directory listing
treeListView
} else if viewModel.navStack.isEmpty {
ContentUnavailableView(
"No Files",
systemImage: "folder",
description: Text("This repository could not be loaded.")
)
} else {
ContentUnavailableView(
"Empty Directory",
systemImage: "folder",
description: Text("This directory has no files.")
)
}
}
private func shareFileContents(_ text: String) {
if text.isEmpty {
showShareUnavailableAlert = true
} else {
showFileShareSheet = true
}
}
private func copyFileContents(_ text: String) {
UIPasteboard.general.string = text
didCopyFileContents = true
copyResetTask?.cancel()
copyResetTask = Task {
try? await Task.sleep(for: .seconds(2))
guard !Task.isCancelled else { return }
await MainActor.run {
didCopyFileContents = false
}
}
}
private func resetCopyConfirmation() {
copyResetTask?.cancel()
copyResetTask = nil
didCopyFileContents = false
}
// MARK: - File Content View
@ViewBuilder
private func fileContentView(entry: TreeEntry, object: GitObject) -> some View {
switch object {
case .textBlob(let blob):
textBlobView(entry, blob: blob)
case .binaryBlob(let blob):
binaryBlobView(entry: entry, blob: blob)
default:
ContentUnavailableView(
"Unknown Object",
systemImage: "questionmark.folder",
description: Text("Cannot display this object type.")
)
}
}
// MARK: - Tree List
private var treeListView: some View {
let sorted = viewModel.entries.sorted { a, b in
let aIsTree = a.object?.isTree == true
let bIsTree = b.object?.isTree == true
if aIsTree != bIsTree { return aIsTree }
return a.name.localizedCaseInsensitiveCompare(b.name) == .orderedAscending
}
return List(sorted) { entry in
TreeEntryRow(entry: entry)
.contentShape(Rectangle())
.onTapGesture {
Task {
await viewModel.navigateInto(entry: entry)
}
}
}
.listStyle(.plain)
.refreshable {
await viewModel.loadRootTree()
}
}
// MARK: - Text Blob
@ViewBuilder
private func textBlobView(_ entry: TreeEntry, blob: GitTextBlob) -> some View {
let text = blob.text ?? ""
VStack(spacing: 0) {
CodeFileTextView(
text: text,
fileName: entry.name,
wrapLines: wrapRepositoryFileLines
)
}
.safeAreaInset(edge: .bottom, spacing: 0) {
fileActionToolbar(text: text)
}
.sheet(isPresented: $showFileShareSheet) {
FileContentShareSheet(activityItems: [text])
}
.alert("Share Unavailable", isPresented: $showShareUnavailableAlert) {
Button("OK", role: .cancel) {}
} message: {
Text(SRHTShareTarget.file.fallbackMessage)
}
}
// MARK: - Binary Blob
@ViewBuilder
private func binaryBlobView(entry: TreeEntry, blob: GitBinaryBlob) -> some View {
VStack(spacing: 16) {
Spacer()
Image(systemName: "doc.zipper")
.font(.system(size: 48))
.foregroundStyle(.secondary)
Text(entry.name)
.font(.headline)
if let size = blob.size {
Text(formatBytes(size))
.font(.subheadline)
.foregroundStyle(.secondary)
}
Text("Binary file — cannot be displayed inline.")
.font(.subheadline)
.foregroundStyle(.tertiary)
if let content = blob.content, let url = URL(string: content) {
Link(destination: url) {
Label("Open in Safari", systemImage: "safari")
}
.buttonStyle(.borderedProminent)
}
Button {
viewModel.dismissFileView()
} label: {
Text("Back to directory")
}
Spacer()
}
.frame(maxWidth: .infinity)
}
// MARK: - Helpers
private func formatBytes(_ bytes: Int) -> String {
let formatter = ByteCountFormatter()
formatter.countStyle = .file
return formatter.string(fromByteCount: Int64(bytes))
}
private func fileActionToolbar(text: String) -> some View {
HStack(spacing: 0) {
toolbarButton(
title: "Share",
systemImage: "square.and.arrow.up"
) {
shareFileContents(text)
}
toolbarButton(
title: didCopyFileContents ? "Copied" : "Copy All",
systemImage: didCopyFileContents ? "checkmark" : "doc.on.doc"
) {
copyFileContents(text)
}
toolbarButton(
title: wrapRepositoryFileLines ? "Wrap On" : "Wrap Off",
systemImage: "text.word.spacing"
) {
wrapRepositoryFileLines.toggle()
}
}
.padding(.horizontal, 8)
.padding(.top, 10)
.padding(.bottom, 8)
.background(.bar)
.overlay(alignment: .top) {
Divider()
}
}
private func toolbarButton(
title: String,
systemImage: String,
action: @escaping () -> Void
) -> some View {
Button(action: action) {
VStack(spacing: 4) {
Image(systemName: systemImage)
.font(.system(size: 17, weight: .semibold))
Text(title)
.font(.caption2)
.lineLimit(1)
}
.frame(maxWidth: .infinity)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.foregroundStyle(.primary)
}
}
private struct CodeFileTextView: UIViewRepresentable {
let text: String
let fileName: String
let wrapLines: Bool
private let font = UIFont(name: "SFMono-Regular", size: 12)
?? UIFont.monospacedSystemFont(ofSize: 12, weight: .regular)
func makeUIView(context _: Context) -> CodeFileUIView {
CodeFileUIView(
text: text,
fileName: fileName,
font: font,
wrapLines: wrapLines
)
}
func updateUIView(_ uiView: CodeFileUIView, context _: Context) {
uiView.updateContent(
text: text,
fileName: fileName,
font: font,
wrapLines: wrapLines
)
}
}
private struct FileContentShareSheet: UIViewControllerRepresentable {
let activityItems: [Any]
func makeUIViewController(context _: Context) -> UIActivityViewController {
UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
}
func updateUIViewController(_ : UIActivityViewController, context _: Context) {}
}
private final class CodeFileUIView: UIView {
private enum Layout {
static let verticalPadding: CGFloat = 12
static let gutterLeadingPadding: CGFloat = 12
static let gutterTrailingPadding: CGFloat = 8
}
private final class LineRow {
let gutterRow = UIView()
let gutterLabel = UILabel()
let codeRow = UIView()
let codeLabel = UILabel()
let gutterHeightConstraint: NSLayoutConstraint
let codeHeightConstraint: NSLayoutConstraint
init() {
gutterRow.translatesAutoresizingMaskIntoConstraints = false
gutterLabel.translatesAutoresizingMaskIntoConstraints = false
codeRow.translatesAutoresizingMaskIntoConstraints = false
codeLabel.translatesAutoresizingMaskIntoConstraints = false
gutterHeightConstraint = gutterRow.heightAnchor.constraint(equalToConstant: 0)
codeHeightConstraint = codeRow.heightAnchor.constraint(equalToConstant: 0)
}
}
private let outerScrollView = UIScrollView()
private let contentView = UIView()
private let gutterContainerView = UIView()
private let gutterStackView = UIStackView()
private let horizontalScrollView = UIScrollView()
private let codeContainerView = UIView()
private let codeStackView = UIStackView()
private var codeContainerWidthConstraint: NSLayoutConstraint?
private var codeContainerExplicitWidthConstraint: NSLayoutConstraint?
private var gutterWidthConstraint: NSLayoutConstraint?
private var rows: [LineRow] = []
private var currentText = ""
private var currentFileName = ""
private var currentFont: UIFont
private var wrapLines: Bool
private var needsLineLayoutUpdate = true
private var lastMeasuredCodeWidth: CGFloat = 0
init(text: String, fileName: String, font: UIFont, wrapLines: Bool) {
self.currentFont = font
self.wrapLines = wrapLines
super.init(frame: .zero)
setupViews()
updateContent(text: text, fileName: fileName, font: font, wrapLines: wrapLines)
}
@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
updateLineLayoutsIfNeeded()
}
func updateContent(text: String, fileName: String, font: UIFont, wrapLines: Bool) {
let contentChanged = text != currentText || fileName != currentFileName || font != currentFont
let wrapChanged = wrapLines != self.wrapLines
currentText = text
currentFileName = fileName
currentFont = font
self.wrapLines = wrapLines
if contentChanged {
rebuildRows()
}
if contentChanged || wrapChanged {
updateWrapConfiguration(resetHorizontalOffset: wrapChanged)
needsLineLayoutUpdate = true
setNeedsLayout()
layoutIfNeeded()
}
}
private func setupViews() {
backgroundColor = .systemBackground
outerScrollView.translatesAutoresizingMaskIntoConstraints = false
contentView.translatesAutoresizingMaskIntoConstraints = false
gutterContainerView.translatesAutoresizingMaskIntoConstraints = false
gutterStackView.translatesAutoresizingMaskIntoConstraints = false
horizontalScrollView.translatesAutoresizingMaskIntoConstraints = false
codeContainerView.translatesAutoresizingMaskIntoConstraints = false
codeStackView.translatesAutoresizingMaskIntoConstraints = false
gutterStackView.axis = .vertical
gutterStackView.alignment = .fill
gutterStackView.distribution = .fill
gutterStackView.spacing = 0
codeStackView.axis = .vertical
codeStackView.alignment = .fill
codeStackView.distribution = .fill
codeStackView.spacing = 0
outerScrollView.alwaysBounceVertical = true
horizontalScrollView.alwaysBounceVertical = false
horizontalScrollView.showsVerticalScrollIndicator = false
addSubview(outerScrollView)
outerScrollView.addSubview(contentView)
contentView.addSubview(gutterContainerView)
contentView.addSubview(horizontalScrollView)
gutterContainerView.addSubview(gutterStackView)
horizontalScrollView.addSubview(codeContainerView)
codeContainerView.addSubview(codeStackView)
codeContainerWidthConstraint = codeContainerView.widthAnchor.constraint(equalTo: horizontalScrollView.frameLayoutGuide.widthAnchor)
codeContainerExplicitWidthConstraint = codeContainerView.widthAnchor.constraint(equalToConstant: 0)
gutterWidthConstraint = gutterContainerView.widthAnchor.constraint(equalToConstant: 0)
NSLayoutConstraint.activate([
outerScrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
outerScrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
outerScrollView.topAnchor.constraint(equalTo: topAnchor),
outerScrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
contentView.leadingAnchor.constraint(equalTo: outerScrollView.contentLayoutGuide.leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: outerScrollView.contentLayoutGuide.trailingAnchor),
contentView.topAnchor.constraint(equalTo: outerScrollView.contentLayoutGuide.topAnchor),
contentView.bottomAnchor.constraint(equalTo: outerScrollView.contentLayoutGuide.bottomAnchor),
contentView.widthAnchor.constraint(equalTo: outerScrollView.frameLayoutGuide.widthAnchor),
gutterContainerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
gutterContainerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: Layout.verticalPadding),
gutterContainerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -Layout.verticalPadding),
horizontalScrollView.leadingAnchor.constraint(equalTo: gutterContainerView.trailingAnchor),
horizontalScrollView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
horizontalScrollView.topAnchor.constraint(equalTo: gutterContainerView.topAnchor),
horizontalScrollView.bottomAnchor.constraint(equalTo: gutterContainerView.bottomAnchor),
gutterStackView.leadingAnchor.constraint(equalTo: gutterContainerView.leadingAnchor, constant: Layout.gutterLeadingPadding),
gutterStackView.trailingAnchor.constraint(equalTo: gutterContainerView.trailingAnchor, constant: -Layout.gutterTrailingPadding),
gutterStackView.topAnchor.constraint(equalTo: gutterContainerView.topAnchor),
gutterStackView.bottomAnchor.constraint(equalTo: gutterContainerView.bottomAnchor),
codeContainerView.leadingAnchor.constraint(equalTo: horizontalScrollView.contentLayoutGuide.leadingAnchor),
codeContainerView.trailingAnchor.constraint(equalTo: horizontalScrollView.contentLayoutGuide.trailingAnchor),
codeContainerView.topAnchor.constraint(equalTo: horizontalScrollView.contentLayoutGuide.topAnchor),
codeContainerView.bottomAnchor.constraint(equalTo: horizontalScrollView.contentLayoutGuide.bottomAnchor),
codeContainerView.heightAnchor.constraint(equalTo: horizontalScrollView.frameLayoutGuide.heightAnchor),
codeStackView.leadingAnchor.constraint(equalTo: codeContainerView.leadingAnchor),
codeStackView.trailingAnchor.constraint(equalTo: codeContainerView.trailingAnchor),
codeStackView.topAnchor.constraint(equalTo: codeContainerView.topAnchor),
codeStackView.bottomAnchor.constraint(equalTo: codeContainerView.bottomAnchor)
])
gutterWidthConstraint?.isActive = true
}
private func rebuildRows() {
rows.forEach { row in
gutterStackView.removeArrangedSubview(row.gutterRow)
row.gutterRow.removeFromSuperview()
codeStackView.removeArrangedSubview(row.codeRow)
row.codeRow.removeFromSuperview()
}
rows.removeAll()
let attributedText = CodeSyntaxHighlighter.attributedText(
for: currentText,
fileName: currentFileName,
font: currentFont
)
let lines = makeLines(from: attributedText, font: currentFont)
gutterWidthConstraint?.constant = Self.gutterWidth(lineCount: lines.count, font: currentFont)
for line in lines {
let row = makeRow(for: line)
rows.append(row)
gutterStackView.addArrangedSubview(row.gutterRow)
codeStackView.addArrangedSubview(row.codeRow)
}
}
private func makeRow(for line: CodeFileLineData) -> LineRow {
let row = LineRow()
row.gutterLabel.font = currentFont
row.gutterLabel.textColor = .secondaryLabel
row.gutterLabel.textAlignment = .right
row.gutterLabel.text = line.number
row.codeLabel.attributedText = line.text
row.codeLabel.font = currentFont
row.gutterRow.addSubview(row.gutterLabel)
row.codeRow.addSubview(row.codeLabel)
NSLayoutConstraint.activate([
row.gutterHeightConstraint,
row.codeHeightConstraint,
row.gutterLabel.leadingAnchor.constraint(equalTo: row.gutterRow.leadingAnchor),
row.gutterLabel.trailingAnchor.constraint(equalTo: row.gutterRow.trailingAnchor),
row.gutterLabel.topAnchor.constraint(equalTo: row.gutterRow.topAnchor),
row.gutterLabel.bottomAnchor.constraint(lessThanOrEqualTo: row.gutterRow.bottomAnchor),
row.codeLabel.leadingAnchor.constraint(equalTo: row.codeRow.leadingAnchor),
row.codeLabel.trailingAnchor.constraint(equalTo: row.codeRow.trailingAnchor),
row.codeLabel.topAnchor.constraint(equalTo: row.codeRow.topAnchor),
row.codeLabel.bottomAnchor.constraint(lessThanOrEqualTo: row.codeRow.bottomAnchor)
])
return row
}
private func updateWrapConfiguration(resetHorizontalOffset: Bool) {
horizontalScrollView.alwaysBounceHorizontal = !wrapLines
horizontalScrollView.isScrollEnabled = !wrapLines
if wrapLines {
codeContainerWidthConstraint?.isActive = true
codeContainerExplicitWidthConstraint?.isActive = false
} else {
codeContainerWidthConstraint?.isActive = false
codeContainerExplicitWidthConstraint?.isActive = true
}
for row in rows {
row.codeLabel.numberOfLines = wrapLines ? 0 : 1
row.codeLabel.lineBreakMode = wrapLines ? .byWordWrapping : .byClipping
}
if resetHorizontalOffset {
horizontalScrollView.setContentOffset(.zero, animated: false)
}
}
private func updateLineLayoutsIfNeeded() {
let availableWidth = max(horizontalScrollView.bounds.width, 0)
let shouldUpdateForWidth = wrapLines && abs(availableWidth - lastMeasuredCodeWidth) > 0.5
guard needsLineLayoutUpdate || shouldUpdateForWidth else { return }
lastMeasuredCodeWidth = availableWidth
let measurementWidth = wrapLines ? max(availableWidth, 1) : CGFloat.greatestFiniteMagnitude
for row in rows {
row.codeLabel.preferredMaxLayoutWidth = wrapLines ? measurementWidth : 0
let measuredSize = row.codeLabel.sizeThatFits(
CGSize(width: measurementWidth, height: CGFloat.greatestFiniteMagnitude)
)
let rowHeight = max(ceil(measuredSize.height), ceil(currentFont.lineHeight))
row.gutterHeightConstraint.constant = rowHeight
row.codeHeightConstraint.constant = rowHeight
}
if wrapLines {
codeContainerExplicitWidthConstraint?.constant = 0
} else {
let maxLineWidth = rows.reduce(CGFloat(0)) { partialResult, row in
let measuredWidth = row.codeLabel.sizeThatFits(
CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
).width
return max(partialResult, ceil(measuredWidth))
}
codeContainerExplicitWidthConstraint?.constant = max(maxLineWidth, availableWidth)
}
needsLineLayoutUpdate = false
}
private func makeLines(from attributedText: NSAttributedString, font: UIFont) -> [CodeFileLineData] {
let string = attributedText.string as NSString
var lines: [CodeFileLineData] = []
var currentLocation = 0
var lineNumber = 1
while currentLocation < attributedText.length {
let searchRange = NSRange(location: currentLocation, length: attributedText.length - currentLocation)
let newlineRange = string.range(of: "\n", options: [], range: searchRange)
let lineRange: NSRange
if newlineRange.location == NSNotFound {
lineRange = searchRange
currentLocation = attributedText.length
} else {
lineRange = NSRange(location: currentLocation, length: newlineRange.location - currentLocation)
currentLocation = newlineRange.location + newlineRange.length
}
lines.append(CodeFileLineData(
number: String(lineNumber),
text: attributedText.attributedSubstring(from: lineRange)
))
lineNumber += 1
}
if attributedText.length == 0 || string.hasSuffix("\n") {
lines.append(CodeFileLineData(
number: String(lineNumber),
text: NSAttributedString(string: "", attributes: [.font: font])
))
}
return lines
}
private static func gutterWidth(lineCount: Int, font: UIFont) -> CGFloat {
let digits = String(max(lineCount, 1)).count
let sample = String(repeating: "8", count: digits)
let numberWidth = ceil((sample as NSString).size(withAttributes: [.font: font]).width)
return Layout.gutterLeadingPadding + numberWidth + Layout.gutterTrailingPadding
}
}
private struct CodeFileLineData {
let number: String
let text: NSAttributedString
}
private enum CodeSyntaxHighlighter {
static func attributedText(for text: String, fileName: String, font: UIFont) -> NSAttributedString {
guard supportsSplashHighlighting(fileName: fileName) else {
return plainText(text, font: font)
}
var splashFont = Font(size: Double(font.pointSize))
splashFont.resource = .preloaded(font)
let theme = Theme(
font: splashFont,
plainTextColor: .label,
tokenColors: [
.keyword: .systemPink,
.string: .systemRed,
.type: .systemTeal,
.call: .systemBlue,
.number: .systemPurple,
.comment: .secondaryLabel,
.property: .systemGreen,
.dotAccess: .systemIndigo,
.preprocessing: .systemOrange
],
backgroundColor: .clear
)
let highlighted = SyntaxHighlighter(
format: AttributedStringOutputFormat(theme: theme)
).highlight(text)
return NSMutableAttributedString(attributedString: highlighted)
}
private static func plainText(_ text: String, font: UIFont) -> NSAttributedString {
NSAttributedString(
string: text,
attributes: [
.font: font,
.foregroundColor: UIColor.label
]
)
}
private static func supportsSplashHighlighting(fileName: String) -> Bool {
let supportedExtensions: Set<String> = [
"swift",
"swiftinterface",
"playground"
]
return supportedExtensions.contains(
(fileName as NSString).pathExtension.lowercased()
)
}
}
// MARK: - Tree Entry Row
private struct TreeEntryRow: View {
let entry: TreeEntry
var body: some View {
Label {
Text(entry.name)
.font(.body.monospaced())
.lineLimit(1)
} icon: {
Image(systemName: iconName)
.foregroundStyle(iconColor)
}
}
private var iconName: String {
switch entry.object {
case .tree: "folder.fill"
case .unknown: "questionmark.circle"
default: "doc"
}
}
private var iconColor: ViewColor {
switch entry.object {
case .tree: .blue
case .unknown: .orange
default: .secondary
}
}
}
// MARK: - Ref Picker Sheet
private struct RefPickerSheet: View {
let viewModel: FileTreeViewModel
@Binding var isPresented: Bool
var body: some View {
NavigationStack {
List {
Section {
Button {
Task {
await viewModel.changeRevspec("HEAD")
isPresented = false
}
} label: {
refRow(
title: "HEAD",
systemImage: "arrow.triangle.branch",
color: .blue,
isSelected: viewModel.revspec == "HEAD"
)
}
.buttonStyle(.plain)
}
if !viewModel.branches.isEmpty {
Section("Branches") {
ForEach(viewModel.branches, id: \.name) { ref in
Button {
Task {
await viewModel.changeRevspec(ref.name)
isPresented = false
}
} label: {
refRow(
title: ref.name.replacingOccurrences(of: "refs/heads/", with: ""),
systemImage: "arrow.triangle.branch",
color: .blue,
isSelected: viewModel.revspec == ref.name
)
}
.buttonStyle(.plain)
}
}
}
if !viewModel.tags.isEmpty {
Section("Tags") {
ForEach(viewModel.tags, id: \.name) { ref in
Button {
Task {
await viewModel.changeRevspec(ref.name)
isPresented = false
}
} label: {
refRow(
title: ref.name.replacingOccurrences(of: "refs/tags/", with: ""),
systemImage: "tag",
color: .orange,
isSelected: viewModel.revspec == ref.name
)
}
.buttonStyle(.plain)
}
}
}
}
.listStyle(.insetGrouped)
.navigationTitle("Select Ref")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
isPresented = false
}
}
}
.overlay {
if viewModel.isLoadingRefs {
SRHTLoadingStateView(message: "Loading references…")
}
}
}
}
private func refRow(title: String, systemImage: String, color: ViewColor, isSelected: Bool) -> some View {
HStack(spacing: 12) {
Image(systemName: systemImage)
.foregroundStyle(color)
Text(title)
.font(.body.monospaced())
.foregroundStyle(.primary)
Spacer()
if isSelected {
Image(systemName: "checkmark")
.font(.caption.weight(.semibold))
.foregroundStyle(.tint)
}
}
.contentShape(Rectangle())
}
}
|