2 Copyright (C) 2024 Rubén Beltrán del Río
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
19 struct MapCommands: Commands {
21 @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
22 @AppStorage("zoom") var zoom = 1.0
23 @FocusedBinding(\.document) var document: MapDocument?
24 @FocusedValue(\.fileURL) var url: URL?
25 @FocusedBinding(\.selectedEvolution) var selectedEvolution: StageType?
26 @FocusedBinding(\.isSearching) var isSearching
28 var body: some Commands {
32 CommandGroup(after: CommandGroupPlacement.saveItem) {
35 if let selectedEvolution, let document {
36 if let image = document.exportAsImage(withEvolution: selectedEvolution) {
38 let filename = url?.deletingPathExtension().lastPathComponent ?? "Untitled"
40 let savePanel = NSSavePanel()
41 savePanel.allowedContentTypes = [.png]
42 savePanel.canCreateDirectories = true
43 savePanel.isExtensionHidden = false
44 savePanel.title = "Save \(filename) as image"
45 savePanel.message = "Choose a location to save the image"
46 savePanel.nameFieldStringValue = "\(filename).png"
47 savePanel.begin { result in
48 if result == .OK, let url = savePanel.url {
49 if let tiffRepresentation = image.tiffRepresentation {
50 let bitmapImage = NSBitmapImageRep(data: tiffRepresentation)
51 let pngData = bitmapImage?.representation(using: .png, properties: [:])
53 try pngData?.write(to: url)
63 "e", modifiers: EventModifiers([.command])
64 ).disabled(document == nil)
69 CommandGroup(after: CommandGroupPlacement.pasteboard) {
73 isSearching = isSearching != nil ? !isSearching! : true
76 "f", modifiers: EventModifiers([.command])
77 ).disabled(document == nil)
82 CommandGroup(after: CommandGroupPlacement.toolbar) {
83 if viewStyle == .horizontal {
84 Button("Use Vertical Layout") {
87 "l", modifiers: EventModifiers([.command])
88 ).disabled(document == nil)
90 Button("Use Horizontal Layout") {
91 viewStyle = .horizontal
93 "l", modifiers: EventModifiers([.command])
94 ).disabled(document == nil)
98 zoom = min(Constants.kMaxZoom, zoom + 0.1)
100 "+", modifiers: EventModifiers([.command])
101 ).disabled(document == nil)
103 zoom = max(Constants.kMinZoom, zoom - 0.1)
105 "-", modifiers: EventModifiers([.command])
106 ).disabled(document == nil)
110 CommandGroup(replacing: CommandGroupPlacement.help) {
111 Button("Map Help") { NSWorkspace.shared.open(URL(string: "https://map.tranquil.systems")!) }