Skip to content

Commit

Permalink
Add AbsoluteEdge support
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye committed Nov 10, 2024
1 parent e8fe751 commit ae6f383
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// AbsoluteEdge.swift
// OpenSwiftUICore
//
// Audited for iOS 18.0
// Status: Complete

package enum AbsoluteEdge: Int8, CaseIterable, Hashable {
case top, left, bottom, right

package struct Set: OptionSet {
package let rawValue: Int8
package init(rawValue: Int8) { self.rawValue = rawValue }
package static let top: AbsoluteEdge.Set = .init(.top)
package static let left: AbsoluteEdge.Set = .init(.left)
package static let bottom: AbsoluteEdge.Set = .init(.bottom)
package static let right: AbsoluteEdge.Set = .init(.right)
package static let all: AbsoluteEdge.Set = [.top, .left, .bottom, .right]
package static let horizontal: AbsoluteEdge.Set = [.left, .right]
package static let vertical: AbsoluteEdge.Set = [.top, .bottom]
package init(_ e: AbsoluteEdge) {
self.init(rawValue: 1 << e.rawValue)
}
package func contains(_ e: AbsoluteEdge) -> Bool {
self.contains(.init(e))
}
}
}

extension AbsoluteEdge.Set {
package init(_ edges: Edge.Set, layoutDirection: LayoutDirection) {
var result: AbsoluteEdge.Set = []
if edges.contains(.top) {
result.insert(.top)

Check warning on line 34 in Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift#L34

Added line #L34 was not covered by tests
}
if edges.contains(.leading) {
switch layoutDirection {
case .leftToRight: result.insert(.left)
case .rightToLeft: result.insert(.right)
}
}
if edges.contains(.bottom) {
result.insert(.bottom)

Check warning on line 43 in Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift#L43

Added line #L43 was not covered by tests
}
if edges.contains(.trailing) {
switch layoutDirection {
case .leftToRight: result.insert(.right)
case .rightToLeft: result.insert(.left)

Check warning on line 48 in Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift#L46-L48

Added lines #L46 - L48 were not covered by tests
}
}
self = result
}
}

extension AbsoluteEdge {
package var horizontal: Bool {
self == .left || self == .right

Check warning on line 57 in Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift#L56-L57

Added lines #L56 - L57 were not covered by tests
}

package var opposite: AbsoluteEdge {
switch self {
case .top: .bottom
case .left: .right
case .bottom: .top
case .right: .left

Check warning on line 65 in Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift

View check run for this annotation

Codecov / codecov/patch

Sources/OpenSwiftUICore/Layout/LayoutAdjustments/Edge/AbsoluteEdge.swift#L60-L65

Added lines #L60 - L65 were not covered by tests
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// AlignmentIDTests.swift
//
//
// Created by Kyle on 2023/12/16.
//
// OpenSwiftUICoreTests

@testable import OpenSwiftUICore
import Testing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// AbsoluteEdgeTests.swift
// OpenSwiftUICoreTests

import Testing
import OpenSwiftUICore

struct AbsoluteEdgeTests {
@Test
func testEdgeValues() {
#expect(AbsoluteEdge.top.rawValue == 0)
#expect(AbsoluteEdge.left.rawValue == 1)
#expect(AbsoluteEdge.bottom.rawValue == 2)
#expect(AbsoluteEdge.right.rawValue == 3)
}

@Test
func testEdgeSetInitialization() {
let topSet = AbsoluteEdge.Set(.top)
let leftSet = AbsoluteEdge.Set(.left)
let bottomSet = AbsoluteEdge.Set(.bottom)
let rightSet = AbsoluteEdge.Set(.right)

#expect(topSet.rawValue == 1 << 0)
#expect(leftSet.rawValue == 1 << 1)
#expect(bottomSet.rawValue == 1 << 2)
#expect(rightSet.rawValue == 1 << 3)
}

@Test
func testEdgeSetContains() {
let horizontalSet = AbsoluteEdge.Set.horizontal
#expect(horizontalSet.contains(.left))
#expect(horizontalSet.contains(.right))
#expect(!horizontalSet.contains(.top))
#expect(!horizontalSet.contains(.bottom))
}

@Test
func testEdgeSetFromEdgeSet() {
// Test LTR layout direction
let ltrSet = AbsoluteEdge.Set(.leading, layoutDirection: .leftToRight)
#expect(ltrSet.contains(.left))
#expect(!ltrSet.contains(.right))
#expect(!ltrSet.contains(.top))
#expect(!ltrSet.contains(.bottom))

// Test RTL layout direction
let rtlSet = AbsoluteEdge.Set(.leading, layoutDirection: .rightToLeft)
#expect(rtlSet.contains(.right))
#expect(!rtlSet.contains(.left))
#expect(!rtlSet.contains(.top))
#expect(!rtlSet.contains(.bottom))
}
}

0 comments on commit ae6f383

Please sign in to comment.