Skip to content

Commit

Permalink
Replace enums with structs
Browse files Browse the repository at this point in the history
  • Loading branch information
robb committed Mar 29, 2015
1 parent 5b442cf commit 593be74
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 221 deletions.
30 changes: 8 additions & 22 deletions Cartography/Dimension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,14 @@ import UIKit
import AppKit
#endif

public enum Dimension : Property, NumericalEquality, RelativeEquality, NumericalInequality, RelativeInequality, Addition, Multiplication {
case Width(Context, View)
case Height(Context, View)
public struct Dimension : Property, NumericalEquality, RelativeEquality, NumericalInequality, RelativeInequality, Addition, Multiplication {
public let attribute: NSLayoutAttribute
public let context: Context
public let view: View

public var attribute: NSLayoutAttribute {
switch (self) {
case .Width(_): return NSLayoutAttribute.Width
case .Height(_): return NSLayoutAttribute.Height
}
}

public var context: Context {
switch (self) {
case let .Width(context, _): return context
case let .Height(context, _): return context
}
}

public var view: View {
switch (self) {
case let .Width(_, view): return view
case let .Height(_, view): return view
}
internal init(_ context: Context, _ view: View, _ attribute: NSLayoutAttribute) {
self.attribute = attribute
self.context = context
self.view = view
}
}
124 changes: 9 additions & 115 deletions Cartography/Edge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,120 +12,14 @@ import UIKit
import AppKit
#endif

public enum Edge : Property, RelativeEquality, RelativeInequality, Addition, Multiplication {
case Top(Context, View)
case Right(Context, View)
case Bottom(Context, View)
case Left(Context, View)

case Leading(Context, View)
case Trailing(Context, View)

case CenterX(Context, View)
case CenterY(Context, View)

case Baseline(Context, View)

// The following properties are iOS exclusive and cannot have their
// `attribute` resolved on OS X.
case FirstBaseline(Context, View)
case LeftMargin(Context, View)
case RightMargin(Context, View)
case TopMargin(Context, View)
case BottomMargin(Context, View)
case LeadingMargin(Context, View)
case TrailingMargin(Context, View)
case CenterXWithinMargins(Context, View)
case CenterYWithinMargins(Context, View)

public var attribute: NSLayoutAttribute {
switch (self) {
case .Top(_): return .Top
case .Right(_): return .Right
case .Bottom(_): return .Bottom
case .Left(_): return .Left
case .Leading(_): return .Leading
case .Trailing(_): return .Trailing
case .CenterX(_): return .CenterX
case .CenterY(_): return .CenterY
case .Baseline(_): return .Baseline

// Swift does not let me mix `#if` statements with `case` statements as
// of Xcode 6.2
default:
#if os(iOS)
// Only on iOS we're able to look up the appropriate properties for
// the remaining cases.
switch (self) {
case .FirstBaseline(_): return .FirstBaseline
case .LeftMargin(_): return .LeftMargin
case .RightMargin(_): return .RightMargin
case .TopMargin(_): return .TopMargin
case .BottomMargin(_): return .BottomMargin
case .LeadingMargin(_): return .LeadingMargin
case .TrailingMargin(_): return .TrailingMargin
case .CenterXWithinMargins(_): return .CenterXWithinMargins
case .CenterYWithinMargins(_): return .CenterYWithinMargins
default:
fatalError("Unexpected case \(self).")
return .NotAnAttribute
}
#else
// Since `LayoutProxy` does not expose them on OS X, we should never
// have to resolve them.
fatalError("\(self) must not be used on OS X.")
return .NotAnAttribute
#endif
}
}

public var context: Context {
switch (self) {
case let .Top(context, _): return context
case let .Right(context, _): return context
case let .Bottom(context, _): return context
case let .Left(context, _): return context
case let .Leading(context, _): return context
case let .Trailing(context, _): return context
case let .CenterX(context, _): return context
case let .CenterY(context, _): return context
case let .Baseline(context, _): return context

// iOS Only
case let .FirstBaseline(context, _): return context
case let .LeftMargin(context, _): return context
case let .RightMargin(context, _): return context
case let .TopMargin(context, _): return context
case let .BottomMargin(context, _): return context
case let .LeadingMargin(context, _): return context
case let .TrailingMargin(context, _): return context
case let .CenterXWithinMargins(context, _): return context
case let .CenterYWithinMargins(context, _): return context
}
}

public var view: View {
switch (self) {
case let .Top(_, view): return view
case let .Right(_, view): return view
case let .Bottom(_, view): return view
case let .Left(_, view): return view
case let .Leading(_, view): return view
case let .Trailing(_, view): return view
case let .CenterX(_, view): return view
case let .CenterY(_, view): return view
case let .Baseline(_, view): return view

// iOS Only
case let .FirstBaseline(_, view): return view
case let .LeftMargin(_, view): return view
case let .RightMargin(_, view): return view
case let .TopMargin(_, view): return view
case let .BottomMargin(_, view): return view
case let .LeadingMargin(_, view): return view
case let .TrailingMargin(_, view): return view
case let .CenterXWithinMargins(_, view): return view
case let .CenterYWithinMargins(_, view): return view
}
public struct Edge : Property, RelativeEquality, RelativeInequality, Addition, Multiplication {
public let attribute: NSLayoutAttribute
public let context: Context
public let view: View

internal init(_ context: Context, _ view: View, _ attribute: NSLayoutAttribute) {
self.attribute = attribute
self.context = context
self.view = view
}
}
25 changes: 6 additions & 19 deletions Cartography/Edges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,13 @@ import UIKit
import AppKit
#endif

public enum Edges : Compound {
case Edges(Context, View)
public struct Edges: Compound {
let context: Context
let properties: [Property]

var context: Context {
switch (self) {
case let .Edges(context, _):
return context
}
}

var properties: [Property] {
switch (self) {
case let .Edges(context, view):
return [
Edge.Top(context, view),
Edge.Leading(context, view),
Edge.Bottom(context, view),
Edge.Trailing(context, view)
]
}
internal init(_ context: Context, _ properties: [Property]) {
self.context = context
self.properties = properties
}
}

Expand Down
66 changes: 40 additions & 26 deletions Cartography/LayoutProxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,41 +102,55 @@ public class LayoutProxy {
self.context = context
self.view = view

width = Dimension.Width(context, view)
height = Dimension.Height(context, view)
width = Dimension(context, view, .Width)
height = Dimension(context, view, .Height)

size = Size.Size(context, view)
size = Size(context, [
Dimension(context, view, .Width),
Dimension(context, view, .Height)
])

top = Edge.Top(context, view)
right = Edge.Right(context, view)
bottom = Edge.Bottom(context, view)
left = Edge.Left(context, view)
top = Edge(context, view, .Top)
right = Edge(context, view, .Right)
bottom = Edge(context, view, .Bottom)
left = Edge(context, view, .Left)

edges = Edges.Edges(context, view)
edges = Edges(context, [
Edge(context, view, .Top),
Edge(context, view, .Leading),
Edge(context, view, .Bottom),
Edge(context, view, .Trailing)
])

leading = Edge.Leading(context, view)
trailing = Edge.Trailing(context, view)
leading = Edge(context, view, .Leading)
trailing = Edge(context, view, .Trailing)

centerX = Edge.CenterX(context, view)
centerY = Edge.CenterY(context, view)
centerX = Edge(context, view, .CenterX)
centerY = Edge(context, view, .CenterY)

center = Point.Center(context, view)
center = Point(context, [
Edge(context, view, .CenterX),
Edge(context, view, .CenterY)
])

baseline = Edge.Baseline(context, view)
baseline = Edge(context, view, .Baseline)

#if os(iOS)
firstBaseline = .FirstBaseline(context, view)

leftMargin = .LeftMargin(context, view)
rightMargin = .RightMargin(context, view)
topMargin = .TopMargin(context, view)
bottomMargin = .BottomMargin(context, view)
leadingMargin = .LeadingMargin(context, view)
trailingMargin = .TrailingMargin(context, view)
centerXWithinMargins = .CenterXWithinMargins(context, view)
centerYWithinMargins = .CenterYWithinMargins(context, view)

centerWithinMargins = .CenterWithinMargins(context, view)
firstBaseline = Edge(context, view, .FirstBaseline)

leftMargin = Edge(context, view, .LeftMargin)
rightMargin = Edge(context, view, .RightMargin)
topMargin = Edge(context, view, .TopMargin)
bottomMargin = Edge(context, view, .BottomMargin)
leadingMargin = Edge(context, view, .LeadingMargin)
trailingMargin = Edge(context, view, .TrailingMargin)
centerXWithinMargins = Edge(context, view, .CenterXWithinMargins)
centerYWithinMargins = Edge(context, view, .CenterYWithinMargins)

centerWithinMargins = Point(context, [
Edge(context, view, .CenterXWithinMargins),
Edge(context, view, .CenterYWithinMargins)
])
#endif
}
}
31 changes: 6 additions & 25 deletions Cartography/Point.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,13 @@ import UIKit
import AppKit
#endif

public enum Point: Compound {
case Center(Context, View)
case CenterWithinMargins(Context, View)
public struct Point: Compound {
let context: Context
let properties: [Property]

var context: Context {
switch (self) {
case let .Center(context, _):
return context
case let .CenterWithinMargins(context, _):
return context
}
}

var properties: [Property] {
switch (self) {
case let .Center(context, view):
return [
Edge.CenterX(context, view),
Edge.CenterY(context, view)
]
case let .CenterWithinMargins(context, view):
return [
Edge.CenterXWithinMargins(context, view),
Edge.CenterYWithinMargins(context, view)
]
}
internal init(_ context: Context, _ properties: [Property]) {
self.context = context
self.properties = properties
}
}

Expand Down
20 changes: 6 additions & 14 deletions Cartography/Size.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,13 @@ import UIKit
import AppKit
#endif

public enum Size : Compound {
case Size(Context, View)

var context: Context {
switch (self) {
case let .Size(context, _):
return context
}
}
public struct Size : Compound {
let context: Context
let properties: [Property]

var properties: [Property] {
switch (self) {
case let .Size(context, view):
return [ Dimension.Width(context, view), Dimension.Height(context, view) ]
}
internal init(_ context: Context, _ properties: [Property]) {
self.context = context
self.properties = properties
}
}

Expand Down

0 comments on commit 593be74

Please sign in to comment.