-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathFileExplorerView.swift
More file actions
668 lines (560 loc) · 26.1 KB
/
FileExplorerView.swift
File metadata and controls
668 lines (560 loc) · 26.1 KB
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
import AppKit
import Bonsplit
import Combine
import SwiftUI
// MARK: - File Explorer Panel (single NSViewRepresentable)
/// The entire file explorer panel as one AppKit view hierarchy.
/// Contains the header bar (path + controls) and NSOutlineView, with no SwiftUI intermediaries.
struct FileExplorerPanelView: NSViewRepresentable {
@ObservedObject var store: FileExplorerStore
@ObservedObject var state: FileExplorerState
func makeCoordinator() -> Coordinator {
Coordinator(store: store, state: state)
}
func makeNSView(context: Context) -> FileExplorerContainerView {
let container = FileExplorerContainerView(coordinator: context.coordinator)
context.coordinator.containerView = container
return container
}
func updateNSView(_ container: FileExplorerContainerView, context: Context) {
context.coordinator.store = store
context.coordinator.state = state
container.updateHeader(store: store)
context.coordinator.reloadIfNeeded()
}
// MARK: - Coordinator
final class Coordinator: NSObject, NSOutlineViewDataSource, NSOutlineViewDelegate, NSMenuDelegate {
var store: FileExplorerStore
var state: FileExplorerState
weak var containerView: FileExplorerContainerView?
weak var outlineView: NSOutlineView?
private var lastRootNodeCount: Int = -1
private var observationCancellable: AnyCancellable?
private var styleObserver: Any?
init(store: FileExplorerStore, state: FileExplorerState) {
self.store = store
self.state = state
super.init()
observeStore()
styleObserver = NotificationCenter.default.addObserver(
forName: .fileExplorerStyleDidChange, object: nil, queue: .main
) { [weak self] _ in
guard let self, let outlineView = self.outlineView else { return }
let style = FileExplorerStyle.current
outlineView.indentationPerLevel = style.indentation
outlineView.noteHeightOfRows(withIndexesChanged: IndexSet(0..<outlineView.numberOfRows))
outlineView.reloadData()
self.restoreExpansionState(self.store.expandedPaths, in: outlineView)
}
}
deinit {
if let observer = styleObserver {
NotificationCenter.default.removeObserver(observer)
}
}
private func observeStore() {
observationCancellable = store.objectWillChange
.debounce(for: .milliseconds(50), scheduler: RunLoop.main)
.sink { [weak self] _ in
self?.reloadIfNeeded()
}
}
func reloadIfNeeded() {
guard let outlineView else { return }
// Update empty state vs tree visibility
containerView?.updateVisibility(hasContent: !store.rootPath.isEmpty, isLoading: store.isRootLoading)
let newCount = store.rootNodes.count
if newCount != lastRootNodeCount {
lastRootNodeCount = newCount
let expandedPaths = store.expandedPaths
outlineView.reloadData()
restoreExpansionState(expandedPaths, in: outlineView)
} else {
refreshLoadedNodes(in: outlineView)
}
}
private func restoreExpansionState(_ expandedPaths: Set<String>, in outlineView: NSOutlineView) {
for row in 0..<outlineView.numberOfRows {
guard let node = outlineView.item(atRow: row) as? FileExplorerNode else { continue }
if expandedPaths.contains(node.path) && outlineView.isExpandable(node) {
outlineView.expandItem(node)
}
}
}
private func refreshLoadedNodes(in outlineView: NSOutlineView) {
for row in 0..<outlineView.numberOfRows {
guard let node = outlineView.item(atRow: row) as? FileExplorerNode else { continue }
if node.isDirectory {
let isCurrentlyExpanded = outlineView.isItemExpanded(node)
let shouldBeExpanded = store.expandedPaths.contains(node.path)
if shouldBeExpanded && !isCurrentlyExpanded && node.children != nil {
outlineView.reloadItem(node, reloadChildren: true)
outlineView.expandItem(node)
} else if !shouldBeExpanded && isCurrentlyExpanded {
outlineView.collapseItem(node)
} else if node.children != nil {
outlineView.reloadItem(node, reloadChildren: true)
if shouldBeExpanded {
outlineView.expandItem(node)
}
}
}
}
}
// MARK: - NSOutlineViewDataSource
func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
if item == nil {
return store.rootNodes.count
}
guard let node = item as? FileExplorerNode else { return 0 }
return node.sortedChildren?.count ?? 0
}
func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
if item == nil {
return store.rootNodes[index]
}
guard let node = item as? FileExplorerNode,
let children = node.sortedChildren else {
return FileExplorerNode(name: "", path: "", isDirectory: false)
}
return children[index]
}
func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
guard let node = item as? FileExplorerNode else { return false }
return node.isExpandable
}
// MARK: - NSOutlineViewDelegate
func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? {
guard let node = item as? FileExplorerNode else { return nil }
let identifier = NSUserInterfaceItemIdentifier("FileExplorerCell")
let cellView: FileExplorerCellView
if let existing = outlineView.makeView(withIdentifier: identifier, owner: nil) as? FileExplorerCellView {
cellView = existing
} else {
cellView = FileExplorerCellView(identifier: identifier)
}
let gitStatus = store.gitStatusByPath[node.path]
cellView.configure(with: node, gitStatus: gitStatus)
cellView.onHover = { [weak self] isHovering in
guard let self else { return }
if isHovering {
self.store.prefetchChildren(for: node)
} else {
self.store.cancelPrefetch(for: node)
}
}
return cellView
}
func outlineView(_ outlineView: NSOutlineView, shouldExpandItem item: Any) -> Bool {
guard let node = item as? FileExplorerNode, node.isDirectory else { return false }
store.expand(node: node)
return node.children != nil
}
func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool {
guard let node = item as? FileExplorerNode else { return false }
store.collapse(node: node)
return true
}
func outlineViewItemDidExpand(_ notification: Notification) {
guard let node = notification.userInfo?["NSObject"] as? FileExplorerNode else { return }
if !store.isExpanded(node) {
store.expand(node: node)
}
}
func outlineViewItemDidCollapse(_ notification: Notification) {
guard let node = notification.userInfo?["NSObject"] as? FileExplorerNode else { return }
if store.isExpanded(node) {
store.collapse(node: node)
}
}
func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
FileExplorerRowView()
}
func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
FileExplorerStyle.current.rowHeight
}
// MARK: - Drag-to-Preview
func outlineView(_ outlineView: NSOutlineView, pasteboardWriterForItem item: Any) -> (any NSPasteboardWriting)? {
guard let node = item as? FileExplorerNode, !node.isDirectory else { return nil }
guard store.provider is LocalFileExplorerProvider else { return nil }
return FilePreviewDragPasteboardWriter(filePath: node.path, displayTitle: node.name)
}
// MARK: - Context Menu (NSMenuDelegate)
func menuNeedsUpdate(_ menu: NSMenu) {
menu.removeAllItems()
guard let outlineView else { return }
let clickedRow = outlineView.clickedRow
guard clickedRow >= 0,
let node = outlineView.item(atRow: clickedRow) as? FileExplorerNode else { return }
let isLocal = store.provider is LocalFileExplorerProvider
if !node.isDirectory && isLocal {
let openItem = NSMenuItem(
title: String(localized: "fileExplorer.contextMenu.openDefault", defaultValue: "Open in Default Editor"),
action: #selector(contextMenuOpenInDefaultEditor(_:)),
keyEquivalent: ""
)
openItem.target = self
openItem.representedObject = node
menu.addItem(openItem)
}
if isLocal {
let revealItem = NSMenuItem(
title: String(localized: "fileExplorer.contextMenu.revealInFinder", defaultValue: "Reveal in Finder"),
action: #selector(contextMenuRevealInFinder(_:)),
keyEquivalent: ""
)
revealItem.target = self
revealItem.representedObject = node
menu.addItem(revealItem)
menu.addItem(.separator())
}
let copyPathItem = NSMenuItem(
title: String(localized: "fileExplorer.contextMenu.copyPath", defaultValue: "Copy Path"),
action: #selector(contextMenuCopyPath(_:)),
keyEquivalent: ""
)
copyPathItem.target = self
copyPathItem.representedObject = node
menu.addItem(copyPathItem)
let copyRelItem = NSMenuItem(
title: String(localized: "fileExplorer.contextMenu.copyRelativePath", defaultValue: "Copy Relative Path"),
action: #selector(contextMenuCopyRelativePath(_:)),
keyEquivalent: ""
)
copyRelItem.target = self
copyRelItem.representedObject = node
menu.addItem(copyRelItem)
}
@objc private func contextMenuOpenInDefaultEditor(_ sender: NSMenuItem) {
guard let node = sender.representedObject as? FileExplorerNode else { return }
NSWorkspace.shared.open(URL(fileURLWithPath: node.path))
}
@objc private func contextMenuRevealInFinder(_ sender: NSMenuItem) {
guard let node = sender.representedObject as? FileExplorerNode else { return }
NSWorkspace.shared.selectFile(node.path, inFileViewerRootedAtPath: "")
}
@objc private func contextMenuCopyPath(_ sender: NSMenuItem) {
guard let node = sender.representedObject as? FileExplorerNode else { return }
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(node.path, forType: .string)
}
@objc private func contextMenuCopyRelativePath(_ sender: NSMenuItem) {
guard let node = sender.representedObject as? FileExplorerNode else { return }
let rootPath = store.rootPath
var relativePath = node.path
if relativePath.hasPrefix(rootPath) {
relativePath = String(relativePath.dropFirst(rootPath.count))
if relativePath.hasPrefix("/") {
relativePath = String(relativePath.dropFirst())
}
}
NSPasteboard.general.clearContents()
NSPasteboard.general.setString(relativePath, forType: .string)
}
}
}
// MARK: - Container View (all-AppKit)
/// Pure AppKit container holding the header bar and outline view.
final class FileExplorerContainerView: NSView {
private let headerView: FileExplorerHeaderView
private let scrollView: NSScrollView
private let outlineView: FileExplorerNSOutlineView
private let emptyLabel: NSTextField
private let loadingIndicator: NSProgressIndicator
init(coordinator: FileExplorerPanelView.Coordinator) {
headerView = FileExplorerHeaderView()
scrollView = NSScrollView()
outlineView = FileExplorerNSOutlineView()
emptyLabel = NSTextField(labelWithString: String(localized: "fileExplorer.empty", defaultValue: "No folder open"))
loadingIndicator = NSProgressIndicator()
super.init(frame: .zero)
// Header
headerView.translatesAutoresizingMaskIntoConstraints = false
addSubview(headerView)
// Empty state label
emptyLabel.translatesAutoresizingMaskIntoConstraints = false
emptyLabel.font = .systemFont(ofSize: 13)
emptyLabel.textColor = .secondaryLabelColor
emptyLabel.alignment = .center
emptyLabel.isHidden = true
addSubview(emptyLabel)
// Loading indicator
loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
loadingIndicator.style = .spinning
loadingIndicator.controlSize = .small
loadingIndicator.isHidden = true
addSubview(loadingIndicator)
// Outline view setup
outlineView.headerView = nil
outlineView.usesAlternatingRowBackgroundColors = false
outlineView.style = .plain
outlineView.selectionHighlightStyle = .regular
outlineView.rowSizeStyle = .default
outlineView.indentationPerLevel = FileExplorerStyle.current.indentation
outlineView.autoresizesOutlineColumn = true
outlineView.floatsGroupRows = false
outlineView.backgroundColor = .clear
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("name"))
column.isEditable = false
column.resizingMask = .autoresizingMask
outlineView.addTableColumn(column)
outlineView.outlineTableColumn = column
outlineView.dataSource = coordinator
outlineView.delegate = coordinator
coordinator.outlineView = outlineView
// Context menu
let menu = NSMenu()
menu.delegate = coordinator
outlineView.menu = menu
// Scroll view
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = false
scrollView.autohidesScrollers = true
scrollView.borderType = .noBorder
scrollView.drawsBackground = false
scrollView.documentView = outlineView
addSubview(scrollView)
NSLayoutConstraint.activate([
headerView.topAnchor.constraint(equalTo: topAnchor),
headerView.leadingAnchor.constraint(equalTo: leadingAnchor),
headerView.trailingAnchor.constraint(equalTo: trailingAnchor),
scrollView.topAnchor.constraint(equalTo: headerView.bottomAnchor),
scrollView.leadingAnchor.constraint(equalTo: leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: trailingAnchor),
scrollView.bottomAnchor.constraint(equalTo: bottomAnchor),
emptyLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
emptyLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
loadingIndicator.centerXAnchor.constraint(equalTo: centerXAnchor),
loadingIndicator.centerYAnchor.constraint(equalTo: centerYAnchor),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateHeader(store: FileExplorerStore) {
headerView.update(displayPath: store.displayRootPath)
}
func updateVisibility(hasContent: Bool, isLoading: Bool) {
scrollView.isHidden = !hasContent || isLoading
headerView.isHidden = !hasContent
emptyLabel.isHidden = hasContent
loadingIndicator.isHidden = !isLoading
if isLoading {
loadingIndicator.startAnimation(nil)
} else {
loadingIndicator.stopAnimation(nil)
}
}
}
// MARK: - Header View (AppKit)
/// Pure AppKit header bar with folder icon, path label, and hidden files toggle.
final class FileExplorerHeaderView: NSView {
private let iconView = NSImageView()
private let pathLabel = NSTextField(labelWithString: "")
override init(frame: NSRect) {
super.init(frame: frame)
setupViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupViews() {
iconView.translatesAutoresizingMaskIntoConstraints = false
let config = NSImage.SymbolConfiguration(pointSize: 11, weight: .regular)
iconView.image = NSImage(systemSymbolName: "folder.fill", accessibilityDescription: nil)?
.withSymbolConfiguration(config)
iconView.contentTintColor = .secondaryLabelColor
pathLabel.translatesAutoresizingMaskIntoConstraints = false
pathLabel.font = .systemFont(ofSize: 11, weight: .medium)
pathLabel.textColor = .secondaryLabelColor
pathLabel.lineBreakMode = .byTruncatingMiddle
pathLabel.maximumNumberOfLines = 1
pathLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
addSubview(iconView)
addSubview(pathLabel)
NSLayoutConstraint.activate([
heightAnchor.constraint(equalToConstant: 28),
iconView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12),
iconView.centerYAnchor.constraint(equalTo: centerYAnchor),
iconView.widthAnchor.constraint(equalToConstant: 14),
iconView.heightAnchor.constraint(equalToConstant: 14),
pathLabel.leadingAnchor.constraint(equalTo: iconView.trailingAnchor, constant: 4),
pathLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
pathLabel.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -8),
])
}
func update(displayPath: String) {
pathLabel.stringValue = displayPath
}
}
// MARK: - Cell View
final class FileExplorerCellView: NSTableCellView {
private let iconView = NSImageView()
private let nameLabel = NSTextField(labelWithString: "")
private let loadingIndicator = NSProgressIndicator()
private var trackingArea: NSTrackingArea?
var onHover: ((Bool) -> Void)?
init(identifier: NSUserInterfaceItemIdentifier) {
super.init(frame: .zero)
self.identifier = identifier
setupViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private var iconWidthConstraint: NSLayoutConstraint!
private var iconHeightConstraint: NSLayoutConstraint!
private var iconToTextConstraint: NSLayoutConstraint!
private func setupViews() {
iconView.translatesAutoresizingMaskIntoConstraints = false
iconView.imageScaling = .scaleProportionallyDown
nameLabel.translatesAutoresizingMaskIntoConstraints = false
nameLabel.textColor = .labelColor
nameLabel.lineBreakMode = .byTruncatingTail
nameLabel.maximumNumberOfLines = 1
loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
loadingIndicator.style = .spinning
loadingIndicator.controlSize = .small
loadingIndicator.isHidden = true
addSubview(iconView)
addSubview(nameLabel)
addSubview(loadingIndicator)
iconWidthConstraint = iconView.widthAnchor.constraint(equalToConstant: 16)
iconHeightConstraint = iconView.heightAnchor.constraint(equalToConstant: 16)
iconToTextConstraint = nameLabel.leadingAnchor.constraint(equalTo: iconView.trailingAnchor, constant: 4)
NSLayoutConstraint.activate([
iconView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0),
iconView.centerYAnchor.constraint(equalTo: centerYAnchor),
iconWidthConstraint,
iconHeightConstraint,
iconToTextConstraint,
nameLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
nameLabel.trailingAnchor.constraint(lessThanOrEqualTo: loadingIndicator.leadingAnchor, constant: -4),
loadingIndicator.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -4),
loadingIndicator.centerYAnchor.constraint(equalTo: centerYAnchor),
loadingIndicator.widthAnchor.constraint(equalToConstant: 12),
loadingIndicator.heightAnchor.constraint(equalToConstant: 12),
])
}
func configure(with node: FileExplorerNode, gitStatus: GitFileStatus? = nil) {
let style = FileExplorerStyle.current
nameLabel.stringValue = node.name
nameLabel.font = style.nameFont
iconWidthConstraint.constant = style.iconSize
iconHeightConstraint.constant = style.iconSize
iconToTextConstraint.constant = style.iconToTextSpacing
if style == .finder {
if node.isDirectory {
let folderIcon = NSWorkspace.shared.icon(for: .folder)
folderIcon.size = NSSize(width: style.iconSize, height: style.iconSize)
iconView.image = folderIcon
iconView.contentTintColor = nil
} else {
let fileIcon = NSWorkspace.shared.icon(forFileType: (node.name as NSString).pathExtension)
fileIcon.size = NSSize(width: style.iconSize, height: style.iconSize)
iconView.image = fileIcon
iconView.contentTintColor = nil
}
} else {
let symbolConfig = NSImage.SymbolConfiguration(pointSize: style.iconSize, weight: style.iconWeight)
if node.isDirectory {
iconView.image = NSImage(systemSymbolName: "folder.fill", accessibilityDescription: nil)?
.withSymbolConfiguration(symbolConfig)
iconView.contentTintColor = style.folderIconTint
} else {
iconView.image = NSImage(systemSymbolName: "doc", accessibilityDescription: nil)?
.withSymbolConfiguration(symbolConfig)
iconView.contentTintColor = style.fileIconTint
}
}
if node.isLoading {
loadingIndicator.isHidden = false
loadingIndicator.startAnimation(nil)
} else {
loadingIndicator.isHidden = true
loadingIndicator.stopAnimation(nil)
}
if let error = node.error {
nameLabel.textColor = .systemRed
nameLabel.toolTip = error
} else if let gitStatus {
nameLabel.textColor = style.gitColor(for: gitStatus)
nameLabel.toolTip = node.path
} else {
nameLabel.textColor = .labelColor
nameLabel.toolTip = node.path
}
}
override func updateTrackingAreas() {
super.updateTrackingAreas()
if let existing = trackingArea {
removeTrackingArea(existing)
}
let area = NSTrackingArea(
rect: bounds,
options: [.mouseEnteredAndExited, .activeInActiveApp],
owner: self,
userInfo: nil
)
addTrackingArea(area)
trackingArea = area
}
override func mouseEntered(with event: NSEvent) {
onHover?(true)
}
override func mouseExited(with event: NSEvent) {
onHover?(false)
}
}
// MARK: - Non-Animating Outline View
/// NSOutlineView subclass that disables expand/collapse animations and adds leading margin.
final class FileExplorerNSOutlineView: NSOutlineView {
/// Leading margin applied to disclosure triangles and content.
static let leadingMargin: CGFloat = 8
override func expandItem(_ item: Any?, expandChildren: Bool) {
NSAnimationContext.beginGrouping()
NSAnimationContext.current.duration = 0
super.expandItem(item, expandChildren: expandChildren)
NSAnimationContext.endGrouping()
}
override func collapseItem(_ item: Any?, collapseChildren: Bool) {
NSAnimationContext.beginGrouping()
NSAnimationContext.current.duration = 0
super.collapseItem(item, collapseChildren: collapseChildren)
NSAnimationContext.endGrouping()
}
override func frameOfOutlineCell(atRow row: Int) -> NSRect {
var frame = super.frameOfOutlineCell(atRow: row)
frame.origin.x += Self.leadingMargin
return frame
}
override func frameOfCell(atColumn column: Int, row: Int) -> NSRect {
var frame = super.frameOfCell(atColumn: column, row: row)
let cellShift: CGFloat = Self.leadingMargin - 6
frame.origin.x += cellShift
frame.size.width -= cellShift
return frame
}
}
// MARK: - Row View
final class FileExplorerRowView: NSTableRowView {
override func drawSelection(in dirtyRect: NSRect) {
guard isSelected else { return }
let style = FileExplorerStyle.current
if style.usesBorderSelection {
let borderRect = NSRect(x: 0, y: 1, width: 2, height: bounds.height - 2)
style.selectionColor.setFill()
borderRect.fill()
} else {
let inset = style.selectionInset
let insetRect = bounds.insetBy(dx: inset, dy: inset > 0 ? 1 : 0)
let path = NSBezierPath(roundedRect: insetRect, xRadius: style.selectionRadius, yRadius: style.selectionRadius)
style.selectionColor.setFill()
path.fill()
}
}
override var interiorBackgroundStyle: NSView.BackgroundStyle {
isSelected ? .emphasized : .normal
}
}