Skip to content

Commit

Permalink
Begin tweaking comments with LLM PT.7
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Sep 22, 2023
1 parent 405c019 commit 4d6a92e
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 178 deletions.
18 changes: 9 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
import PackageDescription

let package = Package(
name: "Spatial",
platforms: [.iOS(.v15), .macOS(.v12)],
name: "Spatial", // The name of the package
platforms: [.iOS(.v15), .macOS(.v12)], // The platforms the package supports
products: [
.library(
name: "Spatial",
targets: ["Spatial"])
name: "Spatial", // The name of the library product
targets: ["Spatial"]) // The targets that make up the product
],
dependencies: [],
dependencies: [], // The dependencies of the package
targets: [
.target(name: "Spatial",
dependencies: []
.target(name: "Spatial", // The name of the target
dependencies: [] // The dependencies of the target
),
.testTarget(name: "SpatialTests", dependencies: ["Spatial"])
.testTarget(name: "SpatialTests", dependencies: ["Spatial"]) // The name of the test target and its dependencies
]
)
)
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@

Definition: **Spatial** | ˈspeɪʃ(ə)l | adjective | **describes how objects fit together in space**

## Table of Contents
- [What is it](#what-is-it)
- [How does it work](#how-does-it-work)
- [How do I get it](#how-do-i-get-it)
- [Gotchas](#gotchas)
- [Example](#example)
- [Todo](#todo)

### What is it
Hassle-free AutoLayout, tailored for interactivity and animation. Created for how our mental model thinks autolayout works. Not optimized for brevity.


### How does it work
- Spatial is just extensions and enums which enable you to write less boilerplate code
- Spatial is interchangeable with Vanilla AutoLayout
Expand Down
106 changes: 66 additions & 40 deletions Sources/Spatial/view/View+Anchor.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Foundation
import Foundation // needed?
#if os(iOS)
import UIKit
#elseif os(macOS)
Expand Down Expand Up @@ -60,76 +60,102 @@ public final class Constraint {
)
}
/**
* Vertical anchoring
* Creates a vertical anchor constraint between the view and the parent view.
* - Parameters:
* - view: target view
* - to: parent view
* - align: object alignment
* - alignTo: parent alginment
* - offset: point offset
* - useMargin: This works, but when you use size constraints then you have to pin to sides, or use the sizeOffset
* - relation: Constraint relation: equal, lessThanOrEqual, greaterThanOrEqual
* - view: The view to create the constraint for.
* - to: The parent view to anchor the constraint to.
* - align: The vertical alignment of the view relative to the parent view.
* - alignTo: The vertical alignment of the parent view to anchor the constraint to.
* - offset: The offset of the view from the parent view.
* - useMargin: Whether to use the view's layout margins when creating the constraint.
* - relation: The relation of the constraint: equal, lessThanOrEqual, greaterThanOrEqual.
* - Returns: A vertical anchor constraint.
* ## Examples:
* label.anchor(to: self, align: .top, alignTo: .top, offset: 16, useMargin: true, relation: .lessThanOrEqual)
*/
public static func anchor(_ view: View, to: View, align: VerticalAlign, alignTo: VerticalAlign, offset: CGFloat = .zero, useMargin: Bool = false, relation: NSLayoutConstraint.Relation = .equal) -> NSLayoutConstraint {
// Get the layout attribute for the specified vertical alignment of the view.
let attr: NSLayoutConstraint.Attribute = layoutAttr(align: align)
// Get the layout attribute for the specified vertical alignment of the parent view.
let relatedByAttr: NSLayoutConstraint.Attribute = layoutAttr(align: alignTo, useMargin: useMargin)
return .init(item: view, attribute: attr, relatedBy: relation, toItem: to, attribute: relatedByAttr, multiplier: 1.0, constant: offset)
// Create a vertical anchor constraint between the view and the parent view.
let constraint = NSLayoutConstraint(
item: view, // The view to create the constraint for.
attribute: attr, // The vertical attribute of the view to create the constraint for.
relatedBy: relation, // The relation of the constraint: equal, lessThanOrEqual, greaterThanOrEqual.
toItem: to, // The parent view to anchor the constraint to.
attribute: relatedByAttr, // The vertical attribute of the parent view to anchor the constraint to.
multiplier: 1.0, // The multiplier of the constraint.
constant: offset // The offset of the view from the parent view.
)
}
}
/**
* Internal helper methods
*/
extension Constraint {
/**
* For aligning in the x-axis (internal)
* - Remark: Layout margin is only available for iOS and tvOS
* Returns the layout attribute for the specified horizontal alignment of the view.
*
* - Parameters:
* - align: object alignment
* - useMargin: os margin
* - align: The horizontal alignment of the view.
* - useMargin: Whether to use the view's layout margins when creating the constraint.
*
* - Returns: The layout attribute for the specified horizontal alignment of the view.
*
* - Remark: Layout margin is only available for iOS and tvOS.
*
* ## Examples:
* ```
* layoutAttr(align: .left, useMargin: true)
* ```
*/
private static func layoutAttr(align: HorizontalAlign, useMargin: Bool = false) -> NSLayoutConstraint.Attribute {
switch align {
case .left:
#if os(iOS)
if useMargin { return .leftMargin }
if useMargin { return .leftMargin } // Use the view's left margin when creating the constraint if `useMargin` is `true`.
#endif
return .left
return .left // Use the view's left edge when creating the constraint if `useMargin` is `false` or if the platform is not iOS.
case .right:
#if os(iOS)
if useMargin { return .rightMargin }
if useMargin { return .rightMargin } // Use the view's right margin when creating the constraint if `useMargin` is `true`.
#endif
return .right
return .right // Use the view's right edge when creating the constraint if `useMargin` is `false` or if the platform is not iOS.
case .centerX:
#if os(iOS)
if useMargin { return .centerXWithinMargins }
if useMargin { return .centerXWithinMargins } // Use the view's horizontal center within its margins when creating the constraint if `useMargin` is `true`.
#endif
return .centerX
return .centerX // Use the view's horizontal center when creating the constraint if `useMargin` is `false` or if the platform is not iOS.
}
}
/**
* For aligning in the y axis (internal)
* - Remark: Layout margin is o ly available for `iOS` and `tvOS
* Returns the layout attribute for the specified vertical alignment of the view.
* - Parameters:
* - align: object alignment
* - useMargin: os alignment
* - align: The vertical alignment of the view.
* - useMargin: Whether to use the view's layout margins when creating the constraint.
* - Returns: The layout attribute for the specified vertical alignment of the view.
* - Remark: Layout margin is only available for iOS and tvOS.
* ## Examples:
* layoutAttr(align: .top, useMargin: true)
*/
private static func layoutAttr(align: VerticalAlign, useMargin: Bool = false) -> NSLayoutConstraint.Attribute {
switch align {
case .top:
#if os(iOS)
if useMargin { return .topMargin }
#endif
return .top
case .bottom:
#if os(iOS)
if useMargin { return .bottomMargin }
#endif
return .bottom
case .centerY:
#if os(iOS)
if useMargin { return .centerYWithinMargins }
#endif
return .centerY
}
case .top:
#if os(iOS)
if useMargin { return .topMargin } // Use the view's top margin when creating the constraint if `useMargin` is `true`.
#endif
return .top // Use the view's top edge when creating the constraint if `useMargin` is `false` or if the platform is not iOS.
case .bottom:
#if os(iOS)
if useMargin { return .bottomMargin } // Use the view's bottom margin when creating the constraint if `useMargin` is `true`.
#endif
return .bottom // Use the view's bottom edge when creating the constraint if `useMargin` is `false` or if the platform is not iOS.
case .centerY:
#if os(iOS)
if useMargin { return .centerYWithinMargins } // Use the view's vertical center within its margins when creating the constraint if `useMargin` is `true`.
#endif
return .centerY // Use the view's vertical center when creating the constraint if `useMargin` is `false` or if the platform is not iOS.
}
}
}
Loading

0 comments on commit 4d6a92e

Please sign in to comment.