Skip to content

Commit

Permalink
Stack improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleve committed Apr 26, 2020
1 parent 63a5a14 commit 0733fd2
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
2 changes: 1 addition & 1 deletion BlueprintUI/Sources/Layout/Column.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import UIKit
/// Displays a list of items in a linear vertical layout.
public struct Column: StackElement {

public var children: [(element: Element, traits: StackLayout.Traits, key: AnyHashable?)] = []
public var children: [StackChild] = []

private (set) public var layout = StackLayout(axis: .vertical)

Expand Down
2 changes: 1 addition & 1 deletion BlueprintUI/Sources/Layout/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import UIKit
/// Displays a list of items in a linear horizontal layout.
public struct Row: StackElement {

public var children: [(element: Element, traits: StackLayout.Traits, key: AnyHashable?)] = []
public var children: [StackChild] = []

private (set) public var layout = StackLayout(axis: .horizontal)

Expand Down
114 changes: 106 additions & 8 deletions BlueprintUI/Sources/Layout/Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,74 @@ import UIKit
public protocol StackElement: Element {
init()
var layout: StackLayout { get }
var children: [(element: Element, traits: StackLayout.Traits, key: AnyHashable?)] { get set }
var children: [StackChild] { get set }
}

public struct StackChild {
public var traits : StackLayout.Traits
public var key : AnyHashable?
public var element : Element

public init(
traits : StackLayout.Traits = .flexible,
key : AnyHashable? = nil,
element : Element
) {
self.traits = traits
self.key = key
self.element = element
}

public static func fixed(
key : AnyHashable? = nil,
_ element : Element
) -> StackChild
{
StackChild(
traits: .fixed,
key: key,
element: element
)
}

public static func flexible(
key : AnyHashable? = nil,
_ element : Element
) -> StackChild
{
StackChild(
traits: .flexible,
key: key,
element: element
)
}

public static func grows(
key : AnyHashable? = nil,
_ element : Element
) -> StackChild
{
StackChild(
traits: .grows,
key: key,
element: element
)
}

public static func shrinks(
key : AnyHashable? = nil,
_ element : Element
) -> StackChild
{
StackChild(
traits: .shrinks,
key: key,
element: element
)
}
}


extension StackElement {

public var content: ElementContent {
Expand Down Expand Up @@ -75,19 +140,25 @@ extension StackElement {
/// - child: The child element to add to this stack
///
mutating public func add(growPriority: CGFloat = 1.0, shrinkPriority: CGFloat = 1.0, key: AnyHashable? = nil, child: Element) {
children.append((
element: child,
self.add(child: StackChild(
traits: StackLayout.Traits(growPriority: growPriority, shrinkPriority: shrinkPriority),
key: key
key: key,
element: child
))
}

mutating public func add(key : AnyHashable? = nil, fixed : Element) {
self.add(growPriority: 0.0, shrinkPriority: 0.0, key: key, child: fixed)
public static func += (lhs : inout Self, rhs : StackChild) {
lhs.add(child: rhs)
}

mutating public func add(key : AnyHashable? = nil, flexible : Element) {
self.add(growPriority: 1.0, shrinkPriority: 1.0, key: key, child: flexible)
public static func += (lhs : inout Self, rhs : [StackChild]) {
for element in rhs {
lhs.add(child: element)
}
}

mutating public func add(child : StackChild) {
children.append(child)
}

public static func += (lhs : inout Self, rhs : Element) {
Expand Down Expand Up @@ -121,6 +192,33 @@ public struct StackLayout: Layout {
self.shrinkPriority = shrinkPriority
}

public static var fixed : Traits {
Traits(
growPriority: 0.0,
shrinkPriority: 0.0
)
}

public static var flexible : Traits {
Traits(
growPriority: 1.0,
shrinkPriority: 1.0
)
}

public static var grows : Traits {
Traits(
growPriority: 1.0,
shrinkPriority: 0.0
)
}

public static var shrinks : Traits {
Traits(
growPriority: 1.0,
shrinkPriority: 0.0
)
}
}

public var axis: Axis
Expand Down
4 changes: 2 additions & 2 deletions SampleApp/Sources/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ fileprivate struct FeedItem: ProxyElement {
cornerStyle: .rounded(radius: 32.0),
wrapping: nil
).constrainedTo(width: .absolute(64), height: .absolute(64))

row.add(fixed: avatar)
row += .fixed(avatar)

row += FeedItemBody(post: post)
}
Expand Down

0 comments on commit 0733fd2

Please sign in to comment.