Skip to content

Commit 7f73b62

Browse files
authored
Add LabeledContent support (#605)
1 parent 3604bca commit 7f73b62

File tree

11 files changed

+143
-23
lines changed

11 files changed

+143
-23
lines changed

Example/HostingExample/ViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ class ViewController: NSViewController {
6666

6767
struct ContentView: View {
6868
var body: some View {
69-
ForEachExample()
69+
ToggleExample()
7070
}
7171
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// LabeledContentUITests.swift
3+
// OpenSwiftUIUITests
4+
5+
import Testing
6+
import SnapshotTesting
7+
8+
@MainActor
9+
@Suite(.snapshots(record: .never, diffTool: diffTool))
10+
struct LabeledContentUITests {
11+
@Test
12+
func defaultStyle() {
13+
struct ContentView: View {
14+
var body: some View {
15+
LabeledContent {
16+
Color.red
17+
} label: {
18+
Color.blue
19+
}
20+
21+
}
22+
}
23+
#if os(iOS) || os(visionOS)
24+
openSwiftUIAssertSnapshot(of: ContentView())
25+
#else
26+
// FIXME: defaultStlye is not the same on macOS platform
27+
withKnownIssue {
28+
openSwiftUIAssertSnapshot(of: ContentView())
29+
}
30+
#endif
31+
}
32+
33+
@Test
34+
func customStyle() {
35+
struct CustomStyle: LabeledContentStyle {
36+
func makeBody(configuration: Configuration) -> some View {
37+
VStack(spacing: 0) {
38+
configuration.label
39+
configuration.content
40+
}
41+
}
42+
}
43+
struct ContentView: View {
44+
var body: some View {
45+
LabeledContent {
46+
Color.red
47+
} label: {
48+
Color.blue
49+
}.labeledContentStyle(CustomStyle())
50+
}
51+
}
52+
openSwiftUIAssertSnapshot(of: ContentView())
53+
}
54+
}

Example/SharedExample/View/ToggleExample.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import SwiftUI
99
#endif
1010

1111
struct ToggleExample: View {
12-
// FIXME: Fix Representable update logic and add test case
13-
@State var toggle = false
14-
12+
// FIXME: Add UI test case
13+
@State private var toggle = false
14+
1515
var body: some View {
1616
Toggle(isOn: $toggle) {
1717
Color.red

Sources/OpenSwiftUI/View/Label/AutomaticLabeledContentStyle.swift renamed to Sources/OpenSwiftUI/View/LabelContent/AutomaticLabeledContentStyle.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,21 @@ public struct AutomaticLabeledContentStyle: LabeledContentStyle {
3333
_openSwiftUIEmptyStub()
3434
}
3535

36-
// FIXME
36+
// FIXME: Blocked by StyleContextAcceptsPredicate
3737
public func makeBody(configuration: AutomaticLabeledContentStyle.Configuration) -> some View {
38-
LabeledContent {
38+
HStack {
39+
configuration.label
3940
configuration.content
40-
.modifier(_LabeledContentStyleModifier(style: self))
41-
} label: {
42-
StaticIf(LabelVisibilityConfigured.self) {
43-
labelsVisibility == .hidden ? nil : configuration.label
44-
} else: {
45-
configuration.label
46-
}
4741
}
42+
// LabeledContent {
43+
// configuration.content
44+
// .labeledContentStyle(self)
45+
// } label: {
46+
// configuration.label
47+
// .staticIf(LabelVisibilityConfigured.self) { label in
48+
// labelsVisibility == .hidden ? nil : configuration.label
49+
// }
50+
// }
4851
}
4952
}
5053

Sources/OpenSwiftUI/View/Label/LabeledContentStyleConfiguration.swift renamed to Sources/OpenSwiftUI/View/LabelContent/LabeledContentStyleConfiguration.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ extension View {
9696
// MARK: - ResolvedLabeledContent
9797

9898
struct ResolvedLabeledContent: StyleableView {
99+
var configuration: LabeledContentStyleConfiguration
100+
101+
static let defaultStyleModifier: _LabeledContentStyleModifier<AutomaticLabeledContentStyle> = .init(style: .init())
102+
99103
struct _Body: View {
100104
var configuration: LabeledContentStyleConfiguration
101105

@@ -126,10 +130,6 @@ struct ResolvedLabeledContent: StyleableView {
126130
var body: _Body {
127131
_Body(configuration: configuration)
128132
}
129-
130-
var configuration: LabeledContentStyleConfiguration
131-
132-
static let defaultStyleModifier: _LabeledContentStyleModifier<AutomaticLabeledContentStyle> = .init(style: .init())
133133
}
134134

135135
// MARK: - _LabeledContentStyleModifier
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// LeadingTrailingLabeledContentStyle.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for 6.5.4
6+
// Status: WIP
7+
// ID: 883FC595DC5A078A9D167DD7587DD054 (SwiftUI)
8+
9+
import Foundation
10+
@_spi(ForOpenSwiftUIOnly)
11+
import OpenSwiftUICore
12+
13+
// MARK: - LabeledContentUsesLegacyLayout
14+
15+
struct LabeledContentUsesLegacyLayout: ViewInputPredicate {
16+
static func evaluate(inputs: _GraphInputs) -> Bool {
17+
!isLinkedOnOrAfter(.v6)
18+
}
19+
}
20+
21+
// MARK: - LeadingTrailingLabeledContentStyle
22+
23+
struct LeadingTrailingLabeledContentStyle: LabeledContentStyle {
24+
let spacing: CGFloat?
25+
26+
func makeBody(configuration: Configuration) -> some View {
27+
HStack {
28+
configuration.label
29+
.staticIf(_SemanticFeature_v4.self) { label in
30+
VStack(alignment: .leading, spacing: spacing){
31+
LabelGroup { label }
32+
}
33+
}
34+
Spacer().layoutPriority(-1)
35+
HStack {
36+
configuration.content
37+
}
38+
.defaultForegroundColor(.secondary)
39+
}
40+
.spacing(Spacing())
41+
}
42+
}
43+
44+
// MARK: - ListLabeledContentPrefersHorizontalLayout
45+
46+
struct ListLabeledContentPrefersHorizontalLayout: ViewInputBoolFlag {}
47+
48+
extension View {
49+
func listLabeledContentPrefersHorizontalLayout() -> some View {
50+
input(ListLabeledContentPrefersHorizontalLayout.self)
51+
}
52+
}
53+
54+
// MARK: - LeadingTrailingLabeledContentStyle_Phone [TODO]
55+
56+
#if os(iOS) || os(visionOS)
57+
struct LeadingTrailingLabeledContentStyle_Phone {}
58+
#endif

0 commit comments

Comments
 (0)