Skip to content

Commit cf79c4e

Browse files
Solve issues with button colors
1 parent 3dbad7a commit cf79c4e

File tree

6 files changed

+197
-4
lines changed

6 files changed

+197
-4
lines changed

Demo/macOS/EditorScreen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private extension EditorScreen {
5959
var toolbar: some View {
6060
RichTextFormatSidebar(
6161
context: context,
62-
colorPickers: .all
62+
colorPickers: .allRelevant
6363
)
6464
.frame(width: 250)
6565
}

Sources/RichTextKit/Alignment/RichTextAlignment+Picker.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public extension RichTextAlignment {
1717
be styled and configured with plain SwiftUI.
1818
*/
1919
struct Picker: View {
20-
20+
@Environment(\.colorScheme) var colorScheme
2121
/**
2222
Create a rich text alignment picker.
2323

@@ -44,6 +44,7 @@ public extension RichTextAlignment {
4444
value.label
4545
.labelStyle(.iconOnly)
4646
}
47+
4748
}
4849
.labelsHidden()
4950
.accessibilityLabel(RTKL10n.textAlignment.text)

Sources/RichTextKit/Colors/RichTextColor.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ public extension RichTextColor {
8787
}
8888

8989
public extension Collection where Element == RichTextColor {
90-
90+
static var allRelevant: [RichTextColor] { Element.allCases - .undefined }
91+
9192
static var all: [RichTextColor] { Element.allCases }
9293
}
94+
95+
extension Array where Element == RichTextColor {
96+
static func - (lhs: [RichTextColor], rhs: RichTextColor) -> [RichTextColor] {
97+
return lhs.filter { $0 != rhs }
98+
}
99+
}
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
//
2+
// RichTextStyleButton.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+
/**
12+
This button can be used to toggle a ``RichTextStyle`` value.
13+
14+
This view renders a plain `Button`, which means you can use
15+
and configure it in all ways supported by SwiftUI. The only
16+
exception is the content color, which is set by a style you
17+
can provide in the initializer.
18+
19+
If you want a more prominent button, you may consider using
20+
a ``RichTextStyleToggle`` instead, but it requires a higher
21+
deployment target.
22+
*/
23+
public struct RichTextStyleButton: View {
24+
25+
/**
26+
Create a rich text style button.
27+
28+
- Parameters:
29+
- style: The style to toggle.
30+
- buttonStyle: The button style to use, by default ``RichTextStyleButton/Style/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 button.
48+
49+
- Parameters:
50+
- style: The style to toggle.
51+
- buttonStyle: The button style to use, by default ``RichTextStyleButton/Style/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+
Button(action: toggle) {
76+
style.icon
77+
.frame(maxHeight: fillVertically ? .infinity : nil)
78+
.foregroundColor(tintColor)
79+
.contentShape(Rectangle())
80+
}
81+
.tint(tintColor)
82+
.keyboardShortcut(for: style)
83+
.accessibilityLabel(style.title)
84+
}
85+
}
86+
87+
public extension RichTextStyleButton {
88+
89+
/**
90+
This style can be used to style a ``RichTextStyleButton``.
91+
*/
92+
struct Style {
93+
94+
/**
95+
Create a rich text style button style.
96+
97+
- Parameters:
98+
- inactiveColor: The color to apply when the button is inactive, by default `.primary`.
99+
- activeColor: The color to apply when the button is active, by default `.blue`.
100+
*/
101+
public init(
102+
inactiveColor: Color? = nil,
103+
activeColor: Color = .blue
104+
) {
105+
self.inactiveColor = inactiveColor
106+
self.activeColor = activeColor
107+
}
108+
109+
/// The color to apply when the button is inactive.
110+
public var inactiveColor: Color?
111+
112+
/// The color to apply when the button is active.
113+
public var activeColor: Color
114+
}
115+
}
116+
117+
public extension RichTextStyleButton.Style {
118+
119+
/**
120+
The standard ``RichTextStyleButton`` style.
121+
*/
122+
static var standard = RichTextStyleButton.Style()
123+
}
124+
125+
private extension RichTextStyleButton {
126+
127+
var isOn: Bool {
128+
value.wrappedValue
129+
}
130+
131+
var tintColor: Color? {
132+
isOn ? buttonStyle.activeColor : buttonStyle.inactiveColor
133+
}
134+
135+
func toggle() {
136+
value.wrappedValue.toggle()
137+
}
138+
}
139+
140+
struct RichTextStyleButton_Previews: PreviewProvider {
141+
142+
struct Preview: View {
143+
144+
@State
145+
private var isBoldOn = false
146+
147+
@State
148+
private var isItalicOn = true
149+
150+
@State
151+
private var isStrikethroughOn = false
152+
153+
@State
154+
private var isUnderlinedOn = false
155+
156+
var body: some View {
157+
HStack {
158+
RichTextStyleButton(
159+
style: .bold,
160+
value: $isBoldOn)
161+
RichTextStyleButton(
162+
style: .italic,
163+
value: $isItalicOn)
164+
RichTextStyleButton(
165+
style: .strikethrough,
166+
value: $isStrikethroughOn)
167+
RichTextStyleButton(
168+
style: .underlined,
169+
value: $isUnderlinedOn)
170+
}.padding()
171+
}
172+
}
173+
174+
static var previews: some View {
175+
Preview()
176+
}
177+
}

Sources/RichTextKit/_Deprecated/RichTextAlignment+Deprecated.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,12 @@ import SwiftUI
22

33
@available(*, deprecated, renamed: "RichTextAlignment.Picker")
44
public typealias RichTextAlignmentPicker = RichTextAlignment.Picker
5+
6+
public extension RichTextAlignment.Picker.Style {
7+
8+
@available(*, deprecated, renamed: "init(lightIconColor:darkIconColor:)")
9+
init(iconColor: Color = .primary) {
10+
self.lightIconColor = iconColor
11+
self.darkIconColor = iconColor
12+
}
13+
}

Sources/RichTextKit/_Deprecated/RichTextViewComponent+Deprecated.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ public extension RichTextViewComponent {
135135
setRichTextAttributes(attributes)
136136
}
137137

138-
139138
@available(*, deprecated, message: "richTextAlignment")
140139
var currentTextAlignment: RichTextAlignment? {
141140
richTextAlignment

0 commit comments

Comments
 (0)