@@ -14,8 +14,11 @@ public extension RichTextStyle {
14
14
This button can be used to toggle a ``RichTextStyle``.
15
15
16
16
This view renders a plain `Button`, which means you can
17
- use and configure with plain SwiftUI. The one exception
18
- is the content color, which is set with a style.
17
+ use and configure with plain SwiftUI.
18
+
19
+ You can apply a `foregroundStyle` to the view to define
20
+ the color to use when the style is not active. The view
21
+ will `.accentColor` when the style is active.
19
22
*/
20
23
struct Button : View {
21
24
@@ -24,18 +27,15 @@ public extension RichTextStyle {
24
27
25
28
- Parameters:
26
29
- style: The style to toggle.
27
- - buttonStyle: The button style to use, by default `.standard`.
28
30
- value: The value to bind to.
29
31
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
30
32
*/
31
33
public init (
32
34
style: RichTextStyle ,
33
- buttonStyle: Style = . standard,
34
35
value: Binding < Bool > ,
35
36
fillVertically: Bool = false
36
37
) {
37
38
self . style = style
38
- self . buttonStyle = buttonStyle
39
39
self . value = value
40
40
self . fillVertically = fillVertically
41
41
}
@@ -45,26 +45,22 @@ public extension RichTextStyle {
45
45
46
46
- Parameters:
47
47
- style: The style to toggle.
48
- - buttonStyle: The button style to use, by default `.standard`.
49
48
- context: The context to affect.
50
49
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
51
50
*/
52
51
public init (
53
52
style: RichTextStyle ,
54
- buttonStyle: Style = . standard,
55
53
context: RichTextContext ,
56
54
fillVertically: Bool = false
57
55
) {
58
56
self . init (
59
57
style: style,
60
- buttonStyle: buttonStyle,
61
58
value: context. binding ( for: style) ,
62
59
fillVertically: fillVertically
63
60
)
64
61
}
65
62
66
63
private let style : RichTextStyle
67
- private let buttonStyle : Style
68
64
private let value : Binding < Bool >
69
65
private let fillVertically : Bool
70
66
@@ -73,58 +69,34 @@ public extension RichTextStyle {
73
69
style. label
74
70
. labelStyle ( . iconOnly)
75
71
. frame ( maxHeight: fillVertically ? . infinity : nil )
76
- . foregroundColor ( tintColor )
72
+ . foreground ( . accentColor , if : isOn )
77
73
. contentShape ( Rectangle ( ) )
78
74
}
75
+ . buttonStyle ( . plain)
79
76
. keyboardShortcut ( for: style)
80
77
. accessibilityLabel ( style. title)
81
78
}
82
79
}
83
80
}
84
81
85
- public extension RichTextStyle . Button {
86
-
87
- struct Style {
88
-
89
- /**
90
- Create a rich text style button style.
91
-
92
- - Parameters:
93
- - inactiveColor: The color to apply when the button is inactive, by default `.primary`.
94
- - activeColor: The color to apply when the button is active, by default `.blue`.
95
- */
96
- public init (
97
- inactiveColor: Color = . primary,
98
- activeColor: Color = . blue
99
- ) {
100
- self . inactiveColor = inactiveColor
101
- self . activeColor = activeColor
82
+ extension View {
83
+
84
+ @ViewBuilder
85
+ func foreground( _ color: Color , if cond: Bool ) -> some View {
86
+ if cond {
87
+ self . foregroundStyle ( Color . accentColor)
88
+ } else {
89
+ self
102
90
}
103
-
104
- /// The color to apply when the button is inactive.
105
- public var inactiveColor : Color
106
-
107
- /// The color to apply when the button is active.
108
- public var activeColor : Color
109
91
}
110
92
}
111
93
112
- public extension RichTextStyle . Button . Style {
113
-
114
- /// The standard ``RichTextStyle/Button`` style.
115
- static var standard = RichTextStyle . Button. Style ( )
116
- }
117
-
118
94
private extension RichTextStyle . Button {
119
95
120
96
var isOn : Bool {
121
97
value. wrappedValue
122
98
}
123
99
124
- var tintColor : Color {
125
- isOn ? buttonStyle. activeColor : buttonStyle. inactiveColor
126
- }
127
-
128
100
func toggle( ) {
129
101
value. wrappedValue. toggle ( )
130
102
}
@@ -162,6 +134,7 @@ struct RichTextStyle_Button_Previews: PreviewProvider {
162
134
value: $isUnderlinedOn)
163
135
}
164
136
. padding ( )
137
+ . foregroundColor ( . red)
165
138
}
166
139
}
167
140
0 commit comments