|
| 1 | +// |
| 2 | +// DataGridBaseCellView.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | + |
| 6 | +import AppKit |
| 7 | +import QuartzCore |
| 8 | + |
| 9 | +class DataGridBaseCellView: NSTableCellView { |
| 10 | + class var reuseIdentifier: NSUserInterfaceItemIdentifier { |
| 11 | + fatalError("subclass must override reuseIdentifier") |
| 12 | + } |
| 13 | + |
| 14 | + let cellTextField: CellTextField |
| 15 | + weak var accessoryDelegate: DataGridCellAccessoryDelegate? |
| 16 | + var nullDisplayString: String = "" |
| 17 | + var cellRow: Int = -1 |
| 18 | + var cellColumnIndex: Int = -1 |
| 19 | + |
| 20 | + private var textFieldTrailingConstraint: NSLayoutConstraint! |
| 21 | + |
| 22 | + var changeBackgroundColor: NSColor? { |
| 23 | + didSet { |
| 24 | + if let color = changeBackgroundColor { |
| 25 | + backgroundView.layer?.backgroundColor = color.cgColor |
| 26 | + backgroundView.isHidden = (backgroundStyle == .emphasized) |
| 27 | + } else { |
| 28 | + backgroundView.layer?.backgroundColor = nil |
| 29 | + backgroundView.isHidden = true |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + var isFocusedCell: Bool = false { |
| 35 | + didSet { |
| 36 | + guard oldValue != isFocusedCell else { return } |
| 37 | + updateFocusRing() |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private(set) lazy var backgroundView: NSView = { |
| 42 | + let view = NSView() |
| 43 | + view.wantsLayer = true |
| 44 | + view.translatesAutoresizingMaskIntoConstraints = false |
| 45 | + addSubview(view, positioned: .below, relativeTo: subviews.first) |
| 46 | + NSLayoutConstraint.activate([ |
| 47 | + view.leadingAnchor.constraint(equalTo: leadingAnchor), |
| 48 | + view.trailingAnchor.constraint(equalTo: trailingAnchor), |
| 49 | + view.topAnchor.constraint(equalTo: topAnchor), |
| 50 | + view.bottomAnchor.constraint(equalTo: bottomAnchor), |
| 51 | + ]) |
| 52 | + view.isHidden = true |
| 53 | + return view |
| 54 | + }() |
| 55 | + |
| 56 | + required override init(frame frameRect: NSRect) { |
| 57 | + cellTextField = Self.makeTextField() |
| 58 | + super.init(frame: frameRect) |
| 59 | + commonInit() |
| 60 | + } |
| 61 | + |
| 62 | + required init?(coder: NSCoder) { |
| 63 | + cellTextField = Self.makeTextField() |
| 64 | + super.init(coder: coder) |
| 65 | + commonInit() |
| 66 | + } |
| 67 | + |
| 68 | + private static func makeTextField() -> CellTextField { |
| 69 | + let field = CellTextField() |
| 70 | + field.font = ThemeEngine.shared.dataGridFonts.regular |
| 71 | + field.drawsBackground = false |
| 72 | + field.isBordered = false |
| 73 | + field.focusRingType = .none |
| 74 | + field.lineBreakMode = .byTruncatingTail |
| 75 | + field.maximumNumberOfLines = 1 |
| 76 | + field.cell?.truncatesLastVisibleLine = true |
| 77 | + field.cell?.usesSingleLineMode = true |
| 78 | + field.translatesAutoresizingMaskIntoConstraints = false |
| 79 | + return field |
| 80 | + } |
| 81 | + |
| 82 | + private func commonInit() { |
| 83 | + wantsLayer = true |
| 84 | + textField = cellTextField |
| 85 | + addSubview(cellTextField) |
| 86 | + |
| 87 | + textFieldTrailingConstraint = cellTextField.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -4) |
| 88 | + |
| 89 | + NSLayoutConstraint.activate([ |
| 90 | + cellTextField.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 4), |
| 91 | + textFieldTrailingConstraint, |
| 92 | + cellTextField.centerYAnchor.constraint(equalTo: centerYAnchor), |
| 93 | + ]) |
| 94 | + |
| 95 | + installAccessory() |
| 96 | + } |
| 97 | + |
| 98 | + func configure(content: DataGridCellContent, state: DataGridCellState) { |
| 99 | + cellRow = state.row |
| 100 | + cellColumnIndex = state.columnIndex |
| 101 | + |
| 102 | + applyContent(content, isLargeDataset: state.isLargeDataset) |
| 103 | + applyVisualState(state) |
| 104 | + |
| 105 | + cellTextField.isEditable = state.isEditable && !state.visualState.isDeleted |
| 106 | + |
| 107 | + let newInset = textFieldTrailingInset(for: content, state: state) |
| 108 | + if textFieldTrailingConstraint.constant != newInset { |
| 109 | + textFieldTrailingConstraint.constant = newInset |
| 110 | + } |
| 111 | + |
| 112 | + updateAccessoryVisibility(content: content, state: state) |
| 113 | + |
| 114 | + cellTextField.setAccessibilityLabel(content.accessibilityLabel) |
| 115 | + setAccessibilityRowIndexRange(NSRange(location: state.row, length: 1)) |
| 116 | + setAccessibilityColumnIndexRange(NSRange(location: state.columnIndex, length: 1)) |
| 117 | + } |
| 118 | + |
| 119 | + func installAccessory() {} |
| 120 | + |
| 121 | + func updateAccessoryVisibility(content: DataGridCellContent, state: DataGridCellState) {} |
| 122 | + |
| 123 | + func textFieldTrailingInset(for content: DataGridCellContent, state: DataGridCellState) -> CGFloat { |
| 124 | + -4 |
| 125 | + } |
| 126 | + |
| 127 | + private func applyContent(_ content: DataGridCellContent, isLargeDataset: Bool) { |
| 128 | + cellTextField.placeholderString = nil |
| 129 | + |
| 130 | + switch content.placeholder { |
| 131 | + case .none: |
| 132 | + cellTextField.stringValue = content.displayText |
| 133 | + cellTextField.originalValue = content.rawValue |
| 134 | + cellTextField.font = ThemeEngine.shared.dataGridFonts.regular |
| 135 | + cellTextField.tag = DataGridFontVariant.regular |
| 136 | + cellTextField.textColor = .labelColor |
| 137 | + |
| 138 | + case .null: |
| 139 | + cellTextField.stringValue = "" |
| 140 | + cellTextField.originalValue = nil |
| 141 | + cellTextField.font = ThemeEngine.shared.dataGridFonts.italic |
| 142 | + cellTextField.tag = DataGridFontVariant.italic |
| 143 | + cellTextField.textColor = .secondaryLabelColor |
| 144 | + if !isLargeDataset { |
| 145 | + cellTextField.placeholderString = nullDisplayString |
| 146 | + } |
| 147 | + |
| 148 | + case .empty: |
| 149 | + cellTextField.stringValue = "" |
| 150 | + cellTextField.originalValue = nil |
| 151 | + cellTextField.font = ThemeEngine.shared.dataGridFonts.italic |
| 152 | + cellTextField.tag = DataGridFontVariant.italic |
| 153 | + cellTextField.textColor = .secondaryLabelColor |
| 154 | + if !isLargeDataset { |
| 155 | + cellTextField.placeholderString = String(localized: "Empty") |
| 156 | + } |
| 157 | + |
| 158 | + case .defaultMarker: |
| 159 | + cellTextField.stringValue = "" |
| 160 | + cellTextField.originalValue = nil |
| 161 | + cellTextField.font = ThemeEngine.shared.dataGridFonts.medium |
| 162 | + cellTextField.tag = DataGridFontVariant.medium |
| 163 | + cellTextField.textColor = .systemBlue |
| 164 | + if !isLargeDataset { |
| 165 | + cellTextField.placeholderString = String(localized: "DEFAULT") |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private func applyVisualState(_ state: DataGridCellState) { |
| 171 | + CATransaction.begin() |
| 172 | + CATransaction.setDisableActions(true) |
| 173 | + |
| 174 | + if state.visualState.isDeleted { |
| 175 | + changeBackgroundColor = ThemeEngine.shared.colors.dataGrid.deleted |
| 176 | + } else if state.visualState.isInserted { |
| 177 | + changeBackgroundColor = ThemeEngine.shared.colors.dataGrid.inserted |
| 178 | + } else if state.visualState.modifiedColumns.contains(state.columnIndex) { |
| 179 | + changeBackgroundColor = ThemeEngine.shared.colors.dataGrid.modified |
| 180 | + } else { |
| 181 | + changeBackgroundColor = nil |
| 182 | + } |
| 183 | + |
| 184 | + isFocusedCell = state.isFocused |
| 185 | + |
| 186 | + CATransaction.commit() |
| 187 | + } |
| 188 | + |
| 189 | + override var backgroundStyle: NSView.BackgroundStyle { |
| 190 | + didSet { |
| 191 | + backgroundView.isHidden = (backgroundStyle == .emphasized) || (changeBackgroundColor == nil) |
| 192 | + if isFocusedCell { updateFocusRing() } |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + override var focusRingMaskBounds: NSRect { bounds } |
| 197 | + |
| 198 | + override func drawFocusRingMask() { |
| 199 | + NSBezierPath(rect: bounds).fill() |
| 200 | + } |
| 201 | + |
| 202 | + override func draw(_ dirtyRect: NSRect) { |
| 203 | + super.draw(dirtyRect) |
| 204 | + guard isFocusedCell, backgroundStyle != .emphasized else { return } |
| 205 | + NSGraphicsContext.saveGraphicsState() |
| 206 | + NSFocusRingPlacement.only.set() |
| 207 | + drawFocusRingMask() |
| 208 | + NSGraphicsContext.restoreGraphicsState() |
| 209 | + } |
| 210 | + |
| 211 | + override func viewDidChangeEffectiveAppearance() { |
| 212 | + super.viewDidChangeEffectiveAppearance() |
| 213 | + if isFocusedCell { |
| 214 | + needsDisplay = true |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private func updateFocusRing() { |
| 219 | + focusRingType = isFocusedCell ? .exterior : .none |
| 220 | + noteFocusRingMaskChanged() |
| 221 | + needsDisplay = true |
| 222 | + } |
| 223 | +} |
0 commit comments