Skip to content

Commit

Permalink
Fix issue layoutBox#234
Browse files Browse the repository at this point in the history
  • Loading branch information
OriTheElf committed Jun 6, 2022
1 parent f054f61 commit abe393b
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 91 deletions.
4 changes: 2 additions & 2 deletions Sources/Extensions/CALayer+PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import QuartzCore

extension CALayer: Layoutable {
public typealias View = CALayer
public typealias PinView = CALayer

public var superview: CALayer? {
return superlayer
Expand Down Expand Up @@ -58,7 +58,7 @@ extension CALayer: Layoutable {
}

public func setRect(_ rect: CGRect, keepTransform: Bool) {
let adjustedRect = Coordinates<View>.adjustRectToDisplayScale(rect)
let adjustedRect = Coordinates<PinView>.adjustRectToDisplayScale(rect)

if keepTransform {
/*
Expand Down
4 changes: 2 additions & 2 deletions Sources/Extensions/NSView+PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Foundation
import AppKit

extension NSView: Layoutable {
public typealias View = NSView
public typealias PinView = NSView

public var pin: PinLayout<NSView> {
return PinLayout(view: self, keepTransform: true)
Expand All @@ -48,7 +48,7 @@ extension NSView: Layoutable {
}

public func setRect(_ rect: CGRect, keepTransform: Bool) {
let adjustedRect = Coordinates<View>.adjustRectToDisplayScale(rect)
let adjustedRect = Coordinates<PinView>.adjustRectToDisplayScale(rect)

if let superview = superview, !superview.isFlipped {
var flippedRect = adjustedRect
Expand Down
8 changes: 4 additions & 4 deletions Sources/Extensions/UIView+PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Foundation
import UIKit

extension UIView: Layoutable, SizeCalculable {
public typealias View = UIView
public typealias PinView = UIView

public var pin: PinLayout<UIView> {
return PinLayout(view: self, keepTransform: true)
Expand Down Expand Up @@ -59,7 +59,7 @@ extension UIView: Layoutable, SizeCalculable {
}

public func setRect(_ rect: CGRect, keepTransform: Bool) {
let adjustedRect = Coordinates<View>.adjustRectToDisplayScale(rect)
let adjustedRect = Coordinates<PinView>.adjustRectToDisplayScale(rect)

if keepTransform {
/*
Expand Down Expand Up @@ -118,8 +118,8 @@ extension UIView: AutoSizeCalculable {
}

public func setAutoSizingRect(_ rect: CGRect, margins: PEdgeInsets) {
self.autoSizingRect = Coordinates<View>.adjustRectToDisplayScale(rect)
self.autoSizingRectWithMargins = Coordinates<View>.adjustRectToDisplayScale(rect.inset(by: margins))
self.autoSizingRect = Coordinates<PinView>.adjustRectToDisplayScale(rect)
self.autoSizingRectWithMargins = Coordinates<PinView>.adjustRectToDisplayScale(rect.inset(by: margins))
}

public func autoSizeThatFits(_ size: CGSize, layoutClosure: () -> Void) -> CGSize {
Expand Down
24 changes: 12 additions & 12 deletions Sources/Impl/Coordinates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,58 +34,58 @@ public func _pinlayoutSetUnitTest(scale: CGFloat?) {
}
}

final class Coordinates<View: Layoutable> {
static func hCenter(_ view: View, keepTransform: Bool) -> CGFloat {
final class Coordinates<PinView: Layoutable> {
static func hCenter(_ view: PinView, keepTransform: Bool) -> CGFloat {
let rect = view.getRect(keepTransform: keepTransform)
return rect.minX + (rect.width / 2)
}

static func vCenter(_ view: View, keepTransform: Bool) -> CGFloat {
static func vCenter(_ view: PinView, keepTransform: Bool) -> CGFloat {
let rect = view.getRect(keepTransform: keepTransform)
return rect.minY + (rect.height / 2)
}

static func topLeft(_ view: View, keepTransform: Bool) -> CGPoint {
static func topLeft(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX, y: rect.minY)
}

static func topCenter(_ view: View, keepTransform: Bool) -> CGPoint {
static func topCenter(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX + (rect.width / 2), y: rect.minY)
}

static func topRight(_ view: View, keepTransform: Bool) -> CGPoint {
static func topRight(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX + rect.width, y: rect.minY)
}

static func centerLeft(_ view: View, keepTransform: Bool) -> CGPoint {
static func centerLeft(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX, y: rect.minY + (rect.height / 2))
}

static func center(_ view: View, keepTransform: Bool) -> CGPoint {
static func center(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX + (rect.width / 2), y: rect.minY + (rect.height / 2))
}

static func centerRight(_ view: View, keepTransform: Bool) -> CGPoint {
static func centerRight(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX + rect.width, y: rect.minY + (rect.height / 2))
}

static func bottomLeft(_ view: View, keepTransform: Bool) -> CGPoint {
static func bottomLeft(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX, y: rect.minY + rect.height)
}

static func bottomCenter(_ view: View, keepTransform: Bool) -> CGPoint {
static func bottomCenter(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX + (rect.width / 2), y: rect.minY + rect.height)
}

static func bottomRight(_ view: View, keepTransform: Bool) -> CGPoint {
static func bottomRight(_ view: PinView, keepTransform: Bool) -> CGPoint {
let rect = view.getRect(keepTransform: keepTransform)
return CGPoint(x: rect.minX + rect.width, y: rect.minY + rect.height)
}
Expand Down
12 changes: 6 additions & 6 deletions Sources/Impl/PinLayout+Coordinates.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,26 +361,26 @@ extension PinLayout {
}
}

private func computeCoordinates(_ point: CGPoint, _ layoutSuperview: View, _ referenceSuperview: View) -> CGPoint {
private func computeCoordinates(_ point: CGPoint, _ layoutSuperview: PinView, _ referenceSuperview: PinView) -> CGPoint {
if layoutSuperview == referenceSuperview {
return point // same superview => no coordinates conversion required.
} else if referenceSuperview == layoutSuperview.superview as? View {
} else if referenceSuperview == layoutSuperview.superview as? PinView {
let layoutSuperviewRect = layoutSuperview.getRect(keepTransform: keepTransform)
return CGPoint(x: point.x - layoutSuperviewRect.origin.x,
y: point.y - layoutSuperviewRect.origin.y)
// TOOD: Handle all cases. computeCoordinates should compute coordinates using only untransformed
// coordinates, but UIView.convert(...) below use transformed coordinates!
// Currently we only support 1 and 2 levels.
} else {
return referenceSuperview.convert(point, to: layoutSuperview as? View.View)
return referenceSuperview.convert(point, to: layoutSuperview as? PinView.PinView)
}
}

internal func computeCoordinates(forAnchors anchors: [Anchor], _ context: Context) -> [CGPoint]? {
guard let layoutSuperview = layoutSuperview(context) else { return nil }
var results: [CGPoint] = []
anchors.forEach({ (anchor) in
let anchor = anchor as! AnchorImpl<View>
let anchor = anchor as! AnchorImpl<PinView>
if let referenceSuperview = referenceSuperview(anchor.view, context) {
results.append(computeCoordinates(anchor.point(keepTransform: keepTransform),
layoutSuperview, referenceSuperview))
Expand All @@ -392,7 +392,7 @@ extension PinLayout {
}

internal func computeCoordinate(forEdge edge: HorizontalEdge, _ context: Context) -> CGFloat? {
let edge = edge as! HorizontalEdgeImpl<View>
let edge = edge as! HorizontalEdgeImpl<PinView>
guard let layoutSuperview = layoutSuperview(context) else { return nil }
guard let referenceSuperview = referenceSuperview(edge.view, context) else { return nil }

Expand All @@ -401,7 +401,7 @@ extension PinLayout {
}

internal func computeCoordinate(forEdge edge: VerticalEdge, _ context: Context) -> CGFloat? {
let edge = edge as! VerticalEdgeImpl<View>
let edge = edge as! VerticalEdgeImpl<PinView>
guard let layoutSuperview = layoutSuperview(context) else { return nil }
guard let referenceSuperview = referenceSuperview(edge.view, context) else { return nil }

Expand Down
2 changes: 1 addition & 1 deletion Sources/Impl/PinLayout+Layouting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension PinLayout {
isLayouted = true
}

private func apply(onView view: View) {
private func apply(onView view: PinView) {
displayLayoutWarnings()

var newRect = view.getRect(keepTransform: keepTransform)
Expand Down
14 changes: 7 additions & 7 deletions Sources/Impl/PinLayout+Warning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ extension PinLayout {
}

internal func relativeEdgeContext(method: String, edge: VerticalEdge) -> String {
let edge = edge as! VerticalEdgeImpl<View>
let edge = edge as! VerticalEdgeImpl<PinView>
return "\(method)(to: .\(edge.type.rawValue), of: \(viewDescription(edge.view)))"
}

internal func relativeEdgeContext(method: String, edge: HorizontalEdge) -> String {
let edge = edge as! HorizontalEdgeImpl<View>
let edge = edge as! HorizontalEdgeImpl<PinView>
return "\(method)(to: .\(edge.type.rawValue), of: \(viewDescription(edge.view))"
}

internal func relativeAnchorContext(method: String, anchor: Anchor) -> String {
let anchor = anchor as! AnchorImpl<View>
let anchor = anchor as! AnchorImpl<PinView>
return "\(method)(to: .\(anchor.type.rawValue), of: \(viewDescription(anchor.view)))"
}

Expand Down Expand Up @@ -114,20 +114,20 @@ extension PinLayout {
}
}

internal func viewDescription(_ view: View) -> String {
internal func viewDescription(_ view: PinView) -> String {
let rect = view.getRect(keepTransform: keepTransform)
return "(\(viewName(view)), Frame: \(rect))"
}

internal func viewName(_ view: View) -> String {
internal func viewName(_ view: PinView) -> String {
return "\(type(of: view))"
}

internal func insetsDescription(_ insets: PEdgeInsets) -> String {
return "UIEdgeInsets(top: \(insets.top), left: \(insets.left), bottom: \(insets.bottom), right: \(insets.right))"
}

internal func pinLayoutDisplayConsoleWarning(_ text: String, _ view: View) {
internal func pinLayoutDisplayConsoleWarning(_ text: String, _ view: PinView) {
var displayText = "\n👉 \(text)"

let rect = view.getRect(keepTransform: keepTransform)
Expand All @@ -142,7 +142,7 @@ extension PinLayout {
var hierarchy: [String] = []
while let parent = currentView.superview {
hierarchy.insert("\(type(of: parent))", at: 0)
currentView = parent as! View
currentView = parent as! PinView
}
if hierarchy.count > 0 {
#if swift(>=4.1)
Expand Down
30 changes: 15 additions & 15 deletions Sources/Impl/TypesImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ struct Size {
var height: CGFloat?
}

class EdgeListImpl<View: Layoutable>: EdgeList {
internal let view: View
class EdgeListImpl<PinView: Layoutable>: EdgeList {
internal let view: PinView

init(view: View) {
init(view: PinView) {
self.view = view
}

Expand All @@ -52,14 +52,14 @@ class EdgeListImpl<View: Layoutable>: EdgeList {
var end: HorizontalEdge { return view.isLTR() ? right : left }
}

class HorizontalEdgeImpl<View: Layoutable>: HorizontalEdge {
class HorizontalEdgeImpl<PinView: Layoutable>: HorizontalEdge {
enum EdgeType: String {
case left
case hCenter
case right
}

let view: View
let view: PinView
let type: EdgeType

func x(keepTransform: Bool) -> CGFloat {
Expand All @@ -72,20 +72,20 @@ class HorizontalEdgeImpl<View: Layoutable>: HorizontalEdge {
}
}

internal init(view: View, type: EdgeType) {
internal init(view: PinView, type: EdgeType) {
self.view = view
self.type = type
}
}

class VerticalEdgeImpl<View: Layoutable>: VerticalEdge {
class VerticalEdgeImpl<PinView: Layoutable>: VerticalEdge {
enum EdgeType: String {
case top
case vCenter
case bottom
}

internal let view: View
internal let view: PinView
internal let type: EdgeType

func y(keepTransform: Bool) -> CGFloat {
Expand All @@ -99,16 +99,16 @@ class VerticalEdgeImpl<View: Layoutable>: VerticalEdge {
}
}

internal init(view: View, type: EdgeType) {
internal init(view: PinView, type: EdgeType) {
self.view = view
self.type = type
}
}

class AnchorListImpl<View: Layoutable>: AnchorList {
internal let view: View
class AnchorListImpl<PinView: Layoutable>: AnchorList {
internal let view: PinView

internal init(view: View) {
internal init(view: PinView) {
self.view = view
}

Expand Down Expand Up @@ -143,8 +143,8 @@ enum AnchorType: String {
case bottomRight
}

class AnchorImpl<View: Layoutable>: Anchor {
let view: View
class AnchorImpl<PinView: Layoutable>: Anchor {
let view: PinView
let type: AnchorType

func point(keepTransform: Bool) -> CGPoint {
Expand All @@ -161,7 +161,7 @@ class AnchorImpl<View: Layoutable>: Anchor {
}
}

fileprivate init(view: View, type: AnchorType) {
fileprivate init(view: PinView, type: AnchorType) {
self.view = view
self.type = type
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Layoutable+PinLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import Foundation

extension Layoutable {
public var anchor: AnchorList {
return AnchorListImpl(view: self as! View)
return AnchorListImpl(view: self as! PinView)
}

public var edge: EdgeList {
return EdgeListImpl(view: self as! View)
return EdgeListImpl(view: self as! PinView)
}
}
8 changes: 4 additions & 4 deletions Sources/Layoutable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ import AppKit
#endif

public protocol Layoutable: AnyObject, Equatable, CustomDebugStringConvertible {
associatedtype View: Layoutable
associatedtype PinView: Layoutable

var superview: View? { get }
var subviews: [View] { get }
var superview: PinView? { get }
var subviews: [PinView] { get }

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

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

func isLTR() -> Bool
}
Loading

0 comments on commit abe393b

Please sign in to comment.