|
| 1 | +import SwiftCrossUI |
| 2 | +import UIKit |
| 3 | + |
| 4 | +/// An item which can be displayed in a keyboard toolbar. Implementers of this do not have |
| 5 | +/// to implement ``SwiftCrossUI/View``. |
| 6 | +/// |
| 7 | +/// Toolbar items are expected to be "stateless". Mutations of `@State` properties of toolbar |
| 8 | +/// items will not cause the toolbar to be updated. The toolbar is only updated when the view |
| 9 | +/// containing the ``View/keyboardToolbar(animateChanges:body:)`` modifier is updated, so any |
| 10 | +/// state necessary for the toolbar should live in the view itself. |
| 11 | +public protocol ToolbarItem { |
| 12 | + /// Convert the item to a `UIBarButtonItem`, which will be placed in the keyboard toolbar. |
| 13 | + func asBarButtonItem() -> UIBarButtonItem |
| 14 | +} |
| 15 | + |
| 16 | +@resultBuilder |
| 17 | +public enum ToolbarBuilder { |
| 18 | + public typealias Component = [any ToolbarItem] |
| 19 | + |
| 20 | + public static func buildExpression(_ expression: some ToolbarItem) -> Component { |
| 21 | + [expression] |
| 22 | + } |
| 23 | + |
| 24 | + public static func buildExpression(_ expression: any ToolbarItem) -> Component { |
| 25 | + [expression] |
| 26 | + } |
| 27 | + |
| 28 | + public static func buildBlock(_ components: Component...) -> Component { |
| 29 | + components.flatMap { $0 } |
| 30 | + } |
| 31 | + |
| 32 | + public static func buildArray(_ components: [Component]) -> Component { |
| 33 | + components.flatMap { $0 } |
| 34 | + } |
| 35 | + |
| 36 | + public static func buildOptional(_ component: Component?) -> Component { |
| 37 | + component ?? [] |
| 38 | + } |
| 39 | + |
| 40 | + public static func buildEither(first component: Component) -> Component { |
| 41 | + component |
| 42 | + } |
| 43 | + |
| 44 | + public static func buildEither(second component: Component) -> Component { |
| 45 | + component |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +final class CallbackBarButtonItem: UIBarButtonItem { |
| 50 | + private var callback: () -> Void |
| 51 | + |
| 52 | + init(title: String, callback: @escaping () -> Void) { |
| 53 | + self.callback = callback |
| 54 | + super.init() |
| 55 | + |
| 56 | + self.title = title |
| 57 | + self.target = self |
| 58 | + self.action = #selector(onTap) |
| 59 | + } |
| 60 | + |
| 61 | + @available(*, unavailable) |
| 62 | + required init?(coder: NSCoder) { |
| 63 | + fatalError("init(coder:) is not used for this item") |
| 64 | + } |
| 65 | + |
| 66 | + @objc |
| 67 | + func onTap() { |
| 68 | + callback() |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +extension Button: ToolbarItem { |
| 73 | + public func asBarButtonItem() -> UIBarButtonItem { |
| 74 | + CallbackBarButtonItem(title: label, callback: action) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +@available(iOS 14, macCatalyst 14, tvOS 14, *) |
| 79 | +extension Spacer: ToolbarItem { |
| 80 | + public func asBarButtonItem() -> UIBarButtonItem { |
| 81 | + if let minLength, minLength > 0 { |
| 82 | + print( |
| 83 | + """ |
| 84 | + Warning: Spacer's minLength property is ignored within keyboard toolbars \ |
| 85 | + due to UIKit limitations. Use `Spacer()` for unconstrained spacers and \ |
| 86 | + `Spacer().frame(width: _)` for fixed-length spacers. |
| 87 | + """ |
| 88 | + ) |
| 89 | + } |
| 90 | + return .flexibleSpace() |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +struct FixedWidthToolbarItem<Base: ToolbarItem>: ToolbarItem { |
| 95 | + var base: Base |
| 96 | + var width: Int? |
| 97 | + |
| 98 | + func asBarButtonItem() -> UIBarButtonItem { |
| 99 | + let item = base.asBarButtonItem() |
| 100 | + if let width { |
| 101 | + item.width = CGFloat(width) |
| 102 | + } |
| 103 | + return item |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// Setting width on a flexible space is ignored, you must use a fixed space from the outset |
| 108 | +@available(iOS 14, macCatalyst 14, tvOS 14, *) |
| 109 | +struct FixedWidthSpacerItem: ToolbarItem { |
| 110 | + var width: Int? |
| 111 | + |
| 112 | + func asBarButtonItem() -> UIBarButtonItem { |
| 113 | + if let width { |
| 114 | + .fixedSpace(CGFloat(width)) |
| 115 | + } else { |
| 116 | + .flexibleSpace() |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +struct ColoredToolbarItem<Base: ToolbarItem>: ToolbarItem { |
| 122 | + var base: Base |
| 123 | + var color: Color |
| 124 | + |
| 125 | + func asBarButtonItem() -> UIBarButtonItem { |
| 126 | + let item = base.asBarButtonItem() |
| 127 | + item.tintColor = color.uiColor |
| 128 | + return item |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +extension ToolbarItem { |
| 133 | + /// A toolbar item with the specified width. |
| 134 | + /// |
| 135 | + /// If `width` is positive, the item will have that exact width. If `width` is zero or |
| 136 | + /// nil, the item will have its natural size. |
| 137 | + public func frame(width: Int?) -> any ToolbarItem { |
| 138 | + if #available(iOS 14, macCatalyst 14, tvOS 14, *), |
| 139 | + self is Spacer || self is FixedWidthSpacerItem |
| 140 | + { |
| 141 | + FixedWidthSpacerItem(width: width) |
| 142 | + } else { |
| 143 | + FixedWidthToolbarItem(base: self, width: width) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// A toolbar item with the specified foreground color. |
| 148 | + public func foregroundColor(_ color: Color) -> some ToolbarItem { |
| 149 | + ColoredToolbarItem(base: self, color: color) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +enum ToolbarKey: EnvironmentKey { |
| 154 | + static let defaultValue: ((UIToolbar) -> Void)? = nil |
| 155 | +} |
| 156 | + |
| 157 | +extension EnvironmentValues { |
| 158 | + var updateToolbar: ((UIToolbar) -> Void)? { |
| 159 | + get { self[ToolbarKey.self] } |
| 160 | + set { self[ToolbarKey.self] = newValue } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +extension View { |
| 165 | + /// Set a toolbar that will be shown above the keyboard for text fields within this view. |
| 166 | + /// - Parameters: |
| 167 | + /// - animateChanges: Whether to animate updates when an item is added, removed, or |
| 168 | + /// updated |
| 169 | + /// - body: The toolbar's contents |
| 170 | + public func keyboardToolbar( |
| 171 | + animateChanges: Bool = true, |
| 172 | + @ToolbarBuilder body: @escaping () -> ToolbarBuilder.Component |
| 173 | + ) -> some View { |
| 174 | + EnvironmentModifier(self) { environment in |
| 175 | + environment.with(\.updateToolbar) { toolbar in |
| 176 | + toolbar.setItems(body().map { $0.asBarButtonItem() }, animated: animateChanges) |
| 177 | + toolbar.sizeToFit() |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments