Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reveal file in the middle of Project Navigator #1352

Merged
merged 7 commits into from
Jun 23, 2023
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ final class ProjectNavigatorViewController: NSViewController {
fatalError()
}

@objc func revealFile(_ sender: Any) {
dscyrescotti marked this conversation as resolved.
Show resolved Hide resolved
updateSelection(itemID: workspace?.tabManager.activeTabGroup.selected?.id)
}

/// Updates the selection of the ``outlineView`` whenever it changes.
///
/// Most importantly when the `id` changes from an external view.
Expand Down Expand Up @@ -290,6 +294,10 @@ extension ProjectNavigatorViewController: NSOutlineViewDelegate {
else {
return
}
/// update outline selection only if the parent of selected item match with expanded item
guard item.parent === notification.userInfo?["NSObject"] as? CEWorkspaceFile else {
return
}
/// select active file under collapsed folder only if its parent is expanding
if outlineView.isItemExpanded(item.parent) {
updateSelection(itemID: item.id)
Expand Down Expand Up @@ -350,7 +358,21 @@ extension ProjectNavigatorViewController: NSOutlineViewDelegate {
alert.runModal()
return
} else {
let visibleRect = scrollView.contentView.visibleRect
let visibleRows = outlineView.rows(in: visibleRect)
guard !visibleRows.contains(row) else {
/// in case that the selected file is not fully visible (some parts are out of the visible rect),
/// `scrollRowToVisible(_:)` method brings the file where it can be fully visible.
outlineView.scrollRowToVisible(row)
return
}
let rowRect = outlineView.rect(ofRow: row)
let centerY = rowRect.midY - (visibleRect.height / 2)
let center = NSPoint(x: 0, y: centerY)
/// `scroll(_:)` method alone doesn't bring the selected file to the center in some cases.
/// calling `scrollRowToVisible(_:)` method before it makes the file reveal in the center more correctly.
outlineView.scrollRowToVisible(row)
outlineView.scroll(center)
}
}

Expand Down
2 changes: 1 addition & 1 deletion CodeEdit/Features/WindowCommands/NavigateCommands.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct NavigateCommands: Commands {
CommandMenu("Navigate") {
Group {
Button("Reveal in Project Navigator") {

NSApp.sendAction(#selector(ProjectNavigatorViewController.revealFile(_:)), to: nil, from: nil)
dscyrescotti marked this conversation as resolved.
Show resolved Hide resolved
}
.keyboardShortcut("j", modifiers: [.shift, .command])

Expand Down