import Adw /// The source of a dialog property, either a fixed value or a live binding. public enum DialogPropertyValue { /// A fixed property value applied once when the dialog mounts. case constant(Value) /// A live property binding reapplied when its value changes. case binding(Binding) } /// Supplies a dialog property as a fixed value or a live binding. public protocol DialogPropertySource { /// The value type accepted by this property source. associatedtype Value /// Returns the fixed value or binding represented by this source. var _dialogPropertyValue: DialogPropertyValue { get } } /// Provides a fixed string dialog property value. extension String: DialogPropertySource { /// Returns this string as a fixed dialog property value. public var _dialogPropertyValue: DialogPropertyValue { .constant(self) } } /// Provides a fixed Boolean dialog property value. extension Bool: DialogPropertySource { /// Returns this Boolean as a fixed dialog property value. public var _dialogPropertyValue: DialogPropertyValue { .constant(self) } } /// Provides an integer literal as an `Int32` dialog property value. extension Int: DialogPropertySource { /// Returns this integer as a fixed `Int32` value, clamped to the `Int32` /// range rather than trapping on an out-of-range argument. public var _dialogPropertyValue: DialogPropertyValue { .constant(Int32(clamping: self)) } } /// Provides a fixed `Int32` dialog property value. extension Int32: DialogPropertySource { /// Returns this value as a fixed dialog property value. public var _dialogPropertyValue: DialogPropertyValue { .constant(self) } } /// Provides a fixed dialog presentation mode. extension DialogPresentationMode: DialogPropertySource { /// Returns this mode as a fixed dialog property value. public var _dialogPropertyValue: DialogPropertyValue { .constant(self) } } /// Provides a live dialog property value through a Portico binding. extension Binding: DialogPropertySource { /// Returns this binding as a live dialog property value. public var _dialogPropertyValue: DialogPropertyValue { .binding(self) } } /// Applies a static or binding dialog property source using the matching applier. @MainActor func appliedDialogProperty( _ source: (any DialogPropertySource)?, to view: Dialog, constant: (Dialog, Value) -> Dialog, binding: (Dialog, Binding) -> Dialog ) -> Dialog { guard let source else { return view } switch source._dialogPropertyValue { case .constant(let value): return constant(view, value) case .binding(let bound): return binding(view, bound) } }