|
| 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