Skip to content

Commit

Permalink
Merge pull request #140 from r-jarvis/feature/persist-toolbar-display
Browse files Browse the repository at this point in the history
Feature/persist toolbar display
  • Loading branch information
danielsaidi authored Feb 13, 2024
2 parents 92bd262 + 8480032 commit 2de9954
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions Sources/RichTextKit/Keyboard/RichTextKeyboardToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
- Parameters:
- context: The context to affect.
- style: The toolbar style to apply, by default ``RichTextKeyboardToolbarStyle/standard``.
- configuration: The toolbar configuration to apply, by default ``RichTextKeyboardToolbarConfiguration/standard``.
- leadingActions: The leading actions, by default `.undo`, `.redo` and `.copy`.
- trailingActions: The trailing actions, by default `.dismissKeyboard`.
- leadingButtons: The leading buttons to place after the leading actions.
Expand All @@ -52,6 +53,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
public init(
context: RichTextContext,
style: RichTextKeyboardToolbarStyle = .standard,
configuration: RichTextKeyboardToolbarConfiguration = .standard,
leadingActions: [RichTextAction] = [.undo, .redo, .copy],
trailingActions: [RichTextAction] = [.dismissKeyboard],
@ViewBuilder leadingButtons: @escaping () -> LeadingButtons,
Expand All @@ -62,6 +64,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
self.leadingActions = leadingActions
self.trailingActions = trailingActions
self.style = style
self.configuration = configuration
self.leadingButtons = leadingButtons
self.trailingButtons = trailingButtons
self.richTextFormatSheet = richTextFormatSheet
Expand All @@ -73,6 +76,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
- Parameters:
- context: The context to affect.
- style: The toolbar style to apply, by default ``RichTextKeyboardToolbarStyle/standard``.
- configuration: The toolbar configuration to apply, by default ``RichTextKeyboardToolbarConfiguration/standard``.
- leadingActions: The leading actions, by default `.undo`, `.redo` and `.copy`.
- trailingActions: The trailing actions, by default `.dismissKeyboard`.
- leadingButtons: The leading buttons to place after the leading actions.
Expand All @@ -82,6 +86,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
public init(
context: RichTextContext,
style: RichTextKeyboardToolbarStyle = .standard,
configuration: RichTextKeyboardToolbarConfiguration = .standard,
leadingActions: [RichTextAction] = [.undo, .redo, .copy],
trailingActions: [RichTextAction] = [.dismissKeyboard],
@ViewBuilder leadingButtons: @escaping () -> LeadingButtons,
Expand All @@ -90,6 +95,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
self.init(
context: context,
style: style,
configuration: configuration,
leadingActions: leadingActions,
trailingActions: trailingActions,
leadingButtons: leadingButtons,
Expand All @@ -101,6 +107,7 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
private let leadingActions: [RichTextAction]
private let trailingActions: [RichTextAction]
private let style: RichTextKeyboardToolbarStyle
private let configuration: RichTextKeyboardToolbarConfiguration

private let leadingButtons: () -> LeadingButtons
private let trailingButtons: () -> TrailingButtons
Expand Down Expand Up @@ -133,9 +140,9 @@ public struct RichTextKeyboardToolbar<LeadingButtons: View, TrailingButtons: Vie
.overlay(Color.white.opacity(0.2))
.shadow(color: style.shadowColor, radius: style.shadowRadius, x: 0, y: 0)
)
.opacity(context.isEditingText ? 1 : 0)
.offset(y: context.isEditingText ? 0 : style.toolbarHeight)
.frame(height: context.isEditingText ? nil : 0)
.opacity(shouldDisplayToolbar ? 1 : 0)
.offset(y: shouldDisplayToolbar ? 0 : style.toolbarHeight)
.frame(height: shouldDisplayToolbar ? nil : 0)
.sheet(isPresented: $isFormatSheetPresented) {
richTextFormatSheet(
RichTextFormatSheet(context: context)
Expand Down Expand Up @@ -167,6 +174,7 @@ private extension RichTextKeyboardToolbar {
var isCompact: Bool { horizontalSizeClass == .compact }
}


private extension RichTextKeyboardToolbar {

var divider: some View {
Expand Down Expand Up @@ -214,6 +222,11 @@ private extension RichTextKeyboardToolbar {
}
}

private extension RichTextKeyboardToolbar {

var shouldDisplayToolbar: Bool { context.isEditingText || configuration.alwaysDisplayToolbar }
}

private extension View {

@ViewBuilder
Expand All @@ -240,6 +253,7 @@ public struct RichTextKeyboardToolbarStyle {
- itemSpacing: The spacing between toolbar items, by default `15`.
- shadowColor: The toolbar's shadow color, by default transparent black.
- shadowRadius: The toolbar's shadow radius, by default `3`.
*/
public init(
toolbarHeight: Double = 50,
Expand Down Expand Up @@ -272,6 +286,33 @@ public extension RichTextKeyboardToolbarStyle {
static var standard = RichTextKeyboardToolbarStyle()
}


/**
This can be used to configure a ``RichTextKeyboardToolbar``.
*/
public struct RichTextKeyboardToolbarConfiguration {
/**
Create a custom toolbar configuration.
- Parameters:
- alwaysDisplayToolbar: Should toolbar always be displayed, by default is `false`.
*/
public init(
alwaysDisplayToolbar: Bool = false
) {
self.alwaysDisplayToolbar = alwaysDisplayToolbar
}

/// Should the toolbar always be displayed.
public var alwaysDisplayToolbar: Bool
}

public extension RichTextKeyboardToolbarConfiguration {

/// This standard configuration is used by default.
static var standard = RichTextKeyboardToolbarConfiguration()
}

private extension RichTextKeyboardToolbar {

func presentFormatSheet() {
Expand Down

0 comments on commit 2de9954

Please sign in to comment.