Skip to content

Commit

Permalink
Revised logic to constraint column width based on both min and max
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed Jan 10, 2025
1 parent 9f862d6 commit eeb7e46
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
25 changes: 20 additions & 5 deletions Proton/Sources/Swift/Grid/Core/GridConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,38 @@ public enum GridColumnWidth {
viewportWidth: CGFloat,
minVal: (() -> ConstrainedWidth)?,
maxVal: (() -> ConstrainedWidth)?) -> CGFloat {

var minCalculated: CGFloat?
var maxCalculated: CGFloat?

if let minVal = minVal?() {
switch minVal {
case .absolute(let value):
return max(value, originalValue)
minCalculated = max(value, originalValue)
case .viewport(let padding):
return max(viewportWidth - padding, originalValue)
minCalculated = max(viewportWidth - padding, originalValue)
}
}

if let maxVal = maxVal?() {
switch maxVal {
case .absolute(let value):
return min(value, originalValue)
maxCalculated = min(value, originalValue)
case .viewport(let padding):
return min(viewportWidth - padding, originalValue)
maxCalculated = min(viewportWidth - padding, originalValue)
}
}

switch (minCalculated, maxCalculated) {
case let (min?, max?):
return originalValue.isBetween(min, max) ? originalValue : (originalValue < min ? min : max)
case let (min?, nil):
return originalValue < min ? min : originalValue
case let (nil, max?):
return originalValue > max ? max : originalValue
case (nil, nil):
return originalValue
}
return originalValue
}
}

Expand Down
34 changes: 34 additions & 0 deletions Proton/Tests/Grid/GridViewAttachmentSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,40 @@ class GridViewAttachmentSnapshotTests: SnapshotTestCase {
XCTAssertEqual((cell02?.frame.width ?? 0) - cellOverlapPixels, 75)
}

func testRendersGridViewAttachmentWithWidthBetweenMinAndMax() {
let viewController = EditorTestViewController()
let editor = viewController.editor
let config = GridConfiguration(
columnsConfiguration: [
GridColumnConfiguration(width: .fractional(0.10, min: { .absolute(50)}, max: { .absolute(80) })),
GridColumnConfiguration(width: .fractional(0.40, min: { .absolute(50)}, max: { .absolute(80) })),
GridColumnConfiguration(width: .fixed(40, min: { .absolute(50)} , max: { .absolute(80)})),
GridColumnConfiguration(width: .fixed(90, min: { .absolute(50)} , max: { .absolute(80)})),
],
rowsConfiguration: [
GridRowConfiguration(initialHeight: 40),
GridRowConfiguration(initialHeight: 40),
])
let attachment = GridViewAttachment(config: config)

editor.replaceCharacters(in: .zero, with: "Some text in editor")
editor.insertAttachment(in: editor.textEndRange, attachment: attachment)
editor.replaceCharacters(in: editor.textEndRange, with: "Text after grid")

viewController.render(size: CGSize(width: 300, height: 200))
assertSnapshot(of: viewController.view, as: .image, record: recordMode)

let cell00 = attachment.view.cellAt(rowIndex: 0, columnIndex: 0)
let cell01 = attachment.view.cellAt(rowIndex: 0, columnIndex: 1)
let cell02 = attachment.view.cellAt(rowIndex: 0, columnIndex: 2)
let cell03 = attachment.view.cellAt(rowIndex: 0, columnIndex: 3)

let cellOverlapPixels: CGFloat = 1
XCTAssertEqual((cell00?.frame.width ?? 0) - cellOverlapPixels, 50)
XCTAssertEqual((cell01?.frame.width ?? 0) - cellOverlapPixels, 80)
XCTAssertEqual((cell02?.frame.width ?? 0) - cellOverlapPixels, 50)
XCTAssertEqual((cell03?.frame.width ?? 0) - cellOverlapPixels, 80)
}

func testUpdatesCellSizeBasedOnContent() {
let viewController = EditorTestViewController()
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eeb7e46

Please sign in to comment.