Skip to content

Commit 11e6ba5

Browse files
committed
Make style views not use a button style
1 parent bd4374f commit 11e6ba5

File tree

7 files changed

+128
-79
lines changed

7 files changed

+128
-79
lines changed

RELEASE_NOTES.md

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

2525
* `RichTextColor` `.adjust` now takes an optional color.
26-
* `RichTextStyle.Button` no longer uses a style - use `foregroundStyle` and `.accentColor` instead.
26+
* `RichTextFormatSheet` no longer hard-codes an accent color.
27+
* `RichTextStyle` views no longer use a style - use `foregroundStyle`, `tint` and `accentColor` instead.
2728

2829
### 🐛 Bug Fixes
2930

Sources/RichTextKit/Format/RichTextFormatSheet.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public struct RichTextFormatSheet: View {
7373
}
7474
.padding(.vertical, padding)
7575
.environment(\.sizeCategory, .medium)
76-
.accentColor(.primary)
7776
.background(background)
7877
}
7978
.withAutomaticToolbarRole()
@@ -134,7 +133,7 @@ private extension RichTextFormatSheet {
134133

135134
@ViewBuilder
136135
var styleButtons: some View {
137-
RichTextStyleToggleGroup(
136+
RichTextStyle.ToggleGroup(
138137
context: context
139138
)
140139
}

Sources/RichTextKit/Format/RichTextFormatSidebar.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public struct RichTextFormatSidebar: View {
7979
Spacer()
8080
}
8181
.padding(8)
82-
.prefersFocusable()
8382
.background(Color.white.opacity(0.05))
8483
}
8584
}

Sources/RichTextKit/Styles/RichTextStyle+Toggle.swift

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,15 @@ public extension RichTextStyle {
2727

2828
- Parameters:
2929
- style: The style to toggle.
30-
- buttonStyle: The button style to use, by default `.standard`.
3130
- value: The value to bind to.
3231
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
3332
*/
3433
public init(
3534
style: RichTextStyle,
36-
buttonStyle: Style = .standard,
3735
value: Binding<Bool>,
3836
fillVertically: Bool = false
3937
) {
4038
self.style = style
41-
self.buttonStyle = buttonStyle
4239
self.value = value
4340
self.fillVertically = fillVertically
4441
}
@@ -48,26 +45,22 @@ public extension RichTextStyle {
4845

4946
- Parameters:
5047
- style: The style to toggle.
51-
- buttonStyle: The button style to use, by default `.standard`.
5248
- context: The context to affect.
5349
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
5450
*/
5551
public init(
5652
style: RichTextStyle,
57-
buttonStyle: Style = .standard,
5853
context: RichTextContext,
5954
fillVertically: Bool = false
6055
) {
6156
self.init(
6257
style: style,
63-
buttonStyle: buttonStyle,
6458
value: context.binding(for: style),
6559
fillVertically: fillVertically
6660
)
6761
}
6862

6963
private let style: RichTextStyle
70-
private let buttonStyle: Style
7164
private let value: Binding<Bool>
7265
private let fillVertically: Bool
7366

@@ -84,62 +77,17 @@ public extension RichTextStyle {
8477
style.icon
8578
.frame(maxHeight: fillVertically ? .infinity : nil)
8679
}
87-
.tint(tintColor)
8880
.keyboardShortcut(for: style)
8981
.accessibilityLabel(style.title)
9082
}
9183
}
9284
}
9385

94-
private extension RichTextStyle.Toggle {
95-
96-
var backgroundColor: Color {
97-
value.wrappedValue ? buttonStyle.activeColor.opacity(0.2) : .clear
98-
}
99-
}
100-
101-
public extension RichTextStyle.Toggle {
102-
103-
struct Style {
104-
105-
/**
106-
Create a rich text style button style.
107-
108-
- Parameters:
109-
- inactiveColor: The color to apply when the button is inactive, by default `nil`.
110-
- activeColor: The color to apply when the button is active, by default `.blue`.
111-
*/
112-
public init(
113-
inactiveColor: Color? = nil,
114-
activeColor: Color = .blue
115-
) {
116-
self.inactiveColor = inactiveColor
117-
self.activeColor = activeColor
118-
}
119-
120-
/// The color to apply when the button is inactive.
121-
public var inactiveColor: Color?
122-
123-
/// The color to apply when the button is active.
124-
public var activeColor: Color
125-
}
126-
}
127-
128-
public extension RichTextStyle.Toggle.Style {
129-
130-
/// The standard ``RichTextStyle/Toggle`` style.
131-
static var standard = RichTextStyle.Toggle.Style()
132-
}
133-
13486
private extension RichTextStyle.Toggle {
13587

13688
var isOn: Bool {
13789
value.wrappedValue
13890
}
139-
140-
var tintColor: Color? {
141-
isOn ? buttonStyle.activeColor : buttonStyle.inactiveColor
142-
}
14391
}
14492

