misc: ConnsTableCellView

This commit is contained in:
mrFq1
2023-06-01 01:02:14 +08:00
parent b0e9365518
commit 3340b37f3c
2 changed files with 50 additions and 42 deletions

View File

@@ -1,37 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="21507" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="21507"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner"/>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<tableCellView identifier="CollectionTableCellView" id="E1E-Yw-tO2">
<rect key="frame" x="0.0" y="0.0" width="100" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" translatesAutoresizingMaskIntoConstraints="NO" id="EkO-g0-3Rf">
<rect key="frame" x="0.0" y="1" width="100" height="16"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" sendsActionOnEndEditing="YES" title="Table View Cell" id="mZD-CG-RaE">
<font key="font" usesAppearanceFont="YES"/>
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="textBackgroundColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="EkO-g0-3Rf" secondAttribute="trailing" constant="2" id="cZb-2s-5cP"/>
<constraint firstItem="EkO-g0-3Rf" firstAttribute="centerY" secondItem="E1E-Yw-tO2" secondAttribute="centerY" id="g8v-Gi-bjM"/>
<constraint firstItem="EkO-g0-3Rf" firstAttribute="leading" secondItem="E1E-Yw-tO2" secondAttribute="leading" constant="2" id="nDQ-TP-ijK"/>
</constraints>
<accessibility identifier="CollectionTableCellView"/>
<connections>
<outlet property="textField" destination="EkO-g0-3Rf" id="Kzu-HK-Onq"/>
</connections>
<point key="canvasLocation" x="-108" y="-102"/>
</tableCellView>
</objects>
</document>

View File

@@ -55,8 +55,6 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
tableView.delegate = context.coordinator
tableView.dataSource = context.coordinator
tableView.register(.init(nibNamed: .init("CollectionTableCellView"), bundle: .main), forIdentifier: .init(rawValue: "CollectionTableCellView"))
TableColumn.allCases.forEach {
let tableColumn = NSTableColumn(identifier: .init($0.rawValue))
tableColumn.title = $0.rawValue
@@ -174,14 +172,15 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
guard let cell = tableView.makeView(withIdentifier: .init(rawValue: "CollectionTableCellView"), owner: nil) as? NSTableCellView,
guard let cellView = createCellView(tableView),
let s = tableColumn?.identifier.rawValue.split(separator: ".").last,
let tc = TableColumn(rawValue: String(s))
else { return nil }
let conn = conns[row]
cell.textField?.objectValue = {
cellView.textField?.objectValue = {
switch tc {
case .host:
return conn.host
@@ -208,7 +207,7 @@ struct CollectionsTableView<Item: Hashable>: NSViewRepresentable {
}
}()
return cell
return cellView
}
func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) {
@@ -216,6 +215,52 @@ struct CollectionsTableView<Item: Hashable>: 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
}
}
}