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

Fix wrap content calculation by filter hidden views #280

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
20 changes: 12 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1371,20 +1371,24 @@ The following methods are useful to adjust view's width and/or height to wrap al

**Methods:**

* **`wrapContent()`**
**`wrapContent(padding: CGFloat)`**
**`wrapContent(padding: UIEdgeInsets)`**
Adjust the view's width and height to wrap all its subviews. The method also adjusts subviews's position to create a tight wrap. It is also possible to specify an optional padding around all subviews.
* **`wrapContent(:WrapType)`**
**`wrapContent(:WrapType, padding: CGFloat)`** **`wrapContent(:WrapType, padding: UIEdgeInsets)`**
Adjust the view's width AND/OR height to wrap all its subviews. Accept a WrapType parameter to define the wrapping type. It is also possible to specify an optional padding around all subviews.
* **`wrapContent(viewFilter: ViewFilter = .all)`**
**`wrapContent(padding: CGFloat, viewFilter: ViewFilter = .all)`**
**`wrapContent(padding: UIEdgeInsets, viewFilter: ViewFilter = .all)`**
Adjust the view's width and height to wrap all its subviews. The method also adjusts subviews's position to create a tight wrap. It is also possible to specify an optional padding around all subviews. Additionally, it's possible to specify whether to include all views or only visible ones.
* **`wrapContent(:WrapType, viewFilter: ViewFilter = .all)`** **`wrapContent(:WrapType, padding: UIEdgeInsets, viewFilter: ViewFilter = .all)`**
**`wrapContent(:WrapType, padding: CGFloat, viewFilter: ViewFilter = .all)`**
Adjust the view's width AND/OR height to wrap all its subviews. Accept a WrapType parameter to define the wrapping type. It is also possible to specify an optional padding around all subviews. Additionally, it's possible to specify whether to include all views or only visible ones.

**Types:**

* **`WrapType`** values:
* `.horizontally`: Adjust the view's width and update subviews's horizontal position.
* `.vertically`: Adjust only the view's height and update subviews's vertical position.
* `.all`: Adjust the view's width AND height and update subviews position. This is the default WrapType parameter value `wrapContent()` methods.

* **`ViewFilter`** values:
* `.all`: Consider all views
* `.visibleOnly`: Consider only visible views (isHidden is false and alpha is > 0)

