oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1#if os(macOS)
2import AppKit
3import SwiftUI
4
5/// The Mac's Format, View and Tools menus (spec §1, "Menus and shortcuts"),
6/// labelled with the web's keys where one exists.
7public struct DocsEditorCommands: Commands {
8 @FocusedValue(\.docsEditor) private var editor
9 /// The floating inspector's app-wide state (spec D1): ⌥⌘I flips it for
10 /// every window at once.
11 @AppStorage(DocsInspectorState.key) private var inspectorOpen = DocsInspectorState.defaultOpen
12 public init() {}
13
14 private static let sizes = [8, 9, 10, 11, 12, 14, 18, 24, 36, 48, 72]
15 private func text(_ key: String) -> Text { Text(LocalizedStringKey(key), bundle: DocsResources.bundle) }
16
17 public var body: some Commands {
18
19 CommandGroup(replacing: .printItem) {}
20
21 CommandMenu(text("docs.editor.menu.format")) {
22 Group {
23 Button { editor?.ready { $0.perform("bold") } } label: { text("docs.editor.toolbar.bold") }.keyboardShortcut("b")
24 Button { editor?.ready { $0.perform("italic") } } label: { text("docs.editor.toolbar.italic") }.keyboardShortcut("i")
25 Button { editor?.ready { $0.perform("code") } } label: { text("docs.editor.toolbar.code") }
26 Divider()
27 // Body text and Heading 1–3, as the web: Headings 4–6 still
28 // render and edit, but are not offered.
29 Menu {
30 Button { editor?.ready { $0.block("paragraph") } } label: { text("docs.editor.toolbar.block.paragraph") }
31 ForEach(1...3, id: \.self) { level in
32 Button { editor?.ready { $0.block("heading", level: level) } } label: { text(EditorToolbar.headingKey(level)) }
33 }
34 } label: { text("docs.editor.toolbar.block") }
35 Button { editor?.ready { $0.block("bullet_list") } } label: { text("docs.editor.toolbar.bulletlist") }
36 Button { editor?.ready { $0.block("ordered_list") } } label: { text("docs.editor.toolbar.orderedlist") }
37 Button { editor?.ready { $0.block("blockquote") } } label: { text("docs.editor.toolbar.quote") }
38 Divider()
39 Menu {
40 // Each family's name in its own face; SwiftUI carries the
41 // font into the menu item's attributed title.
42 ForEach(DocsFontPicker.entries) { entry in
43 Button { editor?.ready { $0.setFont(entry.key) } } label: { DocsFontPicker.label(entry) }
44 }
45 } label: { text("docs.editor.toolbar.font") }
46 Menu {
47 ForEach(Self.sizes, id: \.self) { size in
48 Button(size.formatted()) { editor?.ready { $0.format("size_" + String(size), exclusive: Self.sizes.map { "size_" + String($0) } + ["size_sm", "size_lg", "size_xl"]) } }
49 }
50 } label: { text("docs.editor.toolbar.size") }
51 Divider()
52 ForEach(["left", "center", "right"], id: \.self) { value in
53 Button { editor?.ready { $0.blockAttribute("align", value: value == "left" ? nil : value) } } label: { text("docs.editor.toolbar.align." + value) }
54 }
55 Divider()
56 Button { editor?.ready { $0.perform("indent") } } label: { text("docs.editor.menu.format.indent") }
57 Button { editor?.ready { $0.perform("outdent") } } label: { text("docs.editor.menu.format.outdent") }
58 Button { editor?.ready { $0.perform("clear-format") } } label: { text("docs.editor.menu.format.clear") }
59 // isAvailable, not isEnabled: that one moves on every key (input
60 // pending), and the menu bar's commands were evaluated again with
61 // it. A command chosen then runs once the input is drawn; held for
62 // longer, the menu shows disabled.
63 }.disabled(editor?.actions?.isAvailable != true || editor?.model.commandsHeld == true || !(NSApp.keyWindow?.firstResponder is DocsTextView))
64 }
65 CommandGroup(after: .toolbar) {
66 // Disabled in a reader's window and in the home window (no
67 // editor), where there is no inspector to show.
68 Button { inspectorOpen.toggle() } label: { text(inspectorOpen ? "native.format.hide" : "native.format.show") }
69 .keyboardShortcut("i", modifiers: [.command, .option])
70 .disabled(editor?.model.documentAccess != .writable)
71 Divider()
72 if let editor {
73 Picker(selection: editor.paged) {
74 text("docs.editor.menu.view.paged").tag(true)
75 text("docs.editor.menu.view.infinite").tag(false)
76 } label: { EmptyView() }
77 .pickerStyle(.inline)
78 }
79 Divider()
80 // Native only: the web has no zoom.
81 Menu(docsMacString("Zoom")) {
82 ForEach([0.5, 0.75, 1.0, 1.25, 1.5, 2.0], id: \.self) { value in
83 Button { editor?.zoom.wrappedValue = value } label: { Text(value, format: .percent) }
84 }
85 }.disabled(editor == nil)
86 Button(docsMacString("Actual size")) { editor?.zoom.wrappedValue = 1 }.keyboardShortcut("0").disabled(editor == nil)
87 }
88 // Help: Keyboard shortcuts…, then the Colophon (DocsWindowCommands).
89 CommandGroup(before: .help) {
90 Button { editor?.shortcuts.wrappedValue = true } label: { text("docs.editor.menu.tools.shortcuts") }
91 .disabled(editor == nil)
92 }
93 CommandMenu(text("docs.editor.menu.tools")) {
94 Button { editor?.wordCount.wrappedValue = true } label: { text("docs.editor.menu.tools.wordcount") }
95 .disabled(editor?.model.history.loaded != true)
96 }
97 }
98}
99#endif
100