Added support for OS X

* Added "Insets" class which makes EdgeInsets a typealias on iOS and a custom struct on OS X
* Added OS checks and fixed API issues where necessary
This commit is contained in:
Robert Payne 2014-07-30 12:55:31 +12:00
parent fda663a90b
commit 2edb7b9ff3
7 changed files with 94 additions and 17 deletions

View File

@ -14,6 +14,7 @@
DD03E3241981B72A00E0DE94 /* ConstraintMaker.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD03E3231981B72A00E0DE94 /* ConstraintMaker.swift */; }; DD03E3241981B72A00E0DE94 /* ConstraintMaker.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD03E3231981B72A00E0DE94 /* ConstraintMaker.swift */; };
DD03E32A1981B7BF00E0DE94 /* View.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD03E3291981B7BF00E0DE94 /* View.swift */; }; DD03E32A1981B7BF00E0DE94 /* View.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD03E3291981B7BF00E0DE94 /* View.swift */; };
DDC9FDAE1981B4DD009612C7 /* SnappyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC9FDAD1981B4DD009612C7 /* SnappyTests.swift */; }; DDC9FDAE1981B4DD009612C7 /* SnappyTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = DDC9FDAD1981B4DD009612C7 /* SnappyTests.swift */; };
EE1004CB198879AD006CC759 /* Insets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE1004CA198879AD006CC759 /* Insets.swift */; };
EEC6EB4E1985370500C27B12 /* LayoutConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEC6EB4D1985370500C27B12 /* LayoutConstraint.swift */; }; EEC6EB4E1985370500C27B12 /* LayoutConstraint.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEC6EB4D1985370500C27B12 /* LayoutConstraint.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
@ -52,6 +53,7 @@
DDC9FD951981B4DD009612C7 /* Snappy.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Snappy.app; sourceTree = BUILT_PRODUCTS_DIR; }; DDC9FD951981B4DD009612C7 /* Snappy.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Snappy.app; sourceTree = BUILT_PRODUCTS_DIR; };
DDC9FDA71981B4DD009612C7 /* SnappyTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SnappyTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; DDC9FDA71981B4DD009612C7 /* SnappyTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SnappyTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
DDC9FDAD1981B4DD009612C7 /* SnappyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnappyTests.swift; sourceTree = "<group>"; }; DDC9FDAD1981B4DD009612C7 /* SnappyTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SnappyTests.swift; sourceTree = "<group>"; };
EE1004CA198879AD006CC759 /* Insets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Insets.swift; sourceTree = "<group>"; };
EEC6EB4D1985370500C27B12 /* LayoutConstraint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LayoutConstraint.swift; sourceTree = "<group>"; }; EEC6EB4D1985370500C27B12 /* LayoutConstraint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LayoutConstraint.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
@ -87,6 +89,7 @@
DD03E31A1981B62D00E0DE94 /* Snappy */ = { DD03E31A1981B62D00E0DE94 /* Snappy */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
EE1004CA198879AD006CC759 /* Insets.swift */,
EEC6EB4D1985370500C27B12 /* LayoutConstraint.swift */, EEC6EB4D1985370500C27B12 /* LayoutConstraint.swift */,
DD03E3211981B71C00E0DE94 /* Constraint.swift */, DD03E3211981B71C00E0DE94 /* Constraint.swift */,
DD03E3231981B72A00E0DE94 /* ConstraintMaker.swift */, DD03E3231981B72A00E0DE94 /* ConstraintMaker.swift */,
@ -225,6 +228,7 @@
DD03E32A1981B7BF00E0DE94 /* View.swift in Sources */, DD03E32A1981B7BF00E0DE94 /* View.swift in Sources */,
DD03E3221981B71C00E0DE94 /* Constraint.swift in Sources */, DD03E3221981B71C00E0DE94 /* Constraint.swift in Sources */,
EEC6EB4E1985370500C27B12 /* LayoutConstraint.swift in Sources */, EEC6EB4E1985370500C27B12 /* LayoutConstraint.swift in Sources */,
EE1004CB198879AD006CC759 /* Insets.swift in Sources */,
DD03E3241981B72A00E0DE94 /* ConstraintMaker.swift in Sources */, DD03E3241981B72A00E0DE94 /* ConstraintMaker.swift in Sources */,
DD03E3191981B52F00E0DE94 /* ViewController.swift in Sources */, DD03E3191981B52F00E0DE94 /* ViewController.swift in Sources */,
DD03E3151981B52F00E0DE94 /* AppDelegate.swift in Sources */, DD03E3151981B52F00E0DE94 /* AppDelegate.swift in Sources */,

View File

@ -22,7 +22,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#if os(iOS)
import UIKit import UIKit
#else
import AppKit
#endif
/** /**
* ConstraintAttributes is an options set that maps to NSLayoutAttributes. * ConstraintAttributes is an options set that maps to NSLayoutAttributes.
@ -182,7 +186,7 @@ class Constraint {
func equalTo(other: CGPoint) -> Constraint { func equalTo(other: CGPoint) -> Constraint {
return constrainTo(other, relation: .Equal) return constrainTo(other, relation: .Equal)
} }
func equalTo(other: UIEdgeInsets) -> Constraint { func equalTo(other: EdgeInsets) -> Constraint {
return constrainTo(other, relation: .Equal) return constrainTo(other, relation: .Equal)
} }
@ -206,7 +210,7 @@ class Constraint {
func lessThanOrEqualTo(other: CGPoint) -> Constraint { func lessThanOrEqualTo(other: CGPoint) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return constrainTo(other, relation: .LessThanOrEqualTo)
} }
func lessThanOrEqualTo(other: UIEdgeInsets) -> Constraint { func lessThanOrEqualTo(other: EdgeInsets) -> Constraint {
return constrainTo(other, relation: .LessThanOrEqualTo) return constrainTo(other, relation: .LessThanOrEqualTo)
} }
@ -230,7 +234,7 @@ class Constraint {
func greaterThanOrEqualTo(other: CGPoint) -> Constraint { func greaterThanOrEqualTo(other: CGPoint) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
func greaterThanOrEqualTo(other: UIEdgeInsets) -> Constraint { func greaterThanOrEqualTo(other: EdgeInsets) -> Constraint {
return constrainTo(other, relation: .GreaterThanOrEqualTo) return constrainTo(other, relation: .GreaterThanOrEqualTo)
} }
@ -279,14 +283,14 @@ class Constraint {
self.offset = amount self.offset = amount
return self return self
} }
func offset(amount: UIEdgeInsets) -> Constraint { func offset(amount: EdgeInsets) -> Constraint {
self.offset = amount self.offset = amount
return self return self
} }
// MARK: insets // MARK: insets
func insets(amount: UIEdgeInsets) -> Constraint { func insets(amount: EdgeInsets) -> Constraint {
self.offset = amount self.offset = amount
return self return self
} }
@ -359,7 +363,11 @@ class Constraint {
func uninstall() { func uninstall() {
if let view = self.installedOnView { if let view = self.installedOnView {
#if os(iOS)
var installedConstraints = view.constraints() var installedConstraints = view.constraints()
#else
var installedConstraints = view.constraints
#endif
var constraintsToRemove: Array<LayoutConstraint> = [] var constraintsToRemove: Array<LayoutConstraint> = []
for installedConstraint in installedConstraints { for installedConstraint in installedConstraints {
if let layoutConstraint = installedConstraint as? LayoutConstraint { if let layoutConstraint = installedConstraint as? LayoutConstraint {
@ -425,7 +433,7 @@ class Constraint {
self.constant = other self.constant = other
return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation) return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
} }
private func constrainTo(other: UIEdgeInsets, relation: ConstraintRelation) -> Constraint { private func constrainTo(other: EdgeInsets, relation: ConstraintRelation) -> Constraint {
self.constant = other self.constant = other
return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation) return constrainTo(ConstraintItem(view: nil, attributes: ConstraintAttributes.None), relation: relation)
} }
@ -482,8 +490,8 @@ private extension NSLayoutAttribute {
return -point.y return -point.y
} }
} }
// UIEdgeInsets // EdgeInsets
else if let insets = value as? UIEdgeInsets { else if let insets = value as? EdgeInsets {
if self == .Left { if self == .Left {
return insets.left return insets.left
} else if self == .Top { } else if self == .Top {
@ -531,8 +539,8 @@ private extension NSLayoutAttribute {
return point.y return point.y
} }
} }
// UIEdgeInsets // EdgeInsets
else if let insets = value as? UIEdgeInsets { else if let insets = value as? EdgeInsets {
if self == .Left { if self == .Left {
return insets.left return insets.left
} else if self == .Top { } else if self == .Top {

View File

@ -22,7 +22,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#if os(iOS)
import UIKit import UIKit
#else
import AppKit
#endif
/** /**
* ConstraintMaker is the maker in snappy that gets all constraints kickstarted * ConstraintMaker is the maker in snappy that gets all constraints kickstarted
@ -59,7 +63,11 @@ class ConstraintMaker {
} }
internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> ()) { internal class func makeConstraints(view: View, block: (make: ConstraintMaker) -> ()) {
#if os(iOS)
view.setTranslatesAutoresizingMaskIntoConstraints(false) view.setTranslatesAutoresizingMaskIntoConstraints(false)
#else
view.translatesAutoresizingMaskIntoConstraints = false
#endif
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)
@ -71,7 +79,11 @@ class ConstraintMaker {
} }
internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> ()) { internal class func remakeConstraints(view: View, block: (make: ConstraintMaker) -> ()) {
#if os(iOS)
view.setTranslatesAutoresizingMaskIntoConstraints(false) view.setTranslatesAutoresizingMaskIntoConstraints(false)
#else
view.translatesAutoresizingMaskIntoConstraints = false
#endif
let maker = ConstraintMaker(view: view) let maker = ConstraintMaker(view: view)
block(make: maker) block(make: maker)

45
Snappy/Insets.swift Normal file
View File

@ -0,0 +1,45 @@
//
// Insets.swift
// Snappy
//
// Copyright (c) 2011-2014 Masonry Team - https://github.com/Masonry
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if os(iOS)
import UIKit
typealias EdgeInsets = UIEdgeInsets
func EdgeInsetsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) -> EdgeInsets {
return EdgeInsets(top: top, left: left, bottom: bottom, right: right)
}
let EdgeInsetsZero = EdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
#else
import AppKit
struct EdgeInsets {
var top: CGFloat // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
var left: CGFloat
var bottom: CGFloat
var right: CGFloat
}
func EdgeInsetsMake(top: CGFloat, left: CGFloat, bottom: CGFloat, right: CGFloat) -> EdgeInsets {
return EdgeInsets(top: top, left: left, bottom: bottom, right: right)
}
let EdgeInsetsZero = EdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
#endif

View File

@ -22,7 +22,11 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#if os(iOS)
import UIKit import UIKit
#else
import AppKit
#endif
/** /**
* LayoutConstraint is a subclass of NSLayoutConstraint to assist Snappy and also provide better debugging * LayoutConstraint is a subclass of NSLayoutConstraint to assist Snappy and also provide better debugging

View File

@ -22,9 +22,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
#if os(iOS)
import UIKit import UIKit
typealias View = UIView typealias View = UIView
#else
import AppKit
typealias View = NSView
#endif
extension View { extension View {
#if SNP_SHORTHAND #if SNP_SHORTHAND
var left: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Left) } var left: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Left) }
@ -50,7 +55,6 @@ extension View {
func remakeConstraints(block: (maker: ConstraintMaker) -> ()) { func remakeConstraints(block: (maker: ConstraintMaker) -> ()) {
ConstraintMaker.remakeConstraints(self, block: block) ConstraintMaker.remakeConstraints(self, block: block)
} }
#else #else
var snp_left: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Left) } var snp_left: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Left) }
var snp_top: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Top) } var snp_top: ConstraintItem { return ConstraintItem(view: self, attributes: ConstraintAttributes.Top) }

View File

@ -33,7 +33,7 @@ class ViewController: UIViewController {
view3.layer.borderWidth = 2 view3.layer.borderWidth = 2
superview.addSubview(view3) superview.addSubview(view3)
let padding = UIEdgeInsets(top: 15, left: 10, bottom: 15, right: 10) let padding = EdgeInsets(top: 15, left: 10, bottom: 15, right: 10)
view1.snp_makeConstraints { make in view1.snp_makeConstraints { make in