From 3f57cdf4f76ee0a162d79722acfd0fa3efe84f35 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Sat, 2 Feb 2019 17:01:40 -0800 Subject: [PATCH] Restore the selection after rebuilding the sidebar after a name change. --- .../Sidebar/SidebarViewController.swift | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/NetNewsWire/MainWindow/Sidebar/SidebarViewController.swift b/NetNewsWire/MainWindow/Sidebar/SidebarViewController.swift index 5b8470283..b229f2f66 100644 --- a/NetNewsWire/MainWindow/Sidebar/SidebarViewController.swift +++ b/NetNewsWire/MainWindow/Sidebar/SidebarViewController.swift @@ -118,8 +118,10 @@ import RSCore guard let object = note.object else { return } + let savedSelection = selectedNodes rebuildTreeAndReloadDataIfNeeded() configureCellsForRepresentedObject(object as AnyObject) + restoreSelection(to: savedSelection, sendNotificationIfChanged: true) } @objc func userDidRequestSidebarSelection(_ note: Notification) { @@ -342,7 +344,27 @@ private extension SidebarViewController { outlineView.reloadData() } } - + + func restoreSelection(to nodes: [Node], sendNotificationIfChanged: Bool) { + if selectedNodes == nodes { // Nothing to do? + return + } + + var indexes = IndexSet() + for node in nodes { + let row = outlineView.row(forItem: node as Any) + if row > -1 { + indexes.insert(row) + } + } + + outlineView.selectRowIndexes(indexes, byExtendingSelection: false) + + if selectedNodes != nodes && sendNotificationIfChanged { + postSidebarSelectionDidChangeNotification(selectedObjects) + } + } + func postSidebarSelectionDidChangeNotification(_ selectedObjects: [AnyObject]?) { var userInfo = UserInfoDictionary()