Skip to content

Commit

Permalink
Add LayoutGuide relevant DimensionalLayout
Browse files Browse the repository at this point in the history
  • Loading branch information
kkiermasz committed Nov 11, 2023
1 parent e15b843 commit c0cf2a2
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 38 deletions.
18 changes: 18 additions & 0 deletions Sources/Neron/Contracts/AnchorLayout/DimensionLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public protocol DimensionLayout {
@discardableResult
func greaterThanOrEqualTo(_ sibling: UIView, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier

@discardableResult
func equalTo(_ layoutGuide: UILayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier

@discardableResult
func lessThanOrEqualTo(_ layoutGuide: UILayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier

@discardableResult
func greaterThanOrEqualTo(_ layoutGuide: UILayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier

#else

@discardableResult
Expand All @@ -42,6 +51,15 @@ public protocol DimensionLayout {
@discardableResult
func greaterThanOrEqualTo(_ sibling: NSView, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier

@discardableResult
func equalTo(_ layoutGuide: NSLayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier

@discardableResult
func lessThanOrEqualTo(_ layoutGuide: NSLayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier

@discardableResult
func greaterThanOrEqualTo(_ layoutGuide: NSLayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier

#endif

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
extension LayoutGuideLayout {

final class LayoutGuideDimensionLayout: DimensionLayout {

// MARK: - Properties

private let anchor: LayoutAnchor.Dimension
Expand Down Expand Up @@ -47,7 +47,11 @@ extension LayoutGuideLayout {
}

func equalTo(_ sibling: View, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
let constraint = LayoutConstraint(item: layoutGuide, itemAttribute: self.anchor.attribute, relation: .equal, target: sibling, targetAttribute: anchor.attribute)
let constraint = LayoutConstraint(item: layoutGuide,
itemAttribute: self.anchor.attribute,
relation: .equal,
target: sibling,
targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}

Expand All @@ -68,7 +72,34 @@ extension LayoutGuideLayout {
targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}


func equalTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
let constraint = LayoutConstraint(item: self.layoutGuide,
itemAttribute: self.anchor.attribute,
relation: .equal,
target: layoutGuide,
targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}

func lessThanOrEqualTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
let constraint = LayoutConstraint(item: self.layoutGuide,
itemAttribute: self.anchor.attribute,
relation: .lessThanOrEqual,
target: layoutGuide,
targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}

func greaterThanOrEqualTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
let constraint = LayoutConstraint(item: self.layoutGuide,
itemAttribute: self.anchor.attribute,
relation: .greaterThanOrEqual,
target: layoutGuide,
targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}

// MARK: - Private

private func endorse(_ constraint: LayoutConstraint) -> LayoutConstraint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
extension DefaultLayoutResult {

final class ResultDimensionLayout: DimensionLayout {

// MARK: - Properties

private let anchor: LayoutAnchor.Dimension
Expand Down Expand Up @@ -57,7 +57,19 @@ extension DefaultLayoutResult {
func greaterThanOrEqualTo(_ sibling: View, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
invokerLayout.greaterThanOrEqualTo(sibling, anchor)
}


func equalTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
invokerLayout.equalTo(layoutGuide, anchor)
}

func lessThanOrEqualTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
invokerLayout.lessThanOrEqualTo(layoutGuide, anchor)
}

func greaterThanOrEqualTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
invokerLayout.greaterThanOrEqualTo(layoutGuide, anchor)
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,22 @@ extension ViewLayout {
let constraint = LayoutConstraint(item: view, itemAttribute: self.anchor.attribute, relation: .greaterThanOrEqual, target: sibling, targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}


func equalTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
let constraint = LayoutConstraint(item: view, itemAttribute: self.anchor.attribute, relation: .equal, target: layoutGuide, targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}

func lessThanOrEqualTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
let constraint = LayoutConstraint(item: view, itemAttribute: self.anchor.attribute, relation: .lessThanOrEqual, target: layoutGuide, targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}

func greaterThanOrEqualTo(_ layoutGuide: LayoutGuide, _ anchor: LayoutAnchor.Dimension) -> ConstraintMultiplier {
let constraint = LayoutConstraint(item: view, itemAttribute: self.anchor.attribute, relation: .greaterThanOrEqual, target: layoutGuide, targetAttribute: anchor.attribute)
return makePrioritizer(for: endorse(constraint))
}

// MARK: - Private

private func endorse(_ constraint: LayoutConstraint) -> LayoutConstraint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ final class LayoutGuideLayout_Layout_Dimension_Tests: XCTestCase {

func test_height_EqualToSibling() {
let guide = LayoutGuide()
print(parent.frame)
let constraint = guide.layout
.add(to: parent)
.height.equalTo(parent, .height)
Expand All @@ -73,30 +72,73 @@ final class LayoutGuideLayout_Layout_Dimension_Tests: XCTestCase {

func test_height_LessThanOrEqualToSibling() {
let guide = LayoutGuide()
print(parent.frame)
let constraint = guide.layout
.add(to: parent)
.height.equalTo(parent, .height)
.height.lessThanOrEqualTo(parent, .height)
.activate()
.constraint
parent.prepare()
XCTAssertLessThanOrEqual(guide.frame.height, 200, "Height should be 200")
XCTAssertEqual(constraint.relation, .equal, "Relation should be lessThanOrEqual")
XCTAssertEqual(constraint.relation, .lessThanOrEqual, "Relation should be lessThanOrEqual")
}

func test_height_GreaterThanOrEqualToSibling() {
let guide = LayoutGuide()
print(parent.frame)
let constraint = guide.layout
.add(to: parent)
.height.equalTo(parent, .height)
.height.greaterThanOrEqualTo(parent, .height)
.activate()
.constraint
parent.prepare()
XCTAssertGreaterThanOrEqual(guide.frame.height, 200, "Height should be 200")
XCTAssertEqual(constraint.relation, .equal, "Relation should be greaterThanOrEqual")
XCTAssertEqual(constraint.relation, .greaterThanOrEqual, "Relation should be greaterThanOrEqual")
}


func test_Height_EqualToLayoutGuide() {
let height: CGFloat = 150
let parent = TestView(height: height)
let parentGuide = parent.layoutGuide
let guide = LayoutGuide()
let constraint = guide.layout
.add(to: parent)
.height.equalTo(parentGuide, .height)
.activate()
.constraint
parent.prepare()
XCTAssertEqual(guide.frame.height, height, "Height should be \(height)")
XCTAssertEqual(constraint.relation, .equal, "Relation should be equal")
}

func test_Height_LessThanOrEqualToLayoutGuide() {
let height: CGFloat = 150
let parent = TestView(height: height)
let parentGuide = parent.layoutGuide
let guide = LayoutGuide()
let constraint = guide.layout
.add(to: parent)
.height.lessThanOrEqualTo(parentGuide, .height)
.activate()
.constraint
parent.prepare()
XCTAssertEqual(guide.frame.height, height, "Height should be \(height)")
XCTAssertEqual(constraint.relation, .lessThanOrEqual, "Relation should be lessThanOrEqual")
}

func test_Height_GreaterThanOrEqualToLayoutGuide() {
let height: CGFloat = 150
let parent = TestView(height: height)
let parentGuide = parent.layoutGuide
let guide = LayoutGuide()
let constraint = guide.layout
.add(to: parent)
.height.greaterThanOrEqualTo(parentGuide, .height)
.activate()
.constraint
parent.prepare()
XCTAssertEqual(guide.frame.height, height, "Height should be \(height)")
XCTAssertEqual(constraint.relation, .greaterThanOrEqual, "Relation should be greaterThanOrEqual")
}

// MARK: - Width

func test_width_EqualToConstant() {
Expand Down Expand Up @@ -137,7 +179,6 @@ final class LayoutGuideLayout_Layout_Dimension_Tests: XCTestCase {

func test_width_EqualToSibling() {
let guide = LayoutGuide()
print(parent.frame)
let constraint = guide.layout
.add(to: parent)
.width.equalTo(parent, .width)
Expand All @@ -150,7 +191,6 @@ final class LayoutGuideLayout_Layout_Dimension_Tests: XCTestCase {

func test_width_LessThanOrEqualToSibling() {
let guide = LayoutGuide()
print(parent.frame)
let constraint = guide.layout
.add(to: parent)
.width.equalTo(parent, .width)
Expand All @@ -163,7 +203,6 @@ final class LayoutGuideLayout_Layout_Dimension_Tests: XCTestCase {

func test_width_GreaterThanOrEqualToSibling() {
let guide = LayoutGuide()
print(parent.frame)
let constraint = guide.layout
.add(to: parent)
.width.equalTo(parent, .width)
Expand All @@ -173,5 +212,50 @@ final class LayoutGuideLayout_Layout_Dimension_Tests: XCTestCase {
XCTAssertGreaterThanOrEqual(guide.frame.width, 200, "Width should be 200")
XCTAssertEqual(constraint.relation, .equal, "Relation should be greaterThanOrEqual")
}


func test_Width_EqualToLayoutGuide() {
let width: CGFloat = 150
let parent = TestView(width: width)
let parentGuide = parent.layoutGuide
let guide = LayoutGuide()
let constraint = guide.layout
.add(to: parent)
.width.equalTo(parentGuide, .width)
.activate()
.constraint
parent.prepare()
XCTAssertEqual(guide.frame.width, width, "Width should be \(width)")
XCTAssertEqual(constraint.relation, .equal, "Relation should be equal")
}

func test_Width_LessThanOrEqualToLayoutGuide() {
let width: CGFloat = 150
let parent = TestView(width: width)
let parentGuide = parent.layoutGuide
let guide = LayoutGuide()
let constraint = guide.layout
.add(to: parent)
.width.lessThanOrEqualTo(parentGuide, .width)
.activate()
.constraint
parent.prepare()
XCTAssertEqual(guide.frame.width, width, "Width should be \(width)")
XCTAssertEqual(constraint.relation, .lessThanOrEqual, "Relation should be lessThanOrEqual")
}

func test_Width_GreaterThanOrEqualToLayoutGuide() {
let width: CGFloat = 150
let parent = TestView(width: width)
let parentGuide = parent.layoutGuide
let guide = LayoutGuide()
let constraint = guide.layout
.add(to: parent)
.width.greaterThanOrEqualTo(parentGuide, .width)
.activate()
.constraint
parent.prepare()
XCTAssertEqual(guide.frame.width, width, "Width should be \(width)")
XCTAssertEqual(constraint.relation, .greaterThanOrEqual, "Relation should be greaterThanOrEqual")
}

}
9 changes: 5 additions & 4 deletions Tests/NeronTests/TestClasses/TestView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// Copyright © 2020 Jakub Kiermasz. All rights reserved.
//


@testable import Neron

final class TestView: View {

// MARK: - Properties

let width: CGFloat = 200
let height: CGFloat = 200
let width: CGFloat
let height: CGFloat

// MARK: - Getters

Expand All @@ -26,7 +25,9 @@ final class TestView: View {

// MARK: - Initialization

init() {
init(width: CGFloat = 200, height: CGFloat = 200) {
self.width = width
self.height = height
super.init(frame: CGRect(x: 0, y: 0, width: width, height: height))
}

Expand Down
Loading

0 comments on commit c0cf2a2

Please sign in to comment.