mirror of
https://github.com/yJason/ClashX-Dashboard.git
synced 2026-02-04 10:02:26 +08:00
feat: ConnectionsTableView column state
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
//
|
//
|
||||||
// CollectionsTableView.swift
|
// ConnectionsTableView.swift
|
||||||
// ClashX Dashboard
|
// ClashX Dashboard
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
@@ -7,7 +7,7 @@ import SwiftUI
|
|||||||
import AppKit
|
import AppKit
|
||||||
import DifferenceKit
|
import DifferenceKit
|
||||||
|
|
||||||
struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
|
struct ConnectionsTableView<Item: Hashable>: NSViewRepresentable {
|
||||||
|
|
||||||
enum TableColumn: String, CaseIterable {
|
enum TableColumn: String, CaseIterable {
|
||||||
case host = "Host"
|
case host = "Host"
|
||||||
@@ -56,6 +56,11 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
|
|||||||
tableView.delegate = context.coordinator
|
tableView.delegate = context.coordinator
|
||||||
tableView.dataSource = context.coordinator
|
tableView.dataSource = context.coordinator
|
||||||
|
|
||||||
|
let menu = NSMenu()
|
||||||
|
menu.showsStateColumn = true
|
||||||
|
tableView.headerView?.menu = menu
|
||||||
|
|
||||||
|
|
||||||
TableColumn.allCases.forEach {
|
TableColumn.allCases.forEach {
|
||||||
let tableColumn = NSTableColumn(identifier: .init($0.rawValue))
|
let tableColumn = NSTableColumn(identifier: .init($0.rawValue))
|
||||||
tableColumn.title = $0.rawValue
|
tableColumn.title = $0.rawValue
|
||||||
@@ -97,6 +102,15 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tableColumn.sortDescriptorPrototype = sort
|
tableColumn.sortDescriptorPrototype = sort
|
||||||
|
|
||||||
|
let item = NSMenuItem(
|
||||||
|
title: $0.rawValue,
|
||||||
|
action: #selector(context.coordinator.toggleColumn(_:)),
|
||||||
|
keyEquivalent: "")
|
||||||
|
item.target = context.coordinator
|
||||||
|
item.representedObject = tableColumn
|
||||||
|
|
||||||
|
menu.addItem(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -107,6 +121,14 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
|
|||||||
|
|
||||||
scrollView.documentView = tableView
|
scrollView.documentView = tableView
|
||||||
|
|
||||||
|
tableView.autosaveName = "ClashX_Dashboard.Connections.TableView"
|
||||||
|
tableView.autosaveTableColumns = true
|
||||||
|
|
||||||
|
menu.items.forEach {
|
||||||
|
guard let column = $0.representedObject as? NSTableColumn else { return }
|
||||||
|
$0.state = column.isHidden ? .off : .on
|
||||||
|
}
|
||||||
|
|
||||||
return scrollView
|
return scrollView
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,11 +180,11 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
|
|||||||
|
|
||||||
class Coordinator: NSObject, NSTableViewDelegate, NSTableViewDataSource {
|
class Coordinator: NSObject, NSTableViewDelegate, NSTableViewDataSource {
|
||||||
|
|
||||||
var parent: CollectionsTableView
|
var parent: ConnectionsTableView
|
||||||
|
|
||||||
var conns = [DBConnectionObject]()
|
var conns = [DBConnectionObject]()
|
||||||
|
|
||||||
init(parent: CollectionsTableView) {
|
init(parent: ConnectionsTableView) {
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,6 +238,12 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
|
|||||||
tableView.reloadData()
|
tableView.reloadData()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func toggleColumn(_ menuItem: NSMenuItem) {
|
||||||
|
guard let column = menuItem.representedObject as? NSTableColumn else { return }
|
||||||
|
let hide = menuItem.state == .on
|
||||||
|
column.isHidden = hide
|
||||||
|
menuItem.state = hide ? .off : .on
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ struct ConnectionsView: View {
|
|||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
|
|
||||||
CollectionsTableView(data: data.conns,
|
ConnectionsTableView(data: data.conns,
|
||||||
filterString: searchString)
|
filterString: searchString)
|
||||||
.background(Color(nsColor: .textBackgroundColor))
|
.background(Color(nsColor: .textBackgroundColor))
|
||||||
.searchable(text: $searchString)
|
.searchable(text: $searchString)
|
||||||
|
|||||||
Reference in New Issue
Block a user