diff --git a/ClashX Dashboard Kit/Sources/ClashX Dashboard Kit/Views/ContentTabs/Connections/CollectionsTableView.swift b/ClashX Dashboard Kit/Sources/ClashX Dashboard Kit/Views/ContentTabs/Connections/CollectionsTableView.swift index 69d473e..7ef4eb1 100644 --- a/ClashX Dashboard Kit/Sources/ClashX Dashboard Kit/Views/ContentTabs/Connections/CollectionsTableView.swift +++ b/ClashX Dashboard Kit/Sources/ClashX Dashboard Kit/Views/ContentTabs/Connections/CollectionsTableView.swift @@ -173,7 +173,7 @@ struct CollectionsTableView: NSViewRepresentable { func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { - guard let cellView = createCellView(tableView), + guard let cellView = tableView.createCellView(with: "ConnsTableCellView"), let s = tableColumn?.identifier.rawValue.split(separator: ".").last, let tc = TableColumn(rawValue: String(s)) else { return nil } @@ -215,52 +215,7 @@ struct CollectionsTableView: NSViewRepresentable { tableView.reloadData() } - func createCellView(_ tableView: NSTableView) -> NSTableCellView? { - // https://stackoverflow.com/a/27624927 - - var cellView: NSTableCellView? - if let spareView = tableView.makeView(withIdentifier: .init("ConnsTableCellView"), - owner: self) as? NSTableCellView { - // We can use an old cell - no need to do anything. - cellView = spareView - - } else { - - // Create a text field for the cell - let textField = NSTextField() - textField.backgroundColor = NSColor.clear - textField.translatesAutoresizingMaskIntoConstraints = false - textField.isBordered = false - textField.font = .systemFont(ofSize: 13) - - // Create a cell - let newCell = NSTableCellView() - newCell.identifier = .init("ConnsTableCellView") - newCell.addSubview(textField) - newCell.textField = textField - - // Constrain the text field within the cell - newCell.addConstraints( - NSLayoutConstraint.constraints(withVisualFormat: "H:|[textField]|", - options: [], - metrics: nil, - views: ["textField" : textField])) - - newCell.addConstraint(.init(item: textField, attribute: .centerY, relatedBy: .equal, toItem: newCell, attribute: .centerY, multiplier: 1, constant: 0)) - - - textField.bind(NSBindingName.value, - to: newCell, - withKeyPath: "objectValue", - options: nil) - - cellView = newCell - } - - return cellView - } - } } @@ -313,4 +268,52 @@ extension NSTableView { } } + + func createCellView(with identifier: String) -> NSTableCellView? { + // https://stackoverflow.com/a/27624927 + + var cellView: NSTableCellView? + if let spareView = makeView(withIdentifier: .init(identifier), + owner: self) as? NSTableCellView { + + // We can use an old cell - no need to do anything. + cellView = spareView + + } else { + + // Create a text field for the cell + let textField = NSTextField() + textField.backgroundColor = NSColor.clear + textField.translatesAutoresizingMaskIntoConstraints = false + textField.isBordered = false + textField.font = .systemFont(ofSize: 13) + textField.lineBreakMode = .byTruncatingTail + + // Create a cell + let newCell = NSTableCellView() + newCell.identifier = .init(identifier) + newCell.addSubview(textField) + newCell.textField = textField + + // Constrain the text field within the cell + newCell.addConstraints( + NSLayoutConstraint.constraints(withVisualFormat: "H:|[textField]|", + options: [], + metrics: nil, + views: ["textField" : textField])) + + newCell.addConstraint(.init(item: textField, attribute: .centerY, relatedBy: .equal, toItem: newCell, attribute: .centerY, multiplier: 1, constant: 0)) + + + textField.bind(NSBindingName.value, + to: newCell, + withKeyPath: "objectValue", + options: nil) + + cellView = newCell + } + + return cellView + } + }