From afd87eff7a2100e3696efb71ebed475e6cf245b6 Mon Sep 17 00:00:00 2001 From: Kyle Date: Thu, 29 Oct 2026 16:39:23 +0800 Subject: [PATCH 01/44] fix(tag): limit cached indices to each sublist --- Sources/OpenSwiftUICore/View/Tag.swift | 2 +- .../OpenSwiftUICoreTests/View/TagTests.swift | 76 +++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 Tests/OpenSwiftUICoreTests/View/TagTests.swift diff --git a/Sources/OpenSwiftUICore/View/Tag.swift b/Sources/OpenSwiftUICore/View/Tag.swift index 3362be3ae..f382d6b20 100644 --- a/Sources/OpenSwiftUICore/View/Tag.swift +++ b/Sources/OpenSwiftUICore/View/Tag.swift @@ -248,7 +248,7 @@ private class TagIndexProjection: Projection where Value: Hashable { } tagMap[tag] = nextIndex var index = nextIndex - var count = list.count + var count = sublist.count Swift.precondition(index + count >= index) while count != 0 { indexMap[index] = tag diff --git a/Tests/OpenSwiftUICoreTests/View/TagTests.swift b/Tests/OpenSwiftUICoreTests/View/TagTests.swift new file mode 100644 index 000000000..c7547372c --- /dev/null +++ b/Tests/OpenSwiftUICoreTests/View/TagTests.swift @@ -0,0 +1,76 @@ +// +// TagTests.swift +// OpenSwiftUICoreTests + +import OpenAttributeGraphShims +@_spi(ForOpenSwiftUIOnly) import OpenSwiftUICore +import Testing + +struct TagTests { + @Test + func projectedIndexDoesNotUseThePreviousSublistsTag() { + var selection = 10 + let list = TaggedViewList(groups: [(tag: 10, count: 2), (tag: 20, count: 1)]) + let index = Binding(get: { selection }, set: { selection = $0 }) + .projectingTagIndex(viewList: list) + + #expect(index.wrappedValue == 0) + + index.wrappedValue = 2 + #expect(selection == 20) + #expect(index.wrappedValue == 2) + + index.wrappedValue = 1 + #expect(selection == 10) + #expect(index.wrappedValue == 0) + } + + private struct TaggedViewList: ViewList { + var groups: [(tag: Int, count: Int)] + + func count(style: IteratorStyle) -> Int { + groups.reduce(0) { $0 + style.applyGranularity(to: $1.count) } + } + + func estimatedCount(style: IteratorStyle) -> Int { + count(style: style) + } + + var traitKeys: ViewTraitKeys? { nil } + var viewIDs: ID.Views? { nil } + var traits: ViewTraitCollection { .init() } + + func applyNodes( + from start: inout Int, + style: IteratorStyle, + list: Attribute?, + transform: inout SublistTransform, + to body: ApplyBody + ) -> Bool { + for (offset, group) in groups.enumerated() { + var traits = ViewTraitCollection() + traits.setTag(for: Int.self, value: group.tag) + let sublist = Sublist( + start: start, + count: group.count, + id: ID(implicitID: offset), + elements: EmptyViewListElements(), + traits: traits, + list: list + ) + guard body(&start, style, .sublist(sublist), &transform) else { + return false + } + } + return true + } + + func edit(forID id: ID, since transaction: TransactionID) -> Edit? { + nil + } + + func firstOffset(forID id: OtherID, style: IteratorStyle) -> Int? where OtherID: Hashable { + nil + } + } +} From b7eac28edfa6a4c548ebf601822a1cccb2728c6c Mon Sep 17 00:00:00 2001 From: Kyle Date: Thu, 29 Oct 2026 17:13:50 +0800 Subject: [PATCH 02/44] feat(layout): add vertical stack orientation support --- .../Layout/Stack/StackLayout.swift | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Sources/OpenSwiftUICore/Layout/Stack/StackLayout.swift b/Sources/OpenSwiftUICore/Layout/Stack/StackLayout.swift index 234a4fb87..906d364ca 100644 --- a/Sources/OpenSwiftUICore/Layout/Stack/StackLayout.swift +++ b/Sources/OpenSwiftUICore/Layout/Stack/StackLayout.swift @@ -708,3 +708,25 @@ extension StackLayout { } } } + +// MARK: - View + verticalStackOrientation + +extension View { + package func verticalStackOrientation() -> some View { + modifier(VerticalStackOrientationModifier()) + } +} + +// MARK: - VerticalStackOrientationModifier + +struct VerticalStackOrientationModifier: PrimitiveViewModifier, MultiViewModifier { + nonisolated static func _makeView( + modifier: _GraphValue, + inputs: _ViewInputs, + body: @escaping (_Graph, _ViewInputs) -> _ViewOutputs + ) -> _ViewOutputs { + var inputs = inputs + inputs.stackOrientation = .vertical + return body(_Graph(), inputs) + } +} From 5c923a3b3492df7da7145ad79eec633c089b57ec Mon Sep 17 00:00:00 2001 From: Kyle Date: Thu, 29 Oct 2026 17:13:51 +0800 Subject: [PATCH 03/44] fix(list): match generic and concrete list style contexts --- Sources/OpenSwiftUI/View/List/ListStyle.swift | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/Sources/OpenSwiftUI/View/List/ListStyle.swift b/Sources/OpenSwiftUI/View/List/ListStyle.swift index 737d46877..e1dec0be5 100644 --- a/Sources/OpenSwiftUI/View/List/ListStyle.swift +++ b/Sources/OpenSwiftUI/View/List/ListStyle.swift @@ -7,10 +7,21 @@ import OpenSwiftUICore -struct AnyListStyleContext: StyleContext {} +protocol ListStyleContext: StyleContext {} -// TODO: ListStyleContext conformance and matching rules. -struct PlainListStyleContext: StyleContext {} +extension ListStyleContext { + static func accepts(_ query: Q.Type, at: Int) -> Bool { + query == AnyListStyleContext.self || query == Self.self + } +} + +struct AnyListStyleContext: ListStyleContext { + static func accepts(_ query: Q.Type, at: Int) -> Bool { + query is any ListStyleContext.Type + } +} + +struct PlainListStyleContext: ListStyleContext {} extension StyleContext where Self == PlainListStyleContext { static var plainList: PlainListStyleContext { @@ -18,7 +29,7 @@ extension StyleContext where Self == PlainListStyleContext { } } -struct SidebarListStyleContext: StyleContext {} +struct SidebarListStyleContext: ListStyleContext {} #if os(macOS) // TODO: ListStyleContext conformance and matching rules. @@ -31,7 +42,7 @@ extension StyleContext where Self == SidebarListStyleContext { } } -struct InsetListStyleContext: StyleContext {} +struct InsetListStyleContext: ListStyleContext {} extension StyleContext where Self == InsetListStyleContext { static var insetList: InsetListStyleContext { @@ -39,7 +50,7 @@ extension StyleContext where Self == InsetListStyleContext { } } -struct GroupedListStyleContext: StyleContext {} +struct GroupedListStyleContext: ListStyleContext {} extension StyleContext where Self == GroupedListStyleContext { static var groupedList: GroupedListStyleContext { @@ -47,7 +58,7 @@ extension StyleContext where Self == GroupedListStyleContext { } } -struct InsetGroupedListStyleContext: StyleContext {} +struct InsetGroupedListStyleContext: ListStyleContext {} extension StyleContext where Self == InsetGroupedListStyleContext { static var insetGroupedList: InsetGroupedListStyleContext { From 7d4ecbf77acd7939734747497c16381cdee71aca Mon Sep 17 00:00:00 2001 From: Kyle Date: Thu, 29 Oct 2026 17:52:19 +0800 Subject: [PATCH 04/44] feat(view): add selectionDisabled modifier --- .../View/Control/SelectionDisabled.swift | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Sources/OpenSwiftUI/View/Control/SelectionDisabled.swift diff --git a/Sources/OpenSwiftUI/View/Control/SelectionDisabled.swift b/Sources/OpenSwiftUI/View/Control/SelectionDisabled.swift new file mode 100644 index 000000000..26c9df49f --- /dev/null +++ b/Sources/OpenSwiftUI/View/Control/SelectionDisabled.swift @@ -0,0 +1,38 @@ +// +// SelectionDisabled.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete (Blocked by TableRowContent) + +public import OpenSwiftUICore + +// MARK: - View + SelectionDisabled + +extension View { + /// Controls whether the view can be selected in a container. + @available(OpenSwiftUI_v5_0, *) + @_alwaysEmitIntoClient + nonisolated public func selectionDisabled(_ isDisabled: Bool = true) -> some View { + _trait(IsSelectionEnabledTraitKey.self, !isDisabled) + } +} + +// MARK: - TableRowContent + SelectionDisabled + +// TODO: Add TableRowContent.selectionDisabled(_:) when TableRowContent is available. + +// MARK: - IsSelectionEnabledTraitKey + +@available(OpenSwiftUI_v5_0, *) +@usableFromInline +struct IsSelectionEnabledTraitKey: _ViewTraitKey { + @usableFromInline + static let defaultValue: Bool = true + + @usableFromInline + typealias Value = Bool +} + +@available(*, unavailable) +extension IsSelectionEnabledTraitKey: Sendable {} From c1bfce70a190e5925109d0265673555b86652fa5 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 30 Oct 2026 00:48:03 +0800 Subject: [PATCH 05/44] feat(picker): add Picker view --- .../View/Control/Picker/Picker.swift | 1560 +++++++++++++++++ 1 file changed, 1560 insertions(+) create mode 100644 Sources/OpenSwiftUI/View/Control/Picker/Picker.swift diff --git a/Sources/OpenSwiftUI/View/Control/Picker/Picker.swift b/Sources/OpenSwiftUI/View/Control/Picker/Picker.swift new file mode 100644 index 000000000..033c95cc2 --- /dev/null +++ b/Sources/OpenSwiftUI/View/Control/Picker/Picker.swift @@ -0,0 +1,1560 @@ +// +// Picker.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete +// ID: A416B71D5DC194C14C66720CB55B8C75 (SwiftUI) + +public import OpenSwiftUICore + +// MARK: - Picker + +/// A control for selecting a value from a set of labeled options. +@available(OpenSwiftUI_v1_0, *) +public struct Picker: View where Label: View, SelectionValue: Hashable, Content: View { + var selection: [Binding] + var label: Label + var content: Content + var currentValueLabel: AnyView? + + public var body: some View { + ResolvedPicker(configuration: PickerStyleConfiguration(selection: selection)) + .viewAlias(PickerStyleConfiguration.Label.self) { label } + .viewAlias(PickerStyleConfiguration.Content.self) { content } + .optionalViewAlias(PickerStyleConfiguration.CurrentValueLabel.self) { + currentValueLabel + } + #if os(macOS) + .dynamicToolbarStyleContext() + .allowsWindowActivationEvents(true) + #endif + } + + nonisolated private init( + selection: Binding, + content: Content, + label: Label + ) { + self.selection = [selection] + self.label = label + self.content = content + currentValueLabel = nil + } + + nonisolated private init( + selection: C, + content: Content, + label: Label + ) where C: Collection, C.Element == Binding { + self.selection = selection.compactMap { $0 } + self.label = label + self.content = content + currentValueLabel = nil + } + + nonisolated private init( + selection: Binding, + content: Content, + label: Label, + currentValueLabel: some View + ) { + self.init(selection: selection, content: content, label: label) + self.currentValueLabel = AnyView(currentValueLabel) + } + + nonisolated private init( + selection: C, + content: Content, + label: Label, + currentValueLabel: some View + ) where C: Collection, C.Element == Binding { + self.init(selection: selection, content: content, label: label) + self.currentValueLabel = AnyView(currentValueLabel) + } +} + +@available(*, unavailable) +extension Picker: Sendable {} + +// MARK: - Picker + Selection + +@available(OpenSwiftUI_v1_0, *) +extension Picker { + /// Creates a picker that displays a custom label. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes, which can be of + /// any number. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } label: { + /// Text("Border Thickness") + /// } + /// + /// - Parameters: + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// - label: A view that describes the purpose of selecting an option. + @available(OpenSwiftUI_v4_0, *) + nonisolated public init( + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content, + @ViewBuilder label: () -> Label + ) where C: RandomAccessCollection { + self.init( + selection: sources.lazy.map { $0[keyPath: selection] }, + content: content(), + label: label() + ) + } + + /// Creates a picker that displays a custom label. + /// + /// - Parameters: + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// - label: A view that describes the purpose of selecting an option. + @_alwaysEmitIntoClient + nonisolated public init( + selection: Binding, + @ViewBuilder content: () -> Content, + @ViewBuilder label: () -> Label + ) { + self.init(selection: selection, label: label(), content: content) + } + + /// Creates a picker that displays a custom label. + /// + /// - Parameters: + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - label: A view that describes the purpose of selecting an option. + /// - content: A view that contains the set of options. + @available(iOS, deprecated: 100000.0, renamed: "Picker(selection:content:label:)") + @available(macOS, deprecated: 100000.0, renamed: "Picker(selection:content:label:)") + @available(tvOS, deprecated: 100000.0, renamed: "Picker(selection:content:label:)") + @available(watchOS, deprecated: 100000.0, renamed: "Picker(selection:content:label:)") + @available(visionOS, deprecated: 100000.0, renamed: "Picker(selection:content:label:)") + nonisolated public init( + selection: Binding, + label: Label, + @ViewBuilder content: () -> Content + ) { + self.init(selection: selection, content: content(), label: label) + } +} + +// MARK: - Picker + CurrentValueLabel + +@available(OpenSwiftUI_v6_0, *) +extension Picker { + /// Creates a picker that displays a custom label and a custom value + /// label where applicable. + /// + /// The following example shows a picker with a current value label that + /// only displays the title of the currently selected song: + /// + /// struct Song: Identifiable, Hashable { + /// let id = UUID() + /// let title: String + /// let artist: String + /// let genre: String + /// } + /// + /// private let songs: [Song] = [ /* songs */] + /// + /// @State private var selectedSong: Song? = nil + /// + /// var body: some View { + /// NavigationStack { + /// List { + /// Picker(selection: $selectedSong) { + /// ForEach(songs) { song in + /// VStack(alignment: .leading) { + /// Text(song.title) + /// .bold() + /// Text(song.artist) + /// Text(song.genre) + /// .foregroundColor(.secondary) + /// .font(.caption) + /// } + /// .tag(song as Song?) + /// } + /// } label: { + /// Text("Request a song") + /// } currentValueLabel: { + /// Text(selectedSong?.title ?? "No selection") + /// } + /// } + /// .pickerStyle(.navigationLink) + /// } + /// } + /// + /// - Parameters: + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// - label: A view that describes the purpose of selecting an option. + /// - currentValueLabel: A view that represents the current value of the picker. + nonisolated public init( + selection: Binding, + @ViewBuilder content: () -> Content, + @ViewBuilder label: () -> Label, + @ViewBuilder currentValueLabel: () -> some View + ) { + self.init( + selection: selection, + content: content(), + label: label(), + currentValueLabel: currentValueLabel() + ) + } + + /// Creates a picker that displays a custom label and current value label + /// where applicable. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently selected shapes. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } currentValueLabel: { + /// switch selectedObjectBorders.count { + /// case 0: Text("None") + /// case 1: Text(selectedObjectBorders[0].thickness.rawValue) + /// default: Text("Multiple") + /// } + /// } label: { + /// Text("Border Thickness") + /// } + /// + /// - Parameters: + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + /// - label: A view that describes the purpose of selecting an option. + nonisolated public init( + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content, + @ViewBuilder label: () -> Label, + @ViewBuilder currentValueLabel: () -> some View + ) where C: RandomAccessCollection { + self.init( + selection: sources.lazy.map { $0[keyPath: selection] }, + content: content(), + label: label(), + currentValueLabel: currentValueLabel() + ) + } +} + +// MARK: - Picker + Text + +@available(OpenSwiftUI_v1_0, *) +extension Picker where Label == Text { + /// Creates a picker that generates its label from a localized string key. + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// + /// This initializer creates a ``Text`` view on your behalf, and treats the + /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See + /// ``Text`` for more information about localizing strings. + nonisolated public init( + _ titleKey: LocalizedStringKey, + selection: Binding, + @ViewBuilder content: () -> Content + ) { + self.init( + selection: selection, + content: content, + label: { Text(titleKey) } + ) + } + + @_spi(_) + @available(iOS, introduced: 16.0, deprecated: 16.0, message: "Use Picker.init(_:sources:selection:content).") + @available(macOS, introduced: 13.0, deprecated: 13.0, message: "Use Picker.init(_:sources:selection:content).") + @available(tvOS, introduced: 16.0, deprecated: 16.0, message: "Use Picker.init(_:sources:selection:content).") + @available(watchOS, introduced: 9.0, deprecated: 9.0, message: "Use Picker.init(_:sources:selection:content).") + nonisolated public init( + _ titleKey: LocalizedStringKey, + selection: C, + @ViewBuilder content: () -> Content + ) where C: Collection, C.Element == Binding { + self.init(selection: selection, content: content(), label: Text(titleKey)) + } + + /// Creates a picker that generates its label from a localized string key. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes, which can be of + /// any number. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// "Border Thickness", + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// + /// This initializer creates a ``Text`` view on your behalf, and treats the + /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See + /// ``Text`` for more information about localizing strings. + @available(OpenSwiftUI_v4_0, *) + nonisolated public init( + _ titleKey: LocalizedStringKey, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content + ) where C: RandomAccessCollection { + self.init( + sources: sources, + selection: selection, + content: content, + label: { Text(titleKey) } + ) + } + + /// Creates a picker that generates its label from a string. + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// + /// This initializer creates a ``Text`` view on your behalf. See ``Text`` for more + /// information about localizing strings. + @_disfavoredOverload + nonisolated public init( + _ title: S, + selection: Binding, + @ViewBuilder content: () -> Content + ) where S: StringProtocol { + self.init( + selection: selection, + content: content, + label: { Text(title) } + ) + } + + @_spi(_) + @available(iOS, introduced: 16.0, deprecated: 16.0, message: "Use Picker.init(_:sources:selection:content).") + @available(macOS, introduced: 13.0, deprecated: 13.0, message: "Use Picker.init(_:sources:selection:content).") + @available(tvOS, introduced: 16.0, deprecated: 16.0, message: "Use Picker.init(_:sources:selection:content).") + @available(watchOS, introduced: 9.0, deprecated: 9.0, message: "Use Picker.init(_:sources:selection:content).") + @_disfavoredOverload + nonisolated public init( + _ title: S, + selection: C, + @ViewBuilder content: () -> Content + ) where C: RandomAccessCollection, S: StringProtocol, C.Element == Binding { + self.init(selection: selection, content: content(), label: Text(title)) + } + + /// Creates a picker bound to a collection of bindings that generates its + /// label from a string. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes, which can be of + /// any number. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// "Border Thickness", + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// + /// This initializer creates a ``Text`` view on your behalf. See ``Text`` for more + /// information about localizing strings. + @available(OpenSwiftUI_v4_0, *) + @_disfavoredOverload + nonisolated public init( + _ title: S, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content + ) where C: RandomAccessCollection, S: StringProtocol { + self.init( + sources: sources, + selection: selection, + content: content, + label: { Text(title) } + ) + } +} + +// MARK: - Picker + Text + CurrentValueLabel + +@available(OpenSwiftUI_v6_0, *) +extension Picker where Label == Text { + /// Creates a picker that generates its label from a localized string key + /// and accepts a custom current value label. + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + /// + /// This initializer creates a ``Text`` view on your behalf, and treats the + /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See + /// ``Text`` for more information about localizing strings. + nonisolated public init( + _ titleKey: LocalizedStringKey, + selection: Binding, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) { + self.init( + selection: selection, + content: content, + label: { Text(titleKey) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker that generates its label from a localized string key + /// and accepts a custom current value label. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// "Border Thickness", + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } currentValueLabel: { + /// switch selectedObjectBorders.count { + /// case 0: Text("None") + /// case 1: Text(selectedObjectBorders[0].thickness.rawValue) + /// default: Text("Multiple") + /// } + /// } + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + /// + /// This initializer creates a ``Text`` view on your behalf, and treats the + /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See + /// ``Text`` for more information about localizing strings. + nonisolated public init( + _ titleKey: LocalizedStringKey, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where C: RandomAccessCollection { + self.init( + sources: sources, + selection: selection, + content: content, + label: { Text(titleKey) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker that generates its label from a string and accepts a + /// custom current value label. + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + /// + /// This initializer creates a ``Text`` view on your behalf. See ``Text`` for more + /// information about localizing strings. + @_disfavoredOverload + nonisolated public init( + _ title: S, + selection: Binding, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where S: StringProtocol { + self.init( + selection: selection, + content: content, + label: { Text(title) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker bound to a collection of bindings that generates its + /// label from a string and accepts a custom current value label. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes, which can be of + /// any number. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// "Border Thickness", + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } currentValueLabel: { + /// switch selectedObjectBorders.count { + /// case 0: Text("None") + /// case 1: Text(selectedObjectBorders[0].thickness.rawValue) + /// default: Text("Multiple") + /// } + /// } + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + /// + /// This initializer creates a ``Text`` view on your behalf. See ``Text`` for more + /// information about localizing strings. + @_disfavoredOverload + nonisolated public init( + _ title: S, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where C: RandomAccessCollection, S: StringProtocol { + self.init( + sources: sources, + selection: selection, + content: content, + label: { Text(title) }, + currentValueLabel: currentValueLabel + ) + } +} + +// MARK: - Picker + SystemImage + +@available(OpenSwiftUI_v2_0, *) +extension Picker where Label == OpenSwiftUI.Label { + /// Creates a picker that generates its label from a localized string key + /// and system image. + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// + /// This initializer creates a ``Text`` view on your behalf, and treats the + /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See + /// ``Text`` for more information about localizing strings. + @_alwaysEmitIntoClient + nonisolated public init( + _ titleKey: LocalizedStringKey, + systemImage: String, + selection: Binding, + @ViewBuilder content: () -> Content + ) { + self.init( + selection: selection, + content: content, + label: { OpenSwiftUI.Label(titleKey, systemImage: systemImage) } + ) + } + + /// Creates a picker that generates its label from a localized string key. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes, which can be of + /// any number. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// "Border Thickness", + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + @available(OpenSwiftUI_v4_0, *) + @_alwaysEmitIntoClient + nonisolated public init( + _ titleKey: LocalizedStringKey, + systemImage: String, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content + ) where C: RandomAccessCollection, C.Element == Binding { + self.init( + sources: sources, + selection: selection, + content: content, + label: { OpenSwiftUI.Label(titleKey, systemImage: systemImage) } + ) + } + + /// Creates a picker that generates its label from a string and + /// system image. + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + @_alwaysEmitIntoClient + @_disfavoredOverload + nonisolated public init( + _ title: S, + systemImage: String, + selection: Binding, + @ViewBuilder content: () -> Content + ) where S: StringProtocol { + self.init( + selection: selection, + content: content, + label: { OpenSwiftUI.Label(title, systemImage: systemImage) } + ) + } + + /// Creates a picker bound to a collection of bindings that generates its + /// label from a string. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes, which can be of + /// any number. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// "Border Thickness", + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + @available(OpenSwiftUI_v4_0, *) + @_alwaysEmitIntoClient + @_disfavoredOverload + nonisolated public init( + _ title: S, + systemImage: String, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content + ) where C: RandomAccessCollection, S: StringProtocol, C.Element == Binding { + self.init( + sources: sources, + selection: selection, + content: content, + label: { OpenSwiftUI.Label(title, systemImage: systemImage) } + ) + } +} + +// MARK: - Picker + SystemImage + CurrentValueLabel + +@available(OpenSwiftUI_v6_0, *) +extension Picker where Label == OpenSwiftUI.Label { + /// Creates a picker that accepts a custom current value label and + /// generates its label from a localized string key and system image. + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + /// + /// This initializer creates a ``Text`` view on your behalf, and treats the + /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See + /// ``Text`` for more information about localizing strings. + @_alwaysEmitIntoClient + nonisolated public init( + _ titleKey: LocalizedStringKey, + systemImage: String, + selection: Binding, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) { + self.init( + selection: selection, + content: content, + label: { OpenSwiftUI.Label(titleKey, systemImage: systemImage) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker that accepts a custom current value label and + /// generates its label from a localized string key. + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + @_alwaysEmitIntoClient + nonisolated public init( + _ titleKey: LocalizedStringKey, + systemImage: String, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where C: RandomAccessCollection, C.Element == Binding { + self.init( + sources: sources, + selection: selection, + content: content, + label: { OpenSwiftUI.Label(titleKey, systemImage: systemImage) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker that accepts a custom current value and generates its + /// label from a string and system image. + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + @_alwaysEmitIntoClient + @_disfavoredOverload + nonisolated public init( + _ title: S, + systemImage: String, + selection: Binding, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where S: StringProtocol { + self.init( + selection: selection, + content: content, + label: { OpenSwiftUI.Label(title, systemImage: systemImage) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker bound to a collection of bindings that accepts a + /// custom current value label and generates its label from a string. + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + @_alwaysEmitIntoClient + @_disfavoredOverload + nonisolated public init( + _ title: S, + systemImage: String, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where C: RandomAccessCollection, S: StringProtocol, C.Element == Binding { + self.init( + sources: sources, + selection: selection, + content: content, + label: { OpenSwiftUI.Label(title, systemImage: systemImage) }, + currentValueLabel: currentValueLabel + ) + } +} + +#if canImport(DeveloperToolsSupport) + +// MARK: - Picker + ImageResource + +@available(OpenSwiftUI_v5_0, *) +extension Picker where Label == OpenSwiftUI.Label { + /// Creates a picker that generates its label from a localized string key + /// and image resource + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - image: The name of the image resource to lookup. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// + /// This initializer creates a ``Text`` view on your behalf, and treats the + /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See + /// ``Text`` for more information about localizing strings. + nonisolated public init( + _ titleKey: LocalizedStringKey, + image: ImageResource, + selection: Binding, + @ViewBuilder content: () -> Content + ) { + self.init( + selection: selection, + content: content, + label: { OpenSwiftUI.Label(titleKey, image: image) } + ) + } + + /// Creates a picker that generates its label from a localized string key + /// and image resource. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes, which can be of + /// any number. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// "Border Thickness", + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - image: The name of the image resource to lookup. + /// - sources: A collection of values used as the source for displaying + /// he Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + nonisolated public init( + _ titleKey: LocalizedStringKey, + image: ImageResource, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content + ) where C: RandomAccessCollection, C.Element == Binding { + self.init( + sources: sources, + selection: selection, + content: content, + label: { OpenSwiftUI.Label(titleKey, image: image) } + ) + } + + /// Creates a picker that generates its label from a string and + /// image resource. + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + @_disfavoredOverload + nonisolated public init( + _ title: S, + image: ImageResource, + selection: Binding, + @ViewBuilder content: () -> Content + ) where S: StringProtocol { + self.init( + selection: selection, + content: content, + label: { OpenSwiftUI.Label(title, image: image) } + ) + } + + /// Creates a picker bound to a collection of bindings that generates its + /// label from a string and image resource. + /// + /// If the wrapped values of the collection passed to `sources` are not all + /// the same, some styles render the selection in a mixed state. The + /// specific presentation depends on the style. For example, a Picker + /// with a menu style uses dashes instead of checkmarks to indicate the + /// selected values. + /// + /// In the following example, a picker in a document inspector controls the + /// thickness of borders for the currently-selected shapes, which can be of + /// any number. + /// + /// enum Thickness: String, CaseIterable, Identifiable { + /// case thin + /// case regular + /// case thick + /// + /// var id: String { rawValue } + /// } + /// + /// struct Border { + /// var color: Color + /// var thickness: Thickness + /// } + /// + /// @State private var selectedObjectBorders = [ + /// Border(color: .black, thickness: .thin), + /// Border(color: .red, thickness: .thick) + /// ] + /// + /// Picker( + /// "Border Thickness", + /// sources: $selectedObjectBorders, + /// selection: \.thickness + /// ) { + /// ForEach(Thickness.allCases) { thickness in + /// Text(thickness.rawValue) + /// } + /// } + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - image: The name of the image resource to lookup. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + @_disfavoredOverload + nonisolated public init( + _ title: S, + image: ImageResource, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content + ) where C: RandomAccessCollection, S: StringProtocol, C.Element == Binding { + self.init( + sources: sources, + selection: selection, + content: content, + label: { OpenSwiftUI.Label(title, image: image) } + ) + } +} + +// MARK: - Picker + ImageResource + CurrentValueLabel + +@available(OpenSwiftUI_v6_0, *) +extension Picker where Label == OpenSwiftUI.Label { + /// Creates a picker that accepts a custom current value label and + /// generates its label from a localized string key and image resource + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - image: The name of the image resource to lookup. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + /// + /// This initializer creates a ``Text`` view on your behalf, and treats the + /// localized key similar to ``Text/init(_:tableName:bundle:comment:)``. See + /// ``Text`` for more information about localizing strings. + nonisolated public init( + _ titleKey: LocalizedStringKey, + image: ImageResource, + selection: Binding, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) { + self.init( + selection: selection, + content: content, + label: { OpenSwiftUI.Label(titleKey, image: image) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker that accepts a custom current value label generates + /// its label from a localized string key and image resource. + /// + /// - Parameters: + /// - titleKey: A localized string key that describes the purpose of + /// selecting an option. + /// - image: The name of the image resource to lookup. + /// - sources: A collection of values used as the source for displaying + /// he Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + nonisolated public init( + _ titleKey: LocalizedStringKey, + image: ImageResource, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where C: RandomAccessCollection, C.Element == Binding { + self.init( + sources: sources, + selection: selection, + content: content, + label: { OpenSwiftUI.Label(titleKey, image: image) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker that accepts a custom current value and generates its + /// label from a string and image resource. + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - systemImage: The name of the image resource to lookup. + /// - selection: A binding to a property that determines the + /// currently-selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + @_disfavoredOverload + nonisolated public init( + _ title: S, + image: ImageResource, + selection: Binding, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where S: StringProtocol { + self.init( + selection: selection, + content: content, + label: { OpenSwiftUI.Label(title, image: image) }, + currentValueLabel: currentValueLabel + ) + } + + /// Creates a picker bound to a collection of bindings that accepts a + /// custom current value label and generates its label from a string and + /// image resource. + /// + /// - Parameters: + /// - title: A string that describes the purpose of selecting an option. + /// - image: The name of the image resource to lookup. + /// - sources: A collection of values used as the source for displaying + /// the Picker's selection. + /// - selection: The key path of the values that determines the + /// currently-selected options. When a user selects an option from the + /// picker, the values at the key path of all items in the `sources` + /// collection are updated with the selected option. + /// - content: A view that contains the set of options. + /// - currentValueLabel: A view that represents the current value of the picker. + @_disfavoredOverload + nonisolated public init( + _ title: S, + image: ImageResource, + sources: C, + selection: KeyPath>, + @ViewBuilder content: () -> Content, + @ViewBuilder currentValueLabel: () -> some View + ) where C: RandomAccessCollection, S: StringProtocol, C.Element == Binding { + self.init( + sources: sources, + selection: selection, + content: content, + label: { OpenSwiftUI.Label(title, image: image) }, + currentValueLabel: currentValueLabel + ) + } +} +#endif + +// MARK: - Picker + PickerBuilder + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +extension Picker { + nonisolated public init( + selection: Binding, + @PickerBuilder content: () -> C, + @ViewBuilder label: () -> Label + ) where Content == PickerBuilder.Content, C: PickerContent { + self.init( + selection: selection, + content: PickerBuilder.Content(content()), + label: label() + ) + } + + nonisolated public init( + selection: Selection, + @PickerBuilder content: () -> C, + @ViewBuilder label: () -> Label + ) where Content == PickerBuilder.Content, C: PickerContent, Selection: RandomAccessCollection, Selection.Element == Binding { + self.init( + selection: selection, + content: PickerBuilder.Content(content()), + label: label() + ) + } + + nonisolated public init( + selection: Binding, + @PickerBuilder content: () -> C, + @ViewBuilder label: () -> Label, + @ViewBuilder footer: () -> Footer + ) where Content == PickerBuilder.ContentWithFooter, C: PickerContent, Footer: View { + self.init( + selection: selection, + content: PickerBuilder.ContentWithFooter(content: content(), footer: footer()), + label: label() + ) + } + + nonisolated public init( + selection: Selection, + @PickerBuilder content: () -> C, + @ViewBuilder label: () -> Label, + @ViewBuilder footer: () -> Footer + ) where Content == PickerBuilder.ContentWithFooter, C: PickerContent, Footer: View, Selection: RandomAccessCollection, Selection.Element == Binding { + self.init( + selection: selection, + content: PickerBuilder.ContentWithFooter(content: content(), footer: footer()), + label: label() + ) + } +} + +// MARK: - Picker + Text + PickerBuilder + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +extension Picker where Label == Text { + @_disfavoredOverload + nonisolated public init( + _ title: S, + selection: Binding, + @PickerBuilder content: () -> C + ) where Content == PickerBuilder.Content, C: PickerContent, S: StringProtocol { + self.init( + selection: selection, + content: PickerBuilder.Content(content()), + label: Text(title) + ) + } + + @_disfavoredOverload + nonisolated public init( + _ title: S, + selection: Selection, + @PickerBuilder content: () -> C + ) where Content == PickerBuilder.Content, C: PickerContent, S: StringProtocol, Selection: RandomAccessCollection, Selection.Element == Binding { + self.init( + selection: selection, + content: PickerBuilder.Content(content()), + label: Text(title) + ) + } + + nonisolated public init( + _ titleKey: LocalizedStringKey, + selection: Binding, + @PickerBuilder content: () -> C + ) where Content == PickerBuilder.Content, C: PickerContent { + self.init( + selection: selection, + content: PickerBuilder.Content(content()), + label: Text(titleKey) + ) + } + + nonisolated public init( + _ titleKey: LocalizedStringKey, + selection: Selection, + @PickerBuilder content: () -> C + ) where Content == PickerBuilder.Content, C: PickerContent, Selection: RandomAccessCollection, Selection.Element == Binding { + self.init( + selection: selection, + content: PickerBuilder.Content(content()), + label: Text(titleKey) + ) + } + + @_disfavoredOverload + nonisolated public init( + _ title: S, + selection: Binding, + @PickerBuilder content: () -> C, + @ViewBuilder footer: () -> Footer + ) where Content == PickerBuilder.ContentWithFooter, C: PickerContent, S: StringProtocol, Footer: View { + self.init( + selection: selection, + content: PickerBuilder.ContentWithFooter(content: content(), footer: footer()), + label: Text(title) + ) + } + + @_disfavoredOverload + nonisolated public init( + _ title: S, + selection: Selection, + @PickerBuilder content: () -> C, + @ViewBuilder footer: () -> Footer + ) where Content == PickerBuilder.ContentWithFooter, C: PickerContent, S: StringProtocol, Footer: View, Selection: RandomAccessCollection, Selection.Element == Binding { + self.init( + selection: selection, + content: PickerBuilder.ContentWithFooter(content: content(), footer: footer()), + label: Text(title) + ) + } + + nonisolated public init( + _ titleKey: LocalizedStringKey, + selection: Binding, + @PickerBuilder content: () -> C, + @ViewBuilder footer: () -> Footer + ) where Content == PickerBuilder.ContentWithFooter, C: PickerContent, Footer: View { + self.init( + selection: selection, + content: PickerBuilder.ContentWithFooter(content: content(), footer: footer()), + label: Text(titleKey) + ) + } + + nonisolated public init( + _ titleKey: LocalizedStringKey, + selection: Selection, + @PickerBuilder content: () -> C, + @ViewBuilder footer: () -> Footer + ) where Content == PickerBuilder.ContentWithFooter, C: PickerContent, Footer: View, Selection: RandomAccessCollection, Selection.Element == Binding { + self.init( + selection: selection, + content: PickerBuilder.ContentWithFooter(content: content(), footer: footer()), + label: Text(titleKey) + ) + } +} From 2d8dfc67d82421a35f7f46e6d620a735a5cc364e Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 30 Oct 2026 00:59:46 +0800 Subject: [PATCH 06/44] feat(picker): add PickerOption --- .../View/Control/Picker/PickerOption.swift | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Sources/OpenSwiftUI/View/Control/Picker/PickerOption.swift diff --git a/Sources/OpenSwiftUI/View/Control/Picker/PickerOption.swift b/Sources/OpenSwiftUI/View/Control/Picker/PickerOption.swift new file mode 100644 index 000000000..c52a7ef88 --- /dev/null +++ b/Sources/OpenSwiftUI/View/Control/Picker/PickerOption.swift @@ -0,0 +1,96 @@ +// +// PickerOption.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete + +public import OpenSwiftUICore + +// MARK: - PickerOption + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +public struct PickerOption: PrimitivePickerContent, PickerContent where Value: Hashable, Content: View { + public typealias SelectionValue = Value + + var value: Value + var content: Content + + public init(value: Value, @ViewBuilder content: () -> Content) { + self.value = value + self.content = content() + } + + public init(value: T, @ViewBuilder content: () -> Content) where Value == T?, T: Hashable { + self.value = .some(value) + self.content = content() + } + + @_disfavoredOverload + public init(_ title: S, value: Value) where Content == Text, S: StringProtocol { + self.value = value + content = Text(title) + } + + @_disfavoredOverload + public init(_ title: S, value: T) where Value == T?, Content == Text, T: Hashable, S: StringProtocol { + self.value = .some(value) + content = Text(title) + } + + public init(_ titleKey: LocalizedStringKey, value: Value) where Content == Text { + self.value = value + content = Text(titleKey) + } + + public init(_ titleKey: LocalizedStringKey, value: T) where Value == T?, Content == Text, T: Hashable { + self.value = .some(value) + content = Text(titleKey) + } + + public var _identifiedView: some View { + VStack { content }.tag(value) + } + + public var body: Self { + return bodyError() + } +} + +@_spi(_) +@available(*, unavailable) +extension PickerOption: Sendable {} + +// MARK: - PickerOption + Deprecated Initializers + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +@available(*, deprecated, message: "Use initializers which take an explicitely labeled `value`") +extension PickerOption { + public init(_ value: Value, @ViewBuilder content: () -> Content) { + self.init(value: value, content: content) + } + + public init(_ value: T, @ViewBuilder content: () -> Content) where Value == T?, T: Hashable { + self.init(value: value, content: content) + } + + @_disfavoredOverload + public init(_ title: S, _ value: Value) where Content == Text, S: StringProtocol { + self.init(title, value: value) + } + + @_disfavoredOverload + public init(_ title: S, _ value: T) where Value == T?, Content == Text, T: Hashable, S: StringProtocol { + self.init(title, value: value) + } + + public init(_ titleKey: LocalizedStringKey, _ value: Value) where Content == Text { + self.init(titleKey, value: value) + } + + public init(_ titleKey: LocalizedStringKey, _ value: T) where Value == T?, Content == Text, T: Hashable { + self.init(titleKey, value: value) + } +} From c2f7c751a7da491cc2cfc920ee1ef1e8670c04a3 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 30 Oct 2026 01:01:57 +0800 Subject: [PATCH 07/44] feat(data): add NestedDynamicProperties --- .../NestedDynamicProperties.swift | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 Sources/OpenSwiftUI/Data/DynamicProperty/NestedDynamicProperties.swift diff --git a/Sources/OpenSwiftUI/Data/DynamicProperty/NestedDynamicProperties.swift b/Sources/OpenSwiftUI/Data/DynamicProperty/NestedDynamicProperties.swift new file mode 100644 index 000000000..9af52be50 --- /dev/null +++ b/Sources/OpenSwiftUI/Data/DynamicProperty/NestedDynamicProperties.swift @@ -0,0 +1,40 @@ +// +// NestedDynamicProperties.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete + +@_spi(ForOpenSwiftUIOnly) +public import OpenSwiftUICore + +// MARK: - NestedDynamicProperties + +@_spi(Private) +@available(OpenSwiftUI_v4_0, *) +@propertyWrapper +public struct NestedDynamicProperties: DynamicProperty { + public var wrappedValue: Content + + public init(wrappedValue: Content) { + self.wrappedValue = wrappedValue + } + + public static func _makeProperty( + in buffer: inout _DynamicPropertyBuffer, + container: _GraphValue, + fieldOffset: Int, + inputs: inout _GraphInputs + ) { + buffer.addFields( + DynamicPropertyCache.fields(of: Content.self), + container: container, + inputs: &inputs, + baseOffset: fieldOffset + ) + } +} + +@_spi(Private) +@available(*, unavailable) +extension NestedDynamicProperties: Sendable {} From 2c68f8af5f8384a425fbde953f807c6eea1ed6b5 Mon Sep 17 00:00:00 2001 From: Kyle Date: Fri, 30 Oct 2026 01:08:46 +0800 Subject: [PATCH 08/44] feat(picker): add PickerContent and PickerBuilder --- .../View/Control/Picker/PickerContent.swift | 369 ++++++++++++++++++ 1 file changed, 369 insertions(+) create mode 100644 Sources/OpenSwiftUI/View/Control/Picker/PickerContent.swift diff --git a/Sources/OpenSwiftUI/View/Control/Picker/PickerContent.swift b/Sources/OpenSwiftUI/View/Control/Picker/PickerContent.swift new file mode 100644 index 000000000..b25dc7ad6 --- /dev/null +++ b/Sources/OpenSwiftUI/View/Control/Picker/PickerContent.swift @@ -0,0 +1,369 @@ +// +// PickerContent.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete (Blocked by Section) +// ID: 7AD5F459D455E199176BC90D47D4BC88 (SwiftUI) + +@_spi(ForOpenSwiftUIOnly) +public import OpenSwiftUICore + +// MARK: - PickerContent + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +public protocol PickerContent { + associatedtype SelectionValue: Hashable = Body.SelectionValue + associatedtype _IdentifiedView: View = _PickerContentBodyAdaptor + + var _identifiedView: _IdentifiedView { get } + + associatedtype Body: PickerContent + + @PickerBuilder + @MainActor + @preconcurrency + var body: Body { get } +} + +extension PickerContent { + func bodyError() -> Body { + fatalError("body of \(_typeName(Self.self, qualified: false)) should not be called") + } +} + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +extension PickerContent where _IdentifiedView == _PickerContentBodyAdaptor { + public var _identifiedView: _PickerContentBodyAdaptor { + _PickerContentBodyAdaptor(content: self) + } +} + +// MARK: - _PickerContentBodyAdaptor + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +public struct _PickerContentBodyAdaptor: View where C: PickerContent { + @NestedDynamicProperties private var content: C + + nonisolated init(content: C) { + _content = NestedDynamicProperties(wrappedValue: content) + } + + public var body: C.Body._IdentifiedView { + content.body._identifiedView + } +} + +@_spi(_) +@available(*, unavailable) +extension _PickerContentBodyAdaptor: Sendable {} + +// MARK: - PrimitivePickerContent + +protocol PrimitivePickerContent: PickerContent where Body == Self {} + +extension PrimitivePickerContent { + public var body: Self { + bodyError() + } +} + +// MARK: - PickerBuilder + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +@resultBuilder +public struct PickerBuilder where SelectionValue: Hashable { + public struct Content: View where C: PickerContent { + @NestedDynamicProperties var content: C + + nonisolated init(_ content: C) { + _content = NestedDynamicProperties(wrappedValue: content) + } + + public var body: some View { + content._identifiedView + } + } + + public struct ContentWithFooter: View where C: PickerContent, Footer: View { + @NestedDynamicProperties var content: C + var footer: Footer + + nonisolated init(content: C, footer: Footer) { + _content = NestedDynamicProperties(wrappedValue: content) + self.footer = footer + } + + public var body: some View { + content._identifiedView + Divider()._untagged() + footer._untagged() + } + } + + @_alwaysEmitIntoClient + public static func buildExpression(_ content: Content) -> Content + where SelectionValue == Content.SelectionValue, Content: PickerContent { + content + } + + @_alwaysEmitIntoClient + public static func buildBlock(_ content: Content) -> Content + where SelectionValue == Content.SelectionValue, Content: PickerContent { + content + } + + @_alwaysEmitIntoClient + public static func buildIf(_ content: Content?) -> Content? + where SelectionValue == Content.SelectionValue, Content: PickerContent { + content + } + + @_alwaysEmitIntoClient + public static func buildEither(first: T) -> _ConditionalContent + where SelectionValue == T.SelectionValue, T: PickerContent, F: PickerContent, T.SelectionValue == F.SelectionValue { + .init(storage: .trueContent(first)) + } + + @_alwaysEmitIntoClient + public static func buildEither(second: F) -> _ConditionalContent + where SelectionValue == T.SelectionValue, T: PickerContent, F: PickerContent, T.SelectionValue == F.SelectionValue { + .init(storage: .falseContent(second)) + } +} + +@_spi(_) +@available(*, unavailable) +extension PickerBuilder: Sendable {} + +@_spi(_) +@available(*, unavailable) +extension PickerBuilder.Content: Sendable {} + +@_spi(_) +@available(*, unavailable) +extension PickerBuilder.ContentWithFooter: Sendable {} + +// MARK: - _ConditionalContent + PickerContent + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +extension _ConditionalContent: PickerContent +where TrueContent: PickerContent, FalseContent: PickerContent, TrueContent.SelectionValue == FalseContent.SelectionValue { + public typealias SelectionValue = TrueContent.SelectionValue + public typealias Body = Self + + @usableFromInline + init(storage: Storage) { + self.init(__storage: storage) + } + + public var _identifiedView: _ConditionalContent { + switch storage { + case let .trueContent(content): + .init(__storage: .trueContent(content._identifiedView)) + case let .falseContent(content): + .init(__storage: .falseContent(content._identifiedView)) + } + } + + public var body: Self { + return bodyError() + } +} + +// MARK: - Optional + PickerContent + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +extension Optional: PickerContent where Wrapped: PickerContent { + public typealias SelectionValue = Wrapped.SelectionValue + public typealias Body = Self + + @inlinable + public var _identifiedView: Wrapped._IdentifiedView? { + map(\._identifiedView) + } + + public var body: Self { + return bodyError() + } +} + +// MARK: - ForEach + PickerContent + +@_spi(_) +@available(OpenSwiftUI_v4_4, *) +extension ForEach: PickerContent where Content: PickerContent { + public typealias SelectionValue = Content.SelectionValue + public typealias Body = Self + + public init( + _ data: Data, + id: KeyPath, + @PickerBuilder content: @escaping (Data.Element) -> Content + ) where SV == Content.SelectionValue { + self.init(data, idGenerator: .keyPath(id), content: content) + } + + public var _identifiedView: ForEach { + ForEach(self, transform: \._identifiedView) + } + + public var body: Self { + return bodyError() + } +} + +@_spi(_) +@available(OpenSwiftUI_v4_4, *) +extension ForEach where ID == Data.Element.ID, Content: PickerContent, Data.Element: Identifiable { + public init( + _ data: Data, + @PickerBuilder content: @escaping (Data.Element) -> Content + ) where SV == Content.SelectionValue { + self.init(data, id: \.id, content: content) + } +} + +@_spi(_) +@available(OpenSwiftUI_v4_4, *) +extension ForEach where Data == Range, ID == Int, Content: PickerContent { + @_semantics("openswiftui.requires_constant_range") + // NOTE: Use this to make Xcode Swift Toolchain emit warning for non-constant ranges. eg. "Non-constant range: not an integer range" + @_semantics("swiftui.requires_constant_range") + public init( + _ data: Range, + @PickerBuilder content: @escaping (Int) -> Content + ) where SV == Content.SelectionValue { + self.init(data, idGenerator: .offset, content: content) + } +} + +// MARK: - Group + PickerContent + +@_spi(_) +@available(OpenSwiftUI_v4_4, *) +extension Group: PickerContent where Content: PickerContent { + public typealias SelectionValue = Content.SelectionValue + public typealias Body = Self + + @inlinable + public init(@PickerBuilder content: () -> Content) where SV == Content.SelectionValue { + self = Self._make(content: content()) + } + + @inlinable + public var _identifiedView: Content._IdentifiedView { + content._identifiedView + } + + public var body: Self { + return bodyError() + } +} + +// MARK: - Section + PickerContent [TBA] + +// TODO: Add the Section adapters when Section is available. + +// MARK: - _TuplePickerContent + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +@frozen +public struct _TuplePickerContent: PrimitivePickerContent, PickerContent where T: Hashable { + public typealias SelectionValue = T + public let _identifiedView: TupleView + + @inlinable + init(_ content: U) { + _identifiedView = .init(content) + } +} + +@_spi(_) +@available(*, unavailable) +extension _TuplePickerContent: Sendable {} + +// MARK: - PickerBuilder + Tuple Content + +@_spi(_) +@available(OpenSwiftUI_v4_0, *) +extension PickerBuilder { + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C0.SelectionValue == C1.SelectionValue { + .init((c0._identifiedView, c1._identifiedView)) + } + + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1, _ c2: C2 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C2: PickerContent, C0.SelectionValue == C1.SelectionValue, C1.SelectionValue == C2.SelectionValue { + .init((c0._identifiedView, c1._identifiedView, c2._identifiedView)) + } + + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C2: PickerContent, C3: PickerContent, C0.SelectionValue == C1.SelectionValue, C1.SelectionValue == C2.SelectionValue, C2.SelectionValue == C3.SelectionValue { + .init((c0._identifiedView, c1._identifiedView, c2._identifiedView, c3._identifiedView)) + } + + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C2: PickerContent, C3: PickerContent, C4: PickerContent, C0.SelectionValue == C1.SelectionValue, C1.SelectionValue == C2.SelectionValue, C2.SelectionValue == C3.SelectionValue, C3.SelectionValue == C4.SelectionValue { + .init((c0._identifiedView, c1._identifiedView, c2._identifiedView, c3._identifiedView, c4._identifiedView)) + } + + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C2: PickerContent, C3: PickerContent, C4: PickerContent, C5: PickerContent, C0.SelectionValue == C1.SelectionValue, C1.SelectionValue == C2.SelectionValue, C2.SelectionValue == C3.SelectionValue, C3.SelectionValue == C4.SelectionValue, C4.SelectionValue == C5.SelectionValue { + .init((c0._identifiedView, c1._identifiedView, c2._identifiedView, c3._identifiedView, c4._identifiedView, c5._identifiedView)) + } + + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C2: PickerContent, C3: PickerContent, C4: PickerContent, C5: PickerContent, C6: PickerContent, C0.SelectionValue == C1.SelectionValue, C1.SelectionValue == C2.SelectionValue, C2.SelectionValue == C3.SelectionValue, C3.SelectionValue == C4.SelectionValue, C4.SelectionValue == C5.SelectionValue, C5.SelectionValue == C6.SelectionValue { + .init((c0._identifiedView, c1._identifiedView, c2._identifiedView, c3._identifiedView, c4._identifiedView, c5._identifiedView, c6._identifiedView)) + } + + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C2: PickerContent, C3: PickerContent, C4: PickerContent, C5: PickerContent, C6: PickerContent, C7: PickerContent, C0.SelectionValue == C1.SelectionValue, C1.SelectionValue == C2.SelectionValue, C2.SelectionValue == C3.SelectionValue, C3.SelectionValue == C4.SelectionValue, C4.SelectionValue == C5.SelectionValue, C5.SelectionValue == C6.SelectionValue, C6.SelectionValue == C7.SelectionValue { + .init((c0._identifiedView, c1._identifiedView, c2._identifiedView, c3._identifiedView, c4._identifiedView, c5._identifiedView, c6._identifiedView, c7._identifiedView)) + } + + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C2: PickerContent, C3: PickerContent, C4: PickerContent, C5: PickerContent, C6: PickerContent, C7: PickerContent, C8: PickerContent, C0.SelectionValue == C1.SelectionValue, C1.SelectionValue == C2.SelectionValue, C2.SelectionValue == C3.SelectionValue, C3.SelectionValue == C4.SelectionValue, C4.SelectionValue == C5.SelectionValue, C5.SelectionValue == C6.SelectionValue, C6.SelectionValue == C7.SelectionValue, C7.SelectionValue == C8.SelectionValue { + .init((c0._identifiedView, c1._identifiedView, c2._identifiedView, c3._identifiedView, c4._identifiedView, c5._identifiedView, c6._identifiedView, c7._identifiedView, c8._identifiedView)) + } + + @_alwaysEmitIntoClient + public static func buildBlock( + _ c0: C0, _ c1: C1, _ c2: C2, _ c3: C3, _ c4: C4, _ c5: C5, _ c6: C6, _ c7: C7, _ c8: C8, _ c9: C9 + ) -> _TuplePickerContent + where SelectionValue == C0.SelectionValue, C0: PickerContent, C1: PickerContent, C2: PickerContent, C3: PickerContent, C4: PickerContent, C5: PickerContent, C6: PickerContent, C7: PickerContent, C8: PickerContent, C9: PickerContent, C0.SelectionValue == C1.SelectionValue, C1.SelectionValue == C2.SelectionValue, C2.SelectionValue == C3.SelectionValue, C3.SelectionValue == C4.SelectionValue, C4.SelectionValue == C5.SelectionValue, C5.SelectionValue == C6.SelectionValue, C6.SelectionValue == C7.SelectionValue, C7.SelectionValue == C8.SelectionValue, C8.SelectionValue == C9.SelectionValue { + .init((c0._identifiedView, c1._identifiedView, c2._identifiedView, c3._identifiedView, c4._identifiedView, c5._identifiedView, c6._identifiedView, c7._identifiedView, c8._identifiedView, c9._identifiedView)) + } +} From 6c4d3a109dcdf37c8329cccdee3e34c325289750 Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 31 Oct 2026 12:52:38 +0800 Subject: [PATCH 09/44] feat(tag): add variadic child tag accessors --- Sources/OpenSwiftUI/View/Tag.swift | 47 ++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Sources/OpenSwiftUI/View/Tag.swift diff --git a/Sources/OpenSwiftUI/View/Tag.swift b/Sources/OpenSwiftUI/View/Tag.swift new file mode 100644 index 000000000..080ba1613 --- /dev/null +++ b/Sources/OpenSwiftUI/View/Tag.swift @@ -0,0 +1,47 @@ +// +// Tag.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete + +public import OpenSwiftUICore + +// MARK: - _VariadicView.Children + Tag + +@available(OpenSwiftUI_v4_0, *) +extension _VariadicView.Children { + @_spi(Private) + public func tagIndex(_ value: Binding) -> Binding where V: Hashable { + value.projectingTagIndex(viewList: list) + } + + @_spi(Private) + public func tagIndex(_ value: V) -> Int? where V: Hashable { + var start = 0 + var result: Int? + var index = 0 + list.applySublists(from: &start, list: nil) { sublist in + if sublist.traits.tag(for: V.self) == .some(value), + !sublist.traits.isAuxiliaryContent { + result = index + return false + } + index &+= sublist.count + return true + } + return result + } +} + +// MARK: - _VariadicView.Children.Element + Tag + +extension _VariadicView.Children.Element { + @_spi(Private) + @available(OpenSwiftUI_v4_0, *) + @MainActor + @preconcurrency + public func tag(for type: V.Type = V.self) -> V? where V: Hashable { + traits.tag(for: type) + } +} From 631291f54ea7bc1371d2f9fa010772221980431c Mon Sep 17 00:00:00 2001 From: Kyle Date: Sat, 31 Oct 2026 12:53:22 +0800 Subject: [PATCH 10/44] feat(picker): add picker style infrastructure --- .../View/Control/Picker/PickerStyle.swift | 240 ++++++++++++++++++ 1 file changed, 240 insertions(+) create mode 100644 Sources/OpenSwiftUI/View/Control/Picker/PickerStyle.swift diff --git a/Sources/OpenSwiftUI/View/Control/Picker/PickerStyle.swift b/Sources/OpenSwiftUI/View/Control/Picker/PickerStyle.swift new file mode 100644 index 000000000..e25831cbf --- /dev/null +++ b/Sources/OpenSwiftUI/View/Control/Picker/PickerStyle.swift @@ -0,0 +1,240 @@ +// +// PickerStyle.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete +// ID: C0971D6B8A5FF9F4D88F3BC1A96ED172 (SwiftUI) + +import OpenAttributeGraphShims +@_spi(ForOpenSwiftUIOnly) +public import OpenSwiftUICore + +// MARK: - PickerStyle + +/// A type that specifies the appearance and interaction of all pickers within +/// a view hierarchy. +@available(OpenSwiftUI_v1_0, *) +public protocol PickerStyle { + /// Builds the appearance and interaction content for a picker. + /// + /// All picker styles need to: + /// * Include the ``PickerStyle/label`` of the configuration in some way. + /// * Display the selected ``PickerStyle/content`` that corresponds to + /// `selection.value`. + /// * Enable selection from the complete set of options in + /// ``PickerStyle/content``. + static func _makeView( + value: _GraphValue<_PickerValue>, + inputs: _ViewInputs + ) -> _ViewOutputs where SelectionValue: Hashable + + /// All picker styles need to: + /// * Include the ``PickerStyle/label`` of the configuration in some way. + /// * Display the selected ``PickerStyle/content`` that corresponds to + /// `selection.value`. + /// * Enable selection from the complete set of options in + /// ``PickerStyle/content``. + static func _makeViewList( + value: _GraphValue<_PickerValue>, + inputs: _ViewListInputs + ) -> _ViewListOutputs where SelectionValue: Hashable +} + +// MARK: - _PickerValue + +@available(OpenSwiftUI_v1_0, *) +public struct _PickerValue where Style: PickerStyle, SelectionValue: Hashable { + var style: Style + var configuration: PickerStyleConfiguration + + @_spi(UIFrameworks) + @available(OpenSwiftUI_v6_0, *) + public var label: some View { + PickerStyleConfiguration.Label() + } + + @_spi(UIFrameworks) + @available(OpenSwiftUI_v6_0, *) + public var content: some View { + PickerStyleConfiguration.Content() + } + + @_spi(UIFrameworks) + @available(OpenSwiftUI_v6_0, *) + public var selection: Binding { + configuration.$selection + } + + struct Init1: Rule { + @Attribute var base: ResolvedPicker + var style: Style + + var value: _PickerValue { + _PickerValue(style: style, configuration: base.configuration) + } + } + + struct Init2: Rule { + @Attribute var base: ResolvedPicker + @Attribute var style: Style + + var value: _PickerValue { + _PickerValue(style: style, configuration: base.configuration) + } + } +} + +@available(*, unavailable) +extension _PickerValue: Sendable {} + +// MARK: - View + PickerStyle + +@available(OpenSwiftUI_v1_0, *) +extension View { + /// Sets the style for pickers within this view. + nonisolated public func pickerStyle(_ style: S) -> some View where S: PickerStyle { + modifier(PickerStyleWriter(style: style)) + } + + func pickerStyle(_ style: S, in context: C) -> some View where S: PickerStyle, C: StyleContext { + modifier(PickerStyleWriter(style: style).requiring(C.self)) + } + + func pickerStyle(_ style: S, if predicate: P.Type) -> some View where P: ViewInputPredicate, S: PickerStyle { + modifier(PickerStyleWriter(style: style).requiring(predicate)) + } +} + +// MARK: - PickerStyleConfiguration + +struct PickerStyleConfiguration where SelectionValue: Hashable { + struct Label: ViewAlias {} + struct Content: ViewAlias {} + struct CurrentValueLabel: ViewAlias {} + + @Binding var selection: SelectionValue + var multiSelection: [Binding] + var currentValueLabel: CurrentValueLabel? + + var label: Label { Label() } + var content: Content { Content() } + + init(selection: [Binding]) { + _selection = Binding(flattening: selection) + multiSelection = selection + currentValueLabel = nil + } + + #if os(macOS) + func selectedIndices(children: SubviewsCollection) -> [Int] { + selectedBoundIndices(children: children).compactMap { $0.wrappedValue } + } + #endif + + func selectedBoundIndices(children: SubviewsCollection) -> [Binding] { + multiSelection.map { children.base.tagIndex($0) } + } +} + +// MARK: - ResolvedPicker + +struct ResolvedPicker: PrimitiveView where SelectionValue: Hashable { + var configuration: PickerStyleConfiguration + + nonisolated static func _makeView(view: _GraphValue, inputs: _ViewInputs) -> _ViewOutputs { + if let style = inputs[PickerStyleInput.self] { + return style.type.makeView(view: view, style: style, inputs: inputs) + } else { + return makeView(view: view, style: DefaultPickerStyle(), inputs: inputs) + } + } + + nonisolated static func _makeViewList(view: _GraphValue, inputs: _ViewListInputs) -> _ViewListOutputs { + if let style = inputs[PickerStyleInput.self] { + return style.type.makeViewList(view: view, style: style, inputs: inputs) + } else { + return makeViewList(view: view, style: DefaultPickerStyle(), inputs: inputs) + } + } + + nonisolated private static func makeView( + view: _GraphValue, + style: S, + inputs: _ViewInputs + ) -> _ViewOutputs where S: PickerStyle { + let value = _GraphValue(_PickerValue.Init1(base: view.value, style: style)) + return S._makeView(value: value, inputs: inputs) + } + + nonisolated private static func makeViewList( + view: _GraphValue, + style: S, + inputs: _ViewListInputs + ) -> _ViewListOutputs where S: PickerStyle { + let value = _GraphValue(_PickerValue.Init1(base: view.value, style: style)) + return S._makeViewList(value: value, inputs: inputs) + } +} + +// MARK: - PickerStyleWriter + +struct PickerStyleWriter