Skip to content

Commit

Permalink
Begin tweaking comments with LLM PT.4
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Sep 22, 2023
1 parent d87ae71 commit 3fea4fb
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 162 deletions.
73 changes: 43 additions & 30 deletions Sources/Spatial/ConstraintKind/ConstraintKind+Bulk+Access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,77 @@ import QuartzCore
*/
extension Array where Element: ConstraintKind.ViewConstraintKind {
/**
* Size multiple UIView instance
* Applies size constraints to an array of `UIViewConstraintKind`, with optional parameters for customization.
* ## Examples:
* [btn1, btn2, btn3].applySize(to: self, height: 24, offset: .init(width: -40, height: 0))
* - Parameters:
* - to: target view
* - width: target width
* - height: target height
* - offset: Add margin by providing a size offset
* - multiplier: multiply size with this scalar
* - to: The target view to apply the size constraints to.
* - width: The target width for the views.
* - height: The target height for the views.
* - offset: The size offset to add as margin to the views.
* - multiplier: The scalar to multiply the size of the views by.
* - Example:
* ```
* [btn1, btn2, btn3].applySizes(to: self, height: 24, offset: .init(width: -40, height: 0))
* ```
*/
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
views.map {
Constraint.size($0, to: to, width: width, height: height, offset: offset, multiplier: multiplier)
self.applySizes { views in // Apply size constraints to the views in the array
views.map { // Map each view to a size constraint
Constraint.size($0, to: to, width: width, height: height, offset: offset, multiplier: multiplier) // Create a size constraint for the view
}
}
}
/**
* - Description: Same as regular applySizes but we must provide both width and height
* Applies size constraints to an array of `UIViewConstraintKind`, with fixed width and height.
* - Description: Same as `applySizes` but with fixed width and height.
* - Parameters:
* - width: target width
* - height: target height
* - multiplier: multiply size with this scalar
* - width: The target width for the views.
* - height: The target height for the views.
* - 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
views.map {
Constraint.size($0, size: .init(width: width, height: height), multiplier: multiplier)
self.applySizes { views in // Apply size constraints to the views in the array
views.map { // Map each view to a size constraint
Constraint.size($0, size: .init(width: width, height: height), multiplier: multiplier) // Create a size constraint for the view with fixed width and height
}
}
}
/**
* One-liner for `applyAnchor` for many views (vertical)
* Applies vertical anchor constraints to an array of `UIViewConstraintKind`.
* - Description: Same as `applyAnchors` but just for vertical anchor.
* - Parameters:
* - to: The target view to apply the anchor constraints to.
* - align: The object align point for the views.
* - alignTo: The canvas align point for the views.
* - offset: The point offset to add as margin to the views.
* - useMargin: A Boolean value indicating whether to use the OS margin.
* ## Examples:
* view.applyAnchor(to: self, align: .top, alignTo: .top)
* - Parameters:
* - to: target view
* - align: object align point
* - alignTo: canvas align point
* - offset: point offset
* - useMargin: use os margin
*/
public func applyAnchors(to: View, align: VerticalAlign = .top, alignTo: VerticalAlign = .top, offset: CGFloat = .zero, useMargin: Bool = false) {
self.applyAnchors(axis: .ver) { views in
views.map {
Constraint.anchor($0, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin)
self.applyAnchors(axis: .ver) { views in // Apply vertical anchor constraints to the views in the array
views.map { // Map each view to an anchor constraint
Constraint.anchor($0, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin) // Create a vertical anchor constraint for the view
}
}
}
/**
* One-liner for `applyAnchor` for many views (horizontal)
* Applies horizontal anchor constraints to an array of `UIViewConstraintKind`.
* - Description: Same as `applyAnchors` but just for horizontal anchor.
* - Parameters:
* - to: The target view to apply the anchor constraints to.
* - align: The object align point for the views.
* - alignTo: The canvas align point for the views.
* - offset: The point offset to add as margin to the views.
* - useMargin: A Boolean value indicating whether to use the OS margin.
* ## Examples:
* 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
views.map {
Constraint.anchor($0, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin)
self.applyAnchors(axis: .hor) { views in // Apply horizontal anchor constraints to the views in the array
views.map { // Map each view to an anchor constraint
Constraint.anchor($0, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin) // Create a horizontal anchor constraint for the view
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions Sources/Spatial/ConstraintKind/ConstraintKind+SpaceAround.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import QuartzCore
*/
extension Array where Element: ConstraintKind.ViewConstraintKind {
/**
* Same as spaceBetween but does not pin to sides but rather add equal spacing there as well
* - Important: ⚠️️ Only works with `UIConstraintView` where size is available
* - Important: ⚠️️ Only works were the `parent.bound` are available
* - Description: |--[]--[]--[]--[]--[]--|
* Adds equal spacing between views, including at the ends.
* - Important: ⚠️️ Only works with `UIConstraintView` where size is available.
* - Important: ⚠️️ Only works where the `parent.bound` are available.
* - Description: Adds equal spacing between views, including at the ends, in either the vertical or horizontal direction.
* - Note: |--[]--[]--[]--[]--[]--|
* ## Examples:
* let views: [ConstraintView] = [UIColor.purple, .orange,.red].map {
* let view: ConstraintView = .init(frame: .zero)
Expand All @@ -20,14 +21,14 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
* views.applyAnchors(to: self, align: .top, alignTo: .top, offset: 20)
* views.spaceAround(dir: .hor, parent: self)
* - Parameters:
* - inset: Inset the parent bound
* - dir: Vertical or horizontal axis
* - parent: The parent of the views
* - dir: The axis to add spacing to (`hor` for horizontal, `ver` for vertical).
* - parent: The parent view of the views.
* - inset: The amount to inset the parent bounds.
*/
public func spaceAround(dir: Axis, parent: View, inset: EdgeInsets = .init()) {
switch dir {
case .hor: SpaceAroundUtil.spaceAround(horizontally: parent, views: self, inset: inset)
case .ver: SpaceAroundUtil.spaceAround(vertically: parent, views: self, inset: inset)
case .hor: SpaceAroundUtil.spaceAround(horizontally: parent, views: self, inset: inset) // Add equal spacing between views horizontally
case .ver: SpaceAroundUtil.spaceAround(vertically: parent, views: self, inset: inset) // Add equal spacing between views vertically
}
}
}
Expand Down
80 changes: 45 additions & 35 deletions Sources/Spatial/ConstraintKind/ConstraintKind+SpaceBetween.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
* - Important: ⚠️️ This method from layoutSubViews, as you need the parent.bounds to be realized, and its only relaized from AutoLayout when layoutSubViews is called
* - Important: ⚠️️ Only works with `UIConstraintView` classes (parent does not have to be UIViewConstraintKind)
* - Parameters:
* - parent: The containg view that has the views as subViews
* - dir: Which direction you want to distribute items in
* - inset: Use this to inset where items should be set, if none is provided parent.bounds is used
* - dir: The direction in which to distribute the items.
* - parent: The containing view that has the views as subviews.
* - inset: Use this to inset where items should be set. If none is provided, parent bounds are used.
* ## Examples:
* views.spaceBetween(dir: .horizontal, parent: self, inset:x)
*/
public func spaceBetween(dir: Axis, parent: View, inset: EdgeInsets = .init()) {
switch dir {
// If the direction is horizontal, call the spaceBetween method with the parent view, the views to distribute, and the inset value.
case .hor: SpaceBetweenUtil.spaceBetween(horizontally: parent, views: self, inset: inset)
// If the direction is vertical, call the spaceBetween method with the parent view, the views to distribute, and the inset value.
case .ver: SpaceBetweenUtil.spaceBetween(vertically: parent, views: self, inset: inset)
}
}
Expand All @@ -29,44 +31,50 @@ extension Array where Element: ConstraintKind.ViewConstraintKind {
*/
private class SpaceBetweenUtil {
/**
* Horizontal
* Distributes views horizontally with equal spacing between them (only works on views that adhere to ConstraintKind).
* - Parameters:
* - parent: parent view
* - views: views to align
* - inset: parent inset
* - parent: The containing view that has the views as subviews.
* - views: The views to distribute horizontally.
* - inset: Use this to inset where items should be set. If none is provided, parent bounds are used.
* ## Examples:
* SpaceBetweenUtil.spaceBetween(horizontally: parent, views: self, inset: x)
*/
static func spaceBetween(horizontally parent: View, views: [ConstraintKind.ViewConstraintKind], inset: EdgeInsets) {
// Get the parent view's bounds and inset it by the provided inset value
let rect: CGRect = parent.bounds.inset(by: inset)
// Calculate the amount of space between each item
let itemVoid: CGFloat = horizontalItemVoid(rect: rect, views: views)
// 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(item, to: parent, align: .left, alignTo: .left, offset: x)
item.anchor?.x = constraint
return constraint
}
// Increment the interim x value by the width of the current view plus the itemVoid
x += (item.size?.w.constant ?? 0) + itemVoid
}
}
/**
* Vertical
* - Fixme: ⚠️️ use reduce on y
* Vertically aligns the given views within the parent view, with a fixed amount of space between each view.
* - Parameters:
* - parent: parent view
* - views: views to align
* - inset: parent inset
* - parent: The parent view.
* - views: The views to align.
* - inset: The inset from the parent view's bounds to use for alignment.
*/
static func spaceBetween(vertically parent: View, views: [ConstraintKind.ViewConstraintKind], inset: EdgeInsets) {
let rect: CGRect = parent.bounds.inset(by: inset)
let itemVoid: CGFloat = verticalItemVoid(rect: rect, views: views)
var y: CGFloat = rect.origin.y // Interim y
views.forEach { item in
item.activateConstraint { _ in // Fixme: ⚠️️ Create applyAnchor for hor and ver
let constraint = Constraint.anchor(item, to: parent, align: .top, alignTo: .top, offset: y)
item.anchor?.y = constraint
return constraint
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
item.activateConstraint { _ in // Activate the view's constraints
let constraint = Constraint.anchor(item, to: parent, align: .top, alignTo: .top, offset: y) // Align the view to the top of the parent view with the given y offset
item.anchor?.y = constraint // Store the constraint in the view's anchor property
return constraint // Return the constraint
}
y += (item.size?.h.constant ?? 0) + itemVoid
y += (item.size?.h.constant ?? 0) + itemVoid // Increment the y position by the height of the current view plus the vertical space between views
}
}
}
Expand All @@ -75,27 +83,29 @@ private class SpaceBetweenUtil {
*/
extension SpaceBetweenUtil {
/**
* ItemVoid (horizontal)
* Calculates the horizontal space between each view in order to evenly distribute them within the given canvas.
* - Parameters:
* - rect: canvas
* - views: views to align
* - rect: The canvas to align the views within.
* - views: The views to align.
* - Returns: The amount of horizontal space to insert between each view.
*/
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
return totVoid / numOfVoids // Iterate of each item and inserting itemVoid in + width
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
return totVoid / numOfVoids // Calculate the amount of horizontal space to insert between each view
}
/**
* ItemVoid (vertical)
* Calculates the vertical space between each view in order to evenly distribute them within the given canvas.
* - Parameters:
* - rect: canvas
* - views: views to align
* - rect: The canvas to align the views within.
* - views: The views to align.
* - Returns: The amount of vertical space to insert between each view.
*/
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
return totVoid / numOfVoids // Iterate of each item and inserting itemVoid in + width
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
return totVoid / numOfVoids // Calculate the amount of vertical space to insert between each view
}
}
11 changes: 7 additions & 4 deletions Sources/Spatial/ConstraintKind/ConstraintKind+Type.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import Cocoa
#endif
/**
* Single
* Defines a type alias for a view that conforms to `ConstraintKind`.
* - Remark: This is useful for creating combinational types and closure signatures.
*/
extension ConstraintKind where Self: View {
/**
Expand All @@ -16,10 +18,11 @@ extension ConstraintKind where Self: View {
}
/**
* Bulk
* Defines closures that operate on an array of `ViewConstraintKind` elements.
*/
extension Array where Element: ConstraintKind.ViewConstraintKind {
public typealias AnchorAndSizeClosure = (_ views: [View]) -> AnchorConstraintsAndSizeConstraints
public typealias SizesClosure = (_ views: [View]) -> [SizeConstraint]
public typealias AnchorClosure = (_ views: [View]) -> [AnchorConstraint]
public typealias AxisClosure = (_ views: [View]) -> [NSLayoutConstraint]
public typealias AnchorAndSizeClosure = (_ views: [View]) -> AnchorConstraintsAndSizeConstraints // A closure that returns anchor and size constraints for an array of views
public typealias SizesClosure = (_ views: [View]) -> [SizeConstraint] // A closure that returns size constraints for an array of views
public typealias AnchorClosure = (_ views: [View]) -> [AnchorConstraint] // A closure that returns anchor constraints for an array of views
public typealias AxisClosure = (_ views: [View]) -> [NSLayoutConstraint] // A closure that returns axis constraints for an array of views
}
Loading

0 comments on commit 3fea4fb

Please sign in to comment.