Sources/OrgApp/SuggestionRow.swift
40 lines · 1816 bytes
5 symbols in this file
1import Foundation
2import OrgCore
3
4/// The iOS suggestion row: completion candidates in the key bar's place, tapped as the
5/// system's QuickType bar is. It shows the candidates the completion list would; the list
6/// stays for a hardware keyboard, whose keys move through it.
7public enum SuggestionRow {
8 /// A candidate as the row shows it: what is inserted, and what the list adds after it
9 /// (a snippet's name, a template's block, a heading's file).
10 public struct Item: Equatable, Sendable {
11 public let candidate: String
12 public let detail: String?
13 public let snippet: Bool
14 }
15
16 /// The most candidates the row holds; it scrolls through them.
17 public static let limit = 30
18
19 /// Candidates show in the row rather than the floating list: while the on-screen keyboard
20 /// is used, so not with a hardware keyboard.
21 public static func usesRow(hardwareKeyboard: Bool) -> Bool {
22 !hardwareKeyboard
23 }
24
25 /// The row takes the key bar's place: there are candidates, and the row wasn't put away
26 /// for this completion.
27 public static func shows(_ completion: Completion?, dismissed: Bool) -> Bool {
28 guard let completion, !dismissed else { return false }
29 return !completion.candidates.isEmpty
30 }
31
32 /// The candidates in the list's order, up to `limit`.
33 public static func items(_ completion: Completion, limit: Int = limit) -> [Item] {
34 completion.candidates.prefix(limit).map { candidate in
35 let label = completion.label(candidate)
36 let detail = label.hasPrefix(candidate) ? label.dropFirst(candidate.count).trimmingCharacters(in: .whitespaces) : ""
37 return Item(candidate: candidate, detail: detail.isEmpty ? nil : detail, snippet: completion.kind == .snippet)
38 }
39 }
40}