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

DSL for PlasticView Anchors and Values #76

Merged
merged 24 commits into from Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
ba74bf9
Add offset property to Anchor
m4tbat Nov 22, 2016
b5c634a
Add ability to add an offset to an anchor through `+` and `-` operators
m4tbat Nov 22, 2016
0438cb7
Support anchor's new offset property in PlasticView setters
m4tbat Nov 22, 2016
480c2cf
Add missing operators to Value for completeness
m4tbat Nov 22, 2016
16a8918
Make Value conform to ExpressibleByIntegerLiteral and ExpressibleByFl…
m4tbat Nov 22, 2016
2e04d17
Add `scaled` and `fixed` properties to `CGFloat`, `Double` and `Int` …
m4tbat Nov 22, 2016
599fdb9
Merge remote-tracking branch 'BendingSpoons/master' into anchor-dsl
m4tbat Nov 22, 2016
2948814
Merge remote-tracking branch 'BendingSpoons/master' into anchor-dsl
m4tbat Nov 22, 2016
dde411e
Add tests for laying out plastic views with anchors with offsets
m4tbat Nov 22, 2016
f590924
Merge remote-tracking branch 'BendingSpoons/master' into anchor-dsl
m4tbat Nov 22, 2016
80a408e
Add offset to anchor's coordinate
m4tbat Nov 27, 2016
51ae831
Remove setTop/Bottom/Left/Right/CenterX/CenterY/Width/Height methods …
m4tbat Nov 27, 2016
486eab4
Merge remote-tracking branch 'BendingSpoons/master' into anchor-dsl
m4tbat Nov 27, 2016
7b90a30
Use new anchor + offset setters in Plastic convenience methods
m4tbat Nov 27, 2016
3254e6a
Update tests to match changes to PlasticView anchor setters
m4tbat Nov 27, 2016
762a493
Remove Value initializer `init(_ scalable: CGFloat)`
m4tbat Nov 27, 2016
b9c5287
Improve Anchor.coordinate
m4tbat Nov 27, 2016
cf6aa33
Remove ambiguous initializers from Size
m4tbat Nov 27, 2016
c8109b4
Remove ambiguous inizializers from EdgeInsets
m4tbat Nov 27, 2016
c2c279b
Update Size- and EdgeInsets-related tests
m4tbat Nov 27, 2016
af88c63
Remove Value conformance to ExpressibleByIntegerLiteral and Expressib…
m4tbat Nov 27, 2016
17aad30
Remove extensions to CGFloat, Double and Float to add Value-returning…
m4tbat Nov 27, 2016
eaea787
Add overloads to + and - operators operating on Anchor and CGFloat va…
m4tbat Nov 27, 2016
7791124
Add tests for creation of Anchors through the + operator taking a CGF…
m4tbat Nov 27, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 33 additions & 9 deletions Katana/Plastic/Anchor.swift
Expand Up @@ -32,16 +32,21 @@ public struct Anchor: Equatable {

/// the `PlasticView` to which the anchor is associated to
let view: PlasticView


/// the offset at which this anchor will be set, with respect to the anchor it will be assigned to
var offset: Value

/**
Creates an anchor with a given type, related to a specific `PlasticView`

- parameter kind: the kind of the anchor
- parameter view: the view the anchor pertains to
- parameter offset: the offset at which this anchor will be set, with respect to the anchor it will be assigned to
*/
init(kind: Kind, view: PlasticView) {
init(kind: Kind, view: PlasticView, offset: Value = .zero) {
self.kind = kind
self.view = view
self.offset = offset
}

/**
Expand All @@ -51,27 +56,32 @@ public struct Anchor: Equatable {
var coordinate: CGFloat {
let absoluteOrigin = self.view.absoluteOrigin
let size = self.view.frame

var coord: CGFloat

switch self.kind {
case .left:
return absoluteOrigin.x
coord = absoluteOrigin.x

case .right:
return absoluteOrigin.x + size.width
coord = absoluteOrigin.x + size.width

case .centerX:
return absoluteOrigin.x + size.width / 2.0
coord = absoluteOrigin.x + size.width / 2.0

case .top:
return absoluteOrigin.y
coord = absoluteOrigin.y

case .bottom:
return absoluteOrigin.y + size.height
coord = absoluteOrigin.y + size.height

case .centerY:
return absoluteOrigin.y + size.height / 2.0
coord = absoluteOrigin.y + size.height / 2.0

}

coord += view.scaleValue(offset)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd change this in return coord + view.scaleValue(offset)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


return coord
}

/**
Expand All @@ -85,4 +95,18 @@ public struct Anchor: Equatable {
public static func == (lhs: Anchor, rhs: Anchor) -> Bool {
return lhs.kind == rhs.kind && lhs.view === rhs.view
}

/**
Create an anchor equal to `lhs`, but with an offset equal to `lhs.offset + rhs`
*/
public static func + (lhs: Anchor, rhs: Value) -> Anchor {
return Anchor(kind: lhs.kind, view: lhs.view, offset: lhs.offset + rhs)
}

/**
Create an anchor equal to `lhs`, but with an offset equal to `lhs.offset - rhs`
*/
public static func - (lhs: Anchor, rhs: Value) -> Anchor {
return Anchor(kind: lhs.kind, view: lhs.view, offset: lhs.offset + -rhs)
}
}
16 changes: 8 additions & 8 deletions Katana/Plastic/Array+Plastic.swift
Expand Up @@ -40,13 +40,13 @@ extension Array where Element: PlasticView {
}

let leftmostOffset = (right.coordinate - left.coordinate - totalWidth) / 2.0
firstView.setLeft(left, offset: Value.fixed(leftmostOffset))
firstView.left = left + .fixed(leftmostOffset)

for (index, view) in otherViews.enumerated() {
let leftwardView = self[index] // take prev view
let scaledSpacing = scaledSpacings[index]
let anchor = Anchor(kind: .right, view: leftwardView)
view.setLeft(anchor, offset: .fixed(scaledSpacing))
view.left = anchor + .fixed(scaledSpacing)
}
}

