Skip to content

Commit

Permalink
Draft for generic support
Browse files Browse the repository at this point in the history
  • Loading branch information
foyoodo committed Sep 17, 2024
1 parent d382904 commit dd8ebda
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
26 changes: 14 additions & 12 deletions Sources/Swift/FlexLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import FlexLayoutYogaKit
label.flex.margin(10)
```
*/
public final class Flex {
public final class Flex<Base: UIView> {

//
// MARK: Properties
Expand All @@ -35,20 +35,22 @@ public final class Flex {
/**
Flex items's UIView.
*/
public private(set) weak var view: UIView?
public private(set) weak var base: Base?
private let yoga: YGLayout

public var view: UIView? { base }

/**
Item natural size, considering only properties of the view itself. Independent of the item frame.
*/
public var intrinsicSize: CGSize {
return yoga.intrinsicSize
}

init(view: UIView) {
self.view = view
self.yoga = view.yoga
init(_ base: Base) {
self.base = base
self.yoga = base.yoga

// Enable flexbox and overwrite Yoga default values.
yoga.isEnabled = true
}
Expand All @@ -66,7 +68,7 @@ public final class Flex {
- Returns: The flex interface corresponding to the added view.
*/
@discardableResult
public func addItem() -> Flex {
public func addItem() -> Flex<UIView> {
let view = UIView()
return addItem(view)
}
Expand All @@ -80,8 +82,8 @@ public final class Flex {
- Returns: The flex interface corresponding to the added view.
*/
@discardableResult
public func addItem(_ view: UIView) -> Flex {
if let host = self.view {
public func addItem<Item: UIView>(_ view: Item) -> Flex<Item> {
if let host = base {
host.addSubview(view)
return view.flex
} else {
Expand Down Expand Up @@ -1278,7 +1280,7 @@ public final class Flex {
*/
@discardableResult
public func backgroundColor(_ color: UIColor) -> Flex {
if let host = self.view {
if let host = base {
host.backgroundColor = color
return self
} else {
Expand All @@ -1295,7 +1297,7 @@ public final class Flex {
*/
@discardableResult
public func cornerRadius(_ value: CGFloat) -> Flex {
if let host = self.view {
if let host = base {
host.layer.cornerRadius = value
return self
} else {
Expand All @@ -1313,7 +1315,7 @@ public final class Flex {
*/
@discardableResult
public func border(_ width: CGFloat, _ color: UIColor) -> Flex {
if let host = self.view {
if let host = base {
host.layer.borderWidth = width
host.layer.borderColor = color.cgColor
return self
Expand Down
13 changes: 9 additions & 4 deletions Sources/Swift/Impl/UIView+FlexLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import UIKit

private var flexLayoutAssociatedObjectHandle = 72_399_923

extension UIView {
public var flex: Flex {
if let flex = objc_getAssociatedObject(self, &flexLayoutAssociatedObjectHandle) as? Flex {
public protocol FlexLayoutCompatible: UIView { }

extension FlexLayoutCompatible {

public var flex: Flex<Self> {
if let flex = objc_getAssociatedObject(self, &flexLayoutAssociatedObjectHandle) as? Flex<Self> {
return flex
} else {
let flex = Flex(view: self)
let flex = Flex(self)
objc_setAssociatedObject(self, &flexLayoutAssociatedObjectHandle, flex, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
return flex
}
Expand All @@ -31,3 +34,5 @@ extension UIView {
(objc_getAssociatedObject(self, &flexLayoutAssociatedObjectHandle) as? Flex) != nil
}
}

extension UIView: FlexLayoutCompatible { }

0 comments on commit dd8ebda

Please sign in to comment.