Skip to content

Commit

Permalink
Add types
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Oct 22, 2023
1 parent 65374e7 commit 8d148b6
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 88 deletions.
8 changes: 4 additions & 4 deletions Sources/SpatialLib/ConstraintKind/ConstraintKind+Access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension ConstraintKind where Self: View {
*/
public func applyAnchorAndSize(to: View, sizeTo: View? = nil, width: CGFloat? = nil, height: CGFloat? = nil, align: Alignment = .topLeft, alignTo: Alignment = .topLeft, multiplier: CGSize = .init(width: 1, height: 1), offset: CGPoint = .zero, sizeOffset: CGSize = .zero, useMargin: Bool = false) {
// Call the more customizable `applyAnchorAndSize` method with a closure that defines the anchor and size constraints
self.applyAnchorAndSize { _ in
self.applyAnchorAndSize { (_: View) in
let anchor: AnchorConstraint = Constraint.anchor(
self, // The source view
to: to, // The target view
Expand All @@ -34,7 +34,7 @@ extension ConstraintKind where Self: View {
useMargin: useMargin // Whether to use layout margins or not
) // Create an anchor constraint for the view
let size: SizeConstraint = {
if let width = width, let height = height { // Check if both width and height are defined
if let width: CGFloat = width, let height: CGFloat = height { // Check if both width and height are defined
// If both width and height are defined, create a size constraint with the specified width and height
return Constraint.size(
self, // The source view
Expand Down Expand Up @@ -70,7 +70,7 @@ extension ConstraintKind where Self: View {
*/
public func applyAnchor(to: View, align: Alignment = .topLeft, alignTo: Alignment = .topLeft, offset: CGPoint = .zero, useMargin: Bool = false) {
// Call the more customizable `applyAnchor` method with a closure that defines the anchor constraint
self.applyAnchor { _ in // Call the `applyAnchor` method with a closure that defines the anchor constraint
self.applyAnchor { (_: View) in // Call the `applyAnchor` method with a closure that defines the anchor constraint
Constraint.anchor(
self, // The source view
to: to, // The target view
Expand All @@ -95,7 +95,7 @@ extension ConstraintKind where Self: View {
*/
public func applySize(to: View, width: CGFloat? = nil, height: CGFloat? = nil, offset: CGSize = .zero, multiplier: CGSize = .init(width: 1, height: 1)) {
// Call the more customizable `applySize` method with a closure that defines the size constraint
self.applySize { _ in // Call the `applySize` method with a closure that defines the size constraint
self.applySize { (_: View) in // Call the `applySize` method with a closure that defines the size constraint
Constraint.size(
self, // The source view
to: to, // The target view to use for size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
* ```
*/
public func applySizes(to: View, width: CGFloat? = nil, height: CGFloat? = nil, offset: CGSize = .zero, multiplier: CGSize = .init(width: 1, height: 1)) {
self.applySizes { views in // Apply size constraints to the views in the array
self.applySizes { (views: [View]) in // Apply size constraints to the views in the array
views.map { // Map each view to a size constraint
Constraint.size(
$0, // The source view
Expand All @@ -42,7 +42,7 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
* - multiplier: The scalar to multiply the size of the views by.
*/
public func applySizes(width: CGFloat, height: CGFloat, multiplier: CGSize = .init(width: 1, height: 1)) {
self.applySizes { views in // Apply size constraints to the views in the array
self.applySizes { (views: [View]) in // Apply size constraints to the views in the array
views.map { // Map each view to a size constraint
Constraint.size(
$0, // The source view
Expand All @@ -65,7 +65,7 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
* view.applyAnchor(to: self, align: .top, alignTo: .top)
*/
public func applyAnchors(to: View, align: VerticalAlign = .top, alignTo: VerticalAlign = .top, offset: CGFloat = .zero, useMargin: Bool = false) {
self.applyAnchors(axis: .ver) { views in // Apply vertical anchor constraints to the views in the array
self.applyAnchors(axis: .ver) { (views: [View]) in // Apply vertical anchor constraints to the views in the array
views.map { // Map each view to an anchor constraint
Constraint.anchor(
$0, // The source view
Expand All @@ -91,7 +91,7 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
* view.applyAnchor(to: self, align: .left, alignTo: .left)
*/
public func applyAnchors(to: View, align: HorizontalAlign = .left, alignTo: HorizontalAlign = .left, offset: CGFloat = .zero, useMargin: Bool = false) {
self.applyAnchors(axis: .hor) { views in // Apply horizontal anchor constraints to the views in the array
self.applyAnchors(axis: .hor) { (views: [View]) in // Apply horizontal anchor constraints to the views in the array
views.map { // Map each view to an anchor constraint
Constraint.anchor(
$0, // The source view
Expand Down
20 changes: 15 additions & 5 deletions Sources/SpatialLib/ConstraintKind/ConstraintKind+Bulk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
$0.element.setConstraint(anchor: anchor, size: size) // Set the anchor and size constraints for the view at the current index
}
let layoutConstraints: [NSLayoutConstraint] = { // Create an array of layout constraints
let anchors: [NSLayoutConstraint] = constraints.anchorConstraints.reduce([]) { $0 + [$1.x, $1.y] } // Create an array of anchor constraints
let sizes: [NSLayoutConstraint] = constraints.sizeConstraints.reduce([]) { $0 + [$1.w, $1.h] } // Create an array of size constraints
let anchors: [NSLayoutConstraint] = constraints.anchorConstraints.reduce([]) {
$0 + [$1.x, $1.y]
} // Create an array of anchor constraints
let sizes: [NSLayoutConstraint] = constraints.sizeConstraints.reduce([]) {
$0 + [$1.w, $1.h]
} // Create an array of size constraints
return anchors + sizes // Concatenate the arrays of anchor and size constraints
}()
NSLayoutConstraint.activate(layoutConstraints) // Activate the layout constraints for the views in the array
Expand All @@ -48,7 +52,9 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
let size: SizeConstraint = constraints[$0.offset] // Get the size constraint for the view at the current index
$0.element.size = size // Set the size constraint for the view at the current index
}
let layoutConstraints: [NSLayoutConstraint] = constraints.reduce([]) { $0 + [$1.w, $1.h] } // Create an array of layout constraints
let layoutConstraints: [NSLayoutConstraint] = constraints.reduce([]) {
$0 + [$1.w, $1.h]
} // Create an array of layout constraints
NSLayoutConstraint.activate(layoutConstraints) // Activate the layout constraints for the views in the array
}
/**
Expand All @@ -64,7 +70,9 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
let anchor: AnchorConstraint = constraints[$0.offset] // Get the anchor constraint for the view at the current index
$0.element.anchor = anchor // Set the anchor constraint for the view at the current index
}
let layoutConstraints: [NSLayoutConstraint] = constraints.reduce([]) { $0 + [$1.x, $1.y] } // Create an array of layout constraints
let layoutConstraints: [NSLayoutConstraint] = constraints.reduce([]) {
$0 + [$1.x, $1.y]
} // Create an array of layout constraints
NSLayoutConstraint.activate(layoutConstraints) // Activate the layout constraints for the views in the array
}
/**
Expand All @@ -84,7 +92,9 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
case .ver: $0.element.anchor?.y = anchor // Set the vertical anchor constraint for the view at the current index
}
}
let layoutConstraints: [NSLayoutConstraint] = constraints.reduce([]) { $0 + [$1] } // Create an array of layout constraints
let layoutConstraints: [NSLayoutConstraint] = constraints.reduce([]) {
$0 + [$1]
} // Create an array of layout constraints
NSLayoutConstraint.activate(layoutConstraints) // Activate the layout constraints for the views in the array
}
}
20 changes: 12 additions & 8 deletions Sources/SpatialLib/ConstraintKind/ConstraintKind+SpaceAround.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Foundation
import QuartzCore
#if os(iOS)
import UIKit
#elseif os(macOS)
import Cocoa
#endif
/**
* Extension for spaceAround method
*/
Expand Down Expand Up @@ -58,9 +62,9 @@ fileprivate class SpaceAroundUtil {
let rect: CGRect = parent.bounds.inset(by: inset)
let itemVoid: CGFloat = horizontalItemVoid(rect: rect, views: views)
var x: CGFloat = rect.origin.x + itemVoid // Interim x
views.forEach { item in
item.activateConstraint { _ in
let constraint = Constraint.anchor(
views.forEach { (item: ConstraintKind.ViewConstraintKind) in
item.activateConstraint { (_: View) in
let constraint: NSLayoutConstraint = Constraint.anchor(
item, // The item to anchor
to: parent, // The parent view to anchor to
align: .left, // The alignment to use for the item
Expand All @@ -85,9 +89,9 @@ fileprivate class SpaceAroundUtil {
let rect: CGRect = parent.bounds.inset(by: inset)
let itemVoid: CGFloat = verticalItemVoid(rect: rect, views: views)
var y: CGFloat = rect.origin.y + itemVoid // Interim y
views.forEach { item in
views.forEach { (item: ConstraintKind.ViewConstraintKind) in
item.activateConstraint { _ in
let constraint = Constraint.anchor(
let constraint: NSLayoutConstraint = Constraint.anchor(
item, // The item to anchor
to: parent, // The parent view to anchor to
align: .top, // The alignment to use for the item
Expand All @@ -114,7 +118,7 @@ extension SpaceAroundUtil {
private static func horizontalItemVoid(rect: CGRect, views: [ConstraintKind.ViewConstraintKind]) -> CGFloat {
let totW: CGFloat = views.reduce(0) { $0 + ($1.size?.w.constant ?? 0) } // Find the totalW of all items
let totVoid: CGFloat = rect.width - totW // Find totVoid by doing w - totw
let numOfVoids = CGFloat(views.count + 1) // Then divide this voidSpace with .count - 1 and
let numOfVoids: CGFloat = .init(views.count + 1) // Then divide this voidSpace with .count - 1 and
return totVoid / numOfVoids // Iterate of each item and inserting itemVoid in + width
}
/**
Expand All @@ -126,7 +130,7 @@ extension SpaceAroundUtil {
private static func verticalItemVoid(rect: CGRect, views: [ConstraintKind.ViewConstraintKind]) -> CGFloat {
let totH: CGFloat = views.reduce(0) { $0 + ($1.size?.h.constant ?? 0) } // Find the totalW of all items
let totVoid: CGFloat = rect.height - totH // Find totVoid by doing w - totw
let numOfVoids = CGFloat(views.count + 1) // Then divide this voidSpace with .count - 1 and
let numOfVoids: CGFloat = .init(views.count + 1) // Then divide this voidSpace with .count - 1 and
return totVoid / numOfVoids // Iterate of each item and inserting itemVoid in + width
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Foundation
import QuartzCore
#if os(iOS)
import UIKit
#elseif os(macOS)
import Cocoa
#endif
/**
* Space items evenly to fill length
*/
Expand Down Expand Up @@ -57,9 +61,9 @@ private class SpaceBetweenUtil {
// Initialize the interim x value to the origin of the inset parent bounds
var x: CGFloat = rect.origin.x // Interim x
// Loop through each view and activate a constraint to align it to the left of the parent view
views.forEach { item in
item.activateConstraint { _ in // Fixme: ⚠️️ Create applyAnchor for hor and ver
let constraint = Constraint.anchor(
views.forEach { (item: ConstraintKind.ViewConstraintKind) in
item.activateConstraint { (_: View) in // Fixme: ⚠️️ Create applyAnchor for hor and ver
let constraint: NSLayoutConstraint = Constraint.anchor(
item, // The item to anchor
to: parent, // The parent view to anchor to
align: .left, // The alignment to use for the item
Expand All @@ -84,9 +88,9 @@ private class SpaceBetweenUtil {
let rect: CGRect = parent.bounds.inset(by: inset) // Get the parent view's bounds and inset them by the given amount
let itemVoid: CGFloat = verticalItemVoid(rect: rect, views: views) // Calculate the vertical space between each view
var y: CGFloat = rect.origin.y // Set the initial y position to the top of the parent view
views.forEach { item in // Loop through each view to align
views.forEach { (item: ConstraintKind.ViewConstraintKind) in // Loop through each view to align
item.activateConstraint { _ in // Activate the view's constraints
let constraint = Constraint.anchor(
let constraint: NSLayoutConstraint = Constraint.anchor(
item, // The item to anchor
to: parent, // The parent view to anchor to
align: .top, // The alignment to use for the item
Expand Down Expand Up @@ -114,7 +118,7 @@ extension SpaceBetweenUtil {
private static func horizontalItemVoid(rect: CGRect, views: [ConstraintKind.ViewConstraintKind]) -> CGFloat {
let totW: CGFloat = views.reduce(0) { $0 + ($1.size?.w.constant ?? 0) } // Calculate the total width of all views
let totVoid: CGFloat = rect.width - totW // Calculate the total horizontal space available
let numOfVoids = CGFloat(views.count - 1) // Calculate the number of spaces between views
let numOfVoids: CGFloat = .init(views.count - 1) // Calculate the number of spaces between views
return totVoid / numOfVoids // Calculate the amount of horizontal space to insert between each view
}
/**
Expand All @@ -127,7 +131,7 @@ extension SpaceBetweenUtil {
private static func verticalItemVoid(rect: CGRect, views: [ConstraintKind.ViewConstraintKind]) -> CGFloat {
let totH: CGFloat = views.reduce(0) { $0 + ($1.size?.h.constant ?? 0) } // Calculate the total height of all views
let totVoid: CGFloat = rect.height - totH // Calculate the total vertical space available
let numOfVoids = CGFloat(views.count - 1) // Calculate the number of spaces between views
let numOfVoids: CGFloat = .init(views.count - 1) // Calculate the number of spaces between views
return totVoid / numOfVoids // Calculate the amount of vertical space to insert between each view
}
}
7 changes: 4 additions & 3 deletions Sources/SpatialLib/ConstraintKind/ConstraintKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension ConstraintKind {
get {
self.anchorAndSize?.anchor
} set {
if let newValue = newValue { anchorAndSize?.anchor = newValue }
if let newValue: AnchorConstraint = newValue { anchorAndSize?.anchor = newValue }
}
}
// DEPRECATED ⚠️️
Expand All @@ -28,7 +28,7 @@ extension ConstraintKind {
get {
self.anchorAndSize?.size
} set {
if let newValue = newValue { anchorAndSize?.size = newValue }
if let newValue: SizeConstraint = newValue { anchorAndSize?.size = newValue }
}
}
/**
Expand All @@ -37,7 +37,8 @@ extension ConstraintKind {
*/
public var anchorAndSize: AnchorAndSize? {
get {
guard let anchor = anchor, let size = size else { return nil } // If either anchor or size is nil, return nil
guard let anchor: AnchorConstraint = anchor,
let size: SizeConstraint = size else { return nil } // If either anchor or size is nil, return nil
return (anchor, size) // Return a tuple of anchor and size
} set {
anchor = newValue?.anchor // Set the anchor to the anchor value of the new tuple
Expand Down
Loading

0 comments on commit 8d148b6

Please sign in to comment.