From d482f59648e580a5d550a2e85bee04973c6df9ec Mon Sep 17 00:00:00 2001 From: Josh Converse Date: Sun, 4 Aug 2019 22:16:17 -0700 Subject: [PATCH 1/3] =?UTF-8?q?Adds=20two=20new=20compositions=20=E2=80=93?= =?UTF-8?q?=C2=A0DirectionalEdges=20and=20DirectionalMargins=20(#595)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * WIP * Add directional margins * Whitespace --- Source/Constraint.swift | 26 +++++++++++ Source/ConstraintAttributes.swift | 4 ++ Source/ConstraintDSL.swift | 9 ++++ Source/ConstraintMaker.swift | 8 ++++ Source/ConstraintMakerExtendable.swift | 10 +++++ Source/ConstraintMakerRelatable.swift | 4 +- Tests/SnapKitTests/Tests.swift | 62 ++++++++++++++++++++++++++ 7 files changed, 122 insertions(+), 1 deletion(-) diff --git a/Source/Constraint.swift b/Source/Constraint.swift index ea6159e..390629e 100644 --- a/Source/Constraint.swift +++ b/Source/Constraint.swift @@ -129,6 +129,32 @@ public final class Constraint { default: fatalError() } + } else if self.from.attributes == .directionalEdges && self.to.attributes == .directionalMargins { + switch layoutFromAttribute { + case .leading: + layoutToAttribute = .leadingMargin + case .trailing: + layoutToAttribute = .trailingMargin + case .top: + layoutToAttribute = .topMargin + case .bottom: + layoutToAttribute = .bottomMargin + default: + fatalError() + } + } else if self.from.attributes == .directionalMargins && self.to.attributes == .directionalEdges { + switch layoutFromAttribute { + case .leadingMargin: + layoutToAttribute = .leading + case .trailingMargin: + layoutToAttribute = .trailing + case .topMargin: + layoutToAttribute = .top + case .bottomMargin: + layoutToAttribute = .bottom + default: + fatalError() + } } else if self.from.attributes == self.to.attributes { layoutToAttribute = layoutFromAttribute } else { diff --git a/Source/ConstraintAttributes.swift b/Source/ConstraintAttributes.swift index 5e2367e..2f0fbe9 100644 --- a/Source/ConstraintAttributes.swift +++ b/Source/ConstraintAttributes.swift @@ -99,12 +99,16 @@ internal struct ConstraintAttributes : OptionSet, ExpressibleByIntegerLiteral { // aggregates internal static var edges: ConstraintAttributes { return 15 } + internal static var directionalEdges: ConstraintAttributes { return 58 } internal static var size: ConstraintAttributes { return 192 } internal static var center: ConstraintAttributes { return 768 } @available(iOS 8.0, *) internal static var margins: ConstraintAttributes { return 61440 } + @available(iOS 8.0, *) + internal static var directionalMargins: ConstraintAttributes { return 245760 } + @available(iOS 8.0, *) internal static var centerWithinMargins: ConstraintAttributes { return 786432 } diff --git a/Source/ConstraintDSL.swift b/Source/ConstraintDSL.swift index b7aad57..ecc5173 100644 --- a/Source/ConstraintDSL.swift +++ b/Source/ConstraintDSL.swift @@ -99,6 +99,10 @@ extension ConstraintBasicAttributesDSL { return ConstraintItem(target: self.target, attributes: ConstraintAttributes.edges) } + public var directionalEdges: ConstraintItem { + return ConstraintItem(target: self.target, attributes: ConstraintAttributes.directionalEdges) + } + public var size: ConstraintItem { return ConstraintItem(target: self.target, attributes: ConstraintAttributes.size) } @@ -177,6 +181,11 @@ extension ConstraintAttributesDSL { return ConstraintItem(target: self.target, attributes: ConstraintAttributes.margins) } + @available(iOS 8.0, *) + public var directionalMargins: ConstraintItem { + return ConstraintItem(target: self.target, attributes: ConstraintAttributes.directionalMargins) + } + @available(iOS 8.0, *) public var centerWithinMargins: ConstraintItem { return ConstraintItem(target: self.target, attributes: ConstraintAttributes.centerWithinMargins) diff --git a/Source/ConstraintMaker.swift b/Source/ConstraintMaker.swift index ed2879b..38f4d31 100644 --- a/Source/ConstraintMaker.swift +++ b/Source/ConstraintMaker.swift @@ -126,6 +126,9 @@ public class ConstraintMaker { public var edges: ConstraintMakerExtendable { return self.makeExtendableWithAttributes(.edges) } + public var directionalEdges: ConstraintMakerExtendable { + return self.makeExtendableWithAttributes(.directionalEdges) + } public var size: ConstraintMakerExtendable { return self.makeExtendableWithAttributes(.size) } @@ -138,6 +141,11 @@ public class ConstraintMaker { return self.makeExtendableWithAttributes(.margins) } + @available(iOS 8.0, *) + public var directionalMargins: ConstraintMakerExtendable { + return self.makeExtendableWithAttributes(.directionalMargins) + } + @available(iOS 8.0, *) public var centerWithinMargins: ConstraintMakerExtendable { return self.makeExtendableWithAttributes(.centerWithinMargins) diff --git a/Source/ConstraintMakerExtendable.swift b/Source/ConstraintMakerExtendable.swift index 243e54e..ce47169 100644 --- a/Source/ConstraintMakerExtendable.swift +++ b/Source/ConstraintMakerExtendable.swift @@ -149,6 +149,10 @@ public class ConstraintMakerExtendable: ConstraintMakerRelatable { self.description.attributes += .edges return self } + public var directionalEdges: ConstraintMakerExtendable { + self.description.attributes += .directionalEdges + return self + } public var size: ConstraintMakerExtendable { self.description.attributes += .size return self @@ -160,6 +164,12 @@ public class ConstraintMakerExtendable: ConstraintMakerRelatable { return self } + @available(iOS 8.0, *) + public var directionalMargins: ConstraintMakerExtendable { + self.description.attributes += .directionalMargins + return self + } + @available(iOS 8.0, *) public var centerWithinMargins: ConstraintMakerExtendable { self.description.attributes += .centerWithinMargins diff --git a/Source/ConstraintMakerRelatable.swift b/Source/ConstraintMakerRelatable.swift index 98c7158..7889532 100644 --- a/Source/ConstraintMakerRelatable.swift +++ b/Source/ConstraintMakerRelatable.swift @@ -45,7 +45,9 @@ public class ConstraintMakerRelatable { other.attributes.layoutAttributes.count <= 1 || other.attributes.layoutAttributes == self.description.attributes.layoutAttributes || other.attributes == .edges && self.description.attributes == .margins || - other.attributes == .margins && self.description.attributes == .edges else { + other.attributes == .margins && self.description.attributes == .edges || + other.attributes == .directionalEdges && self.description.attributes == .directionalMargins || + other.attributes == .directionalMargins && self.description.attributes == .directionalEdges else { fatalError("Cannot constraint to multiple non identical attributes. (\(file), \(line))"); } diff --git a/Tests/SnapKitTests/Tests.swift b/Tests/SnapKitTests/Tests.swift index 750ea1d..8addd50 100644 --- a/Tests/SnapKitTests/Tests.swift +++ b/Tests/SnapKitTests/Tests.swift @@ -575,6 +575,28 @@ class SnapKitTests: XCTestCase { XCTAssert(fromAttributes == [.top, .left, .bottom, .right]) XCTAssert(toAttributes == [.top, .left, .bottom, .right]) } + + func testDirectionalEdgesToDirectionalEdges() { + var fromAttributes = Set() + var toAttributes = Set() + + let view = View() + self.container.addSubview(view) + + view.snp.remakeConstraints { (make) -> Void in + make.directionalEdges.equalTo(self.container.snp.directionalEdges) + } + + XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints") + + for constraint in (container.snp_constraints as! [NSLayoutConstraint]) { + fromAttributes.insert(constraint.firstAttribute) + toAttributes.insert(constraint.secondAttribute) + } + + XCTAssert(fromAttributes == [.top, .leading, .bottom, .trailing]) + XCTAssert(toAttributes == [.top, .leading, .bottom, .trailing]) + } #if os(iOS) || os(tvOS) func testEdgesToMargins() { @@ -616,6 +638,46 @@ class SnapKitTests: XCTestCase { XCTAssert(fromAttributes == [.topMargin, .leftMargin, .bottomMargin, .rightMargin]) } + + func testDirectionalEdgesToDirectionalMargins() { + var fromAttributes = Set() + var toAttributes = Set() + + let view = View() + self.container.addSubview(view) + + view.snp.remakeConstraints { (make) -> Void in + make.directionalEdges.equalTo(self.container.snp.directionalMargins) + } + + XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints") + + for constraint in (container.snp_constraints as! [NSLayoutConstraint]) { + fromAttributes.insert(constraint.firstAttribute) + toAttributes.insert(constraint.secondAttribute) + } + + XCTAssert(fromAttributes == [.top, .leading, .bottom, .trailing]) + XCTAssert(toAttributes == [.topMargin, .leadingMargin, .bottomMargin, .trailingMargin]) + + fromAttributes.removeAll() + toAttributes.removeAll() + + view.snp.remakeConstraints { (make) -> Void in + make.directionalMargins.equalTo(self.container.snp.directionalEdges) + } + + XCTAssertEqual(self.container.snp_constraints.count, 4, "Should have 4 constraints") + + for constraint in (container.snp_constraints as! [NSLayoutConstraint]) { + fromAttributes.insert(constraint.firstAttribute) + toAttributes.insert(constraint.secondAttribute) + } + + XCTAssert(toAttributes == [.top, .leading, .bottom, .trailing]) + XCTAssert(fromAttributes == [.topMargin, .leadingMargin, .bottomMargin, .trailingMargin]) + + } func testLayoutGuideConstraints() { let vc = UIViewController() From df9df818dd2254f9f884f7bd814141c694f5a905 Mon Sep 17 00:00:00 2001 From: Shiva Huang Date: Mon, 5 Aug 2019 13:16:46 +0800 Subject: [PATCH 2/3] Add Playground Sample (#608) --- README.md | 7 +++++ SnapKit.xcworkspace/contents.xcworkspacedata | 3 ++ .../xcshareddata/IDEWorkspaceChecks.plist | 8 +++++ SnapKitPlayground.playground/Contents.swift | 29 +++++++++++++++++++ .../contents.xcplayground | 4 +++ .../timeline.xctimeline | 6 ++++ 6 files changed, 57 insertions(+) create mode 100644 SnapKit.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 SnapKitPlayground.playground/Contents.swift create mode 100644 SnapKitPlayground.playground/contents.xcplayground create mode 100644 SnapKitPlayground.playground/timeline.xctimeline diff --git a/README.md b/README.md index 32b6518..0945ce6 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,13 @@ class MyViewController: UIViewController { } ``` +### Playground +You can try SnapKit in Playground. + +**Note:** + +> To try SnapKit in playground, open `SnapKit.xcworkspace` and build SnapKit.framework for any simulator first. + ### Resources - [Documentation](http://snapkit.io/docs/) diff --git a/SnapKit.xcworkspace/contents.xcworkspacedata b/SnapKit.xcworkspace/contents.xcworkspacedata index 6814365..c59d3d6 100644 --- a/SnapKit.xcworkspace/contents.xcworkspacedata +++ b/SnapKit.xcworkspace/contents.xcworkspacedata @@ -1,6 +1,9 @@ + + diff --git a/SnapKit.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/SnapKit.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/SnapKit.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/SnapKitPlayground.playground/Contents.swift b/SnapKitPlayground.playground/Contents.swift new file mode 100644 index 0000000..d84900a --- /dev/null +++ b/SnapKitPlayground.playground/Contents.swift @@ -0,0 +1,29 @@ +//: A UIKit based Playground for presenting user interface +// To use this playground, build SnapKit.framework for any simulator first. + +import SnapKit +import UIKit +import PlaygroundSupport + +class MyViewController : UIViewController { + override func loadView() { + let view = UIView() + view.backgroundColor = .white + + let label = UILabel() + label.text = "Hello World!" + label.textColor = .black + + view.addSubview(label) + + label.snp.makeConstraints { (make) in + make.left.equalToSuperview().offset(150) + make.top.equalToSuperview().offset(200) + make.size.equalTo(CGSize(width: 200, height: 20)) + } + + self.view = view + } +} +// Present the view controller in the Live View window +PlaygroundPage.current.liveView = MyViewController() diff --git a/SnapKitPlayground.playground/contents.xcplayground b/SnapKitPlayground.playground/contents.xcplayground new file mode 100644 index 0000000..3b15108 --- /dev/null +++ b/SnapKitPlayground.playground/contents.xcplayground @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/SnapKitPlayground.playground/timeline.xctimeline b/SnapKitPlayground.playground/timeline.xctimeline new file mode 100644 index 0000000..bf468af --- /dev/null +++ b/SnapKitPlayground.playground/timeline.xctimeline @@ -0,0 +1,6 @@ + + + + + From bffbeecd1096dd5875a121d9ce24b411b53ab27d Mon Sep 17 00:00:00 2001 From: Max Cobb <5754073+maxxfrazer@users.noreply.github.com> Date: Mon, 5 Aug 2019 06:17:03 +0100 Subject: [PATCH 3/3] added updates to Package.swift so it can be imported with Xcode 11 (#615) --- .gitignore | 1 + Package.swift | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/.gitignore b/.gitignore index 79fa0df..c15ee17 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ Examples/ Gemfile Gemfile.lock *.sketch +.swiftpm diff --git a/Package.swift b/Package.swift index 865eabf..fb37579 100644 --- a/Package.swift +++ b/Package.swift @@ -27,11 +27,19 @@ import PackageDescription let package = Package( name: "SnapKit", + platforms: [ + .iOS(.v10), + .macOS(.v10_12), + .tvOS(.v10) + ], products: [ .library(name: "SnapKit", targets: ["SnapKit"]), ], targets: [ .target(name: "SnapKit", path: "Source"), .testTarget(name: "SnapKitTests", dependencies: ["SnapKit"]), + ], + swiftLanguageVersions: [ + .v5 ] )