Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Sources/macOS/MenuBarExtra.swift3// Description: The menu bar extra — unread counts, the five most recent unread conversations (hidden when notifications must not reveal content,4// and always while the app is locked), « Nouveau message », « Ouvrir SP Mail » and the pause-notifications toggle.5import AppKit6import SPMailCore7import SwiftUI89struct MenuBarLabel: View {10 let app: AppModel11 var body: some View {12 let unread = app.phase == .ready ? app.totalInboxUnread : 013 HStack(spacing: 3) {14 Image(systemName: app.settings.notificationsPaused ? "envelope.badge.shield.half.filled" : unread > 0 ? "envelope.badge" : "envelope")15 if unread > 0 { Text("\(unread)").monospacedDigit() }16 }17 .accessibilityLabel(Text(unread > 0 ? "SP Mail, \(unread) non lus" : "SP Mail"))18 }19}2021struct MenuBarContent: View {22 let app: AppModel23 @Environment(\.openWindow) private var openWindow2425 /// The same privacy level as notifications: no sender, no subject when they are hidden there — and never while locked.26 private var concealed: Bool { app.settings.hideNotificationContent || app.phase != .ready || app.settings.lockEnabled && app.lock.concealsContent }2728 var body: some View {29 @Bindable var settings = app.settings30 switch app.phase {31 case .ready:32 let unread = app.totalInboxUnread33 Text(unread == 0 ? L("Aucun message non lu") : unread == 1 ? L("1 message non lu") : L("\(unread) messages non lus"))34 if app.accounts.count > 1 {35 ForEach(app.accounts) { a in36 let n = app.inboxUnread[a.id] ?? 037 if n > 0 { Text("\(a.address) — \(n)") }38 }39 }40 if !app.recentUnread.isEmpty, !concealed {41 Divider()42 ForEach(app.recentUnread) { item in43 let e = item.thread.email44 Button { open(item) } label: {45 Text("\(e.sender?.displayName ?? L("(expéditeur inconnu)")) — \(e.subject.isEmpty ? L("(sans objet)") : String(e.subject.prefix(60)))")46 }47 }48 }49 case .locked: Text("SP Mail est verrouillé")50 default: Text("Non connecté")51 }52 Divider()53 Button("Nouveau message") { compose() }.disabled(app.phase != .ready)54 Button("Ouvrir SP Mail") { activate(); openWindow(id: "main") }55 Divider()56 Toggle("Suspendre les notifications", isOn: $settings.notificationsPaused)57 Divider()58 Button("Quitter SP Mail") { NSApp.terminate(nil) }59 }6061 private func activate() { NSApp.activate(ignoringOtherApps: true) }62 private func open(_ item: RecentUnread) {63 activate()64 openWindow(id: "message", value: MessageRef(account: item.account, emailId: item.thread.id, conversation: app.settings.threaded))65 }66 private func compose() {67 Task {68 guard let account = app.main.currentAccount ?? app.accounts.first?.id, let id = await app.makeComposer(.new, account: account) else { return }69 activate()70 openWindow(id: "compose", value: id)71 }72 }73}74