###### Usage examples:
```swift
Expand Down
4 changes: 4 additions & 0 deletions Sources/Extensions/CALayer+PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ extension CALayer: Layoutable {
return sublayers ?? []
}

public var isConsideredVisibleForViewFilters: Bool {
return !isHidden && opacity > 0
}

public var pin: PinLayout<CALayer> {
return PinLayout(view: self, keepTransform: true)
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Extensions/NSView+PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ extension NSView: Layoutable {
return PinLayout(view: self, keepTransform: false)
}

public var isConsideredVisibleForViewFilters: Bool {
return !isHidden && alphaValue > 0
}

@objc public var pinObjc: PinLayoutObjC {
return PinLayoutObjCImpl(view: self, keepTransform: true)
}
Expand Down
4 changes: 4 additions & 0 deletions Sources/Extensions/UIView+PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ extension UIView: Layoutable, SizeCalculable {
return PinLayoutObjCImpl(view: self, keepTransform: true)
}

public var isConsideredVisibleForViewFilters: Bool {
return !isHidden && alpha > 0
}

public func getRect(keepTransform: Bool) -> CGRect {
guard !Pin.autoSizingInProgress || autoSizingRect == nil else { return autoSizingRect ?? CGRect.zero }

Expand Down
2 changes: 2 additions & 0 deletions Sources/Layoutable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public protocol Layoutable: AnyObject, Equatable, CustomDebugStringConvertible {
var superview: PinView? { get }
var subviews: [PinView] { get }

var isConsideredVisibleForViewFilters: Bool { get }

func getRect(keepTransform: Bool) -> CGRect
func setRect(_ rect: CGRect, keepTransform: Bool)

Expand Down
43 changes: 29 additions & 14 deletions Sources/PinLayout+WrapContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,25 @@ import AppKit
extension PinLayout {
/**
Adjust the view's width & height to wrap all its subviews. The method also adjust subviews position to create a tight wrap.

- Parameters:
- viewFilter: Specify whether to include all views or only visible ones.
*/
@discardableResult
public func wrapContent() -> PinLayout {
return wrapContent(.all, padding: PEdgeInsets(top: 0, left: 0, bottom: 0, right: 0), { return "wrapContent()" })
public func wrapContent(viewFilter: ViewFilter = .all) -> PinLayout {
return wrapContent(.all, padding: PEdgeInsets(top: 0, left: 0, bottom: 0, right: 0), viewFilter: viewFilter, { return "wrapContent()" })
}

/**
Adjust the view's width & height to wrap all its subviews. The method also adds a padding around all subviews.

- Parameters:
- padding: Specify a padding value.
- viewFilter: Specify whether to include all views or only visible ones.
*/
@discardableResult
public func wrapContent(padding: CGFloat) -> PinLayout {
return wrapContent(.all, padding: PEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), { return "wrapContent(padding: \(padding)" })
public func wrapContent(padding: CGFloat, viewFilter: ViewFilter = .all) -> PinLayout {
return wrapContent(.all, padding: PEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), viewFilter: viewFilter, { return "wrapContent(padding: \(padding)" })
}

/**
Expand All @@ -51,10 +55,11 @@ extension PinLayout {

- Parameters:
- padding: Specify a padding using an UIEdgeInsets.
- viewFilter: Specify whether to include all views or only visible ones.
*/
@discardableResult
public func wrapContent(padding: PEdgeInsets) -> PinLayout {
return wrapContent(.all, padding: padding, { return "wrapContent(padding: \(insetsDescription(padding))" })
public func wrapContent(padding: PEdgeInsets, viewFilter: ViewFilter = .all) -> PinLayout {
return wrapContent(.all, padding: padding, viewFilter: viewFilter, { return "wrapContent(padding: \(insetsDescription(padding))" })
}

/**
Expand All @@ -64,10 +69,11 @@ extension PinLayout {

- Parameters:
- type: Specify the wrap type (.all, .horizontally, .vertically)
- viewFilter: Specify whether to include all views or only visible ones.
*/
@discardableResult
public func wrapContent(_ type: WrapType) -> PinLayout {
return wrapContent(type, padding: PEdgeInsets(top: 0, left: 0, bottom: 0, right: 0), { return "wrapContent(\(type.description)" })
public func wrapContent(_ type: WrapType, viewFilter: ViewFilter = .all) -> PinLayout {
return wrapContent(type, padding: PEdgeInsets(top: 0, left: 0, bottom: 0, right: 0), viewFilter: viewFilter, { return "wrapContent(\(type.description)" })
}

/**
Expand All @@ -79,10 +85,11 @@ extension PinLayout {
- Parameters:
- type: Specify the wrap type (.all, .horizontally, .vertically)
- padding: Specify a padding value.
- viewFilter: Specify whether to include all views or only visible ones.
*/
@discardableResult
public func wrapContent(_ type: WrapType, padding: CGFloat) -> PinLayout {
return wrapContent(type, padding: PEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), { return "wrapContent(\(type.description), padding: \(padding)" })
public func wrapContent(_ type: WrapType, padding: CGFloat, viewFilter: ViewFilter = .all) -> PinLayout {
return wrapContent(type, padding: PEdgeInsets(top: padding, left: padding, bottom: padding, right: padding), viewFilter: viewFilter, { return "wrapContent(\(type.description), padding: \(padding)" })
}

/**
Expand All @@ -94,14 +101,22 @@ extension PinLayout {
- Parameters:
- type: Specify the wrap type (.all, .horizontally, .vertically)
- padding: Specify a padding using an UIEdgeInsets.
- viewFilter: Specify whether to include all views or only visible ones.
*/
@discardableResult
public func wrapContent(_ type: WrapType, padding: PEdgeInsets) -> PinLayout {
return wrapContent(type, padding: padding, { return "wrapContent(\(type.description), padding: \(insetsDescription(padding))" })
public func wrapContent(_ type: WrapType, padding: PEdgeInsets, viewFilter: ViewFilter = .all) -> PinLayout {
return wrapContent(type, padding: padding, viewFilter: viewFilter, { return "wrapContent(\(type.description), padding: \(insetsDescription(padding))" })
}

private func wrapContent(_ type: WrapType, padding: PEdgeInsets, _ context: Context) -> PinLayout {
let subviews = view.subviews
private func wrapContent(_ type: WrapType, padding: PEdgeInsets, viewFilter: ViewFilter, _ context: Context) -> PinLayout {
let subviews: [PinView.PinView]
switch viewFilter {
case .visibleOnly:
subviews = view.subviews.filter { $0.isConsideredVisibleForViewFilters }
case .all:
subviews = view.subviews
}

guard !subviews.isEmpty else { return self }

let firstViewRect = subviews[0].getRect(keepTransform: keepTransform)
Expand Down
7 changes: 7 additions & 0 deletions Sources/Types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ public enum FitType {
case vertically
}

@objc public enum ViewFilter: Int {
/// Consider all views
case all
/// Consider only visible views (isHidden is false and alpha is > 0)
case visibleOnly
}

@objc public enum LayoutDirection: Int {
case auto
case ltr
Expand Down