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

Minor improvements and fixes to Cartography #278

Merged
merged 6 commits into from
Nov 22, 2017
Merged
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
4 changes: 2 additions & 2 deletions Cartography.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "Cartography"
s.version = "3.0.0"
s.version = "3.0.1"
s.summary = "Declarative Auto Layout in Swift"

s.description = <<-DESC
Expand All @@ -13,7 +13,7 @@ Pod::Spec.new do |s|
s.author = { "Robert Böhnke" => "[email protected]" }

s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.9"
s.osx.deployment_target = "10.10"
s.tvos.deployment_target = "9.0"

s.source = { :git => "https://github.com/robb/Cartography.git", :tag => s.version }
Expand Down
16 changes: 8 additions & 8 deletions Cartography.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
977C36821F9FAC890057A057 /* AutoresizingMaskLayoutProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 977C36811F9FAC890057A057 /* AutoresizingMaskLayoutProxy.swift */; };
977C36831F9FC2900057A057 /* AutoresizingMaskLayoutProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 977C36811F9FAC890057A057 /* AutoresizingMaskLayoutProxy.swift */; };
977C36841F9FC2910057A057 /* AutoresizingMaskLayoutProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 977C36811F9FAC890057A057 /* AutoresizingMaskLayoutProxy.swift */; };
97885B1E1FC4F7C800BB4E51 /* LayoutGuideSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 979F29C91F94DC6B00257363 /* LayoutGuideSpec.swift */; };
979558E41F9700B40096BBEA /* LayoutGuideSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 979F29C91F94DC6B00257363 /* LayoutGuideSpec.swift */; };
979558EC1F97015E0096BBEA /* TestView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97D17C9E1F8E774700C57CE1 /* TestView.swift */; };
979558ED1F97015E0096BBEA /* TestView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97D17C9E1F8E774700C57CE1 /* TestView.swift */; };
Expand Down Expand Up @@ -138,8 +139,8 @@
97D17CAC1F8E779300C57CE1 /* LayoutGuideProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97D17CA91F8E779300C57CE1 /* LayoutGuideProxy.swift */; };
97E7F0A91F8D598A004857CE /* ViewProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97E7F0A81F8D598A004857CE /* ViewProxy.swift */; };
97F50E521F962CF300C6DCF5 /* LayoutProxy+TypeErasure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97F50E511F962CF300C6DCF5 /* LayoutProxy+TypeErasure.swift */; };
97F50E541F9633AA00C6DCF5 /* SafeAreaLayoutGuideSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97F50E531F9633AA00C6DCF5 /* SafeAreaLayoutGuideSpec.swift */; };
97F50E551F9633AA00C6DCF5 /* SafeAreaLayoutGuideSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97F50E531F9633AA00C6DCF5 /* SafeAreaLayoutGuideSpec.swift */; };
97F50E541F9633AA00C6DCF5 /* ViewLayoutGuideSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97F50E531F9633AA00C6DCF5 /* ViewLayoutGuideSpec.swift */; };
97F50E551F9633AA00C6DCF5 /* ViewLayoutGuideSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97F50E531F9633AA00C6DCF5 /* ViewLayoutGuideSpec.swift */; };
97F50E561F96401200C6DCF5 /* LayoutProxy+TypeErasure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97F50E511F962CF300C6DCF5 /* LayoutProxy+TypeErasure.swift */; };
97F50E571F96401300C6DCF5 /* LayoutProxy+TypeErasure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97F50E511F962CF300C6DCF5 /* LayoutProxy+TypeErasure.swift */; };
A75B6143FF12C54FF3223B47 /* Pods_TestPods_Cartography_tvOS_tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0827A83361EACF1E6062607E /* Pods_TestPods_Cartography_tvOS_tests.framework */; };
Expand Down Expand Up @@ -259,7 +260,6 @@
97D17C9F1F8E774700C57CE1 /* Matchers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Matchers.swift; sourceTree = "<group>"; };
97D17CA01F8E774700C57CE1 /* DistributeSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DistributeSpec.swift; sourceTree = "<group>"; };
97D17CA11F8E774700C57CE1 /* PointSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PointSpec.swift; sourceTree = "<group>"; };
97D17CA21F8E774700C57CE1 /* EdgesSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EdgesSpec.swift; sourceTree = "<group>"; };
97D17CA31F8E774700C57CE1 /* ViewHierarchySpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewHierarchySpec.swift; sourceTree = "<group>"; };
97D17CA41F8E774700C57CE1 /* DimensionSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DimensionSpec.swift; sourceTree = "<group>"; };
97D17CA51F8E774700C57CE1 /* MemoryLeakSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryLeakSpec.swift; sourceTree = "<group>"; };
Expand All @@ -268,7 +268,7 @@
97D17CA91F8E779300C57CE1 /* LayoutGuideProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutGuideProxy.swift; sourceTree = "<group>"; };
97E7F0A81F8D598A004857CE /* ViewProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewProxy.swift; sourceTree = "<group>"; };
97F50E511F962CF300C6DCF5 /* LayoutProxy+TypeErasure.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "LayoutProxy+TypeErasure.swift"; sourceTree = "<group>"; };
97F50E531F9633AA00C6DCF5 /* SafeAreaLayoutGuideSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SafeAreaLayoutGuideSpec.swift; sourceTree = "<group>"; };
97F50E531F9633AA00C6DCF5 /* ViewLayoutGuideSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewLayoutGuideSpec.swift; sourceTree = "<group>"; };
EE85314F1F9363DC003EC021 /* LayoutItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LayoutItem.swift; sourceTree = "<group>"; };
EEDD4098FF7503B1F9188F10 /* Pods_Cartography_iOS_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Cartography_iOS_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -467,13 +467,12 @@
97D17C9F1F8E774700C57CE1 /* Matchers.swift */,
97D17CA01F8E774700C57CE1 /* DistributeSpec.swift */,
97D17CA11F8E774700C57CE1 /* PointSpec.swift */,
97D17CA21F8E774700C57CE1 /* EdgesSpec.swift */,
97D17CA31F8E774700C57CE1 /* ViewHierarchySpec.swift */,
97D17CA41F8E774700C57CE1 /* DimensionSpec.swift */,
97D17CA51F8E774700C57CE1 /* MemoryLeakSpec.swift */,
97D17CA61F8E774700C57CE1 /* ConstraintGroupSpec.swift */,
97D17CA81F8E774700C57CE1 /* SizeSpec.swift */,
97F50E531F9633AA00C6DCF5 /* SafeAreaLayoutGuideSpec.swift */,
97F50E531F9633AA00C6DCF5 /* ViewLayoutGuideSpec.swift */,
);
path = CartographyTests;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -935,7 +934,7 @@
buildActionMask = 2147483647;
files = (
979558F61F97017D0096BBEA /* LayoutSupportSpec.swift in Sources */,
97F50E541F9633AA00C6DCF5 /* SafeAreaLayoutGuideSpec.swift in Sources */,
97F50E541F9633AA00C6DCF5 /* ViewLayoutGuideSpec.swift in Sources */,
9795590B1F9701CD0096BBEA /* ConstraintGroupSpec.swift in Sources */,
979559081F9701C90096BBEA /* MemoryLeakSpec.swift in Sources */,
979558FC1F97019E0096BBEA /* Matchers.swift in Sources */,
Expand Down Expand Up @@ -1000,6 +999,7 @@
9795590C1F9701CE0096BBEA /* ConstraintGroupSpec.swift in Sources */,
979559061F9701BC0096BBEA /* ViewHierarchySpec.swift in Sources */,
979558F91F9701860096BBEA /* PrioritySpec.swift in Sources */,
97885B1E1FC4F7C800BB4E51 /* LayoutGuideSpec.swift in Sources */,
979558F21F97016C0096BBEA /* EdgesSpec.swift in Sources */,
979558ED1F97015E0096BBEA /* TestView.swift in Sources */,
979559091F9701C90096BBEA /* MemoryLeakSpec.swift in Sources */,
Expand Down Expand Up @@ -1045,7 +1045,7 @@
buildActionMask = 2147483647;
files = (
979558F71F97017F0096BBEA /* LayoutSupportSpec.swift in Sources */,
97F50E551F9633AA00C6DCF5 /* SafeAreaLayoutGuideSpec.swift in Sources */,
97F50E551F9633AA00C6DCF5 /* ViewLayoutGuideSpec.swift in Sources */,
9795590D1F9701CF0096BBEA /* ConstraintGroupSpec.swift in Sources */,
9795590A1F9701CA0096BBEA /* MemoryLeakSpec.swift in Sources */,
979558FD1F9701A10096BBEA /* Matchers.swift in Sources */,
Expand Down
134 changes: 125 additions & 9 deletions Cartography/Edges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,106 @@ public struct Edges: Compound, RelativeCompoundEquality, RelativeCompoundInequal
public let properties: [Property]

internal init(_ context: Context, _ properties: [Property]) {
guard properties.count == 4 else {
fatalError("No valid edges were used")
}

self.context = context
self.properties = properties
}

/// Insets all edges individually.
///
/// - parameter top: The amount by which to inset the top edge, in points.
/// - parameter leading: The amount by which to inset the leading edge, in points.
/// - parameter bottom: The amount by which to inset the bottom edge, in points.
/// - parameter trailing: The amount by which to inset the trailing edge, in points.
///
/// - returns: A new expression with the inseted edges.
///
public func inseted(top: CGFloat, leading: CGFloat, bottom: CGFloat, trailing: CGFloat) -> Expression<Edges> {
return Expression(
self,
[
Coefficients(1, top),
Coefficients(1, leading),
Coefficients(1, -bottom),
Coefficients(1, -trailing)
]
)
}

/// Insets all horizontal and vertical edges.
///
/// - parameter horizontally: The amount by which to inset the leading and trailing edges, in points.
/// - parameter vertically: The amount by which to inset the top and bottom edges, in points.
///
/// - returns: A new expression with the inseted edges.
///
public func inseted(horizontally: CGFloat, vertically: CGFloat) -> Expression<Edges> {
return self.inseted(
top: vertically,
leading: horizontally,
bottom: vertically,
trailing: horizontally
)
}

/// Insets all horizontal edges.
///
/// - parameter horizontally: The amount by which to inset the leading and trailing edges, in points.
///
/// - returns: A new expression with the inseted edges.
///
public func inseted(horizontally: CGFloat) -> Expression<Edges> {
return self.inseted(
horizontally: horizontally,
vertically: 0
)
}

/// Insets all vertical edges.
///
/// - parameter vertically: The amount by which to inset the top and bottom edges, in points.
///
/// - returns: A new expression with the inseted edges.
///
public func inseted(vertically: CGFloat) -> Expression<Edges> {
return self.inseted(
horizontally: 0,
vertically: vertically
)
}

/// Insets all edges by a single value.
///
/// - parameter by: The amount by which to inset the top and bottom edges, in points.
///
/// - returns: A new expression with the inseted edges.
///
public func inseted(by value: CGFloat) -> Expression<Edges> {
return self.inseted(
horizontally: value,
vertically: value
)
}

#if os(iOS) || os(tvOS)
/// Insets all edges individually using an existing UIEdgeInsets.
///
/// - parameter by: The UIEdgeInsets to use as a base value.
///
/// - returns: A new expression with the inseted edges.
///
public func inseted(by insets: UIEdgeInsets) -> Expression<Edges> {
return self.inseted(
top: insets.top,
leading: insets.left,
bottom: insets.bottom,
trailing: insets.right
)
}
#endif
}

/// Insets all edges.
Expand All @@ -30,7 +127,7 @@ public struct Edges: Compound, RelativeCompoundEquality, RelativeCompoundInequal
/// - returns: A new expression with the inset edges.
///
public func inset(_ edges: Edges, _ all: CGFloat) -> Expression<Edges> {
return inset(edges, all, all, all, all)
return edges.inseted(by: all)
}

/// Insets the horizontal and vertical edges.
Expand All @@ -44,7 +141,31 @@ public func inset(_ edges: Edges, _ all: CGFloat) -> Expression<Edges> {
/// - returns: A new expression with the inset edges.
///
public func inset(_ edges: Edges, _ horizontal: CGFloat, _ vertical: CGFloat) -> Expression<Edges> {
return inset(edges, vertical, horizontal, vertical, horizontal)
return edges.inseted(horizontally: horizontal, vertically: vertical)
}

/// Insets the horizontal edges.
///
/// - parameter edges: The edges to inset.
/// - parameter horizontally: The amount by which to inset the horizontal edges, in
/// points.
///
/// - returns: A new expression with the inset edges.
///
public func inset(_ edges: Edges, horizontally horizontal: CGFloat) -> Expression<Edges> {
return edges.inseted(horizontally: horizontal)
}

/// Insets the vertical edges.
///
/// - parameter edges: The edges to inset.
/// - parameter vertically: The amount by which to inset the vertical edges, in
/// points.
///
/// - returns: A new expression with the inset edges.
///
public func inset(_ edges: Edges, vertically vertical: CGFloat) -> Expression<Edges> {
return edges.inseted(vertically: vertical)
}

/// Insets edges individually.
Expand All @@ -58,12 +179,7 @@ public func inset(_ edges: Edges, _ horizontal: CGFloat, _ vertical: CGFloat) ->
/// - returns: A new expression with the inset edges.
///
public func inset(_ edges: Edges, _ top: CGFloat, _ leading: CGFloat, _ bottom: CGFloat, _ trailing: CGFloat) -> Expression<Edges> {
return Expression(edges, [
Coefficients(1, top),
Coefficients(1, leading),
Coefficients(1, -bottom),
Coefficients(1, -trailing)
])
return edges.inseted(top: top, leading: leading, bottom: bottom, trailing: trailing)
}

#if os(iOS) || os(tvOS)
Expand All @@ -75,6 +191,6 @@ public func inset(_ edges: Edges, _ top: CGFloat, _ leading: CGFloat, _ bottom:
/// - returns: A new expression with the inset edges.
///
public func inset(_ edges: Edges, _ insets: UIEdgeInsets) -> Expression<Edges> {
return inset(edges, insets.top, insets.left, insets.bottom, insets.right)
return edges.inseted(by: insets)
}
#endif
18 changes: 18 additions & 0 deletions Cartography/LayoutGuide.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#if os(iOS) || os(tvOS)
import UIKit

@available(iOS, introduced: 9.0)
@available(tvOS, introduced: 9.0)
public typealias LayoutGuide = UILayoutGuide

@available(iOS, introduced: 9.0)
@available(tvOS, introduced: 9.0)
extension UILayoutGuide: LayoutItem {
Expand All @@ -19,4 +23,18 @@ extension UILayoutGuide: LayoutItem {
return LayoutGuideProxy(context: context, item: self)
}
}
#elseif os(OSX)
import AppKit

@available(OSX, introduced: 10.11)
public typealias LayoutGuide = NSLayoutGuide

@available(OSX, introduced: 10.11)
extension NSLayoutGuide: LayoutItem {

@available(OSX, introduced: 10.11)
public func asProxy(context: Context) -> LayoutGuideProxy {
return LayoutGuideProxy(context: context, item: self)
}
}
#endif
9 changes: 3 additions & 6 deletions Cartography/LayoutGuideProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@
// Copyright © 2017 Robert Böhnke. All rights reserved.
//

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

@available(iOS, introduced: 9.0)
@available(tvOS, introduced: 9.0)
@available(OSX, introduced: 10.11)
public final class LayoutGuideProxy: SupportsPositioningLayoutProxy {
public let context: Context

private let layoutGuide: UILayoutGuide
private let layoutGuide: LayoutGuide
public var item: AnyObject {
return layoutGuide
}

public init(context: Context, item: UILayoutGuide) {
public init(context: Context, item: LayoutGuide) {
self.context = context
self.layoutGuide = item
}
Expand All @@ -28,4 +26,3 @@ public final class LayoutGuideProxy: SupportsPositioningLayoutProxy {
return layoutGuide.owningView?.asProxy(context: context)
}
}
#endif
12 changes: 12 additions & 0 deletions Cartography/ViewProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,17 @@ public final class ViewProxy: SupportsPositioningLayoutProxy, SupportsBaselineLa
public var safeAreaLayoutGuide: LayoutGuideProxy {
return view.safeAreaLayoutGuide.asProxy(context: context)
}

@available(iOS, introduced: 9.0)
@available(tvOS, introduced: 9.0)
public var layoutMarginsGuide: LayoutGuideProxy {
return view.layoutMarginsGuide.asProxy(context: context)
}

@available(iOS, introduced: 9.0)
@available(tvOS, introduced: 9.0)
public var readableContentGuide: LayoutGuideProxy {
return view.readableContentGuide.asProxy(context: context)
}
#endif
}
20 changes: 20 additions & 0 deletions CartographyTests/EdgesSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ class EdgesSpec: QuickSpec {
expect(view.frame).to(equal(CGRect(x: 20, y: 30, width: 360, height: 340)))
}

it("should inset the horizontal edges") {
constrain(view) { view in
view.edges == inset(view.superview!.edges, horizontally: 20)
}

window.layoutIfNeeded()

expect(view.frame).to(equal(CGRect(x: 20, y: 0, width: 360, height: 400)))
}

it("should inset the vertical edges") {
constrain(view) { view in
view.edges == inset(view.superview!.edges, vertically: 30)
}

window.layoutIfNeeded()

expect(view.frame).to(equal(CGRect(x: 0, y: 30, width: 400, height: 340)))
}

it("should inset all edges individually") {
constrain(view) { view in
view.edges == inset(view.superview!.edges, 10, 20, 30, 40)
Expand Down
Loading