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 4 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
1 change: 1 addition & 0 deletions Sources/Layoutable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public protocol Layoutable: AnyObject, Equatable, CustomDebugStringConvertible {

var superview: PinView? { get }
var subviews: [PinView] { get }
var isHidden: 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 = .none) -> 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 = .none) -> 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 = .none) -> 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 = .none) -> 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 = .none) -> 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 = .none) -> 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 = .none, _ context: Context) -> PinLayout {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need default argument value (viewFilter = .none) in private function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed here, the parameter should always be specified by callers

let subviews: [PinView.PinView]
switch viewFilter {
case .visibleOnly:
subviews = view.subviews.filter { !$0.isHidden }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about views with alpha = 0?

There is public visible(_ views: [UIView]) views filter that takes alpha into account -

return views.filter({ !$0.isHidden && $0.alpha > 0 })
, it may lead to confusion if name "view filter" will behave differently here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, you could use the visible() here to filter views.

case .none:
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 {
/// No filter, use all views
case none
lucdion marked this conversation as resolved.
Show resolved Hide resolved
/// Consider only views where `hidden` is false
lucdion marked this conversation as resolved.
Show resolved Hide resolved
case visibleOnly
}

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