Skip to content

Commit 9161131

Browse files
committed
Draft for generic support
1 parent 772d8fc commit 9161131

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

Sources/Swift/FlexLayout.swift

+14-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import FlexLayoutYogaKit
2626
label.flex.margin(10)
2727
```
2828
*/
29-
public final class Flex {
29+
public final class Flex<Base: UIView> {
3030

3131
//
3232
// MARK: Properties
@@ -35,20 +35,22 @@ public final class Flex {
3535
/**
3636
Flex items's UIView.
3737
*/
38-
public private(set) weak var view: UIView?
38+
public private(set) weak var base: Base?
3939
private let yoga: YGLayout
4040

41+
public var view: UIView? { base }
42+
4143
/**
4244
Item natural size, considering only properties of the view itself. Independent of the item frame.
4345
*/
4446
public var intrinsicSize: CGSize {
4547
return yoga.intrinsicSize
4648
}
4749

48-
init(view: UIView) {
49-
self.view = view
50-
self.yoga = view.yoga
51-
50+
init(_ base: Base) {
51+
self.base = base
52+
self.yoga = base.yoga
53+
5254
// Enable flexbox and overwrite Yoga default values.
5355
yoga.isEnabled = true
5456
}
@@ -66,7 +68,7 @@ public final class Flex {
6668
- Returns: The flex interface corresponding to the added view.
6769
*/
6870
@discardableResult
69-
public func addItem() -> Flex {
71+
public func addItem() -> Flex<UIView> {
7072
let view = UIView()
7173
return addItem(view)
7274
}
@@ -80,8 +82,8 @@ public final class Flex {
8082
- Returns: The flex interface corresponding to the added view.
8183
*/
8284
@discardableResult
83-
public func addItem(_ view: UIView) -> Flex {
84-
if let host = self.view {
85+
public func addItem<Item: UIView>(_ view: Item) -> Flex<Item> {
86+
if let host = base {
8587
host.addSubview(view)
8688
return view.flex
8789
} else {
@@ -1278,7 +1280,7 @@ public final class Flex {
12781280
*/
12791281
@discardableResult
12801282
public func backgroundColor(_ color: UIColor) -> Flex {
1281-
if let host = self.view {
1283+
if let host = base {
12821284
host.backgroundColor = color
12831285
return self
12841286
} else {
@@ -1295,7 +1297,7 @@ public final class Flex {
12951297
*/
12961298
@discardableResult
12971299
public func cornerRadius(_ value: CGFloat) -> Flex {
1298-
if let host = self.view {
1300+
if let host = base {
12991301
host.layer.cornerRadius = value
13001302
return self
13011303
} else {
@@ -1313,7 +1315,7 @@ public final class Flex {
13131315
*/
13141316
@discardableResult
13151317
public func border(_ width: CGFloat, _ color: UIColor) -> Flex {
1316-
if let host = self.view {
1318+
if let host = base {
13171319
host.layer.borderWidth = width
13181320
host.layer.borderColor = color.cgColor
13191321
return self

Sources/Swift/Impl/UIView+FlexLayout.swift

+9-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import UIKit
1616

1717
private var flexLayoutAssociatedObjectHandle = 72_399_923
1818

19-
extension UIView {
20-
public var flex: Flex {
21-
if let flex = objc_getAssociatedObject(self, &flexLayoutAssociatedObjectHandle) as? Flex {
19+
public protocol FlexLayoutCompatible: UIView { }
20+
21+
extension FlexLayoutCompatible {
22+
23+
public var flex: Flex<Self> {
24+
if let flex = objc_getAssociatedObject(self, &flexLayoutAssociatedObjectHandle) as? Flex<Self> {
2225
return flex
2326
} else {
24-
let flex = Flex(view: self)
27+
let flex = Flex(self)
2528
objc_setAssociatedObject(self, &flexLayoutAssociatedObjectHandle, flex, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
2629
return flex
2730
}
@@ -31,3 +34,5 @@ extension UIView {
3134
(objc_getAssociatedObject(self, &flexLayoutAssociatedObjectHandle) as? Flex) != nil
3235
}
3336
}
37+
38+
extension UIView: FlexLayoutCompatible { }

0 commit comments

Comments
 (0)