Skip to content

Commit bd4374f

Browse files
committed
Make style button not use a button style
1 parent 287b09d commit bd4374f

File tree

4 files changed

+75
-45
lines changed

4 files changed

+75
-45
lines changed

RELEASE_NOTES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ This release starts moving types and views that relate to other types into the t
2222
* Many value types implement `RichTextLabelValue` to get a `label` property.
2323
* All types that implement `RichTextLabelValue` get a `label` that has improved accessibility.
2424

25-
* `RichTextColor` `.adjust` now takes an optional color.
25+
* `RichTextColor` `.adjust` now takes an optional color.
26+
* `RichTextStyle.Button` no longer uses a style - use `foregroundStyle` and `.accentColor` instead.
2627

2728
### 🐛 Bug Fixes
2829

Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ private extension RichTextKeyboardToolbar {
190190
.contentShape(Rectangle())
191191
}
192192

193-
RichTextStyleToggleStack(context: context)
193+
RichTextStyle.ToggleStack(context: context)
194194
.keyboardShortcutsOnly(if: isCompact)
195195

196196
RichTextFont.SizePickerStack(context: context)

Sources/RichTextKit/Styles/RichTextStyle+Button.swift

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ public extension RichTextStyle {
1414
This button can be used to toggle a ``RichTextStyle``.
1515

1616
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.
1922
*/
2023
struct Button: View {
2124

@@ -24,18 +27,15 @@ public extension RichTextStyle {
2427

2528
- Parameters:
2629
- style: The style to toggle.
27-
- buttonStyle: The button style to use, by default `.standard`.
2830
- value: The value to bind to.
2931
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
3032
*/
3133
public init(
3234
style: RichTextStyle,
33-
buttonStyle: Style = .standard,
3435
value: Binding<Bool>,
3536
fillVertically: Bool = false
3637
) {
3738
self.style = style
38-
self.buttonStyle = buttonStyle
3939
self.value = value
4040
self.fillVertically = fillVertically
4141
}
@@ -45,26 +45,22 @@ public extension RichTextStyle {
4545

4646
- Parameters:
4747
- style: The style to toggle.
48-
- buttonStyle: The button style to use, by default `.standard`.
4948
- context: The context to affect.
5049
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
5150
*/
5251
public init(
5352
style: RichTextStyle,
54-
buttonStyle: Style = .standard,
5553
context: RichTextContext,
5654
fillVertically: Bool = false
5755
) {
5856
self.init(
5957
style: style,
60-
buttonStyle: buttonStyle,
6158
value: context.binding(for: style),
6259
fillVertically: fillVertically
6360
)
6461
}
6562

6663
private let style: RichTextStyle
67-
private let buttonStyle: Style
6864
private let value: Binding<Bool>
6965
private let fillVertically: Bool
7066

@@ -73,58 +69,34 @@ public extension RichTextStyle {
7369
style.label
7470
.labelStyle(.iconOnly)
7571
.frame(maxHeight: fillVertically ? .infinity : nil)
76-
.foregroundColor(tintColor)
72+
.foreground(.accentColor, if: isOn)
7773
.contentShape(Rectangle())
7874
}
75+
.buttonStyle(.plain)
7976
.keyboardShortcut(for: style)
8077
.accessibilityLabel(style.title)
8178
}
8279
}
8380
}
8481

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
10290
}
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
10991
}
11092
}
11193

112-
public extension RichTextStyle.Button.Style {
113-
114-
/// The standard ``RichTextStyle/Button`` style.
115-
static var standard = RichTextStyle.Button.Style()
116-
}
117-
11894
private extension RichTextStyle.Button {
11995

12096
var isOn: Bool {
12197
value.wrappedValue
12298
}
12399

124-
var tintColor: Color {
125-
isOn ? buttonStyle.activeColor : buttonStyle.inactiveColor
126-
}
127-
128100
func toggle() {
129101
value.wrappedValue.toggle()
130102
}
@@ -162,6 +134,7 @@ struct RichTextStyle_Button_Previews: PreviewProvider {
162134
value: $isUnderlinedOn)
163135
}
164136
.padding()
137+
.foregroundColor(.red)
165138
}
166139
}
167140

Sources/RichTextKit/_Deprecated/RichTextStyle+Deprecated.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,59 @@ public typealias RichTextStyleToggleGroup = RichTextStyle.ToggleGroup
1111

1212
@available(*, deprecated, renamed: "RichTextStyle.ToggleStack")
1313
public typealias RichTextStyleToggleStack = RichTextStyle.ToggleStack
14+
15+
public extension RichTextStyle.Button {
16+
17+
@available(*, deprecated, message: "Use foregroundStyle and accentColor instead of style.")
18+
init(
19+
style: RichTextStyle,
20+
buttonStyle: Style = .standard,
21+
value: Binding<Bool>,
22+
fillVertically: Bool = false
23+
) {
24+
self.init(
25+
style: style,
26+
value: value,
27+
fillVertically: fillVertically
28+
)
29+
}
30+
31+
@available(*, deprecated, message: "Use foregroundStyle and accentColor instead of style.")
32+
init(
33+
style: RichTextStyle,
34+
buttonStyle: Style = .standard,
35+
context: RichTextContext,
36+
fillVertically: Bool = false
37+
) {
38+
self.init(
39+
style: style,
40+
context: context,
41+
fillVertically: fillVertically
42+
)
43+
}
44+
}
45+
46+
public extension RichTextStyle.Button {
47+
48+
@available(*, deprecated, message: "Use foregroundStyle and accentColor instead of style.")
49+
struct Style {
50+
51+
public init(
52+
inactiveColor: Color = .primary,
53+
activeColor: Color = .blue
54+
) {
55+
self.inactiveColor = inactiveColor
56+
self.activeColor = activeColor
57+
}
58+
59+
public var inactiveColor: Color
60+
61+
public var activeColor: Color
62+
}
63+
}
64+
65+
@available(*, deprecated, message: "Use foregroundStyle and accentColor instead of style.")
66+
public extension RichTextStyle.Button.Style {
67+
68+
static var standard = RichTextStyle.Button.Style()
69+
}

0 commit comments

Comments
 (0)