From 1dcb1159bf945917e645f9edaed2f12ff0b08d1a Mon Sep 17 00:00:00 2001 From: Kyle Date: Sun, 26 Oct 2025 17:17:36 +0800 Subject: [PATCH] Add LabelGroup support --- .../View/LabelGroup/BodyLabelGroupStyle.swift | 27 ++++++ .../View/LabelGroup/LabelGroup.swift | 27 ++++++ .../View/LabelGroup/LabelGroupStyle.swift | 85 +++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 Sources/OpenSwiftUI/View/LabelGroup/BodyLabelGroupStyle.swift create mode 100644 Sources/OpenSwiftUI/View/LabelGroup/LabelGroup.swift create mode 100644 Sources/OpenSwiftUI/View/LabelGroup/LabelGroupStyle.swift diff --git a/Sources/OpenSwiftUI/View/LabelGroup/BodyLabelGroupStyle.swift b/Sources/OpenSwiftUI/View/LabelGroup/BodyLabelGroupStyle.swift new file mode 100644 index 000000000..ff47c7eb6 --- /dev/null +++ b/Sources/OpenSwiftUI/View/LabelGroup/BodyLabelGroupStyle.swift @@ -0,0 +1,27 @@ +// +// BodyLabelGroupStyle.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete + +// MARK: BodyLabelGroupStyle + +struct BodyLabelGroupStyle: LabelGroupStyle_v0 { + func font(at level: Int) -> Font { + switch level { + case 0: .body + case 1: .subheadline + case 2: .footnote + default: .footnote + } + } + + func foregroundStyle(at level: Int) -> HierarchicalShapeStyle { + switch level { + case 0: .primary + case 1, 2: .secondary + default: .tertiary + } + } +} diff --git a/Sources/OpenSwiftUI/View/LabelGroup/LabelGroup.swift b/Sources/OpenSwiftUI/View/LabelGroup/LabelGroup.swift new file mode 100644 index 000000000..50d00baeb --- /dev/null +++ b/Sources/OpenSwiftUI/View/LabelGroup/LabelGroup.swift @@ -0,0 +1,27 @@ +// +// LabelGroup.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete + +@_spi(Private) +@available(OpenSwiftUI_v4_0, *) +public struct LabelGroup: View where Content: View { + var content: Content + + public init(@ViewBuilder content: () -> Content) { + self.content = content() + } + + public var body: some View { + ResolvedLabelGroupStyle() + .viewAlias(LabelGroupStyleConfiguration.Content.self) { + content + } + } +} + +@_spi(Private) +@available(*, unavailable) +extension LabelGroup: Sendable {} diff --git a/Sources/OpenSwiftUI/View/LabelGroup/LabelGroupStyle.swift b/Sources/OpenSwiftUI/View/LabelGroup/LabelGroupStyle.swift new file mode 100644 index 000000000..aea8e5aa6 --- /dev/null +++ b/Sources/OpenSwiftUI/View/LabelGroup/LabelGroupStyle.swift @@ -0,0 +1,85 @@ +// +// LabelGroupStyle.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: WIP +// ID: D603B70BAAC92B7EC91530E54167B115 (SwiftUI) + +// MARK: - LabelGroupStyle_v0 + +@_spi(UIFrameworks) +@available(OpenSwiftUI_v6_0, *) +public protocol LabelGroupStyle_v0 { + associatedtype Foreground: ShapeStyle = HierarchicalShapeStyle + + func font(at level: Int) -> Font + + func foregroundStyle(at level: Int) -> Self.Foreground +} + +// MARK: - View + labelGroupStyle_v0 + +@_spi(UIFrameworks) +@available(OpenSwiftUI_v6_0, *) +extension View { + nonisolated public func labelGroupStyle_v0(_ style: some LabelGroupStyle_v0) -> some View { + modifier(LabelGroupStyleModifier(style: style)) + } +} + +// MARK: - LabelGroupStyleConfiguration + +struct LabelGroupStyleConfiguration { + struct Content: ViewAlias {} + + let content: Content +} + +// MARK: - LabelGroupStyleModifier + +struct LabelGroupStyleModifier: StyleModifier where S: LabelGroupStyle_v0 { + var style: S + + init(style: S) { + self.style = style + } + + func styleBody(configuration: LabelGroupStyleConfiguration) -> some View { + _VariadicView.Tree(StyleApplicator(style: style)) { + configuration.content + } + } +} + +// MARK: - ResolvedLabelGroupStyle + +struct ResolvedLabelGroupStyle: StyleableView { + static let defaultStyleModifier: LabelGroupStyleModifier = .init(style: .init()) + + var configuration: LabelGroupStyleConfiguration { .init(content: .init()) } +} + +// MARK: - StyleApplicator [WIP] + +private struct StyleApplicator: _VariadicView.MultiViewRoot where S: LabelGroupStyle_v0 { + + struct EnumeratedView { + var view: _VariadicView.Children.Element + var offset: Int + } + + var style: S + + func body(children: _VariadicView.Children) -> some View { + ForEach( + children.enumerated().map { EnumeratedView(view: $0.element, offset: $0.offset) }, + id: \.view.id + ) { view in + view.view + // defaultForegroundStyle + // font + // platformItem + } + } +}