|
| 1 | +// |
| 2 | +// KeyboardAction+PrimaryType.swift |
| 3 | +// KeyboardKit |
| 4 | +// |
| 5 | +// Created by Daniel Saidi on 202-06-23. |
| 6 | +// Copyright © 2022 Daniel Saidi. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +public extension KeyboardAction { |
| 12 | + |
| 13 | + /** |
| 14 | + This enum can be used together with ``primary(_:)``. |
| 15 | + |
| 16 | + Primary buttons are color accented buttons that trigger |
| 17 | + a submit action in the keyboard, just like ``return``. |
| 18 | + */ |
| 19 | + enum PrimaryType: CaseIterable, Codable, Equatable, Identifiable { |
| 20 | + |
| 21 | + /// A done key used in e.g. Calendar add location. |
| 22 | + case done |
| 23 | + |
| 24 | + /// A go key used in e.g. the Safari address bar. |
| 25 | + case go |
| 26 | + |
| 27 | + /// A join key used when e.g. joining a wifi network. |
| 28 | + case join |
| 29 | + |
| 30 | + /// A new line key used to force using a line arrow. |
| 31 | + case newLine |
| 32 | + |
| 33 | + /// An ok key, which isn't actually used in native. |
| 34 | + case ok |
| 35 | + |
| 36 | + /// A "search" key used in e.g. web search. |
| 37 | + case search |
| 38 | + |
| 39 | + /// A custom key with a custom title. |
| 40 | + case custom(title: String) |
| 41 | + |
| 42 | + /** |
| 43 | + The type's unique identifier. |
| 44 | + */ |
| 45 | + public var id: String { |
| 46 | + switch self { |
| 47 | + case .done: return "done" |
| 48 | + case .go: return "go" |
| 49 | + case .join: return "join" |
| 50 | + case .newLine: return "newLine" |
| 51 | + case .ok: return "ok" |
| 52 | + case .search: return "search" |
| 53 | + case .custom(let title): return title |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + All unique primary button types, excluding custom. |
| 59 | + */ |
| 60 | + public static var allCases: [KeyboardAction.PrimaryType] { |
| 61 | + return [.done, .go, .join, .newLine, .ok, .search] |
| 62 | + } |
| 63 | + |
| 64 | + #if canImport(UIKit) |
| 65 | + public static func type(for nativeType: UIReturnKeyType) -> PrimaryType { |
| 66 | + switch nativeType { |
| 67 | + case .default: return .ok |
| 68 | + case .go: return .go |
| 69 | + case .google: return .custom(title: "Google") |
| 70 | + case .join: return .join |
| 71 | + case .next: return .custom(title: "next") |
| 72 | + case .route: return .custom(title: "route") |
| 73 | + case .search: return .search |
| 74 | + case .send: return .custom(title: "send") |
| 75 | + case .yahoo: return .custom(title: "Google") |
| 76 | + case .done: return .done |
| 77 | + case .emergencyCall: return .custom(title: "emergencyCall") |
| 78 | + case .continue: return .custom(title: "continue") |
| 79 | + @unknown default: return .custom(title: "unknown") |
| 80 | + } |
| 81 | + } |
| 82 | + #endif |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +public extension KeyboardAction.PrimaryType { |
| 87 | + |
| 88 | + func standardButtonImage(for locale: Locale) -> Image? { |
| 89 | + switch self { |
| 90 | + case .newLine: return .keyboardNewline(for: locale) |
| 91 | + default: return nil |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + func standardButtonText(for locale: Locale) -> String? { |
| 96 | + switch self { |
| 97 | + case .custom(let title): return title |
| 98 | + case .done: return KKL10n.done.text(for: locale) |
| 99 | + case .go: return KKL10n.go.text(for: locale) |
| 100 | + case .join: return "join" // TODO: Localize |
| 101 | + case .newLine: return nil |
| 102 | + case .ok: return KKL10n.ok.text(for: locale) |
| 103 | + case .search: return KKL10n.search.text(for: locale) |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments