Skip to content

Add AbsoluteEdge support #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
if edges.contains(.leading) {
switch layoutDirection {
case .leftToRight: result.insert(.left)
case .rightToLeft: result.insert(.right)
}
}
if edges.contains(.bottom) {
result.insert(.bottom)
}
if edges.contains(.trailing) {
switch layoutDirection {
case .leftToRight: result.insert(.right)
case .rightToLeft: result.insert(.left)
}
}
self = result
}
}

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

package var opposite: AbsoluteEdge {
switch self {
case .top: .bottom
case .left: .right
case .bottom: .top
case .right: .left
}
}
}
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 absoluteEdgeValues() {
#expect(AbsoluteEdge.top.rawValue == 0)
#expect(AbsoluteEdge.left.rawValue == 1)
#expect(AbsoluteEdge.bottom.rawValue == 2)
#expect(AbsoluteEdge.right.rawValue == 3)
}

@Test
func absoluteEdgeSetInitialization() {
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 absoluteEdgeSetContains() {
let horizontalSet = AbsoluteEdge.Set.horizontal
#expect(horizontalSet.contains(.left))
#expect(horizontalSet.contains(.right))
#expect(!horizontalSet.contains(.top))
#expect(!horizontalSet.contains(.bottom))
}

@Test
func absoluteEdgeSetFromEdgeSet() {
// 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))
}
}
Loading