Skip to content

Commit

Permalink
Make style button not use a button style
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Jan 23, 2024
1 parent 287b09d commit bd4374f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 45 deletions.
3 changes: 2 additions & 1 deletion RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ This release starts moving types and views that relate to other types into the t
* Many value types implement `RichTextLabelValue` to get a `label` property.
* All types that implement `RichTextLabelValue` get a `label` that has improved accessibility.

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

### 🐛 Bug Fixes

Expand Down
2 changes: 1 addition & 1 deletion Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private extension RichTextKeyboardToolbar {
.contentShape(Rectangle())
}

RichTextStyleToggleStack(context: context)
RichTextStyle.ToggleStack(context: context)
.keyboardShortcutsOnly(if: isCompact)

RichTextFont.SizePickerStack(context: context)
Expand Down
59 changes: 16 additions & 43 deletions Sources/RichTextKit/Styles/RichTextStyle+Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ public extension RichTextStyle {
This button can be used to toggle a ``RichTextStyle``.

This view renders a plain `Button`, which means you can
use and configure with plain SwiftUI. The one exception
is the content color, which is set with a style.
use and configure with plain SwiftUI.

You can apply a `foregroundStyle` to the view to define
the color to use when the style is not active. The view
will `.accentColor` when the style is active.
*/
struct Button: View {

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

- Parameters:
- style: The style to toggle.
- buttonStyle: The button style to use, by default `.standard`.
- value: The value to bind to.
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
*/
public init(
style: RichTextStyle,
buttonStyle: Style = .standard,
value: Binding<Bool>,
fillVertically: Bool = false
) {
self.style = style
self.buttonStyle = buttonStyle
self.value = value
self.fillVertically = fillVertically
}
Expand All @@ -45,26 +45,22 @@ public extension RichTextStyle {

- Parameters:
- style: The style to toggle.
- buttonStyle: The button style to use, by default `.standard`.
- context: The context to affect.
- fillVertically: Whether or not fill up vertical space in a non-greedy way, by default `false`.
*/
public init(
style: RichTextStyle,
buttonStyle: Style = .standard,
context: RichTextContext,
fillVertically: Bool = false
) {
self.init(
style: style,
buttonStyle: buttonStyle,
value: context.binding(for: style),
fillVertically: fillVertically
)
}

private let style: RichTextStyle
private let buttonStyle: Style
private let value: Binding<Bool>
private let fillVertically: Bool

Expand All @@ -73,58 +69,34 @@ public extension RichTextStyle {
style.label
.labelStyle(.iconOnly)
.frame(maxHeight: fillVertically ? .infinity : nil)
.foregroundColor(tintColor)
.foreground(.accentColor, if: isOn)
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.keyboardShortcut(for: style)
.accessibilityLabel(style.title)
}
}
}

public extension RichTextStyle.Button {

struct Style {

/**
Create a rich text style button style.

- Parameters:
- inactiveColor: The color to apply when the button is inactive, by default `.primary`.
- activeColor: The color to apply when the button is active, by default `.blue`.
*/
public init(
inactiveColor: Color = .primary,
activeColor: Color = .blue
) {
self.inactiveColor = inactiveColor
self.activeColor = activeColor
extension View {

@ViewBuilder
func foreground(_ color: Color, if cond: Bool) -> some View {
if cond {
self.foregroundStyle(Color.accentColor)
} else {
self
}

/// The color to apply when the button is inactive.
public var inactiveColor: Color

/// The color to apply when the button is active.
public var activeColor: Color
}
}

public extension RichTextStyle.Button.Style {

/// The standard ``RichTextStyle/Button`` style.
static var standard = RichTextStyle.Button.Style()
}

private extension RichTextStyle.Button {

var isOn: Bool {
value.wrappedValue
}

var tintColor: Color {
isOn ? buttonStyle.activeColor : buttonStyle.inactiveColor
}

func toggle() {
value.wrappedValue.toggle()
}
Expand Down Expand Up @@ -162,6 +134,7 @@ struct RichTextStyle_Button_Previews: PreviewProvider {
value: $isUnderlinedOn)
}
.padding()
.foregroundColor(.red)
}
}

Expand Down
56 changes: 56 additions & 0 deletions Sources/RichTextKit/_Deprecated/RichTextStyle+Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,59 @@ public typealias RichTextStyleToggleGroup = RichTextStyle.ToggleGroup

@available(*, deprecated, renamed: "RichTextStyle.ToggleStack")
public typealias RichTextStyleToggleStack = RichTextStyle.ToggleStack

public extension RichTextStyle.Button {

@available(*, deprecated, message: "Use foregroundStyle and accentColor instead of style.")
init(
style: RichTextStyle,
buttonStyle: Style = .standard,
value: Binding<Bool>,
fillVertically: Bool = false
) {
self.init(
style: style,
value: value,
fillVertically: fillVertically
)
}

@available(*, deprecated, message: "Use foregroundStyle and accentColor instead of style.")
init(
style: RichTextStyle,
buttonStyle: Style = .standard,
context: RichTextContext,
fillVertically: Bool = false
) {
self.init(
style: style,
context: context,
fillVertically: fillVertically
)
}
}

public extension RichTextStyle.Button {

@available(*, deprecated, message: "Use foregroundStyle and accentColor instead of style.")
struct Style {

public init(
inactiveColor: Color = .primary,
activeColor: Color = .blue
) {
self.inactiveColor = inactiveColor
self.activeColor = activeColor
}

public var inactiveColor: Color

public var activeColor: Color
}
}

@available(*, deprecated, message: "Use foregroundStyle and accentColor instead of style.")
public extension RichTextStyle.Button.Style {

static var standard = RichTextStyle.Button.Style()
}

0 comments on commit bd4374f

Please sign in to comment.