Skip to content

Commit 57797b1

Browse files
authored
Add AbsoluteEdge support (#155)
1 parent e8fe751 commit 57797b1

File tree

3 files changed

+124
-4
lines changed

3 files changed

+124
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// AbsoluteEdge.swift
3+
// OpenSwiftUICore
4+
//
5+
// Audited for iOS 18.0
6+
// Status: Complete
7+
8+
package enum AbsoluteEdge: Int8, CaseIterable, Hashable {
9+
case top, left, bottom, right
10+
11+
package struct Set: OptionSet {
12+
package let rawValue: Int8
13+
package init(rawValue: Int8) { self.rawValue = rawValue }
14+
package static let top: AbsoluteEdge.Set = .init(.top)
15+
package static let left: AbsoluteEdge.Set = .init(.left)
16+
package static let bottom: AbsoluteEdge.Set = .init(.bottom)
17+
package static let right: AbsoluteEdge.Set = .init(.right)
18+
package static let all: AbsoluteEdge.Set = [.top, .left, .bottom, .right]
19+
package static let horizontal: AbsoluteEdge.Set = [.left, .right]
20+
package static let vertical: AbsoluteEdge.Set = [.top, .bottom]
21+
package init(_ e: AbsoluteEdge) {
22+
self.init(rawValue: 1 << e.rawValue)
23+
}
24+
package func contains(_ e: AbsoluteEdge) -> Bool {
25+
self.contains(.init(e))
26+
}
27+
}
28+
}
29+
30+
extension AbsoluteEdge.Set {
31+
package init(_ edges: Edge.Set, layoutDirection: LayoutDirection) {
32+
var result: AbsoluteEdge.Set = []
33+
if edges.contains(.top) {
34+
result.insert(.top)
35+
}
36+
if edges.contains(.leading) {
37+
switch layoutDirection {
38+
case .leftToRight: result.insert(.left)
39+
case .rightToLeft: result.insert(.right)
40+
}
41+
}
42+
if edges.contains(.bottom) {
43+
result.insert(.bottom)
44+
}
45+
if edges.contains(.trailing) {
46+
switch layoutDirection {
47+
case .leftToRight: result.insert(.right)
48+
case .rightToLeft: result.insert(.left)
49+
}
50+
}
51+
self = result
52+
}
53+
}
54+
55+
extension AbsoluteEdge {
56+
package var horizontal: Bool {
57+
self == .left || self == .right
58+
}
59+
60+
package var opposite: AbsoluteEdge {
61+
switch self {
62+
case .top: .bottom
63+
case .left: .right
64+
case .bottom: .top
65+
case .right: .left
66+
}
67+
}
68+
}

Tests/OpenSwiftUICoreTests/Layout/LayoutAdjustments/Alignment/AlignmentIDTests.swift

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
//
22
// AlignmentIDTests.swift
3-
//
4-
//
5-
// Created by Kyle on 2023/12/16.
6-
//
3+
// OpenSwiftUICoreTests
74

85
@testable import OpenSwiftUICore
96
import Testing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// AbsoluteEdgeTests.swift
3+
// OpenSwiftUICoreTests
4+
5+
import Testing
6+
import OpenSwiftUICore
7+
8+
struct AbsoluteEdgeTests {
9+
@Test
10+
func absoluteEdgeValues() {
11+
#expect(AbsoluteEdge.top.rawValue == 0)
12+
#expect(AbsoluteEdge.left.rawValue == 1)
13+
#expect(AbsoluteEdge.bottom.rawValue == 2)
14+
#expect(AbsoluteEdge.right.rawValue == 3)
15+
}
16+
17+
@Test
18+
func absoluteEdgeSetInitialization() {
19+
let topSet = AbsoluteEdge.Set(.top)
20+
let leftSet = AbsoluteEdge.Set(.left)
21+
let bottomSet = AbsoluteEdge.Set(.bottom)
22+
let rightSet = AbsoluteEdge.Set(.right)
23+
24+
#expect(topSet.rawValue == 1 << 0)
25+
#expect(leftSet.rawValue == 1 << 1)
26+
#expect(bottomSet.rawValue == 1 << 2)
27+
#expect(rightSet.rawValue == 1 << 3)
28+
}
29+
30+
@Test
31+
func absoluteEdgeSetContains() {
32+
let horizontalSet = AbsoluteEdge.Set.horizontal
33+
#expect(horizontalSet.contains(.left))
34+
#expect(horizontalSet.contains(.right))
35+
#expect(!horizontalSet.contains(.top))
36+
#expect(!horizontalSet.contains(.bottom))
37+
}
38+
39+
@Test
40+
func absoluteEdgeSetFromEdgeSet() {
41+
// Test LTR layout direction
42+
let ltrSet = AbsoluteEdge.Set(.leading, layoutDirection: .leftToRight)
43+
#expect(ltrSet.contains(.left))
44+
#expect(!ltrSet.contains(.right))
45+
#expect(!ltrSet.contains(.top))
46+
#expect(!ltrSet.contains(.bottom))
47+
48+
// Test RTL layout direction
49+
let rtlSet = AbsoluteEdge.Set(.leading, layoutDirection: .rightToLeft)
50+
#expect(rtlSet.contains(.right))
51+
#expect(!rtlSet.contains(.left))
52+
#expect(!rtlSet.contains(.top))
53+
#expect(!rtlSet.contains(.bottom))
54+
}
55+
}

0 commit comments

Comments
 (0)