]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | Copyright (C) 2024 Rubén Beltrán del Río | |
3 | ||
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. | |
8 | ||
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. | |
13 | ||
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. | |
16 | */ | |
17 | import SwiftUI | |
18 | ||
19 | struct MapCommands: Commands { | |
20 | ||
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 | |
27 | ||
28 | var body: some Commands { | |
29 | ||
30 | // File | |
31 | ||
32 | CommandGroup(after: CommandGroupPlacement.saveItem) { | |
33 | Divider() | |
34 | Button("Export...") { | |
35 | if let selectedEvolution, let document { | |
36 | if let image = document.exportAsImage(withEvolution: selectedEvolution) { | |
37 | ||
38 | let filename = url?.deletingPathExtension().lastPathComponent ?? "Untitled" | |
39 | ||
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: [:]) | |
52 | do { | |
53 | try pngData?.write(to: url) | |
54 | } catch { | |
55 | return | |
56 | } | |
57 | } | |
58 | } | |
59 | } | |
60 | } | |
61 | } | |
62 | }.keyboardShortcut( | |
63 | "e", modifiers: EventModifiers([.command]) | |
64 | ).disabled(document == nil) | |
65 | } | |
66 | ||
67 | // Edit | |
68 | ||
69 | CommandGroup(after: CommandGroupPlacement.pasteboard) { | |
70 | Divider() | |
71 | Button("Find...") { | |
72 | withAnimation { | |
73 | isSearching = isSearching != nil ? !isSearching! : true | |
74 | } | |
75 | }.keyboardShortcut( | |
76 | "f", modifiers: EventModifiers([.command]) | |
77 | ).disabled(document == nil) | |
78 | } | |
79 | ||
80 | // View | |
81 | ||
82 | CommandGroup(after: CommandGroupPlacement.toolbar) { | |
83 | if viewStyle == .horizontal { | |
84 | Button("Use Vertical Layout") { | |
85 | viewStyle = .vertical | |
86 | }.keyboardShortcut( | |
87 | "l", modifiers: EventModifiers([.command]) | |
88 | ).disabled(document == nil) | |
89 | } else { | |
90 | Button("Use Horizontal Layout") { | |
91 | viewStyle = .horizontal | |
92 | }.keyboardShortcut( | |
93 | "l", modifiers: EventModifiers([.command]) | |
94 | ).disabled(document == nil) | |
95 | } | |
96 | Divider() | |
97 | Button("Zoom In") { | |
98 | zoom = min(Constants.kMaxZoom, zoom + 0.1) | |
99 | }.keyboardShortcut( | |
100 | "+", modifiers: EventModifiers([.command]) | |
101 | ).disabled(document == nil) | |
102 | Button("Zoom Out") { | |
103 | zoom = max(Constants.kMinZoom, zoom - 0.1) | |
104 | }.keyboardShortcut( | |
105 | "-", modifiers: EventModifiers([.command]) | |
106 | ).disabled(document == nil) | |
107 | Divider() | |
108 | } | |
109 | ||
110 | CommandGroup(replacing: CommandGroupPlacement.help) { | |
111 | Button("Map Help") { NSWorkspace.shared.open(URL(string: "https://map.tranquil.systems")!) } | |
112 | } | |
113 | } | |
114 | } |