Skip to content

Commit

Permalink
convert to custom operator ~==
Browse files Browse the repository at this point in the history
  • Loading branch information
anomaddev committed Dec 14, 2022
1 parent d0dc3f3 commit 1105670
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Cartography/Compound.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,28 @@ public protocol RelativeCompoundInequality : Compound { }
@discardableResult public func >= <P: RelativeCompoundInequality>(lhs: P, rhs: Expression<P>) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients, to: rhs.value, relation: .greaterThanOrEqual)
}

// MARK: - Aliases
@discardableResult public func ~== <P: RelativeCompoundEquality>(lhs: P, rhs: Expression<P>) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients, to: rhs.value)
}

@discardableResult public func ~== <P: RelativeCompoundEquality>(lhs: P, rhs: P) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, to: rhs)
}

@discardableResult public func ~<= <P: RelativeCompoundInequality>(lhs: P, rhs: P) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, to: rhs, relation: .lessThanOrEqual)
}

@discardableResult public func ~>= <P: RelativeCompoundInequality>(lhs: P, rhs: P) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, to: rhs, relation: .greaterThanOrEqual)
}

@discardableResult public func ~<= <P: RelativeCompoundInequality>(lhs: P, rhs: Expression<P>) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients, to: rhs.value, relation: .lessThanOrEqual)
}

@discardableResult public func ~>= <P: RelativeCompoundInequality>(lhs: P, rhs: Expression<P>) -> [NSLayoutConstraint] {
return lhs.context.addConstraint(lhs, coefficients: rhs.coefficients, to: rhs.value, relation: .greaterThanOrEqual)
}
8 changes: 8 additions & 0 deletions Cartography/Priority.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ precedencegroup CarthographyPriorityPrecedence {

infix operator ~: CarthographyPriorityPrecedence

infix operator ~==: AssignmentPrecedence
infix operator ~<=: AssignmentPrecedence
infix operator ~>=: AssignmentPrecedence
infix operator ~+: AdditionPrecedence
infix operator ~-: AdditionPrecedence
infix operator ~*: MultiplicationPrecedence
infix operator ~/: MultiplicationPrecedence

/// Sets the priority for a constraint.
///
/// - parameter lhs: The constraint to update.
Expand Down
105 changes: 105 additions & 0 deletions Cartography/Property.swift
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,108 @@ public func / <P: Multiplication>(lhs: Expression<P>, rhs: CGFloat) -> Expressio
public func / <P: Multiplication>(lhs: P, rhs: CGFloat) -> Expression<P> {
return lhs * (1 / rhs)
}

// MARK: - Aliases
@discardableResult public func ~== (lhs: NumericalEquality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs))
}

@discardableResult public func ~== <P: RelativeEquality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs.value, coefficients: rhs.coefficients[0])
}

@discardableResult public func ~== <P: RelativeEquality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs)
}

public func ~+ <P: Addition>(c: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(1, c) ])
}

public func ~+ <P: Addition>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs + lhs
}

public func ~+ <P: Addition>(c: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 + c })
}

public func ~+ <P: Addition>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs + lhs
}

public func ~- <P: Addition>(c: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(1, -c) ])
}

public func ~- <P: Addition>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs - lhs
}

public func ~- <P: Addition>(c: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 - c})
}

public func ~- <P: Addition>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs - lhs
}

#if os(iOS) || os(tvOS)

public func ~+ (lhs: LayoutSupport, c : CGFloat) -> Expression<LayoutSupport> {
return Expression<LayoutSupport>(lhs, [Coefficients(1, c)])
}

public func ~- (lhs: LayoutSupport, c : CGFloat) -> Expression<LayoutSupport> {
return lhs + (-c)
}

#endif

public func ~* <P: Multiplication>(m: CGFloat, rhs: Expression<P>) -> Expression<P> {
return Expression(rhs.value, rhs.coefficients.map { $0 * m })
}

public func ~* <P: Multiplication>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return rhs * lhs
}

public func ~* <P: Multiplication>(m: CGFloat, rhs: P) -> Expression<P> {
return Expression(rhs, [ Coefficients(m, 0) ])
}

public func ~* <P: Multiplication>(lhs: P, rhs: CGFloat) -> Expression<P> {
return rhs * lhs
}

public func ~/ <P: Multiplication>(lhs: Expression<P>, rhs: CGFloat) -> Expression<P> {
return lhs * (1 / rhs)
}

public func ~/ <P: Multiplication>(lhs: P, rhs: CGFloat) -> Expression<P> {
return lhs * (1 / rhs)
}

@discardableResult public func ~<= (lhs: NumericalInequality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs), relation: .lessThanOrEqual)
}

@discardableResult public func ~>= (lhs: NumericalInequality, rhs: CGFloat) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, coefficients: Coefficients(1, rhs), relation: .greaterThanOrEqual)
}

@discardableResult public func ~<= <P: RelativeInequality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs, relation: .lessThanOrEqual)
}

@discardableResult public func ~>= <P: RelativeInequality>(lhs: P, rhs: P) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs, relation: .greaterThanOrEqual)
}

@discardableResult public func ~<= <P: RelativeInequality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs.value, coefficients: rhs.coefficients[0], relation: .lessThanOrEqual)
}

@discardableResult public func ~>= <P: RelativeInequality>(lhs: P, rhs: Expression<P>) -> NSLayoutConstraint {
return lhs.context.addConstraint(lhs, to: rhs.value, coefficients: rhs.coefficients[0], relation: .greaterThanOrEqual)
}

0 comments on commit 1105670

Please sign in to comment.