diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-06 12:53:24 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-07-06 12:53:24 +0200 |
| commit | f4edef273e4501a28cac7cb43fa7ad6f224fcb4a (patch) | |
| tree | 9bbe13e1af4d3e235d46345de1dd74989bf1d0b0 /Map/Presentation/Commands | |
| parent | 54f89bcd1a7fb0b92b65d8c0ea3847d635e11133 (diff) | |
Use templates in file menu
Diffstat (limited to 'Map/Presentation/Commands')
| -rw-r--r-- | Map/Presentation/Commands/MapCommands.swift | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Map/Presentation/Commands/MapCommands.swift b/Map/Presentation/Commands/MapCommands.swift index 35250e2..e7ee8f8 100644 --- a/Map/Presentation/Commands/MapCommands.swift +++ b/Map/Presentation/Commands/MapCommands.swift @@ -18,15 +18,42 @@ struct MapCommands: Commands { @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal @AppStorage("zoom") var zoom = 1.0 + @AppStorage("mapTemplates") private var templatesData: Data = Data() @FocusedBinding(\.document) var document: MapDocument? @FocusedValue(\.fileURL) var url: URL? @FocusedBinding(\.selectedEvolution) var selectedEvolution: StageType? @FocusedBinding(\.isSearching) var isSearching + private var templates: [Template] { + if let decoded = try? JSONDecoder().decode([Template].self, from: templatesData) { + return decoded + } + return [] + } + + private var defaultTemplate: Template? { + templates.first { $0.isDefault } + } + var body: some Commands { // File + CommandGroup(after: CommandGroupPlacement.newItem) { + Menu(String(localized: "commands.file.new_from_template")) { + if templates.isEmpty { + Text("No templates available") + .disabled(true) + } else { + ForEach(templates) { template in + Button(template.name) { + createNewDocument(from: template) + } + } + } + } + } + CommandGroup(after: CommandGroupPlacement.saveItem) { Divider() Button("Export...") { @@ -107,4 +134,20 @@ struct MapCommands: Commands { Button("Map Help") { NSWorkspace.shared.open(URL(string: "https://map.tranquil.systems")!) } } } + + @MainActor + private func createNewDocument(from template: Template) { + // Create a temporary file with the template content + let temporaryDirectory = FileManager.default.temporaryDirectory + let temporaryFile = temporaryDirectory.appendingPathComponent("Template-\(template.name).wmap") + + do { + try template.content.write(to: temporaryFile, atomically: true, encoding: .utf8) + NSWorkspace.shared.open(temporaryFile) + } catch { + // Fallback: just trigger the new document command + NSApplication.shared.sendAction( + #selector(NSDocumentController.newDocument(_:)), to: nil, from: nil) + } + } } |