| 1 | #if os(macOS) |
| 2 | import SwiftUI |
| 3 | |
| 4 | /// File ▸ Open a document… (⌘K): the jump palette as a sheet over the |
| 5 | /// focused document window, or over the home window (spec §1, menus). |
| 6 | public struct DocsOpenDocumentMenuItem: View { |
| 7 | @FocusedValue(\.docsJump) private var jump |
| 8 | public init() {} |
| 9 | public var body: some View { |
| 10 | Button { jump?.wrappedValue = true } label: { Text("docs.editor.menu.file.open", bundle: DocsResources.bundle) } |
| 11 | .keyboardShortcut("k") |
| 12 | .disabled(jump == nil) |
| 13 | } |
| 14 | } |
| 15 | extension FocusedValues { @Entry var docsJump: Binding<Bool>? } |
| 16 | |
| 17 | public struct DocsDocumentMenu: View { |
| 18 | @FocusedValue(\.docsEditor) private var editor |
| 19 | public init() {} |
| 20 | public var body: some View { |
| 21 | Group { |
| 22 | // Absent, not disabled, for a reader (spec §1, reader mode). |
| 23 | if editor == nil || editor?.model.documentAccess == .writable { |
| 24 | Button { editor?.rename() } label: { Text("docs.document.rename", bundle: DocsResources.bundle) } |
| 25 | .keyboardShortcut("r", modifiers: [.command, .shift]).disabled(editor?.model.documentAccess != .writable) |
| 26 | } |
| 27 | Button(docsMacString("Save")) { if let editor { Task { _ = await editor.model.saveAll() } } } |
| 28 | .keyboardShortcut("s").disabled(editor == nil) |
| 29 | // Export and Print download from the server: offline they wait |
| 30 | // (native.export.offline). |
| 31 | let offline = editor?.offline == true |
| 32 | Menu(docsMacString("Export")) { |
| 33 | Button(docsMacString("PDF…")) { editor?.exportRequest.wrappedValue = "pdf" } |
| 34 | Button(docsMacString("Markdown…")) { editor?.exportRequest.wrappedValue = "markdown" } |
| 35 | }.disabled(editor == nil || editor?.model.isDraft == true || offline) |
| 36 | .help(offline ? Text("native.export.offline", bundle: DocsResources.bundle) : Text(verbatim: "")) |
| 37 | // The popover under Share… in the window's toolbar. |
| 38 | Button { editor?.share() } label: { |
| 39 | Text("docs.share.action", bundle: DocsResources.bundle) |
| 40 | }.disabled(editor?.model.detail?.can_share != true || editor?.model.isDraft == true || editor?.model.documentAccess == .unavailable || offline) |
| 41 | |
| 42 | Divider() |
| 43 | |
| 44 | Button(docsMacString("Print…")) { editor?.exportRequest.wrappedValue = "print" }.keyboardShortcut("p").disabled(editor == nil || editor?.model.isDraft == true || offline) |
| 45 | // Owners only, and absent for everybody else (spec §1, Delete…). |
| 46 | if let editor, editor.model.detail?.can_share == true, !editor.model.isDraft { |
| 47 | Divider() |
| 48 | Button { editor.delete() } label: { Text("docs.editor.menu.file.delete", bundle: DocsResources.bundle) } |
| 49 | .disabled(editor.model.documentAccess == .unavailable || offline) |
| 50 | .accessibilityIdentifier("docs.delete") |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | #endif |
| 56 | |