Skip to content

Commit 179bfca

Browse files
committed
feat: add FeatureFlags shim with SwiftUI domain fallback
1 parent 37385fb commit 179bfca

3 files changed

Lines changed: 63 additions & 19 deletions

File tree

‎Sources/OpenSwiftUI/Event/HitTest/ResponderBasedHitTesting.swift‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99
#if os(macOS)
1010
import Foundation
1111
import OpenSwiftUICore
12-
#if OPENSWIFTUI_LINK_FEATUREFLAGS
13-
import FeatureFlags
14-
#endif
1512

1613
struct ResponderBasedHitTesting: Feature {
1714
static var isEnabled: Bool {
@@ -30,16 +27,12 @@ struct ResponderBasedHitTesting: Feature {
3027
Bundle.main.bundleIdentifier == "com.apple.ScreenContinuity" ? false : nil
3128
}()
3229

33-
#if OPENSWIFTUI_LINK_FEATUREFLAGS
3430
fileprivate struct Key: FeatureFlagsKey {
3531
let domain: StaticString = "OpenSwiftUI"
3632
let feature: StaticString = "ResponderBasedHitTesting"
3733
}
3834
private static let featureFlagValue: Bool = {
39-
FeatureFlags.isFeatureEnabled(Key())
35+
isFeatureEnabled(Key())
4036
}()
41-
#else
42-
private static let featureFlagValue: Bool = false
43-
#endif
4437
}
4538
#endif

‎Sources/OpenSwiftUICore/Event/Gesture/GestureFeature.swift‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
// Audited for 6.5.4
66
// Status: Complete
77

8-
#if OPENSWIFTUI_LINK_FEATUREFLAGS
9-
import FeatureFlags
10-
#endif
11-
128
// MARK: - UnifiedHitTestingFeature
139

1410
package struct UnifiedHitTestingFeature: Feature {
@@ -65,15 +61,10 @@ package struct GestureContainerFeature: Feature {
6561
package static var isEnabledOverride: Bool?
6662

6763
package static func _isFeatureEnabled() -> Bool {
68-
#if OPENSWIFTUI_LINK_FEATUREFLAGS
69-
FeatureFlags.isFeatureEnabled(Self()) && Semantics.UnifiedHitTesting.isEnabled
70-
#else
71-
Semantics.UnifiedHitTesting.isEnabled
72-
#endif
64+
isFeatureEnabled(Self()) && Semantics.UnifiedHitTesting.isEnabled
7365
}
7466
}
7567

76-
#if OPENSWIFTUI_LINK_FEATUREFLAGS
7768
extension GestureContainerFeature: FeatureFlagsKey {
7869
package var domain: StaticString {
7970
"OpenSwiftUI"
@@ -83,7 +74,6 @@ extension GestureContainerFeature: FeatureFlagsKey {
8374
"gestureContainer"
8475
}
8576
}
86-
#endif
8777

8878
// MARK: - GestureRecognizerBasedEvents
8979

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// FeatureFlagsShims.swift
3+
// OpenSwiftUICore
4+
//
5+
// Status: Complete
6+
7+
#if OPENSWIFTUI_LINK_FEATUREFLAGS
8+
package import FeatureFlags
9+
#endif
10+
11+
#if OPENSWIFTUI_LINK_FEATUREFLAGS
12+
/// Identifies a feature flag by its domain and feature name.
13+
package protocol FeatureFlagsKey: FeatureFlags.FeatureFlagsKey {}
14+
#else
15+
/// Identifies a feature flag by its domain and feature name.
16+
package protocol FeatureFlagsKey {
17+
/// The domain that owns the feature, such as `"OpenSwiftUI"`.
18+
var domain: StaticString { get }
19+
20+
/// The feature name within the domain.
21+
var feature: StaticString { get }
22+
}
23+
#endif
24+
25+
/// Returns whether the feature identified by a key is enabled.
26+
///
27+
/// When `OPENSWIFTUI_LINK_FEATUREFLAGS` is enabled, queries the key's domain
28+
/// in the system FeatureFlags framework. If an `"OpenSwiftUI"` lookup returns
29+
/// `false`, also checks the same feature in `"SwiftUI"`. A `true` result in
30+
/// either domain enables the feature. An OpenSwiftUI flag cannot disable a
31+
/// feature enabled by SwiftUI.
32+
///
33+
/// The SwiftUI feature flag configuration is stored at
34+
/// `/System/Library/FeatureFlags/Domain/SwiftUI.plist`.
35+
///
36+
/// Returns `false` when `OPENSWIFTUI_LINK_FEATUREFLAGS` is disabled.
37+
package func isFeatureEnabled(_ key: any FeatureFlagsKey) -> Bool {
38+
#if OPENSWIFTUI_LINK_FEATUREFLAGS
39+
/// Reuses an OpenSwiftUI feature name in the SwiftUI domain.
40+
struct SwiftUICompatibilityKey: FeatureFlagsKey {
41+
/// Creates a compatibility key only for the `"OpenSwiftUI"` domain.
42+
init?<Key>(_ key: Key) where Key: FeatureFlagsKey {
43+
guard key.domain.description == "OpenSwiftUI" else { return nil }
44+
self.feature = key.feature
45+
}
46+
47+
var domain: StaticString { "SwiftUI" }
48+
let feature: StaticString
49+
}
50+
51+
if let compatibilityKey = SwiftUICompatibilityKey(key) {
52+
return FeatureFlags.isFeatureEnabled(key) || FeatureFlags.isFeatureEnabled(compatibilityKey)
53+
} else {
54+
return FeatureFlags.isFeatureEnabled(key)
55+
}
56+
#else
57+
// TODO: Provide a platform implementation when FeatureFlags framework is not avaiable
58+
_openSwiftUIPlatformUnimplementedWarning()
59+
return false
60+
#endif
61+
}

0 commit comments

Comments
 (0)