Skip to content

Commit

Permalink
Add support for slicing from directional edges
Browse files Browse the repository at this point in the history
Currently slicing uses min/max x/y definitions for specifying edges. Adding an API for specifying leading/trailing edges (as well as top/bottom for convenience) makes layout easier for apps that support both LTR and RTL layout.
  • Loading branch information
NickEntin committed Jul 28, 2024
1 parent 420dc31 commit 67459d0
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions Paralayout/GeometryAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ extension CGSize {

// MARK: -

public enum DirectionalEdge {
case top
case bottom
case leading
case trailing
}

extension CGRect {

/// Initialize a CGRect with bounding coordinates (always with non-negative size).
Expand Down Expand Up @@ -141,10 +148,35 @@ extension CGRect {
public func offset(by offset: UIOffset) -> CGRect {
return CGRect(origin: origin.offset(by: offset), size: size)
}


/// Divides the receiver in two.
///
/// - parameter edge: The edge from which the amount is interpreted.
/// - parameter amount: The size of the slice (absolute).
/// - returns: A tuple (slice: A rect with a width/height of the `amount`, remainder: A rect with a width/height of
/// the receiver reduced by `amount`).
public func slice(
from edge: DirectionalEdge,
amount: CGFloat,
in layoutDirectionProvider: LayoutDirectionProviding
) -> (slice: CGRect, remainder: CGRect) {
switch (edge, layoutDirectionProvider.effectiveUserInterfaceLayoutDirection) {
case (.top, _):
slice(from: .minYEdge, amount: amount)
case (.bottom, _):
slice(from: .maxYEdge, amount: amount)
case (.leading, .leftToRight), (.trailing, .rightToLeft):
slice(from: .minXEdge, amount: amount)
case (.trailing, .leftToRight), (.leading, .rightToLeft):
slice(from: .maxXEdge, amount: amount)
@unknown default:
fatalError("Unknown user interface layout direction")
}
}

/// Divides the receiver in two.
///
/// - parameter from: The edge from which the amount is interpreted.
/// - parameter edge: The edge from which the amount is interpreted.
/// - parameter amount: The size of the slice (absolute).
/// - returns: A tuple (slice: A rect with a width/height of the `amount`, remainder: A rect with a width/height of
/// the receiver reduced by `amount`).
Expand Down

0 comments on commit 67459d0

Please sign in to comment.