From 08e4f626c687257a32bbf6af1e8217541a10b72e Mon Sep 17 00:00:00 2001 From: mrFq1 <1xxbx0il0@mozmail.com> Date: Mon, 11 Sep 2023 20:46:30 +0800 Subject: [PATCH] misc: connections tableview select --- .../Connections/ConnectionsTableView.swift | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Sources/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsTableView.swift b/Sources/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsTableView.swift index 2f721ad..e1d1d0e 100644 --- a/Sources/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsTableView.swift +++ b/Sources/ClashX Dashboard/Views/ContentTabs/Connections/ConnectionsTableView.swift @@ -214,6 +214,15 @@ struct ConnectionsTableView: NSViewRepresentable { connHistorys[$0.id] = ($0.download, $0.upload) } + let selectedID: String? = { + let selectedRow = tableView.selectedRow + guard selectedRow >= 0, selectedRow < self.conns.count else { + return nil + } + return self.conns[selectedRow].id + }() + + guard let partialChanges = self.conns.applying(changes) else { return } @@ -222,8 +231,12 @@ struct ConnectionsTableView: NSViewRepresentable { let indicesToReload = IndexSet(zip(partialChanges, conns).enumerated().compactMap { index, pair -> Int? in (pair.0.id == pair.1.id && pair.0 != pair.1) ? index : nil }) - + tableView.reloadData(changes, indexs: indicesToReload) + + if let index = self.conns.firstIndex(where: { $0.id == selectedID }) { + tableView.selectRowIndexes(.init(integer: index), byExtendingSelection: true) + } }