iOS: suggestion row above the keyboard; editor file name !484

merged merged by cmc on 2026-11-26 06:03 UTC · krz/orgstar:m4-suggestions into main

7 files changed, +261 −9

One commit's changes, read-only. Review comments go on all commits.

Layout: unified · split

Changed files (7)
Sources/OrgApp/SuggestionRow.swift added +40 · view
@@ -0,0 +1,40 @@
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}
Sources/OrgstarMobile/KeyBar.swift +93 −7 · view
@@ -7,7 +7,8 @@ import UIKit
77/// Org's keys for a touch keyboard: each button does what its key does at the caret, as the
88/// Emacs keymap binds it (M-→ demotes a heading, indents an item, moves a table column), or
99/// runs a command. The buttons are `[key-bar]` in config.toml, or the default; they share the
10/// width without scrolling, and those that don't fit go under More.
10/// width without scrolling, and those that don't fit go under More. While completion has
11/// candidates at the caret, they show in the bar's place.
1112struct KeyBar: View {
1213 let editor: MobileEditor
1314 let palette: () -> Void
@@ -109,6 +110,22 @@ struct KeyBar: View {
109110 @ScaledMetric(relativeTo: .body) private var size = KeyBar.target
110111
111112 var body: some View {
113 Group {
114 if SuggestionRow.shows(editor.suggestions.completion, dismissed: editor.suggestions.dismissed), let completion = editor.suggestions.completion {
115 suggestionRow(completion)
116 } else {
117 keys
118 }
119 }
120 .labelStyle(.iconOnly)
121 .padding(.horizontal, 6)
122 .padding(.vertical, Self.padding)
123 .frame(maxWidth: .infinity)
124 .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width = $0 }
125 .background(.bar)
126 }
127
128 private var keys: some View {
112129 let layout = sizeClass == .regular ? KeyBarLayout.Width.ipad : .iphone
113130 var ids = ((layout == .ipad ? ipad : iphone).map(KeyBarLayout.ids) ?? layout.defaultIDs).filter { Self.item($0) != nil }
114131 if editor.fields.active { ids = KeyBarLayout.withSnippetFields(ids) }
@@ -120,12 +137,52 @@ struct KeyBar: View {
120137 }
121138 if !more.isEmpty { moreButton(more) }
122139 }
123 .labelStyle(.iconOnly)
124 .padding(.horizontal, 6)
125 .padding(.vertical, Self.padding)
126 .frame(maxWidth: .infinity)
127 .onGeometryChange(for: CGFloat.self) { $0.size.width } action: { width = $0 }
128 .background(.bar)
140 }
141
142 /// The candidates at the caret in the bar's place, scrolling on their own, and at the end
143 /// the button that puts them away and brings the keys back.
144 private func suggestionRow(_ completion: Completion) -> some View {
145 HStack(spacing: 4) {
146 ScrollView(.horizontal, showsIndicators: false) {
147 HStack(spacing: 4) {
148 ForEach(SuggestionRow.items(completion), id: \.candidate) { item in
149 Button {
150 editor.chooseSuggestion(item.candidate)
151 } label: {
152 suggestionLabel(item)
153 }
154 .buttonStyle(SuggestionStyle(size: size))
155 .accessibilityLabel([item.candidate, item.detail, item.snippet ? String(localized: "snippet", comment: "Marks a snippet in the completion list") : nil]
156 .compactMap { $0 }.joined(separator: ", "))
157 }
158 }
159 .padding(.horizontal, 2)
160 }
161 // Back at the start for each new completion.
162 .id(completion.range.lowerBound)
163 Button {
164 editor.suggestions.dismissed = true
165 } label: {
166 Label(String(localized: "Hide Suggestions"), systemImage: "xmark")
167 }
168 .buttonStyle(KeyStyle(size: size, slots: 1))
169 .frame(width: KeyBar.widest)
170 .accessibilityLabel(String(localized: "Hide Suggestions"))
171 }
172 .dynamicTypeSize(...Self.largestSymbolSize)
173 .accessibilityIdentifier("suggestions")
174 }
175
176 private func suggestionLabel(_ item: SuggestionRow.Item) -> some View {
177 CappedWidth(width: 240) {
178 HStack(spacing: 4) {
179 if item.snippet { Image(systemName: "text.badge.plus").foregroundStyle(.tint) }
180 Text(item.candidate).foregroundStyle(.primary)
181 if let detail = item.detail { Text(detail).foregroundStyle(.secondary) }
182 }
183 .font(.body)
184 .lineLimit(1)
185 }
129186 }
130187
131188 @ViewBuilder
@@ -266,6 +323,35 @@ private struct KeyShape: ViewModifier {
266323 }
267324}
268325
326/// As wide as its content wants up to `width`, where a long suggestion is cut short.
327private struct CappedWidth: Layout {
328 let width: CGFloat
329
330 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {
331 guard let view = subviews.first else { return .zero }
332 let ideal = view.sizeThatFits(.unspecified)
333 return view.sizeThatFits(ProposedViewSize(width: min(ideal.width, width), height: proposal.height))
334 }
335
336 func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {
337 subviews.first?.place(at: bounds.origin, proposal: ProposedViewSize(bounds.size))
338 }
339}
340
341/// A suggestion: its text on a tile as tall as the keys, as wide as the text needs.
342private struct SuggestionStyle: ButtonStyle {
343 let size: CGFloat
344
345 func makeBody(configuration: Configuration) -> some View {
346 configuration.label
347 .padding(.horizontal, 12)
348 .frame(minHeight: size)
349 .background(.fill.tertiary, in: .rect(cornerRadius: 10))
350 .opacity(configuration.isPressed ? 0.5 : 1)
351 .contentShape(.rect)
352 }
353}
354
269355private struct KeyStyle: ButtonStyle {
270356 let size: CGFloat
271357 let slots: Int
Sources/OrgstarMobile/MobileEditor+Completion.swift +15 −1 · view
@@ -1,5 +1,6 @@
11#if os(iOS)
22import OrgApp
3import GameController
34import OrgCore
45import UIKit
56
@@ -100,6 +101,7 @@ extension MobileEditor {
100101
101102 func openCompletion(_ completion: Completion, select: Int? = nil) {
102103 completionFetched = nil
104 suggestions.dismissed = false
103105 if !completion.candidates.isEmpty { showCompletion(completion, select: select) }
104106 guard let query = completion.query, let source = completionSource else {
105107 if completion.candidates.isEmpty { closeCompletion() }
@@ -140,7 +142,10 @@ extension MobileEditor {
140142 completionList = view
141143 textView.addSubview(view)
142144 }
143 guard let position = textView.position(from: textView.beginningOfDocument, offset: completion.range.lowerBound) else { return }
145 let row = SuggestionRow.usesRow(hardwareKeyboard: GCKeyboard.coalesced != nil)
146 view.isHidden = row
147 suggestions.completion = row ? view.completion : nil
148 guard !row, let position = textView.position(from: textView.beginningOfDocument, offset: completion.range.lowerBound) else { return }
144149 let caret = textView.caretRect(for: position)
145150 let insets = textView.adjustedContentInset
146151 let visible = CGRect(x: textView.contentOffset.x, y: textView.contentOffset.y + insets.top,
@@ -154,6 +159,15 @@ extension MobileEditor {
154159 completionTask?.cancel()
155160 completionList?.removeFromSuperview()
156161 completionList = nil
162 suggestions.completion = nil
163 suggestions.dismissed = false
164 }
165
166 /// A candidate tapped in the suggestion row, inserted as one chosen from the list.
167 func chooseSuggestion(_ candidate: String) {
168 guard let completion = completionList?.completion else { return }
169 closeCompletion()
170 insertCompletion(completion, candidate)
157171 }
158172
159173 /// After typing: the open list follows the text; otherwise, as the settings say, a list
Sources/OrgstarMobile/MobileEditor.swift +9 · view
@@ -130,6 +130,8 @@ final class MobileEditor: NSObject, DocumentBuffer {
130130 var completionTimer: Timer?
131131 var completionFetched: CompletionFlow.Fetched?
132132 var completionTask: Task<Void, Never>?
133 /// The candidates the key bar shows in its place while the on-screen keyboard is used.
134 let suggestions = Suggestions()
133135 /// `dabbrev-expand` being repeated, and the revision its last expansion left.
134136 var dabbrev: DabbrevExpansion?
135137 var dabbrevRevision: Int?
@@ -849,6 +851,13 @@ extension MobileEditor: NSTextLayoutManagerDelegate {
849851 }
850852}
851853
854/// The completion the suggestion row shows, and whether it was put away for it.
855@Observable
856final class Suggestions {
857 var completion: Completion?
858 var dismissed = false
859}
860
852861/// Whether a snippet's fields are being filled in.
853862@Observable
854863final class SnippetFields {
Tests/OrgAppTests/SuggestionRowTests.swift added +62 · view
@@ -0,0 +1,62 @@
1import Foundation
2import Testing
3import OrgCore
4@testable import OrgApp
5
6/// The iOS suggestion row (#145).
7struct SuggestionRowTests {
8 func context(_ text: String, options: EditingOptions = .org) -> EditContext {
9 let caret = text.utf16.count
10 return EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], options: options)
11 }
12
13 @Test func rowOnlyWithTheOnScreenKeyboard() {
14 #expect(SuggestionRow.usesRow(hardwareKeyboard: false))
15 #expect(!SuggestionRow.usesRow(hardwareKeyboard: true))
16 }
17
18 @Test func showsWhileThereAreCandidates() {
19 let completion = OrgCompletion.at(context("#+TIT"))
20 #expect(SuggestionRow.shows(completion, dismissed: false))
21 #expect(!SuggestionRow.shows(completion, dismissed: true))
22 #expect(!SuggestionRow.shows(nil, dismissed: false))
23 // Nothing matches: the key bar stays.
24 #expect(!SuggestionRow.shows(OrgCompletion.at(context("#+XYZQ")), dismissed: false))
25 }
26
27 @Test func showsWhenTheListWouldOpenByItself() {
28 let options = CompletionOptions(auto: true, prefix: 3)
29 for text in ["#+TI", "#+TIT"] {
30 let completion = OrgCompletion.at(context(text))
31 let opened = CompletionAuto.opens(completion, options) ? completion : nil
32 #expect(SuggestionRow.shows(opened, dismissed: false) == (text == "#+TIT"))
33 }
34 }
35
36 @Test func itemsFollowTheListsOrder() throws {
37 let completion = try #require(OrgCompletion.at(context("#+TIT")))
38 let items = SuggestionRow.items(completion)
39 #expect(items.map(\.candidate) == completion.candidates)
40 #expect(items.allSatisfy { !$0.snippet && $0.detail == nil })
41 #expect(SuggestionRow.items(completion, limit: 1).count == min(1, completion.candidates.count))
42 }
43
44 @Test func snippetsWithTheirNames() throws {
45 var options = EditingOptions.org
46 options.snippets = [Snippet(trigger: "meeting", name: "Meeting notes", body: "x"), Snippet(trigger: "mail", body: "y")]
47 let completion = try #require(OrgCompletion.at(context("m", options: options)))
48 #expect(completion.kind == .snippet)
49 let items = SuggestionRow.items(completion)
50 #expect(Set(items.map(\.candidate)) == ["meeting", "mail"])
51 #expect(items.allSatisfy { $0.snippet })
52 #expect(items.first { $0.candidate == "meeting" }?.detail == "Meeting notes")
53 #expect(items.first { $0.candidate == "mail" }?.detail == nil)
54 }
55
56 @Test func templatesWithTheirBlocks() throws {
57 let completion = try #require(OrgCompletion.at(context("<s")))
58 #expect(completion.kind == .template)
59 let item = try #require(SuggestionRow.items(completion).first { $0.candidate == "<s" })
60 #expect(item.detail == "src")
61 }
62}
docs/manual/guide/14-ios.org +7 −1 · view
@@ -164,6 +164,12 @@ When the buttons don't fit the width, the bar shows as many as fit, in order, an
164164
165165While a snippet's fields are filled in, Previous Snippet Field and Next Snippet Field take the places of Promote and Demote, so the fields work without a hardware keyboard; Promote and Demote come back when the snippet ends. A bar without Promote or Demote shows the field buttons first.
166166
167*** Suggestions
168
169While completion has candidates at the caret, the bar shows them in place of its buttons, as the system's QuickType bar does: the candidates the completion list would show, in its order, each with what the list adds after it, such as a snippet's name (with a snippet symbol) or a template's block. Tap one to insert it as choosing it from the list does; a snippet expands. The suggestions scroll sideways when they don't fit; the × at the end stays in place and puts them away, bringing the buttons back until the word is finished or Complete at Point is run again. The row is as tall as the buttons and follows the text size the same way.
170
171Suggestions appear when the list would open by itself (Settings ▸ Completion, with its pause and minimum length) and on Complete at Point in Commands. They take the list's place while the on-screen keyboard is used, on iPhone and iPad; with a hardware keyboard the list opens at the caret instead, so its keys move through it, and the bar keeps its buttons.
172
167173** Commands
168174
169175Commands, in the key bar and in the toolbar of the editor, the reader and the agenda, lists the commands of the screen. With nothing typed, the ten you ran from Commands most recently come first under Recent, then every command by subject, in the Org menu's groups (Visibility, Headings, TODO and Tags, and so on) followed by Clock, Export, File, Edit and View. Type to search: the letters you type must appear in the title in order, as in the Mac's command palette, and commands you run often rank a little higher. The command runs once the list closes.
@@ -222,7 +228,7 @@ See [[file:03-keys.org][Keys and commands]] for the Emacs bindings.
222228
223229** Completion
224230
225With a hardware keyboard, Complete at Point (=C-M-i=, or =C-SPC= in Doom's insert state), Expand Word (=M-/=, or =F5= with the Mac keys) and Complete Word (=C-M-/=, or =C-n= and =C-p= in Doom's insert state) work as on the Mac; see [[file:02-the-editor.org][The editor]]. The list shows up to 8 rows under the caret; the keys move through it as on the Mac, and tapping a row inserts it. The settings in Settings ▸ Completion are the Mac's: whether the list opens by itself, after which pause and how many characters, prefix or fuzzy matching, and words in the list. With them on, the list also opens as you type on the on-screen keyboard, where tapping a row is the way to choose.
231With a hardware keyboard, Complete at Point (=C-M-i=, or =C-SPC= in Doom's insert state), Expand Word (=M-/=, or =F5= with the Mac keys) and Complete Word (=C-M-/=, or =C-n= and =C-p= in Doom's insert state) work as on the Mac; see [[file:02-the-editor.org][The editor]]. The list shows up to 8 rows under the caret; the keys move through it as on the Mac, and tapping a row inserts it. The settings in Settings ▸ Completion are the Mac's: whether the list opens by itself, after which pause and how many characters, prefix or fuzzy matching, and words in the list. With them on, candidates also show as you type on the on-screen keyboard, in the key bar's place; see Suggestions under The key bar.
226232
227233** Snippets
228234
l10n/Localizable.xcstrings +35 · view
@@ -18970,6 +18970,41 @@
1897018970 }
1897118971 }
1897218972 },
18973 "Hide Suggestions": {
18974 "comment": "Key bar: puts the completion suggestions away and shows the keys again",
18975 "localizations": {
18976 "de": {
18977 "stringUnit": {
18978 "state": "translated",
18979 "value": "Vorschläge ausblenden"
18980 }
18981 },
18982 "es": {
18983 "stringUnit": {
18984 "state": "translated",
18985 "value": "Ocultar sugerencias"
18986 }
18987 },
18988 "fr": {
18989 "stringUnit": {
18990 "state": "translated",
18991 "value": "Masquer les suggestions"
18992 }
18993 },
18994 "ja": {
18995 "stringUnit": {
18996 "state": "translated",
18997 "value": "候補を隠す"
18998 }
18999 },
19000 "pt-BR": {
19001 "stringUnit": {
19002 "state": "translated",
19003 "value": "Ocultar Sugestões"
19004 }
19005 }
19006 }
19007 },
1897319008 "Hide entries from earlier days": {
1897419009 "localizations": {
1897519010 "de": {
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form