]> git.r.bdr.sh - rbdr/map/blame - Map/Presentation/Commands/MapCommands.swift
Add some debouncing
[rbdr/map] / Map / Presentation / Commands / MapCommands.swift
CommitLineData
be897af3 1// Copyright (C) 2024 Rubén Beltrán del Río
98f09799 2
be897af3
RBR
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
98f09799 7
be897af3
RBR
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
98f09799 12
be897af3
RBR
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see https://map.tranquil.systems.
e2c37ac1
RBR
15import SwiftUI
16
17struct MapCommands: Commands {
18
19 @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
20 @AppStorage("zoom") var zoom = 1.0
14491563
RBR
21 @FocusedBinding(\.document) var document: MapDocument?
22 @FocusedValue(\.fileURL) var url: URL?
23 @FocusedBinding(\.selectedEvolution) var selectedEvolution: StageType?
24 @FocusedBinding(\.isSearching) var isSearching
e2c37ac1
RBR
25
26 var body: some Commands {
27
14491563
RBR
28 // File
29
30 CommandGroup(after: CommandGroupPlacement.saveItem) {
31 Divider()
32 Button("Export...") {
33 if let selectedEvolution, let document {
34 if let image = document.exportAsImage(withEvolution: selectedEvolution) {
35
36 let filename = url?.deletingPathExtension().lastPathComponent ?? "Untitled"
37
38 let savePanel = NSSavePanel()
39 savePanel.allowedContentTypes = [.png]
40 savePanel.canCreateDirectories = true
41 savePanel.isExtensionHidden = false
42 savePanel.title = "Save \(filename) as image"
43 savePanel.message = "Choose a location to save the image"
44 savePanel.nameFieldStringValue = "\(filename).png"
45 savePanel.begin { result in
46 if result == .OK, let url = savePanel.url {
47 if let tiffRepresentation = image.tiffRepresentation {
48 let bitmapImage = NSBitmapImageRep(data: tiffRepresentation)
49 let pngData = bitmapImage?.representation(using: .png, properties: [:])
50 do {
51 try pngData?.write(to: url)
52 } catch {
53 return
54 }
55 }
56 }
57 }
58 }
59 }
60 }.keyboardShortcut(
61 "e", modifiers: EventModifiers([.command])
62 ).disabled(document == nil)
63 }
64
65 // Edit
66
67 CommandGroup(after: CommandGroupPlacement.pasteboard) {
68 Divider()
69 Button("Find...") {
ed10ac19 70 isSearching = isSearching != nil ? !isSearching! : true
14491563
RBR
71 }.keyboardShortcut(
72 "f", modifiers: EventModifiers([.command])
73 ).disabled(document == nil)
74 }
75
e2c37ac1
RBR
76 // View
77
78 CommandGroup(after: CommandGroupPlacement.toolbar) {
79 if viewStyle == .horizontal {
80 Button("Use Vertical Layout") {
81 viewStyle = .vertical
82 }.keyboardShortcut(
83 "l", modifiers: EventModifiers([.command])
14491563 84 ).disabled(document == nil)
e2c37ac1
RBR
85 } else {
86 Button("Use Horizontal Layout") {
87 viewStyle = .horizontal
88 }.keyboardShortcut(
89 "l", modifiers: EventModifiers([.command])
14491563 90 ).disabled(document == nil)
e2c37ac1
RBR
91 }
92 Divider()
93 Button("Zoom In") {
94 zoom = min(Constants.kMaxZoom, zoom + 0.1)
95 }.keyboardShortcut(
96 "+", modifiers: EventModifiers([.command])
14491563 97 ).disabled(document == nil)
e2c37ac1
RBR
98 Button("Zoom Out") {
99 zoom = max(Constants.kMinZoom, zoom - 0.1)
100 }.keyboardShortcut(
101 "-", modifiers: EventModifiers([.command])
14491563 102 ).disabled(document == nil)
e2c37ac1
RBR
103 Divider()
104 }
105
106 CommandGroup(replacing: CommandGroupPlacement.help) {
107 Button("Map Help") { NSWorkspace.shared.open(URL(string: "https://map.tranquil.systems")!) }
108 }
109 }
110}