Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace assertions in slice method with alert func #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions Paralayout/GeometryAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
//

import os
import UIKit

// MARK: - Operator Overloads
Expand Down Expand Up @@ -144,6 +145,8 @@ extension CGRect {

/// Divides the receiver in two.
///
/// The `slice` will always have a length of the specified `amount` in the perpendicular axis to the `edge`. The `remainder` will have the length remaining along that axis, clamped to zero if the `amount` is longer than the receiver's length.
///
/// - parameter from: 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
Expand All @@ -152,34 +155,42 @@ extension CGRect {
switch edge {
case .minXEdge:
// Left.
assert(amount <= width, "Cannot slice rect \(self) at edge \(edge) by \(amount)!")
if amount > width {
ParalayoutAlertForInvalidSliceDimensions()
}
return (
CGRect(x: minX, y: minY, width: amount, height: height),
CGRect(x: minX + amount, y: minY, width: width - amount, height: height)
CGRect(x: minX + amount, y: minY, width: max(width - amount, 0), height: height)
)

case .minYEdge:
// Top.
assert(amount <= height, "Cannot slice rect \(self) at edge \(edge) by \(amount)!")
if amount > height {
ParalayoutAlertForInvalidSliceDimensions()
}
return(
CGRect(x: minX, y: minY, width: width, height: amount),
CGRect(x: minX, y: minY + amount, width: width, height: height - amount)
CGRect(x: minX, y: minY + amount, width: width, height: max(height - amount, 0))
)

case .maxXEdge:
// Right.
assert(amount <= width, "Cannot slice rect \(self) at edge \(edge) by \(amount)!")
if amount > width {
ParalayoutAlertForInvalidSliceDimensions()
}
return(
CGRect(x: maxX - amount, y: minY, width: amount, height: height),
CGRect(x: minX, y: minY, width: width - amount, height: height)
CGRect(x: minX, y: minY, width: max(width - amount, 0), height: height)
)

case .maxYEdge:
// Bottom.
assert(amount <= height, "Cannot slice rect \(self) at edge \(edge) by \(amount)!")
if amount > height {
ParalayoutAlertForInvalidSliceDimensions()
}
return(
CGRect(x: minX, y: maxY - amount, width: width, height: amount),
CGRect(x: minX, y: minY, width: width, height: height - amount)
CGRect(x: minX, y: minY, width: width, height: max(height - amount, 0))
)
}
}
Expand Down Expand Up @@ -282,3 +293,20 @@ extension NSDirectionalEdgeInsets {
}

}

// MARK: -

nonisolated
private func ParalayoutAlertForInvalidSliceDimensions() {
os_log(
"%@",
log: ParalayoutLog,
type: .default,
"""
Paralayout detected a slice with an `amount` larger than the receiver's length in the specified axis. \
Set a symbolic breakpoint for \"ParalayoutAlertForInvalidSliceDimensions\" to debug. \
Call stack:
\(Thread.callStackSymbols.dropFirst(1).joined(separator: "\n"))
"""
)
}
20 changes: 20 additions & 0 deletions Paralayout/ParalayoutLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright © 2024 Block, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import os

nonisolated
internal let ParalayoutLog = OSLog(subsystem: "com.squareup.Paralayout", category: "layout")
2 changes: 0 additions & 2 deletions Paralayout/UIView+Alignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ extension AlignmentContext {

// MARK: -

@MainActor
private let ParalayoutLog = OSLog(subsystem: "com.squareup.Paralayout", category: "layout")

/// Triggered when an alignment method is called that uses mismatched position types, i.e. aligning a view's leading or
/// trailing edge to another view's left or right edge, or vice versa. This type of mismatch is likely to look correct
Expand Down