Skip to content

Commit 287b09d

Browse files
committed
Move style views into RichTextStyle namespace
1 parent e4dae8a commit 287b09d

13 files changed

+555
-551
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This release starts moving types and views that relate to other types into the t
4040
* `RichTextComponent` deprecates the font name and size functions.
4141
* `RichTextDataFormatMenu` has been renamed to `RichTextDataFormat.Menu`.
4242
* `RichTextFont*` views have been renamed to `RichTextFont.*`.
43+
* `RichTextStyle*` views have been renamed to `RichTextStyle.*`.
4344

4445

4546

Sources/RichTextKit/Format/RichTextFormatSidebar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public struct RichTextFormatSidebar: View {
4848
SidebarSection(title: RTKL10n.font.text) {
4949
RichTextFont.Picker(selection: $context.fontName, fontSize: 12)
5050
HStack {
51-
RichTextStyleToggleGroup(context: context)
51+
RichTextStyle.ToggleGroup(context: context)
5252
RichTextFont.SizePickerStack(context: context)
5353
}
5454
}

Sources/RichTextKit/RichTextKit.docc/RichTextKit.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ RichTextKit is available under the MIT license. See the [LICENSE][License] file
161161

162162
- ``RichTextHighlightingStyle``
163163
- ``RichTextStyle``
164-
- ``RichTextStyleButton``
165-
- ``RichTextStyleToggle``
166-
- ``RichTextStyleToggleGroup``
167-
- ``RichTextStyleToggleStack``
168164

169165

170166

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
//
2+
// RichTextStyle+Button.swift
3+
// RichTextKit
4+
//
5+
// Created by Daniel Saidi on 2022-06-04.
6+
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
public extension RichTextStyle {
12+
13+
/**
14+
This button can be used to toggle a ``RichTextStyle``.
15+
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.
19+
*/
20+
struct Button: View {
21+
22+
/**
23+
Create a rich text style button.
24+
25+
- Parameters:
26+
- style: The style to toggle.
27+
- buttonStyle: The button style to use, by default `.standard`.
28+
- value: The value to bind to.
29+
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
30+
*/
31+
public init(
32+
style: RichTextStyle,
33+
buttonStyle: Style = .standard,
34+
value: Binding<Bool>,
35+
fillVertically: Bool = false
36+
) {
37+
self.style = style
38+
self.buttonStyle = buttonStyle
39+
self.value = value
40+
self.fillVertically = fillVertically
41+
}
42+
43+
/**
44+
Create a rich text style button.
45+
46+
- Parameters:
47+
- style: The style to toggle.
48+
- buttonStyle: The button style to use, by default `.standard`.
49+
- context: The context to affect.
50+
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
51+
*/
52+
public init(
53+
style: RichTextStyle,
54+
buttonStyle: Style = .standard,
55+
context: RichTextContext,
56+
fillVertically: Bool = false
57+
) {
58+
self.init(
59+
style: style,
60+
buttonStyle: buttonStyle,
61+
value: context.binding(for: style),
62+
fillVertically: fillVertically
63+
)
64+
}
65+
66+
private let style: RichTextStyle
67+
private let buttonStyle: Style
68+
private let value: Binding<Bool>
69+
private let fillVertically: Bool
70+
71+
public var body: some View {
72+
SwiftUI.Button(action: toggle) {
73+
style.label
74+
.labelStyle(.iconOnly)
75+
.frame(maxHeight: fillVertically ? .infinity : nil)
76+
.foregroundColor(tintColor)
77+
.contentShape(Rectangle())
78+
}
79+
.keyboardShortcut(for: style)
80+
.accessibilityLabel(style.title)
81+
}
82+
}
83+
}
84+
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
102+
}
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+
}
110+
}
111+
112+
public extension RichTextStyle.Button.Style {
113+
114+
/// The standard ``RichTextStyle/Button`` style.
115+
static var standard = RichTextStyle.Button.Style()
116+
}
117+
118+
private extension RichTextStyle.Button {
119+
120+
var isOn: Bool {
121+
value.wrappedValue
122+
}
123+
124+
var tintColor: Color {
125+
isOn ? buttonStyle.activeColor : buttonStyle.inactiveColor
126+
}
127+
128+
func toggle() {
129+
value.wrappedValue.toggle()
130+
}
131+
}
132+
133+
struct RichTextStyle_Button_Previews: PreviewProvider {
134+
135+
struct Preview: View {
136+
137+
@State
138+
private var isBoldOn = false
139+
140+
@State
141+
private var isItalicOn = true
142+
143+
@State
144+
private var isStrikethroughOn = false
145+
146+
@State
147+
private var isUnderlinedOn = false
148+
149+
var body: some View {
150+
HStack {
151+
RichTextStyle.Button(
152+
style: .bold,
153+
value: $isBoldOn)
154+
RichTextStyle.Button(
155+
style: .italic,
156+
value: $isItalicOn)
157+
RichTextStyle.Button(
158+
style: .strikethrough,
159+
value: $isStrikethroughOn)
160+
RichTextStyle.Button(
161+
style: .underlined,
162+
value: $isUnderlinedOn)
163+
}
164+
.padding()
165+
}
166+
}
167+
168+
static var previews: some View {
169+
Preview()
170+
}
171+
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
//
2+
// RichTextStyle+Toggle.swift
3+
// RichTextKit
4+
//
5+
// Created by Daniel Saidi on 2022-12-08.
6+
// Copyright © 2022-2023 Daniel Saidi. All rights reserved.
7+
//
8+
9+
import SwiftUI
10+
11+
public extension RichTextStyle {
12+
13+
/**
14+
This toggle can be used to toggle a ``RichTextStyle``.
15+
16+
This view renders a plain `Toggle`, which means you can
17+
use and configure with plain SwiftUI. The one exception
18+
is the tint color, which is set with a style.
19+
20+
> Note: The view uses a ``RichTextStyleButton`` if it's
21+
used on iOS 14, macOS 11, tvOS 14 and watchOS 8.
22+
*/
23+
struct Toggle: View {
24+
25+
/**
26+
Create a rich text style toggle toggle.
27+
28+
- Parameters:
29+
- style: The style to toggle.
30+
- buttonStyle: The button style to use, by default `.standard`.
31+
- value: The value to bind to.
32+
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
33+
*/
34+
public init(
35+
style: RichTextStyle,
36+
buttonStyle: Style = .standard,
37+
value: Binding<Bool>,
38+
fillVertically: Bool = false
39+
) {
40+
self.style = style
41+
self.buttonStyle = buttonStyle
42+
self.value = value
43+
self.fillVertically = fillVertically
44+
}
45+
46+
/**
47+
Create a rich text style toggle.
48+
49+
- Parameters:
50+
- style: The style to toggle.
51+
- buttonStyle: The button style to use, by default `.standard`.
52+
- context: The context to affect.
53+
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
54+
*/
55+
public init(
56+
style: RichTextStyle,
57+
buttonStyle: Style = .standard,
58+
context: RichTextContext,
59+
fillVertically: Bool = false
60+
) {
61+
self.init(
62+
style: style,
63+
buttonStyle: buttonStyle,
64+
value: context.binding(for: style),
65+
fillVertically: fillVertically
66+
)
67+
}
68+
69+
private let style: RichTextStyle
70+
private let buttonStyle: Style
71+
private let value: Binding<Bool>
72+
private let fillVertically: Bool
73+
74+
public var body: some View {
75+
#if os(tvOS) || os(watchOS)
76+
toggle
77+
#else
78+
toggle.toggleStyle(.button)
79+
#endif
80+
}
81+
82+
private var toggle: some View {
83+
SwiftUI.Toggle(isOn: value) {
84+
style.icon
85+
.frame(maxHeight: fillVertically ? .infinity : nil)
86+
}
87+
.tint(tintColor)
88+
.keyboardShortcut(for: style)
89+
.accessibilityLabel(style.title)
90+
}
91+
}
92+
}
93+
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+
134+
private extension RichTextStyle.Toggle {
135+
136+
var isOn: Bool {
137+
value.wrappedValue
138+
}
139+
140+
var tintColor: Color? {
141+
isOn ? buttonStyle.activeColor : buttonStyle.inactiveColor
142+
}
143+
}
144+
145+
struct RichTextStyle_Toggle_Previews: PreviewProvider {
146+
147+
struct Preview: View {
148+
149+
@State
150+
private var isBoldOn = false
151+
152+
@State
153+
private var isItalicOn = true
154+
155+
@State
156+
private var isStrikethroughOn = true
157+
158+
@State
159+
private var isUnderlinedOn = true
160+
161+
var body: some View {
162+
HStack {
163+
RichTextStyle.Toggle(
164+
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)
175+
}
176+
.padding()
177+
}
178+
}
179+
180+
static var previews: some View {
181+
Preview()
182+
}
183+
}

0 commit comments

Comments
 (0)