Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revised logic to constraint column width based on both min and max #357

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading