Skip to content

Fix wrap content calculation by filter hidden views #280

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions Sources/Extensions/CALayer+PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import QuartzCore
extension CALayer: Layoutable {
public typealias PinView = CALayer

public var alpha: CGFloat {
CGFloat(opacity)
}

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

var isHidden: Bool { get }
var alpha: CGFloat { 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, _ context: Context) -> PinLayout {
let subviews: [PinView.PinView]
switch viewFilter {
case .visibleOnly:
subviews = view.subviews.filter { !$0.isHidden && $0.alpha > 0 }
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
/// Consider only visible views (isHidden is false and alpha is > 0)
case visibleOnly
}

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