-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathGridView.swift
691 lines (580 loc) · 28 KB
/
GridView.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
//
// GridView.swift
// ProtonTests
//
// Created by Rajdeep Kwatra on 5/6/2022.
// Copyright © 2022 Rajdeep Kwatra. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UIKit
/// An object capable of handing `GridView` events
public protocol GridViewDelegate: AnyObject {
/// Invoked when `EditorView` within the cell receives focus
/// - Parameters:
/// - gridView: GridView containing cell
/// - range: Range of content in the `EditorView` within the Cell
/// - cell: Cell containing Editor
func gridView(_ gridView: GridView, didReceiveFocusAt range: NSRange, in cell: GridCell)
/// Invoked when `EditorView` within the cell loses focus
/// - Parameters:
/// - gridView: GridView containing cell
/// - range: Range of content in the `EditorView` within the Cell
/// - cell: Cell containing Editor
func gridView(_ gridView: GridView, didLoseFocusFrom range: NSRange, in cell: GridCell)
/// Invoked when tap event occurs within the Editor contained in the cell.
/// - Parameters:
/// - gridView: GridView containing cell
/// - location: Tapped location
/// - characterRange: Range of characters in the Editor at the tapped location
/// - cell: Cell containing Editor
func gridView(_ gridView: GridView, didTapAtLocation location: CGPoint, characterRange: NSRange?, in cell: GridCell)
/// Invoked on selection changes with in the Editor contained in the cell.
/// - Parameters:
/// - gridView: GridView containing cell
/// - range: Range of selection in the `EditorView` within the Cell
/// - attributes: Attributes at selected range
/// - contentType: `ContentType` at selected range
/// - cell: Cell containing Editor
func gridView(_ gridView: GridView, didChangeSelectionAt range: NSRange, attributes: [NSAttributedString.Key : Any], contentType: EditorContent.Name, in cell: GridCell)
/// Invoked on change of bounds of the Editor within the cell
/// - Parameters:
/// - gridView: GridView containing cell
/// - bounds: Bounds of the EditorView within the cell. Height of EditorView may be less than that of the Cell.
/// - cell: Cell containing Editor
func gridView(_ gridView: GridView, didChangeBounds bounds: CGRect, in cell: GridCell)
/// Invoked when selection of cells is changed.
/// - Parameters:
/// - gridView: GridView containing cell
/// - cells: Selected cells
func gridView(_ gridView: GridView, didSelectCells cells: [GridCell])
/// Invoked when selection of cells is changed.
/// - Parameters:
/// - gridView: GridView containing cell
/// - cells: Cells that are changed from selected to unselected.
func gridView(_ gridView: GridView, didUnselectCells cells: [GridCell])
/// Invoked when special keys are intercepted in the Editor contained in the cell.
/// - Parameters:
/// - gridView: GridView containing cell
/// - key: Special key
/// - range: Range at with the key is intercepted.
/// - cell: Cell containing Editor
func gridView(_ gridView: GridView, didReceiveKey key: EditorKey, at range: NSRange, in cell: GridCell)
/// Invoked when a column in `GridView` is resized.
/// - Parameters:
/// - gridView: GridView containing column
/// - proposedWidth: Proposed column width before the change
/// - columnIndex: Index of column being resized
/// - Returns: `true` if column resizing should be allowed, else false.
func gridView(_ gridView: GridView, shouldChangeColumnWidth proposedWidth: CGFloat, for columnIndex: Int) -> Bool
/// Notifies when `GridView` lays out a cell. This is called after the bounds calculation for the cell have been performed.
/// Rendering of cell may not have been completed at this time.
/// - Parameters:
/// - gridView: GridView containing the cell.
/// - cell: Cell being laid out
func gridView(_ gridView: GridView, didLayoutCell cell: GridCell)
}
/// A view that provides a tabular structure where each cell is an `EditorView`.
/// Since the cells contains an `EditorView` in itself, it is capable of hosting any attachment that `EditorView` can host
/// including another `GridView` as an attachment.
public class GridView: UIView {
let gridView: GridContentView
private let leadingShadowView: UIView
private let trailingShadowView: UIView
private var columnResizingHandles = [CellHandleButton]()
private let handleSize: CGFloat = 20
private let config: GridConfiguration
private let selectionView = SelectionView()
private var resizingDragHandleLastLocation: CGPoint? = nil
private var leadingShadowConstraint: NSLayoutConstraint!
private lazy var columnRightBorderView: UIView = {
makeSelectionBorderView()
}()
private lazy var columnLeftBorderView: UIView = {
makeSelectionBorderView()
}()
private lazy var columnTopBorderView: UIView = {
makeSelectionBorderView()
}()
private lazy var columnBottomBorderView: UIView = {
makeSelectionBorderView()
}()
private var shadowWidth: CGFloat {
10.0
}
/// Delegate for `GridView` which can be used to handle cell specific `EditorView` events
public weak var delegate: GridViewDelegate?
/// Gets the attachment containing the `GridView`
public var containerAttachment: Attachment? {
attachmentContentView?.attachment
}
/// Determines if column resizing handles are visible or not.
public private(set) var isColumnResizingHandlesVisible = false {
didSet {
if isColumnResizingHandlesVisible == false {
removeColumnResizingHandles()
}
}
}
/// Bounds observer for the `GridView`. Typically, this will be the `Attachment` that hosts the `GridView`.
/// - Note: In absence of a `boundObserver`, the `GridView` will not autoresize when the content in the cells
/// are changed.
public var boundsObserver: BoundsObserving? {
get { gridView.boundsObserver }
set { gridView.boundsObserver = newValue }
}
/// Selection color for the `GridView`. Defaults to `tintColor`
public var selectionColor: UIColor?
/// Determines if `GridView` is selected or not.
public var isSelected: Bool = false {
didSet {
if isSelected {
selectionView.addTo(parent: self, selectionColor: selectionColor)
} else {
selectionView.removeFromSuperview()
}
}
}
/// Allows scrolling grid in any direction. Defaults to `false`
/// Default behaviour restricts scrolling to horizontal or vertical direction at a time.
public var isFreeScrollingEnabled: Bool {
get { gridView.isFreeScrollingEnabled }
set { gridView.isFreeScrollingEnabled = newValue }
}
/// Maximum index up till which columns are frozen. Columns are frozen from 0 to this index value.
public var frozenColumnMaxIndex: Int? {
return gridView.frozenColumnMaxIndex
}
/// Maximum index up till which rows are frozen. Rows are frozen from 0 to this index value.
public var frozenRowMaxIndex: Int? {
return gridView.frozenRowMaxIndex
}
/// Determines if there are any frozen columns in the `GridView`
public var containsFrozenColumns: Bool {
gridView.frozenColumnMaxIndex != nil
}
/// Determines if there are any frozen rows in the `GridView`
public var containsFrozenRows: Bool {
gridView.frozenRowMaxIndex != nil
}
/// Collection of cells contained in the `GridView`
public var cells: [GridCell] {
gridView.cells
}
// Collection of cells currently selected in the `GridView`
public var selectedCells: [GridCell] {
gridView.selectedCells
}
/// Number of columns in the `GridView`.
public var numberOfColumns: Int {
gridView.numberOfColumns
}
/// Number of rows in the `GridView`
public var numberOfRows: Int {
gridView.numberOfRows
}
/// Initializes `GridView` using the provided configuration.
/// - Parameter
/// - config: Configuration for `GridView`
/// - cellEditorInitializer: Custom initializer for `EditorView` within `GridCell`. This will also be used when creating new cells as a
/// return of adding new row or column, or cells being split.
public convenience init(config: GridConfiguration, cellEditorInitializer: GridCell.EditorInitializer? = nil) {
let gridView = GridContentView(config: config, editorInitializer: cellEditorInitializer)
self.init(config: config, gridView: gridView)
}
/// Initializes `GridView` using the provided configuration.
/// - Parameters:
/// - config: Configuration for `GridView`
/// - cells: Cells contained within `GridView`
/// - cellEditorInitializer: Custom initializer for `EditorView` within `GridCell`. This will also be used when creating new cells as a
/// return of adding new row or column, or cells being split.
/// - Important:
/// Care must be taken that the number of cells are correct per the configuration provided, failing which the `GridView` rendering may be broken.
public convenience init(config: GridConfiguration, cells: [GridCell], cellEditorInitializer: GridCell.EditorInitializer? = nil) {
let gridView = GridContentView(config: config, cells: cells, editorInitializer: cellEditorInitializer)
self.init(config: config, gridView: gridView)
}
private init(config: GridConfiguration, gridView: GridContentView) {
self.gridView = gridView
let boundsShadowColors = [
config.boundsLimitShadowColors.primary.cgColor,
config.boundsLimitShadowColors.secondary.cgColor
]
self.leadingShadowView = GradientView(colors: boundsShadowColors)
self.leadingShadowView.alpha = 0.2
self.trailingShadowView = GradientView(colors: boundsShadowColors.reversed())
self.trailingShadowView.alpha = 0.2
self.config = config
super.init(frame: .zero)
self.leadingShadowConstraint = leadingShadowView.leadingAnchor.constraint(equalTo: self.leadingAnchor)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override var backgroundColor: UIColor? {
didSet {
gridView.backgroundColor = backgroundColor
}
}
private func setup() {
gridView.translatesAutoresizingMaskIntoConstraints = false
leadingShadowView.translatesAutoresizingMaskIntoConstraints = false
trailingShadowView.translatesAutoresizingMaskIntoConstraints = false
gridView.gridContentViewDelegate = self
gridView.delegate = self
addSubview(gridView)
addSubview(leadingShadowView)
addSubview(trailingShadowView)
NSLayoutConstraint.activate([
gridView.topAnchor.constraint(equalTo: topAnchor),
gridView.bottomAnchor.constraint(equalTo: bottomAnchor),
gridView.leadingAnchor.constraint(equalTo: leadingAnchor),
gridView.trailingAnchor.constraint(equalTo: trailingAnchor),
leadingShadowView.widthAnchor.constraint(equalToConstant: shadowWidth),
leadingShadowConstraint,
leadingShadowView.topAnchor.constraint(equalTo: topAnchor),
leadingShadowView.bottomAnchor.constraint(equalTo: bottomAnchor),
trailingShadowView.widthAnchor.constraint(equalToConstant: shadowWidth),
trailingShadowView.trailingAnchor.constraint(equalTo: trailingAnchor),
trailingShadowView.topAnchor.constraint(equalTo: topAnchor),
trailingShadowView.bottomAnchor.constraint(equalTo: bottomAnchor)
])
}
private func makeSelectionBorderView() -> UIView {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = tintColor
view.alpha = 0.4
return view
}
private func addColumnResizingHandles(selectedCell: GridCell) {
guard isColumnResizingHandlesVisible else { return }
for cell in cells where cell.columnSpan.max() == selectedCell.columnSpan.max() {
let handleView = makeColumnResizingHandle(cell: cell)
columnResizingHandles.append(handleView)
handleView.translatesAutoresizingMaskIntoConstraints = false
addSubview(handleView)
NSLayoutConstraint.activate([
handleView.widthAnchor.constraint(equalToConstant: handleSize),
handleView.heightAnchor.constraint(equalTo: handleView.widthAnchor),
handleView.centerYAnchor.constraint(equalTo: cell.contentView.bottomAnchor),
handleView.centerXAnchor.constraint(equalTo: cell.contentView.trailingAnchor)
])
}
addSelectionBorders(grid: self, cell: selectedCell)
}
private func addSelectionBorders(grid: GridView, cell: GridCell) {
addSubview(columnRightBorderView)
addSubview(columnLeftBorderView)
addSubview(columnTopBorderView)
addSubview(columnBottomBorderView)
NSLayoutConstraint.activate([
columnRightBorderView.centerXAnchor.constraint(equalTo: cell.contentView.trailingAnchor),
columnRightBorderView.widthAnchor.constraint(equalToConstant: cell.gridStyle.borderWidth * 2),
columnRightBorderView.heightAnchor.constraint(equalTo: gridView.heightAnchor),
columnRightBorderView.topAnchor.constraint(equalTo: gridView.topAnchor),
columnLeftBorderView.centerXAnchor.constraint(equalTo: cell.contentView.leadingAnchor),
columnLeftBorderView.widthAnchor.constraint(equalToConstant: cell.gridStyle.borderWidth * 2),
columnLeftBorderView.heightAnchor.constraint(equalTo: gridView.heightAnchor),
columnLeftBorderView.topAnchor.constraint(equalTo: gridView.topAnchor),
columnTopBorderView.centerYAnchor.constraint(equalTo: gridView.topAnchor),
columnTopBorderView.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor),
columnTopBorderView.heightAnchor.constraint(equalToConstant: cell.gridStyle.borderWidth * 2),
columnTopBorderView.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor),
columnBottomBorderView.centerYAnchor.constraint(equalTo: gridView.bottomAnchor),
columnBottomBorderView.widthAnchor.constraint(equalTo: cell.contentView.widthAnchor),
columnBottomBorderView.heightAnchor.constraint(equalToConstant: cell.gridStyle.borderWidth * 2),
columnBottomBorderView.leadingAnchor.constraint(equalTo: cell.contentView.leadingAnchor),
])
}
private func removeColumnResizingHandles() {
columnResizingHandles.forEach { $0.removeFromSuperview() }
columnResizingHandles.removeAll()
removeSelectionBorders()
}
private func removeSelectionBorders() {
columnRightBorderView.removeFromSuperview()
columnLeftBorderView.removeFromSuperview()
columnTopBorderView.removeFromSuperview()
columnBottomBorderView.removeFromSuperview()
}
private func resetColumnResizingHandles(selectedCell: GridCell) {
removeColumnResizingHandles()
addColumnResizingHandles(selectedCell: selectedCell)
}
private func makeColumnResizingHandle(cell: GridCell) -> CellHandleButton {
let dragHandle = CellHandleButton(cell: cell, cornerRadius: handleSize/2)
dragHandle.translatesAutoresizingMaskIntoConstraints = false
dragHandle.addGestureRecognizer(UIPanGestureRecognizer(target: self, action: #selector(dragHandler(gesture:))))
return dragHandle
}
@objc
private func dragHandler(gesture: UIPanGestureRecognizer){
guard let draggedView = gesture.view,
let cell = (draggedView as? CellHandleButton)?.cell else { return }
let location = gesture.location(in: self)
if gesture.state == .changed {
if let lastLocation = resizingDragHandleLastLocation {
let deltaX = location.x - lastLocation.x
gridView.changeColumnWidth(index: cell.columnSpan.max() ?? 0, delta: deltaX)
}
resizingDragHandleLastLocation = location
}
if gesture.state == .ended
|| gesture.state == .cancelled
|| gesture.state == .ended {
resizingDragHandleLastLocation = nil
}
}
/// Enables or disables column resizing
/// - Parameter enabled: `true` to enable resizing
public func setColumnResizing(_ enabled: Bool) {
isColumnResizingHandlesVisible = enabled
}
/// Gets the cell for the `EditorView` contained in the current instance
/// - Parameter editor: Editor for which cell needs to be queried.
/// - Returns: `GridCell` that contains the passed in `EditorView`, if present
public func cellFor(_ editor: EditorView) -> GridCell? {
return cells.first(where: { $0.editor == editor })
}
/// Selects given cells. Also, deselects any previously selected cells
/// - Parameter cells: Cells to select.
/// - Note:
/// Any combination of cells can be passed in, and will be selected, if possible.
public func selectCells(_ cells: [GridCell]) {
gridView.selectCells(cells)
}
/// Deselects any selected cell.
public func deselectCells() {
gridView.deselectCells()
}
/// Determines if the collection of cells can be merged. For cells to be mergable, they need to
/// be adjacent to each other, and the shape of selection needs to be rectangular.
/// - Parameter cells: Collection of cells to check if these can be merged.
/// - Returns: `true` is cells can be merged.
public func isCellSelectionMergeable(_ cells: [GridCell]) -> Bool {
gridView.isMergeable(cells: cells)
}
/// Merges the cells if the collection is mergeable.
/// - Parameter cells: Cells to merge.
public func merge(cells: [GridCell]) {
if let mergedCell = gridView.merge(cells: cells) {
resetColumnResizingHandles(selectedCell: mergedCell)
}
}
/// Splits the cell into original constituent cells from earlier Merge operation.
/// After split, the contents are held in the first original cell and all new split cells
/// are added as empty,
/// - Parameter cell: Cell to split.
public func split(cell: GridCell) {
let cells = gridView.split(cell: cell)
if let cell = cells.last {
resetColumnResizingHandles(selectedCell: cell)
}
}
/// Inserts a new row at given index.
/// - Parameters:
/// - index: Index at which new row should be inserted.
/// If the index is out of bounds, row will be inserted at the top or bottom of the grid based on index value
/// - configuration: Configuration for the new row
/// - Returns: Result with newly added cells for `.success`, error in case of `.failure`
@discardableResult
public func insertRow(at index: Int, configuration: GridRowConfiguration) -> Result<[GridCell], GridViewError> {
gridView.insertRow(at: index, configuration: configuration)
}
/// Inserts a new column at given index.
/// - Parameters:
/// - index: Index at which new column should be inserted.
/// If the index is out of bounds, column will be inserted at the beginning or end of the grid based on index value
/// - configuration: Configuration for the new column
/// - Returns: Result with newly added cells for `.success`, error in case of `.failure`
@discardableResult
public func insertColumn(at index: Int, configuration: GridColumnConfiguration) -> Result<[GridCell], GridViewError> {
gridView.insertColumn(at: index, configuration: configuration)
}
/// Deletes the row at given index
/// - Parameter index: Index to delete
public func deleteRow(at index: Int) {
gridView.deleteRow(at: index)
}
/// Deletes the column at given index
/// - Parameter index: Index to delete
public func deleteColumn(at index: Int) {
gridView.deleteColumn(at: index)
}
/// Freezes all the columns from 0 to the index provided
/// - Parameter maxIndex: Index to freeze upto
public func freezeColumns(upTo maxIndex: Int) {
gridView.frozenColumnMaxIndex = maxIndex
}
/// Freezes all the rows from 0 to the index provided
/// - Parameter maxIndex: Index to freeze upto
public func freezeRows(upTo maxIndex: Int) {
gridView.frozenRowMaxIndex = maxIndex
}
public func unfreezeColumns() {
gridView.frozenColumnMaxIndex = nil
}
public func unfreezeRows() {
gridView.frozenRowMaxIndex = nil
}
public func collapseRow(at index: Int) {
gridView.collapseRow(at: index)
}
func expandRow(at index: Int) {
gridView.expandRow(at: index)
}
func collapseColumn(at index: Int) {
gridView.collapseColumn(at: index)
}
func expandColumn(at index: Int) {
gridView.expandColumn(at: index)
}
func getCollapsedRowIndices() -> [Int] {
return gridView.getCollapsedRowIndices()
}
func getCollapsedColumnIndices() -> [Int] {
return gridView.getCollapsedColumnIndices()
}
/// Gets the cell at given row and column index. Indexes may be contained in a merged cell.
/// - Parameters:
/// - rowIndex: Row index for the cell
/// - columnIndex: Column index for the cell
/// - Returns: Cell at given row and column, if exists`
public func cellAt(rowIndex: Int, columnIndex: Int) -> GridCell? {
return gridView.cellAt(rowIndex: rowIndex, columnIndex: columnIndex)
}
/// Scrolls the cell at given index into viewable area. Indexes may be contained in a merged cell.
/// - Parameters:
/// - rowIndex: Row index of the cell
/// - columnIndex: Column index for the cell
/// - animated: Animates scroll if `true`
public func scrollToCellAt(rowIndex: Int, columnIndex: Int, animated: Bool = false) {
if let cell = cellAt(rowIndex: rowIndex, columnIndex: columnIndex) {
gridView.scrollTo(cell: cell, animated: animated)
}
}
/// Applies style to row at given index
/// - Parameters:
/// - style: Style to apply
/// - index: Index of the row
public func applyStyle(_ style: GridCellStyle, toRow index: Int) {
for cell in cells where cell.rowSpan.contains (index) {
cell.applyStyle(style)
}
}
/// Applies style to column at given index
/// - Parameters:
/// - style: Style to apply
/// - index: Index of the column
public func applyStyle(_ style: GridCellStyle, toColumn index: Int) {
for cell in cells where cell.columnSpan.contains (index) {
cell.applyStyle(style)
}
}
private func resetShadows() {
if let frozenColumnMaxIndex {
let frozenColumnWidth = gridView.columnWidths.prefix(upTo: frozenColumnMaxIndex + 1).reduce(0) { partialResult, dimension in
let viewport = gridView.bounds
return partialResult + dimension.value(basedOn: frame.size.width, viewportWidth: viewport.width)
}
let borderOffSet = self.config.style.borderWidth
leadingShadowConstraint.constant = frozenColumnWidth + borderOffSet
leadingShadowView.isHidden = gridView.contentOffset.x < 1
} else {
leadingShadowView.isHidden = gridView.contentOffset.x <= 0
}
trailingShadowView.isHidden = gridView.contentOffset.x + gridView.bounds.width >= gridView.contentSize.width
}
}
extension GridView: UIScrollViewDelegate {
public func scrollViewDidScroll(_ scrollView: UIScrollView) {
resetShadows()
}
}
extension GridView: GridContentViewDelegate {
func gridContentView(_ gridContentView: GridContentView, didCompleteLayoutWithBounds bounds: CGRect) {
resetShadows()
}
func gridContentView(_ gridContentView: GridContentView, didLayoutCell cell: GridCell) {
delegate?.gridView(self, didLayoutCell: cell)
}
func gridContentView(_ gridContentView: GridContentView, didSelectCells cells: [GridCell]) {
delegate?.gridView(self, didSelectCells: cells)
}
func gridContentView(_ gridContentView: GridContentView, didUnselectCells cells: [GridCell]) {
delegate?.gridView(self, didUnselectCells: cells)
}
func gridContentView(_ gridContentView: GridContentView, didReceiveFocusAt range: NSRange, in cell: GridCell) {
resetColumnResizingHandles(selectedCell: cell)
delegate?.gridView(self, didReceiveFocusAt: range, in: cell)
}
func gridContentView(_ gridContentView: GridContentView, didLoseFocusFrom range: NSRange, in cell: GridCell) {
removeSelectionBorders()
delegate?.gridView(self, didLoseFocusFrom: range, in: cell)
}
func gridContentView(_ gridContentView: GridContentView, didTapAtLocation location: CGPoint, characterRange: NSRange?, in cell: GridCell) {
delegate?.gridView(self, didTapAtLocation: location, characterRange: characterRange, in: cell)
}
func gridContentView(_ gridContentView: GridContentView, didChangeSelectionAt range: NSRange, attributes: [NSAttributedString.Key : Any], contentType: EditorContent.Name, in cell: GridCell) {
delegate?.gridView(self, didChangeSelectionAt: range, attributes: attributes, contentType: contentType, in: cell)
}
func gridContentView(_ gridContentView: GridContentView, didChangeBounds bounds: CGRect, in cell: GridCell) {
delegate?.gridView(self, didChangeBounds: bounds, in: cell)
}
func gridContentView(_ gridContentView: GridContentView, didReceiveKey key: EditorKey, at range: NSRange, in cell: GridCell) {
delegate?.gridView(self, didReceiveKey: key, at: range, in: cell)
}
func gridContentView(_ gridContentView: GridContentView, didAddNewRowAt index: Int) {
if let cell = gridView.cellAt(rowIndex: index, columnIndex: 0) {
cell.setFocus()
gridView.scrollTo(cell: cell)
}
}
func gridContentView(_ gridContentView: GridContentView, didAddNewColumnAt index: Int) {
if let cell = gridView.cellAt(rowIndex: 0, columnIndex: index) {
cell.setFocus()
gridView.scrollTo(cell: cell)
}
}
func gridContentView(_ gridContentView: GridContentView, didDeleteRowAt index: Int) {
}
func gridContentView(_ gridContentView: GridContentView, didDeleteColumnAt index: Int) {
}
func gridContentView(_ gridContentView: GridContentView, shouldChangeColumnWidth proposedWidth: CGFloat, for columnIndex: Int) -> Bool {
delegate?.gridView(self, shouldChangeColumnWidth: proposedWidth, for: columnIndex) ?? true
}
func gridContentView(_ gridContentView: GridContentView, cell: GridCell, didChangeBackgroundColor color: UIColor?, oldColor: UIColor?) {
}
}
class CellHandleButton: UIButton {
let cell: GridCell
init(cell: GridCell, cornerRadius: CGFloat) {
self.cell = cell
super.init(frame: .zero)
layer.cornerRadius = cornerRadius
alpha = 0.4
backgroundColor = tintColor
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension GridView: AsyncDeferredRenderable { }
extension GridView: BackgroundColorObserving {
public func containerEditor(_ editor: EditorView, backgroundColorUpdated color: UIColor?, oldColor: UIColor?) {
backgroundColor = color
}
}