Skip to content

Commit

Permalink
Fixed viewport calculations for handling multiple attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed Apr 16, 2024
1 parent ff0d76b commit fb644a8
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions ExampleApp/ExampleApp/Commands/TableViewCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand All @@ -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..<numRows {
for col in 0..<numColumns {
let text = generateRandomString(from: "Text in cell ")
let text = ""//generateRandomString(from: "Text in cell ")
let editorInit = {
let editor = EditorView(allowAutogrowing: false)
editor.attributedText = NSAttributedString(string: "Table \(id) {\(row), \(col)} \(text)")
Expand Down
6 changes: 6 additions & 0 deletions Proton/Sources/Swift/Table/TableContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ protocol TableContentViewDelegate: AnyObject {
func tableContentView(_ tableContentView: TableContentView, cell: TableCell, didChangeBackgroundColor color: UIColor?, oldColor: UIColor?)

func tableContentView(_ tableContentView: TableContentView, didUpdateCells cells: [TableCell])

func tableContentView(_ tableContentView: TableContentView, needsUpdateViewport delta: CGPoint)
}

class TableContentView: UIScrollView {
Expand Down Expand Up @@ -376,6 +378,10 @@ class TableContentView: UIScrollView {
contentSize = table.size
superview?.layoutIfNeeded()
boundsObserver?.didChangeBounds(CGRect(origin: bounds.origin, size: frame.size), oldBounds: bounds)

if diff > 0 {
// tableContentViewDelegate?.tableContentView(self, needsUpdateViewport: CGPoint(x: 0, y: diff))
}
}

private func recalculateCellBounds(initiatingCell: TableCell? = nil) {
Expand Down
28 changes: 16 additions & 12 deletions Proton/Sources/Swift/Table/TableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit fb644a8

Please sign in to comment.