SPB Git

Repository Public

ShellCommands.swift 6.8 KB 129 lines swift

1// SP Notes iOS2// File: Features/Commands/ShellCommands.swift3// Description: The menus of the app (`.commands`): they make every shortcut discoverable in the hardware-keyboard overlay (hold ⌘)4//              and in the iPadOS menu bar. Fichier: new note, new folder, quick capture, new note window, sync. Format: the5//              editor's own actions, forwarded to the note that has the focus. Présentation: the places (⌘1…9), search, the6//              command palette. Réglages. The menus act on the key window through focused scene values.7//              Shortcuts: 1.0 ones are unchanged (⌘N, ⇧⌘N, ⌘F, ⌘,, ⌘R and the editor's). New: ⌘K palette (the editor's « Lien »8//              moved to ⇧⌘K), ⌥⌘N quick capture, ⌃⌘N note in a new window, ⌘1…9 places.9//10// Author: Simon-Pierre Boucher11// Contact: contact@spboucher.ai12import SPNotesCore13import SwiftUI1415/// What the menus and the command palette can do in one window.16@MainActor17struct ShellActions {18    let app: AppModel19    let shell: ShellState20    /// Opens a note in a window of its own (nil where the device has one window).21    var openNoteWindow: (@MainActor (NoteWindowTarget) -> Void)?2223    func newNote() { Task { await NoteCommands(app: app, shell: shell).newNote(in: shell.currentSource) } }24    func newFolder() { shell.sheet = NewFolderTarget.sheet(for: shell.currentSource) }25    func capture(kind: String? = nil) { shell.requestCapture(kind: kind) }26    func search() { shell.openSearch() }27    func palette() { shell.isPaletteOpen.toggle() }28    func sync() { app.syncNow() }29    func settings() { shell.sheet = .settings }30    func go(_ item: SidebarItem) { shell.go(to: item) }3132    var canOpenCurrentNoteInWindow: Bool { openNoteWindow != nil && shell.currentNote != nil }33    func openCurrentNoteInWindow() {34        guard let id = shell.currentNote else { return }35        openNoteWindow?(NoteWindowTarget(noteId: id))36    }3738    /// The actions of the main window used last. The menus fall back to it when the key window publishes none (a note window39    /// is in front, or a window was just closed and no scene has the focus yet), so a shortcut never silently does nothing.40    static var lastActive: ShellActions?4142    /// Runs a command of the palette.43    func run(_ action: PaletteAction) {44        switch action {45        case .go(let item): go(item)46        case .openNote(let id): shell.open(note: id)47        case .newNote: newNote()48        case .newFolder: newFolder()49        case .capture: capture()50        case .sync: sync()51        case .syncDetails: shell.sheet = .syncDetails52        case .appearance(let appearance): UserDefaults.standard.set(appearance.rawValue, forKey: "appearance")53        case .settings: settings()54        }55    }56}5758/// The note that has the focus, for the Format menu (set by the note screen; the editor engine keeps its own key commands).59@MainActor60struct EditorCommandTarget {61    let isEditable: Bool62    let perform: @MainActor (EditorAction) -> Void63}6465extension FocusedValues {66    @Entry var shellActions: ShellActions?67    @Entry var editorCommands: EditorCommandTarget?68}6970struct ShellCommands: Commands {71    @FocusedValue(\.shellActions) private var focusedActions72    @FocusedValue(\.editorCommands) private var editor73    private var actions: ShellActions? { focusedActions ?? ShellActions.lastActive }7475    var body: some Commands {76        CommandGroup(replacing: .newItem) {77            Button("Nouvelle note") { actions?.newNote() }.keyboardShortcut("n", modifiers: .command)78            Button("Nouveau dossier") { actions?.newFolder() }.keyboardShortcut("n", modifiers: [.command, .shift])79            Button("Capture rapide") { actions?.capture() }.keyboardShortcut("n", modifiers: [.command, .option])80            Button("Ouvrir la note dans une nouvelle fenêtre") { actions?.openCurrentNoteInWindow() }81                .keyboardShortcut("n", modifiers: [.command, .control])82                .disabled(actions?.canOpenCurrentNoteInWindow != true)83            Divider()84            Button("Synchroniser maintenant") { actions?.sync() }.keyboardShortcut("r", modifiers: .command)85        }86        CommandGroup(replacing: .appSettings) {87            Button("Réglages…") { actions?.settings() }.keyboardShortcut(",", modifiers: .command)88        }89        CommandGroup(after: .sidebar) {90            Button("Rechercher") { actions?.search() }.keyboardShortcut("f", modifiers: .command)91            Button("Palette de commandes") { actions?.palette() }.keyboardShortcut("k", modifiers: .command)92            Divider()93            ForEach(Array(CommandRegistry.numberedPlaces(sections: actions?.shell.sections ?? .shared).enumerated()), id: \.element.id) { index, place in94                Button(place.title) { actions?.go(place.item) }95                    .keyboardShortcut(KeyEquivalent(Character("\(index + 1)")), modifiers: .command)96            }97        }98        CommandMenu("Format") {99            let disabled = editor?.isEditable != true100            Group {101                Button("Gras") { editor?.perform(.mark("bold")) }.keyboardShortcut("b", modifiers: .command)102                Button("Italique") { editor?.perform(.mark("italic")) }.keyboardShortcut("i", modifiers: .command)103                Button("Souligné") { editor?.perform(.mark("underline")) }.keyboardShortcut("u", modifiers: .command)104                Button("Barré") { editor?.perform(.mark("strike")) }.keyboardShortcut("x", modifiers: [.command, .shift])105                Button("Code") { editor?.perform(.mark("code")) }.keyboardShortcut("e", modifiers: .command)106                Button("Lien…") { editor?.perform(.link) }.keyboardShortcut("k", modifiers: [.command, .shift])107            }108            .disabled(disabled)109            Divider()110            Group {111                Button("Texte") { editor?.perform(.body) }.keyboardShortcut("0", modifiers: [.command, .option])112                Button("Titre 1") { editor?.perform(.heading(1)) }.keyboardShortcut("1", modifiers: [.command, .option])113                Button("Titre 2") { editor?.perform(.heading(2)) }.keyboardShortcut("2", modifiers: [.command, .option])114                Button("Titre 3") { editor?.perform(.heading(3)) }.keyboardShortcut("3", modifiers: [.command, .option])115            }116            .disabled(disabled)117            Divider()118            Group {119                Button("Liste numérotée") { editor?.perform(.list(.ordered)) }.keyboardShortcut("7", modifiers: [.command, .shift])120                Button("Liste à puces") { editor?.perform(.list(.bullet)) }.keyboardShortcut("8", modifiers: [.command, .shift])121                Button("Liste de tâches") { editor?.perform(.list(.task)) }.keyboardShortcut("9", modifiers: [.command, .shift])122                Button("Citation") { editor?.perform(.quote) }123                Button("Bloc de code") { editor?.perform(.codeBlock) }124            }125            .disabled(disabled)126        }127    }128}129
Files main
Menu

Theme