Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/Shared/Commands.swift3// Description: Menus and keyboard shortcuts (macOS menu bar; iPadOS: shown when ⌘ is held on a hardware keyboard) — Boîte, Message, Présentation,4// Format, Modèles, Fenêtre. Commands act on the FOCUSED window's MailScene, so each window and each iPad scene drives its own navigation.5// No key combination is declared twice in the menu bar (Scripts/check-shortcuts.py verifies it, system combinations included): ⌘K is the6// command palette and the composer's link is ⇧⌘K; ⌘S and ⌥⌘0 change owner with the window in front (`composer == nil ? … : nil`).7import SPMailCore8import SwiftUI910struct MailCommands: Commands {11 let app: AppModel12 @FocusedValue(MailScene.self) private var focusedScene: MailScene?13 @FocusedValue(ComposeModel.self) private var composer: ComposeModel?14 @Environment(\.openWindow) private var openWindow1516 var body: some Commands {17 let ready = app.phase == .ready18 let scene = ready ? focusedScene : nil19 let hasSelection = scene?.hasSelection == true20 let target = scene?.replyTarget()21 let settings = app.settings2223 CommandGroup(replacing: .newItem) {24 Button("Nouveau message") { Task { await (scene ?? app.main).compose(.new) } }.keyboardShortcut("n").disabled(!ready)25 #if os(macOS)26 Button("Ouvrir dans une nouvelle fenêtre") {27 if let scene, let account = scene.activeAccount, let e = target { openWindow(id: "message", value: MessageRef(account: account, emailId: e.id, conversation: settings.threaded)) }28 }.keyboardShortcut("o").disabled(target == nil || scene?.isDetached == true)29 #endif30 }31 CommandGroup(after: .textEditing) {32 Button("Rechercher dans le courrier") { scene?.openSearch() }.keyboardShortcut("f").disabled(scene == nil || scene?.isDetached == true)33 }34 SidebarCommands()35 #if os(macOS)36 ToolbarCommands()37 InspectorCommands()38 #endif39 CommandGroup(after: .sidebar) {40 Button("Palette de commandes…") { if let scene { spWithAnimation(SPMotion.press) { scene.showPalette.toggle() } } }.keyboardShortcut("k").disabled(scene == nil || scene?.isDetached == true)41 Divider()42 Button("Conversation suivante") { scene?.selectAdjacent(1) }.keyboardShortcut(.downArrow, modifiers: [.command, .option]).disabled(scene == nil || scene?.isDetached == true)43 Button("Conversation précédente") { scene?.selectAdjacent(-1) }.keyboardShortcut(.upArrow, modifiers: [.command, .option]).disabled(scene == nil || scene?.isDetached == true)44 Divider()45 Picker("Thème", selection: Binding(get: { settings.theme }, set: { settings.theme = $0 })) { ForEach(SPTheme.allCases) { Text($0.title).tag($0) } }46 Picker("Densité", selection: Binding(get: { settings.density }, set: { settings.density = $0 })) { ForEach(Density.allCases) { Text($0.title).tag($0) } }47 Picker("Lignes d’aperçu", selection: Binding(get: { settings.previewLines }, set: { settings.previewLines = $0 })) { ForEach(0..<3, id: \.self) { Text("\($0)").tag($0) } }48 Toggle("Afficher les avatars", isOn: Binding(get: { settings.showAvatars }, set: { settings.showAvatars = $0 }))49 Toggle("Regrouper par conversation", isOn: Binding(get: { settings.threaded }, set: { settings.threaded = $0; Task { for s in app.scenes { await s.reloadThreads() } } }))50 Divider()51 }52 CommandMenu("Boîte") {53 Button("Relever le courrier") { Task { await (scene ?? app.main).refresh() } }.keyboardShortcut("n", modifiers: [.command, .shift]).disabled(!ready)54 Button("Nouveau dossier…") { scene?.newFolderRequest += 1 }.disabled(scene == nil || scene?.isDetached == true)55 Divider()56 Button("Toutes les boîtes") { scene?.selection = .unified(.inbox) }.keyboardShortcut("0", modifiers: .command).disabled(scene == nil || scene?.isDetached == true)57 if app.next.isAvailable {58 let off = scene == nil || scene?.isDetached == true59 if app.next.has(.dashboard) { Button("Centre de commande") { scene?.selection = .dashboard }.keyboardShortcut("c", modifiers: [.command, .shift]).disabled(off) }60 if app.next.has(.contacts) { Button("Contacts") { scene?.selection = .contacts }.keyboardShortcut("c", modifiers: [.command, .option]).disabled(off) }61 if app.next.has(.attachments) { Button("Pièces jointes") { scene?.selection = .attachments }.keyboardShortcut("j", modifiers: [.command, .option]).disabled(off) }62 if app.next.has(.snooze) { Button("En veille") { scene?.selection = .snoozed }.disabled(off) }63 if app.next.has(.reminders) { Button("Suivis") { scene?.selection = .followUps }.disabled(off) }64 Divider()65 }66 ForEach(0..<9, id: \.self) { n in67 Button(folderTitle(n, scene)) { scene?.selectSidebar(index: n) }.keyboardShortcut(KeyEquivalent(Character(String(n + 1))), modifiers: .command)68 .disabled(scene == nil || scene?.isDetached == true || (scene?.sidebarOrder.count ?? 0) <= n)69 }70 if app.accounts.count > 1 {71 Divider()72 ForEach(app.accounts) { a in73 Button(a.address) { if let scene { Task { await scene.switchAccount(a.id) } } }.disabled(scene == nil || scene?.isDetached == true || scene?.currentAccount == a.id)74 }75 }76 Divider()77 Button("Verrouiller SP Mail") { app.lockIfNeeded(force: true) }.keyboardShortcut("l", modifiers: [.command, .control]).disabled(!ready || !settings.lockEnabled)78 }79 CommandMenu("Message") {80 Button("Répondre") { if let e = target { Task { await scene?.compose(.reply(e)) } } }.keyboardShortcut("r").disabled(target == nil)81 Button("Répondre à tous") { if let e = target { Task { await scene?.compose(.replyAll(e)) } } }.keyboardShortcut("r", modifiers: [.command, .shift]).disabled(target == nil)82 Button("Transférer") { if let e = target { Task { await scene?.compose(.forward(e)) } } }.keyboardShortcut("f", modifiers: [.command, .shift]).disabled(target == nil)83 Divider()84 Button("Drapeau") { Task { await scene?.toggleFlag() } }.keyboardShortcut("l", modifiers: [.command, .shift]).disabled(!hasSelection)85 Button("Marquer comme lu / non lu") { Task { await scene?.toggleRead() } }.keyboardShortcut("u", modifiers: [.command, .shift]).disabled(!hasSelection)86 Button("Épingler / désépingler") { Task { await scene?.togglePin() } }.keyboardShortcut("p", modifiers: [.command, .shift]).disabled(!hasSelection)87 Button("Mettre en veille…") { if let scene { Task { await scene.snooze(ids: scene.list.selectedIds) } } }.keyboardShortcut("z", modifiers: [.command, .control]).disabled(!hasSelection || !app.next.has(.snooze))88 Button("Me le rappeler…") { scene?.remindSelection() }.keyboardShortcut("r", modifiers: [.command, .control]).disabled(target == nil || !app.next.has(.reminders))89 if let scene, app.next.has(.labels) {90 Menu("Libellés") { LabelMenuItems(scene: scene, keywords: scene.selectionKeywords) { action in Task { await scene.perform(action) } } }.disabled(!hasSelection)91 }92 Divider()93 Button("Archiver") { Task { await scene?.perform(.archive) } }.keyboardShortcut("a", modifiers: [.command, .control]).disabled(!hasSelection)94 if let scene { Menu("Déplacer vers") { MoveMenuItems(scene: scene, account: scene.activeAccount, ids: nil) }.disabled(!hasSelection) }95 Button("Indésirable") { Task { await scene?.perform(.junk) } }.keyboardShortcut("j", modifiers: [.command, .shift]).disabled(!hasSelection)96 Button("Supprimer") { Task { await scene?.perform(.delete) } }.keyboardShortcut(.delete, modifiers: .command).disabled(!hasSelection)97 }98 CommandMenu("Format") {99 // acts on the composer in front: the items live with the composer (Sources/Shared/Compose/FormatBar.swift)100 ComposeFormatMenu(composer: composer)101 }102 CommandMenu("Modèles") {103 // ⇧⌘M: ⌥⌘M is the system's « Minimize All »104 Button("Studio de modèles") { (scene ?? app.main).studioRequest += 1 }.keyboardShortcut("m", modifiers: [.command, .shift]).disabled(!ready)105 Button("Créer un modèle avec l’IA…") { app.studio.showGenerator = true; (scene ?? app.main).studioRequest += 1 }.keyboardShortcut("g", modifiers: [.command, .option]).disabled(!ready)106 Button("Nouveau modèle vierge") { (scene ?? app.main).studioRequest += 1; Task { await app.studio.createBlank() } }.disabled(!ready)107 Divider()108 Button("Enregistrer le modèle") { Task { await app.studio.current?.save() } }.keyboardShortcut(composer == nil ? KeyboardShortcut("s") : nil).disabled(app.studio.current?.isDirty != true)109 Button("Envoyer ce modèle…") { app.studio.current?.showSend = true }.disabled(app.studio.current == nil)110 }111 #if os(macOS)112 CommandGroup(before: .windowList) {113 Button("Fenêtre principale") { openWindow(id: "main") }.keyboardShortcut(composer == nil ? KeyboardShortcut("0", modifiers: [.command, .option]) : nil)114 Divider()115 }116 #endif117 }118119 private func folderTitle(_ n: Int, _ scene: MailScene?) -> String {120 guard let scene, scene.sidebarOrder.indices.contains(n) else { return L("Dossier \(n + 1)") }121 switch scene.sidebarOrder[n] {122 case .mailbox(let id): return scene.mailboxes.first { $0.id == id }.map(MailScene.title(of:)) ?? L("Dossier \(n + 1)")123 case .unread: return L("Non lus")124 case .flagged: return L("Avec drapeau")125 case .outbox: return L("Boîte d’envoi")126 default: return L("Dossier \(n + 1)")127 }128 }129}130