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 025e8ba commit d87ae71
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 113 deletions.
70 changes: 39 additions & 31 deletions Sources/Spatial/ConstraintKind/ConstraintKind+Access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,64 +6,72 @@ import QuartzCore
*/
extension ConstraintKind where Self: View {
/**
* One-liner for `applyAnchorAndSize`
* Applies anchor and size constraints to the view, with optional parameters for customization.
* - Remark: This is a one-liner for `applyAnchorAndSize`, which is a more customizable method.
* ## Examples:
* view.applyAnchorAndSize(to: self, height: 100, align: .centerCenter, alignTo: .centerCenter)
* - Parameters:
* - to: The instance to apply to
* - sizeTo: Provide this if you need to base the size on another view
* - width: The width to apply to instance
* - height: The height to apply to instance
* - align: Alignment for the `to` view
* - alignTo: Alignment for the `sizeTo` view, if one was provided
* - multiplier: Multiplies the `size` or `sizeTo`
* - offset: Offset for the `to` parameter
* - sizeOffset: Offset for the `sizeTo` parameter (use negative values for inset)
* - useMargin: Aligns to AutoLayout margins or not
* - to: The instance to apply the constraints to.
* - sizeTo: The view to base the size on, if any.
* - width: The width to apply to the view.
* - height: The height to apply to the view.
* - align: The alignment for the `to` view.
* - alignTo: The alignment for the `sizeTo` view, if one was provided.
* - multiplier: The multiplier for the size constraints.
* - offset: The offset for the `to` parameter.
* - sizeOffset: The offset for the `sizeTo` parameter (use negative values for inset).
* - useMargin: Whether to align to AutoLayout margins or not.
*/
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
let anchor: AnchorConstraint = Constraint.anchor(self, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin)
let anchor: AnchorConstraint = Constraint.anchor(self, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin) // Create an anchor constraint for the view
let size: SizeConstraint = {
if let width = width, let height = height { // This method exists when you have size, but don't want to set size based on another view, which is the case if you have defined both width and height params
if let width = width, let height = 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, size: .init(width: width, height: height), multiplier: multiplier)
} else {
// If either width or height is not defined, create a size constraint based on the size of another view or the view itself
return Constraint.size(self, to: sizeTo ?? to, width: width, height: height, offset: sizeOffset, multiplier: multiplier)
}
}()
return (anchor, size)
}() // Create a size constraint for the view, based on the width, height, sizeTo, and sizeOffset parameters
return (anchor, size) // Return a tuple containing the anchor and size constraints for the view
}
}
/**
* One-liner for `applyAnchor`
* Applies an anchor constraint to the view, with optional parameters for customization.
* ## Examples:
* view.applyAnchor(to: self, align: .center, alignTo: .center)
* - Remark: This is a one-liner for `applyAnchor`, which is a more customizable method.
* - Parameters:
* - to: The instance to apply to
* - align: Alignment for the `to` view
* - alignTo: Alignment for the `sizeTo` view, if one was provided
* - offset: Offset for the `to` parameter
* - useMargin: Aligns to AutoLayout margins or not
* - to: The instance to apply the constraint to.
* - align: The alignment for the `to` view.
* - alignTo: The alignment for the `sizeTo` view, if one was provided.
* - offset: The offset for the `to` parameter.
* - useMargin: Whether to align to AutoLayout margins or not.
*/
public func applyAnchor(to: View, align: Alignment = .topLeft, alignTo: Alignment = .topLeft, offset: CGPoint = .zero, useMargin: Bool = false) {
self.applyAnchor { _ in
Constraint.anchor(self, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin)
// 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
Constraint.anchor(self, to: to, align: align, alignTo: alignTo, offset: offset, useMargin: useMargin) // Create an anchor constraint for the view
}
}
/**
* One-liner method for the long-hand method self.applySize
* Applies a size constraint to the view, with optional parameters for customization.
* - Remark: This is a one-liner for `applySize`, which is a more customizable method.
* ## Examples:
* view.applySize(to:self) // multiplier,offset
* - Parameters:
* - to: The instance to apply to
* - width: the width to apply to instance
* - height: the height to apply to instance
* - multiplier: multiplies the `size` or `sizeTo` default is (width:1,height:1)
* - offset: offset for the `to` parameter
* - to: The instance to apply the constraint to.
* - width: The width to apply to the view.
* - height: The height to apply to the view.
* - multiplier: The multiplier for the size constraints.
* - offset: The offset for the `to` parameter.
*/
public func applySize(to: View, width: CGFloat? = nil, height: CGFloat? = nil, offset: CGSize = .zero, multiplier: CGSize = .init(width: 1, height: 1)) {
self.applySize { _ in
Constraint.size(self, to: to, width: width, height: height, offset: offset, multiplier: multiplier)
// 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
Constraint.size(self, to: to, width: width, height: height, offset: offset, multiplier: multiplier) // Create a size constraint for the view
}
}
}
64 changes: 34 additions & 30 deletions Sources/Spatial/ConstraintKind/ConstraintKind+Apply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,63 @@ import UIKit
import Cocoa
#endif
/**
* Update constraints (For items that are of type `ConstraintKind`)
* - Remark: adding a method called `activateConstraints` doesn't make any sense because you have only anchor and size or either
* Extension for updating constraints on items that are of type `ConstraintKind` and `View`.
* - Remark: Adding a method called `activateConstraints` doesn't make sense because you can only have anchor and size constraints, or either.
*/
extension ConstraintKind where Self: View {
/**
* Activates and sets size and anchor to a `ConstraintKind`
* - Important: ⚠️️ Remember to deactive constraints before calling this method
* - Remark: Same as UIView().activateConstraint... but also sets size and anchor constraints (ConstraintKind) (For animation etc)
* Activates and sets the anchor and size constraints for a `ConstraintKind`.
* - Important: ⚠️️ Remember to deactivate constraints before calling this method.
* - Remark: This method is similar to `UIView().activateConstraint...`, but also sets the size and anchor constraints for animation purposes.
* - Parameters:
* - closure: A closure that returns the anchor and size constraints for the view.
* ## Examples:
* sliderBar.applyAnchorAndSize { view in
* let anchor = Constraint.anchor(view, to: self, align: .topLeft, alignTo: .topLeft)
* let size = Constraint.size(view, size: size)
* return (anchor: anchor, size: size) // (anchor, size) also works
* }
* - Parameter closure: The constraints is returned from the closure
*/
public func applyAnchorAndSize(closure: AnchorAndSizeClosure) {
self.translatesAutoresizingMaskIntoConstraints = false
let constraints: AnchorAndSize = closure(self)
setConstraint(anchor: constraints.anchor, size: constraints.size)
NSLayoutConstraint.activate([constraints.anchor.x, constraints.anchor.y, constraints.size.w, constraints.size.h])
self.translatesAutoresizingMaskIntoConstraints = false // Disable the view's translation of autoresizing mask into constraints
let constraints: AnchorAndSize = closure(self) // Call the closure to get the anchor and size constraints for the view
setConstraint(anchor: constraints.anchor, size: constraints.size) // Set the anchor and size constraints for the view
NSLayoutConstraint.activate([constraints.anchor.x, constraints.anchor.y, constraints.size.w, constraints.size.h]) // Activate the anchor and size constraints for the view
}
/**
* Activates and sets anchor to a `ConstraintKind`
* - Important: ⚠️️ Remember to deactive constraints before calling this method
* - Parameter closure: The constraints is returned from the closure
* Applies an anchor constraint to the view, with optional parameters for customization.
* - Remark: This is a one-liner for `applyAnchorAndSize`, which also sets the size constraints for animation purposes.
* - Parameters:
* - closure: A closure that returns the anchor constraint for the view.
* - Important: ⚠️️ Remember to deactivate constraints before calling this method.
*/
public func applyAnchor(closure: AnchorClosure) {
self.translatesAutoresizingMaskIntoConstraints = false
let anchorConstraint: AnchorConstraint = closure(self)
let constraints: [NSLayoutConstraint] = [anchorConstraint.x, anchorConstraint.y]
self.anchor = anchorConstraint
NSLayoutConstraint.activate(constraints)
self.translatesAutoresizingMaskIntoConstraints = false // Disable the view's translation of autoresizing mask into constraints
let anchorConstraint: AnchorConstraint = closure(self) // Call the closure to get the anchor constraint for the view
let constraints: [NSLayoutConstraint] = [anchorConstraint.x, anchorConstraint.y] // Create an array of constraints for the view
self.anchor = anchorConstraint // Set the anchor constraint for the view
NSLayoutConstraint.activate(constraints) // Activate the anchor constraints for the view
}
/**
* Activates and sets size to a `ConstraintKind`
* - Important: ⚠️️ Remember to deactive constraints before calling this method
* - Parameter closure: The constraints is returned from the closure
* Applies a size constraint to the view, with optional parameters for customization.
* - Important: ⚠️️ Remember to deactivate constraints before calling this method.
* - Parameters:
* - closure: A closure that returns the size constraint for the view.
*/
public func applySize(closure: SizeClosure) {
self.translatesAutoresizingMaskIntoConstraints = false
let sizeConstraint: SizeConstraint = closure(self)
let constraints: [NSLayoutConstraint] = [sizeConstraint.w, sizeConstraint.h]
self.size = sizeConstraint
NSLayoutConstraint.activate(constraints)
self.translatesAutoresizingMaskIntoConstraints = false // Disable the view's translation of autoresizing mask into constraints
let sizeConstraint: SizeConstraint = closure(self) // Call the closure to get the size constraint for the view
let constraints: [NSLayoutConstraint] = [sizeConstraint.w, sizeConstraint.h] // Create an array of constraints for the view
self.size = sizeConstraint // Set the size constraint for the view
NSLayoutConstraint.activate(constraints) // Activate the size constraints for the view
}
/**
* Sets both anchor and size to a ConstraintKind
* Sets both anchor and size constraints for a `ConstraintKind`.
* - Parameters:
* - anchor: constraint anchor
* - size: constraint size
* - anchor: The anchor constraint to set.
* - size: The size constraint to set.
*/
public func setConstraint(anchor: AnchorConstraint, size: SizeConstraint) {
self.anchorAndSize = (anchor, size)
self.anchorAndSize = (anchor, size) // Set the anchor and size constraints for the view
}
}
Loading

0 comments on commit d87ae71

Please sign in to comment.