Expand Down Expand Up @@ -80,13 +80,13 @@ extension Array where Element: PlasticView {
}

let upmostOffset = (bottom.coordinate - top.coordinate - totalHeight) / 2.0
firstView.setTop(top, offset: .fixed(upmostOffset))
firstView.top = top + .fixed(upmostOffset)

for (index, view) in otherViews.enumerated() {
let upperView = self[index]
let scaledSpacing = scaledSpacings[index]
let anchor = Anchor(kind: .bottom, view: upperView)
view.setTop(anchor, offset: .fixed(scaledSpacing))
view.top = anchor + .fixed(scaledSpacing)
}
}

Expand Down Expand Up @@ -144,12 +144,12 @@ extension Array where Element: PlasticView {
view.width = .fixed(width)

if index == 0 {
view.setLeft(left, offset: insets.left)
view.left = left + insets.left

} else {
let leftwardView = self[index-1]
let spacing = spacings[index-1]
view.setLeft(leftwardView.right, offset: spacing)
view.left = leftwardView.right + spacing
}
}
}
Expand Down Expand Up @@ -207,12 +207,12 @@ extension Array where Element: PlasticView {
view.height = .fixed(height)

if index == 0 {
view.setTop(top, offset: insets.top)
view.top = top + insets.top

} else {
let upperView = self[index-1]
let spacing = spacings[index - 1]
view.setTop(upperView.bottom, offset: spacing)
view.top = upperView.bottom + spacing
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Katana/Plastic/EdgeInsets.swift
Expand Up @@ -67,10 +67,10 @@ public struct EdgeInsets: Equatable {
- warning: Always prefer the static method `scalable(_:_:_:_:)` instead of this constructor
*/
public init(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) {
self.top = Value(top)
self.left = Value(left)
self.bottom = Value(bottom)
self.right = Value(right)
self.top = .scalable(top)
self.left = .scalable(left)
self.bottom = .scalable(bottom)
self.right = .scalable(right)
}


Expand Down
50 changes: 24 additions & 26 deletions Katana/Plastic/PlasticView+Convenience.swift
Expand Up @@ -18,8 +18,8 @@ extension PlasticView {
- parameter insets: the insets to use when filling the view frame. Only the left and right insets are used.
*/
public func fillHorizontally(_ view: PlasticView, insets: EdgeInsets = .zero) {
self.setLeft(view.left, offset: insets.left)
self.setRight(view.right, offset: -insets.right)
self.left = view.left + insets.left
self.right = view.right - insets.right
}

/**
Expand All @@ -30,8 +30,8 @@ extension PlasticView {
- parameter insets: the insets to use when filling the view frame. Only the left and right insets are used
*/
public func fillVertically(_ view: PlasticView, insets: EdgeInsets = .zero) {
self.setTop(view.top, offset: insets.top)
self.setBottom(view.bottom, offset: -insets.bottom)
self.top = view.top + insets.top
self.bottom = view.bottom - insets.bottom
}

/**
Expand Down Expand Up @@ -81,10 +81,11 @@ extension PlasticView {
bottom: Anchor,
right: Anchor,
insets: EdgeInsets = .zero) {
self.setLeft(left, offset: insets.left)
self.setRight(right, offset: -insets.right)
self.setTop(top, offset: insets.top)
self.setBottom(bottom, offset: -insets.bottom)

self.left = left + insets.left
self.right = right - insets.right
self.top = top + insets.top
self.bottom = bottom - insets.bottom
}

/**
Expand All @@ -106,10 +107,7 @@ extension PlasticView {
aspectRatio: CGFloat = 1,
insets: EdgeInsets = .zero) {

self.setLeft(left, offset: insets.left)
self.setRight(right, offset: -insets.right)
self.setTop(top, offset: insets.top)
self.setBottom(bottom, offset: -insets.bottom)
self.fill(top: top, left: left, bottom: bottom, right: right, insets: insets)

let width = self.width.unscaledValue
let height = self.height.unscaledValue
Expand All @@ -133,7 +131,7 @@ extension PlasticView {
*/
public func centerBetween(left: Anchor, right: Anchor) {
let offset: Value = .fixed((right.coordinate - left.coordinate) / 2.0)
self.setCenterX(left, offset: offset)
self.centerX = left + offset
}

/**
Expand All @@ -145,7 +143,7 @@ extension PlasticView {
*/
public func centerBetween(top: Anchor, bottom: Anchor) {
let offset: Value = .fixed((bottom.coordinate - top.coordinate) / 2.0)
self.setCenterY(top, offset: offset)
self.centerY = top + offset
}

/**
Expand All @@ -167,9 +165,9 @@ extension PlasticView {
- parameter insets: the insets to use when aligning edges
*/
public func coverLeft(_ view: PlasticView, insets: EdgeInsets = .zero) {
self.setLeft(view.left, offset: insets.left)
self.setTop(view.top, offset: insets.top)
self.setBottom(view.bottom, offset: -insets.bottom)
self.left = view.left + insets.left
self.top = view.top + insets.top
self.bottom = view.bottom - insets.bottom
}

/**
Expand All @@ -180,9 +178,9 @@ extension PlasticView {
- parameter insets: the insets to use when aligning edges
*/
public func coverRight(_ view: PlasticView, insets: EdgeInsets = .zero) {
self.setRight(view.right, offset: -insets.right)
self.setTop(view.top, offset: insets.top)
self.setBottom(view.bottom, offset: -insets.bottom)
self.right = view.right - insets.right
self.top = view.top + insets.top
self.bottom = view.bottom - insets.bottom
}

/**
Expand All @@ -193,9 +191,9 @@ extension PlasticView {
- parameter insets: the insets to use when aligning edges
*/
public func asHeader(_ view: PlasticView, insets: EdgeInsets = EdgeInsets.zero) {
self.setLeft(view.left, offset: insets.left)
self.setRight(view.right, offset: -insets.right)
self.setTop(view.top, offset: insets.top)
self.left = view.left + insets.left
self.right = view.right - insets.right
self.top = view.top + insets.top
}

/**
Expand All @@ -206,8 +204,8 @@ extension PlasticView {
- parameter insets: the insets to use when aligning edges
*/
public func asFooter(_ view: PlasticView, insets: EdgeInsets = EdgeInsets.zero) {
self.setLeft(view.left, offset: insets.left)
self.setRight(view.right, offset: -insets.right)
self.setBottom(view.bottom, offset: -insets.bottom)
self.left = view.left + insets.left
self.right = view.right - insets.right
self.bottom = view.bottom - insets.bottom
}
}