Skip to content

Commit b7401ac

Browse files
committed
Add isIncludedInPinLayout property to Layoutable
1 parent ce929a6 commit b7401ac

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

Sources/Extensions/CALayer+PinLayout.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
import QuartzCore
2121

2222
extension CALayer: Layoutable {
23+
private struct pinlayoutAssociatedKeys {
24+
static var pinlayoutIsIncludedInPinLayoutSizeCalculation = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
25+
}
26+
2327
public typealias PinView = CALayer
2428

2529
public var superview: CALayer? {
@@ -38,6 +42,15 @@ extension CALayer: Layoutable {
3842
return PinLayout(view: self, keepTransform: false)
3943
}
4044

45+
public var isIncludedInPinLayoutSizeCalculation: Bool {
46+
get {
47+
return objc_getAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayoutSizeCalculation) as? Bool ?? true
48+
}
49+
set {
50+
objc_setAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayoutSizeCalculation, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
51+
}
52+
}
53+
4154
public func getRect(keepTransform: Bool) -> CGRect {
4255
if keepTransform {
4356
/*

Sources/Extensions/NSView+PinLayout.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import Foundation
2323
import AppKit
2424

2525
extension NSView: Layoutable {
26+
private struct pinlayoutAssociatedKeys {
27+
static var pinlayoutIsIncludedInPinLayoutSizeCalculation = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
28+
}
29+
2630
public typealias PinView = NSView
2731

2832
public var pin: PinLayout<NSView> {
@@ -37,6 +41,15 @@ extension NSView: Layoutable {
3741
return PinLayoutObjCImpl(view: self, keepTransform: true)
3842
}
3943

44+
public var isIncludedInPinLayoutSizeCalculation: Bool {
45+
get {
46+
return objc_getAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayoutSizeCalculation) as? Bool ?? true
47+
}
48+
set {
49+
objc_setAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayoutSizeCalculation, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
50+
}
51+
}
52+
4053
public func getRect(keepTransform: Bool) -> CGRect {
4154
if let superview = superview, !superview.isFlipped {
4255
var flippedRect = frame

Sources/Extensions/UIView+PinLayout.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ extension UIView: Layoutable, SizeCalculable {
3737
return PinLayoutObjCImpl(view: self, keepTransform: true)
3838
}
3939

40+
public var isIncludedInPinLayoutSizeCalculation: Bool {
41+
get {
42+
return objc_getAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayoutSizeCalculation) as? Bool ?? true
43+
}
44+
set {
45+
objc_setAssociatedObject(self, &pinlayoutAssociatedKeys.pinlayoutIsIncludedInPinLayoutSizeCalculation, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
46+
}
47+
}
48+
4049
public func getRect(keepTransform: Bool) -> CGRect {
4150
guard !Pin.autoSizingInProgress || autoSizingRect == nil else { return autoSizingRect ?? CGRect.zero }
4251

@@ -95,6 +104,7 @@ extension UIView: Layoutable, SizeCalculable {
95104

96105
extension UIView: AutoSizeCalculable {
97106
private struct pinlayoutAssociatedKeys {
107+
static var pinlayoutIsIncludedInPinLayoutSizeCalculation = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
98108
static var pinlayoutAutoSizingRect = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
99109
static var pinlayoutAutoSizingRectWithMargins = UnsafeMutablePointer<Int8>.allocate(capacity: 1)
100110
}

Sources/Layoutable.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,23 @@ public protocol Layoutable: AnyObject, Equatable, CustomDebugStringConvertible {
2929
var superview: PinView? { get }
3030
var subviews: [PinView] { get }
3131

32+
/// A Boolean value that determines whether the view is included in the PinLayout's size calculation.
33+
///
34+
/// An excluded view does not take up space in the layout
35+
/// when using `wrapContent(_:padding:_:)` or `autoSizeThatFits(_:layoutClosure:)`.
36+
/// The default value is `true`.
37+
var isIncludedInPinLayoutSizeCalculation: Bool { get set }
38+
3239
func getRect(keepTransform: Bool) -> CGRect
3340
func setRect(_ rect: CGRect, keepTransform: Bool)
3441

3542
func convert(_ point: CGPoint, to view: PinView?) -> CGPoint
3643

3744
func isLTR() -> Bool
3845
}
46+
47+
extension Layoutable {
48+
var subviewsIncludedInSizeCalculation: [PinView] {
49+
return subviews.filter(\.isIncludedInPinLayoutSizeCalculation)
50+
}
51+
}

0 commit comments

Comments
 (0)