|
| 1 | +// |
| 2 | +// ProjectNavigatorViewController+Delegate.swift |
| 3 | +// CodeEdit |
| 4 | +// |
| 5 | +// Created by Khan Winter on 7/14/24. |
| 6 | +// |
| 7 | + |
| 8 | +import AppKit |
| 9 | + |
| 10 | +extension ProjectNavigatorViewController: NSOutlineViewDelegate { |
| 11 | + func outlineView( |
| 12 | + _ outlineView: NSOutlineView, |
| 13 | + shouldShowCellExpansionFor tableColumn: NSTableColumn?, |
| 14 | + item: Any |
| 15 | + ) -> Bool { |
| 16 | + true |
| 17 | + } |
| 18 | + |
| 19 | + func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: Any) -> Bool { |
| 20 | + true |
| 21 | + } |
| 22 | + |
| 23 | + func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { |
| 24 | + guard let tableColumn else { return nil } |
| 25 | + |
| 26 | + let frameRect = NSRect(x: 0, y: 0, width: tableColumn.width, height: rowHeight) |
| 27 | + |
| 28 | + return ProjectNavigatorTableViewCell(frame: frameRect, item: item as? CEWorkspaceFile, delegate: self) |
| 29 | + } |
| 30 | + |
| 31 | + func outlineViewSelectionDidChange(_ notification: Notification) { |
| 32 | + guard let outlineView = notification.object as? NSOutlineView else { return } |
| 33 | + |
| 34 | + let selectedIndex = outlineView.selectedRow |
| 35 | + |
| 36 | + guard let item = outlineView.item(atRow: selectedIndex) as? CEWorkspaceFile else { return } |
| 37 | + |
| 38 | + if !item.isFolder && shouldSendSelectionUpdate { |
| 39 | + DispatchQueue.main.async { |
| 40 | + self.shouldSendSelectionUpdate = false |
| 41 | + self.workspace?.editorManager.activeEditor.openTab(file: item, asTemporary: true) |
| 42 | + self.shouldSendSelectionUpdate = true |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat { |
| 48 | + rowHeight // This can be changed to 20 to match Xcode's row height. |
| 49 | + } |
| 50 | + |
| 51 | + func outlineViewItemDidExpand(_ notification: Notification) { |
| 52 | + guard let id = workspace?.editorManager.activeEditor.selectedTab?.file.id, |
| 53 | + let item = workspace?.workspaceFileManager?.getFile(id, createIfNotFound: true), |
| 54 | + /// update outline selection only if the parent of selected item match with expanded item |
| 55 | + item.parent === notification.userInfo?["NSObject"] as? CEWorkspaceFile else { |
| 56 | + return |
| 57 | + } |
| 58 | + /// select active file under collapsed folder only if its parent is expanding |
| 59 | + if outlineView.isItemExpanded(item.parent) { |
| 60 | + updateSelection(itemID: item.id) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + func outlineViewItemDidCollapse(_ notification: Notification) {} |
| 65 | + |
| 66 | + func outlineView(_ outlineView: NSOutlineView, itemForPersistentObject object: Any) -> Any? { |
| 67 | + guard let id = object as? CEWorkspaceFile.ID, |
| 68 | + let item = workspace?.workspaceFileManager?.getFile(id, createIfNotFound: true) else { return nil } |
| 69 | + return item |
| 70 | + } |
| 71 | + |
| 72 | + func outlineView(_ outlineView: NSOutlineView, persistentObjectForItem item: Any?) -> Any? { |
| 73 | + guard let item = item as? CEWorkspaceFile else { return nil } |
| 74 | + return item.id |
| 75 | + } |
| 76 | + |
| 77 | + /// Finds and selects an ``Item`` from an array of ``Item`` and their `children` based on the `id`. |
| 78 | + /// - Parameters: |
| 79 | + /// - id: the id of the item item |
| 80 | + /// - collection: the array to search for |
| 81 | + /// - forcesReveal: The boolean to indicates whether or not it should force to reveal the selected file. |
| 82 | + func select(by id: EditorTabID, forcesReveal: Bool) { |
| 83 | + guard case .codeEditor(let path) = id, |
| 84 | + let item = workspace?.workspaceFileManager?.getFile(path, createIfNotFound: true) else { |
| 85 | + return |
| 86 | + } |
| 87 | + // If the user has set "Reveal file on selection change" to on or it is forced to reveal, |
| 88 | + // we need to reveal the item before selecting the row. |
| 89 | + if Settings.shared.preferences.general.revealFileOnFocusChange || forcesReveal { |
| 90 | + reveal(item) |
| 91 | + } |
| 92 | + let row = outlineView.row(forItem: item) |
| 93 | + if row == -1 { |
| 94 | + outlineView.deselectRow(outlineView.selectedRow) |
| 95 | + } |
| 96 | + shouldSendSelectionUpdate = false |
| 97 | + outlineView.selectRowIndexes(.init(integer: row), byExtendingSelection: false) |
| 98 | + shouldSendSelectionUpdate = true |
| 99 | + } |
| 100 | + |
| 101 | + /// Reveals the given `fileItem` in the outline view by expanding all the parent directories of the file. |
| 102 | + /// If the file is not found, it will present an alert saying so. |
| 103 | + /// - Parameter fileItem: The file to reveal. |
| 104 | + public func reveal(_ fileItem: CEWorkspaceFile) { |
| 105 | + if let parent = fileItem.parent { |
| 106 | + expandParent(item: parent) |
| 107 | + } |
| 108 | + let row = outlineView.row(forItem: fileItem) |
| 109 | + shouldSendSelectionUpdate = false |
| 110 | + outlineView.selectRowIndexes(.init(integer: row), byExtendingSelection: false) |
| 111 | + shouldSendSelectionUpdate = true |
| 112 | + |
| 113 | + if row < 0 { |
| 114 | + let alert = NSAlert() |
| 115 | + alert.messageText = NSLocalizedString( |
| 116 | + "Could not find file", |
| 117 | + comment: "Could not find file" |
| 118 | + ) |
| 119 | + alert.runModal() |
| 120 | + return |
| 121 | + } else { |
| 122 | + let visibleRect = scrollView.contentView.visibleRect |
| 123 | + let visibleRows = outlineView.rows(in: visibleRect) |
| 124 | + guard !visibleRows.contains(row) else { |
| 125 | + /// in case that the selected file is not fully visible (some parts are out of the visible rect), |
| 126 | + /// `scrollRowToVisible(_:)` method brings the file where it can be fully visible. |
| 127 | + outlineView.scrollRowToVisible(row) |
| 128 | + return |
| 129 | + } |
| 130 | + let rowRect = outlineView.rect(ofRow: row) |
| 131 | + let centerY = rowRect.midY - (visibleRect.height / 2) |
| 132 | + let center = NSPoint(x: 0, y: centerY) |
| 133 | + /// `scroll(_:)` method alone doesn't bring the selected file to the center in some cases. |
| 134 | + /// calling `scrollRowToVisible(_:)` method before it makes the file reveal in the center more correctly. |
| 135 | + outlineView.scrollRowToVisible(row) |
| 136 | + outlineView.scroll(center) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /// Method for recursively expanding a file's parent directories. |
| 141 | + /// - Parameter item: |
| 142 | + private func expandParent(item: CEWorkspaceFile) { |
| 143 | + if let parent = item.parent as CEWorkspaceFile? { |
| 144 | + expandParent(item: parent) |
| 145 | + } |
| 146 | + outlineView.expandItem(item) |
| 147 | + } |
| 148 | +} |
0 commit comments