-
Notifications
You must be signed in to change notification settings - Fork 8
/
Table.swift
180 lines (152 loc) · 6 KB
/
Table.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// File.swift
//
//
// Created by Chris Eidhof on 15.07.22.
//
import Foundation
import Cocoa
// TODO would be nice to have a builder here as well
@MainActor
public struct Table: AttributedStringConvertible {
public init(contentWidth: Width = .percentage(100), rows: [TableRow]) {
self.rows = rows
self.contentWidth = contentWidth
}
public var rows: [TableRow]
public var contentWidth: Width
public func attributedString(context: inout Context) -> [NSAttributedString] {
guard !rows.isEmpty else { return [] }
let result = NSMutableAttributedString()
let table = NSTextTable()
table.setContentWidth(contentWidth.value, type: contentWidth.type)
table.numberOfColumns = rows[0].cells.count
for (rowIx, row) in rows.enumerated() {
assert(row.cells.count == table.numberOfColumns)
row.render(row: rowIx, table: table, context: &context, result: result)
}
result.replaceCharacters(in: NSRange(location: result.length-1, length: 1), with: "")
return [result]
}
}
@MainActor
public struct TableRow {
public init(cells: [TableCell]) {
self.cells = cells
}
public var cells: [TableCell]
func render(row: Int, table: NSTextTable, context: inout Context, result: NSMutableAttributedString) {
for (column, cell) in cells.enumerated() {
let block = NSTextTableBlock(table: table, startingRow: row, rowSpan: 1, startingColumn: column, columnSpan: 1)
if let w = cell.width {
block.setContentWidth(w.value, type: w.type)
}
cell.render(block: block, context: &context, result: result)
}
}
}
@MainActor
public struct TableCell {
public init(
width: Table.Width? = nil,
height: Table.Width? = nil,
borderColor: NSColor = .black,
borderWidth: WidthValue = 0,
padding: WidthValue = 0,
margin: WidthValue = 0,
alignment: NSTextAlignment = .left,
verticalAlignment: NSTextBlock.VerticalAlignment = .topAlignment,
contents: AttributedStringConvertible) {
self.borderColor = borderColor
self.borderWidth = borderWidth
self.padding = padding
self.margin = margin
self.contents = contents
self.width = width
self.height = height
self.alignment = alignment
self.verticalAlignment = verticalAlignment
}
public var borderColor: NSColor = .black
public var borderWidth: WidthValue = 0
public var padding: WidthValue = 0
public var margin: WidthValue = 0
public var contents: AttributedStringConvertible
public var width: Table.Width?
public var height: Table.Width? = nil
public var alignment: NSTextAlignment = .left
public var verticalAlignment: NSTextBlock.VerticalAlignment
@MainActor
func render(block: NSTextTableBlock, context: inout Context, result: NSMutableAttributedString) {
block.setBorderColor(borderColor)
for (edge, value) in borderWidth.allEdges {
block.setWidth(value.value, type: value.type, for: .border, edge: edge)
}
for (edge, value) in padding.allEdges {
block.setWidth(value.value, type: value.type, for: .padding, edge: edge)
}
for (edge, value) in margin.allEdges {
block.setWidth(value.value, type: value.type, for: .margin, edge: edge)
}
if let h = height {
block.setValue(h.value, type: h.type, for: .height)
}
block.verticalAlignment = verticalAlignment
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = alignment
paragraph.textBlocks = [block]
let contentsA = NSMutableAttributedString(attributedString: contents.joined().run(context: &context))
// Copy some style attributes from the cell contents if possible
if let style = contentsA.attributes(at: 0, effectiveRange: nil)[.paragraphStyle] as? NSParagraphStyle {
paragraph.lineHeightMultiple = style.lineHeightMultiple
paragraph.minimumLineHeight = style.minimumLineHeight
paragraph.maximumLineHeight = style.maximumLineHeight
}
contentsA.mutableString.append("\n") // This is necessary to be recognized as a cell!
let range = NSRange(location: 0, length: contentsA.string.count)
contentsA.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraph, range: range)
result.append(contentsA)
}
}
extension Table {
public enum Width: Hashable, Codable {
case absolute(CGFloat)
case percentage(CGFloat)
var value: CGFloat {
switch self {
case .absolute(let x): return x
case .percentage(let x): return x
}
}
var type: NSTextBlock.ValueType {
switch self {
case .percentage: return .percentageValueType
case .absolute: return .absoluteValueType
}
}
}
}
public struct WidthValue: ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral, Hashable, Codable {
public init(integerLiteral value: Int) {
self.init(floatLiteral: .init(value))
}
public init(floatLiteral value: Double) {
self.init(top: value, right: value, bottom: value, left: value)
}
public init(top: CGFloat = 0, right: CGFloat = 0, bottom: CGFloat = 0, left: CGFloat = 0) {
self.top = .absolute(top)
self.right = .absolute(right)
self.bottom = .absolute(bottom)
self.left = .absolute(left)
}
public init(top: Table.Width = .absolute(0), right: Table.Width = .absolute(0), bottom: Table.Width = .absolute(0), left: Table.Width = .absolute(0)) {
self.top = top
self.right = right
self.bottom = bottom
self.left = left
}
public var top, right, bottom, left: Table.Width // todo should these be Width?
var allEdges: [NSRectEdge: Table.Width] {
[.minY: top, .maxY: bottom, .minX: left, .maxX: right]
}
}