Environment

public struct Environment

Environment is a container for values to be passed down an element tree.

Environment values are not resolved until the tree is being rendered, so they do not need to be explicitly passed to elements at the time they are created.

Environment key-value pairs are strongly typed: keys are types conforming to the EnvironmentKey protocol, and each key’s value is the type of that key’s EnvironmentKey.Value associated value. Keys must provide a default value.

Example

To set an environment value, so that it will cascade to child elements, use AdaptedEnvironment. Here, every element in childElement will have access to someValue via the key MyEnvironmentKey.

AdaptedEnvironment(
    key: MyEnvironmentKey.self,
    value: someValue,
    wrapping: childElement
)

To read an environment value, use EnvironmentReader. If this element were part of the child element in the previous example, myValue would be set to someValue. If the key had not been set in an ancestor element, the value would be MyEnvironmentKey.defaultValue.

struct MyElement: ProxyElement {
    var elementRepresentation: Element {
        return EnvironmentReader { environment -> Element in
            let myValue = environment[MyEnvironmentKey.self]
            return SomeElement(using: myValue)
        }
    }
}
  • A default “empty” environment, with no values overridden. Each key will return its default value.

    Declaration

    Swift

    public static let empty: Environment
  • Gets or sets an environment value by its key.

    Declaration

    Swift

    public subscript<Key>(key: Key.Type) -> Key.Value where Key : EnvironmentKey { get set }
  • The localised accessibility label elements should use when handling links.

    Defaults to UIImage(systemName: "link")?.accessibilityLabel.

    Declaration

    Swift

    public var linkAccessibilityLabel: String? { get set }
  • The current calendar that elements should use when handling dates.

    Defaults to Calendar.current.

    Declaration

    Swift

    public var calendar: Calendar { get set }
  • The display scale of this environment.

    This value is the number of pixels per point. A value of 1.0 indicates non-Retina screens, 2.0 indicates 2x Retina screens, etc.

    Declaration

    Swift

    public var displayScale: CGFloat { get set }
  • Declaration

    Swift

    public enum LayoutDirection : Hashable
  • The layout direction associated with the current environment.

    Declaration

    Swift

    public var layoutDirection: LayoutDirection { get set }
  • The current locale that elements should use.

    Defaults to Locale.current.

    Declaration

    Swift

    public var locale: Locale { get set }
  • The insets representing the safe area for content.

    Declaration

    Swift

    public var safeAreaInsets: UIEdgeInsets { get set }
  • The current time zone that elements should use when handling dates.

    Defaults to TimeZone.current.

    Declaration

    Swift

    public var timeZone: TimeZone { get set }
  • The size of the window that contains the hosting BlueprintView. Defaults to nil if the hosting view is not in a window.

    Declaration

    Swift

    public var windowSize: CGSize? { get set }
  • This mode will be inherited by descendant BlueprintViews that do not have an explicit mode set.

    Declaration

    Swift

    public internal(set) var layoutMode: LayoutMode { get set }
  • The link handler to use to open links tapped in an AttributedLabel.

    Declaration

    Swift

    public var urlHandler: URLHandler { get set }