From fb644a8cdbcd393cdebf66d0b4e08543c739e1e4 Mon Sep 17 00:00:00 2001 From: Rajdeep Kwatra Date: Tue, 16 Apr 2024 13:16:07 +1000 Subject: [PATCH] Fixed viewport calculations for handling multiple attachments --- .../CommandsExampleViewController.swift | 8 +++--- .../Commands/TableViewCommand.swift | 8 +++--- .../Swift/Table/TableContentView.swift | 6 ++++ Proton/Sources/Swift/Table/TableView.swift | 28 +++++++++++-------- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/ExampleApp/ExampleApp/AdvancedFeatures/CommandsExampleViewController.swift b/ExampleApp/ExampleApp/AdvancedFeatures/CommandsExampleViewController.swift index 050e4ce6..1e737bdc 100644 --- a/ExampleApp/ExampleApp/AdvancedFeatures/CommandsExampleViewController.swift +++ b/ExampleApp/ExampleApp/AdvancedFeatures/CommandsExampleViewController.swift @@ -476,10 +476,10 @@ extension CommandsExampleViewController: EditorViewDelegate { extension CommandsExampleViewController: TableViewDelegate { var viewport: CGRect? { var viewport = editor.scrollView.bounds -// let offset: CGFloat = 100 -// viewport.origin.y += offset -// viewport.size.height -= (offset * 2) -// Utility.drawRect(rect: CGRect(origin: CGPoint(x: 0, y: offset), size: viewport.size), color: .red, in: editor) + let offset: CGFloat = 100 + viewport.origin.y += offset + viewport.size.height -= (offset * 2) + Utility.drawRect(rect: CGRect(origin: CGPoint(x: 0, y: offset), size: viewport.size), color: .red, in: editor) return viewport } diff --git a/ExampleApp/ExampleApp/Commands/TableViewCommand.swift b/ExampleApp/ExampleApp/Commands/TableViewCommand.swift index 8e4fb3b5..5d0f491f 100644 --- a/ExampleApp/ExampleApp/Commands/TableViewCommand.swift +++ b/ExampleApp/ExampleApp/Commands/TableViewCommand.swift @@ -34,8 +34,8 @@ public class TableViewCommand: EditorCommand { text.append(NSAttributedString(string: "Text before Grid")) timeEvent(label: "Create") - for i in 1..<2 { - text.append(makeGridViewAttachment(id: i, numRows: 200, numColumns: 5).string) + for i in 1..<10 { + text.append(makeGridViewAttachment(id: i, numRows: 3, numColumns: 5).string) // text.append(makePanelAttachment(id: i).string) text.append(NSAttributedString(string: "\ntest middle\n")) } @@ -58,12 +58,12 @@ public class TableViewCommand: EditorCommand { private func makeGridViewAttachment(id: Int, numRows: Int, numColumns: Int) -> TableViewAttachment { let config = GridConfiguration(columnsConfiguration: [GridColumnConfiguration](repeating: GridColumnConfiguration(width: .fixed(100)), count: numColumns), - rowsConfiguration: [GridRowConfiguration](repeating: GridRowConfiguration(initialHeight: 100), count: numRows)) + rowsConfiguration: [GridRowConfiguration](repeating: GridRowConfiguration(initialHeight: 20), count: numRows)) var cells = [TableCell]() for row in 0.. 0 { +// tableContentViewDelegate?.tableContentView(self, needsUpdateViewport: CGPoint(x: 0, y: diff)) + } } private func recalculateCellBounds(initiatingCell: TableCell? = nil) { diff --git a/Proton/Sources/Swift/Table/TableView.swift b/Proton/Sources/Swift/Table/TableView.swift index 490cac75..44eae89d 100644 --- a/Proton/Sources/Swift/Table/TableView.swift +++ b/Proton/Sources/Swift/Table/TableView.swift @@ -347,20 +347,20 @@ public class TableView: UIView { } private func viewportChanged() { - guard tableView.bounds != .zero, - let container = delegate?.containerScrollView else { return } - let containerViewport = delegate?.viewport ?? container.bounds - let tableViewport = tableView.bounds - - let x = max(tableViewport.minX, containerViewport.minX) - let y = max(tableViewport.minY, containerViewport.minY) - - let width = min(tableViewport.width, containerViewport.width) - let height = min(tableViewport.height, containerViewport.height) + guard let attachmentContentView = tableView.attachmentContentView, + tableView.bounds != .zero, + let container = delegate?.containerScrollView, + (delegate?.viewport ?? container.bounds).intersects(attachmentContentView.frame) else { + cellsInViewport = [] + return + } - let viewport = CGRect(x: x, y: y, width: width, height: height) + let rootOrigin = attachmentContentView.frame.origin + let containerViewport = delegate?.viewport ?? container.bounds - cellsInViewport = tableView.cells.filter{ $0.frame != .zero && $0.frame.intersects(viewport) } + cellsInViewport = tableView.cells.filter{ + $0.frame != .zero + && $0.frame.offsetBy(dx: rootOrigin.x, dy: rootOrigin.y).intersects(containerViewport) } } private func makeSelectionBorderView() -> UIView { @@ -670,6 +670,10 @@ extension TableView: TableContentViewDelegate { self.delegate?.viewport } + func tableContentView(_ tableContentView: TableContentView, needsUpdateViewport delta: CGPoint) { + viewportChanged() + } + func tableContentView(_ tableContentView: TableContentView, didChangeBounds bounds: CGRect, oldBounds: CGRect) { viewportChanged() }