Skip to content

Commit

Permalink
Added min-max constraints to fixed column width for Grids
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep committed Mar 15, 2024
1 parent 421390e commit 6bb11ae
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
62 changes: 40 additions & 22 deletions Proton/Sources/Swift/Grid/Core/GridConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,23 @@ class GridColumnDimension {

/// Defines how Grid Column width should be calculated
public enum GridColumnWidth {
public enum FractionalWidth {
public enum ConstrainedWidth {
case absolute(CGFloat)
case viewport(padding: CGFloat)
}

/// Defines a fixed with for column
/// - Parameter : `CGFloat` value for width.
case fixed(CGFloat)
/// - min: Closure providing minimum value for column. If computed fractional value is less than min, min is used.
/// - max: Closure providing maximum value for column. If computed fractional value is more than max, max is used.
case fixed(CGFloat, min: (() -> ConstrainedWidth)? = nil, max: (() -> ConstrainedWidth)? = nil)
/// Defines a fixed with for column
/// - Parameters :
/// - : `CGFloat` value for percentage of available width.
/// - min: Closure providing minimum value for column. If computed fractional value is less than min, min is used.
/// - max: Closure providing maximum value for column. If computed fractional value is more than max, max is used.
/// - Note: Percentage is calculated based on total available width for GridView, typically, width of containing `EditorView`
case fractional(CGFloat, min: (() -> FractionalWidth)? = nil, max: (() -> FractionalWidth)? = nil)
case fractional(CGFloat, min: (() -> ConstrainedWidth)? = nil, max: (() -> ConstrainedWidth)? = nil)

/// Defines width based on available viewport.
/// - Parameter padding: Padding for adjusting width with respect to viewport. Positive values decreases column width from viewport width and negative
Expand All @@ -69,31 +71,47 @@ public enum GridColumnWidth {

func value(basedOn total: CGFloat, viewportWidth: CGFloat) -> CGFloat {
switch self {
case let .fixed(value):
return value
case let .fixed(fixedValue, minVal, maxVal):
return getConstrainedWidth(originalValue: fixedValue,
viewportWidth: viewportWidth,
minVal: minVal,
maxVal: maxVal)

case let .fractional(value, minVal, maxVal):
let fractionalValue = value * total
if let minVal = minVal?() {
switch minVal {
case .absolute(let value):
return max(value, fractionalValue)
case .viewport(let padding):
return max(viewportWidth - padding, fractionalValue)
}
}
if let maxVal = maxVal?() {
switch maxVal {
case .absolute(let value):
return min(value, fractionalValue)
case .viewport(let padding):
return min(viewportWidth - padding, fractionalValue)
}
}
return fractionalValue
return getConstrainedWidth(originalValue: fractionalValue,
viewportWidth: viewportWidth,
minVal: minVal,
maxVal: maxVal)


case let .viewport(padding):
return viewportWidth - padding
}
}

private func getConstrainedWidth(originalValue: CGFloat,
viewportWidth: CGFloat,
minVal: (() -> ConstrainedWidth)?,
maxVal: (() -> ConstrainedWidth)?) -> CGFloat {
if let minVal = minVal?() {
switch minVal {
case .absolute(let value):
return max(value, originalValue)
case .viewport(let padding):
return max(viewportWidth - padding, originalValue)
}
}
if let maxVal = maxVal?() {
switch maxVal {
case .absolute(let value):
return min(value, originalValue)
case .viewport(let padding):
return min(viewportWidth - padding, originalValue)
}
}
return originalValue
}
}

public struct GridColumnConfiguration {
Expand Down
38 changes: 38 additions & 0 deletions Proton/Tests/Grid/GridViewAttachmentSnapshotTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ class GridViewAttachmentSnapshotTests: SnapshotTestCase {
assertSnapshot(matching: viewController.view, as: .image, record: recordMode)
}

func testRendersGridViewAttachmentWithConstrainedFixedWidth() {
recordMode = true
let viewController = EditorTestViewController()
let editor = viewController.editor
let config = GridConfiguration(
columnsConfiguration: [
GridColumnConfiguration(width: .fixed(40)),
GridColumnConfiguration(width: .fixed(50, min: { .absolute(60) })),
GridColumnConfiguration(width: .fixed(500, max: { .viewport(padding: 0) })),
],
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")

XCTAssertEqual(attachment.view.containerAttachment, attachment)

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

Check failure on line 76 in Proton/Tests/Grid/GridViewAttachmentSnapshotTests.swift

View workflow job for this annotation

GitHub Actions / Xcode test results

Assertion Failure

failed - Record mode is on. Turn record mode off and re-run "testRendersGridViewAttachmentWithConstrainedFixedWidth" to test against the newly-recorded snapshot. open "/Users/runner/work/proton/proton/Proton/Tests/Grid/__Snapshots__/GridViewAttachmentSnapshotTests/testRendersGridViewAttachmentWithConstrainedFixedWidth.1.png" Recorded snapshot: …

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 lineFragmentPadding = editor.lineFragmentPadding
let cellOverlapPixels: CGFloat = 1

XCTAssertEqual((cell00?.frame.width ?? 0) - cellOverlapPixels, 40)
XCTAssertEqual((cell01?.frame.width ?? 0) - cellOverlapPixels, 60)
XCTAssertEqual((cell02?.frame.width ?? 0) - cellOverlapPixels, 350)
}


func testRendersGridViewAttachmentWithViewportConstraints() {
let viewController = EditorTestViewController()
let editor = viewController.editor
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 6bb11ae

Please sign in to comment.