14593
struct RichTextStyle_Toggle_Previews: PreviewProvider {
@@ -157,24 +105,34 @@ struct RichTextStyle_Toggle_Previews: PreviewProvider {
157105

158106
@State
159107
private var isUnderlinedOn = true
108+
109+
@StateObject
110+
private var context = RichTextContext()
160111

161112
var body: some View {
162113
HStack {
114+
toggle(for: .bold, $isBoldOn)
115+
toggle(for: .italic, $isItalicOn)
116+
toggle(for: .strikethrough, $isStrikethroughOn)
117+
toggle(for: .underlined, $isUnderlinedOn)
118+
Divider()
163119
RichTextStyle.Toggle(
164120
style: .bold,
165-
value: $isBoldOn)
166-
RichTextStyle.Toggle(
167-
style: .italic,
168-
value: $isItalicOn)
169-
RichTextStyle.Toggle(
170-
style: .strikethrough,
171-
value: $isStrikethroughOn)
172-
RichTextStyle.Toggle(
173-
style: .underlined,
174-
value: $isUnderlinedOn)
121+
context: context,
122+
fillVertically: true
123+
)
175124
}
125+
.fixedSize(horizontal: false, vertical: true)
176126
.padding()
177127
}
128+
129+
func toggle(for style: RichTextStyle, _ binding: Binding<Bool>) -> some View {
130+
RichTextStyle.Toggle(
131+
style: style,
132+
value: binding,
133+
fillVertically: true
134+
)
135+
}
178136
}
179137

180138
static var previews: some View {

Sources/RichTextKit/Styles/RichTextStyle+ToggleGroup.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public extension RichTextStyle {
1717

1818
Since this view uses multiple styles, it binds directly
1919
to a ``RichTextContext`` instead of individual values.
20+
21+
> Important: Since the `ControlGroup` doesn't highlight
22+
active buttons in iOS, the view will use a `ToggleStack`
23+
on iOS.
2024
*/
2125
struct ToggleGroup: View {
2226

@@ -27,23 +31,19 @@ public extension RichTextStyle {
2731
- context: The context to affect.
2832
- styles: The styles to list, by default ``RichTextStyle/all``.
2933
- greedy: Whether or not the group is horizontally greedy, by default `true`.
30-
- buttonStyle: The button style to use, by default ``RichTextStyleToggle/Style/standard``.
3134
*/
3235
public init(
3336
context: RichTextContext,
3437
styles: [RichTextStyle] = .all,
35-
greedy: Bool = true,
36-
buttonStyle: RichTextStyle.Toggle.Style = .standard
38+
greedy: Bool = true
3739
) {
3840
self._context = ObservedObject(wrappedValue: context)
3941
self.isGreedy = greedy
4042
self.styles = styles
41-
self.buttonStyle = buttonStyle
4243
}
4344

4445
private let styles: [RichTextStyle]
4546
private let isGreedy: Bool
46-
private let buttonStyle: RichTextStyle.Toggle.Style
4747

4848
private var groupWidth: CGFloat? {
4949
if isGreedy { return nil }
@@ -59,17 +59,23 @@ public extension RichTextStyle {
5959
private var context: RichTextContext
6060

6161
public var body: some View {
62+
#if os(macOS)
6263
ControlGroup {
6364
ForEach(styles) {
6465
RichTextStyle.Toggle(
6566
style: $0,
66-
buttonStyle: buttonStyle,
6767
context: context,
6868
fillVertically: true
6969
)
7070
}
7171
}
7272
.frame(width: groupWidth)
73+
#else
74+
RichTextStyle.ToggleStack(
75+
context: context,
76+
styles: styles
77+
)
78+
#endif
7379
}
7480
}
7581
}

Sources/RichTextKit/Styles/RichTextStyle+ToggleStack.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// RichTextStyleToggleStack.swift
2+
// RichTextStyle+ToggleStack.swift
33
// RichTextKit
44
//
55
// Created by Daniel Saidi on 2022-12-08.
@@ -25,23 +25,19 @@ public extension RichTextStyle {
2525
- Parameters:
2626
- context: The context to affect.
2727
- styles: The styles to list, by default ``RichTextStyle/all``.
28-
- buttonStyle: The button style to use, by default `.standard`.
2928
- spacing: The spacing to apply to stack items, by default `5`.
3029
*/
3130
public init(
3231
context: RichTextContext,
3332
styles: [RichTextStyle] = .all,
34-
buttonStyle: RichTextStyle.Toggle.Style = .standard,
3533
spacing: Double = 5
3634
) {
3735
self._context = ObservedObject(wrappedValue: context)
3836
self.styles = styles
39-
self.buttonStyle = buttonStyle
4037
self.spacing = spacing
4138
}
4239

4340
private let styles: [RichTextStyle]
44-
private let buttonStyle: RichTextStyle.Toggle.Style
4541
private let spacing: Double
4642

4743
@ObservedObject
@@ -52,12 +48,12 @@ public extension RichTextStyle {
5248
ForEach(styles) {
5349
RichTextStyle.Toggle(
5450
style: $0,
55-
buttonStyle: buttonStyle,
5651
context: context,
5752
fillVertically: true
5853
)
5954
}
60-
}.fixedSize(horizontal: false, vertical: true)
55+
}
56+
.fixedSize(horizontal: false, vertical: true)
6157
}
6258
}
6359
}

0 commit comments

Comments
 (0)