]> git.r.bdr.sh - rbdr/map/blobdiff - Map/Presentation/Commands/MapCommands.swift
Add search
[rbdr/map] / Map / Presentation / Commands / MapCommands.swift
index b0fbf9cf2447112b13998a34d18028e187514cc4..83c4b273eb60a49459b07feb3a2ade21fad35064 100644 (file)
@@ -20,9 +20,63 @@ struct MapCommands: Commands {
 
   @AppStorage("viewStyle") var viewStyle: ViewStyle = .horizontal
   @AppStorage("zoom") var zoom = 1.0
+  @FocusedBinding(\.document) var document: MapDocument?
+  @FocusedValue(\.fileURL) var url: URL?
+  @FocusedBinding(\.selectedEvolution) var selectedEvolution: StageType?
+  @FocusedBinding(\.isSearching) var isSearching
 
   var body: some Commands {
 
+    // File
+
+    CommandGroup(after: CommandGroupPlacement.saveItem) {
+      Divider()
+      Button("Export...") {
+        if let selectedEvolution, let document {
+          if let image = document.exportAsImage(withEvolution: selectedEvolution) {
+
+            let filename = url?.deletingPathExtension().lastPathComponent ?? "Untitled"
+
+            let savePanel = NSSavePanel()
+            savePanel.allowedContentTypes = [.png]
+            savePanel.canCreateDirectories = true
+            savePanel.isExtensionHidden = false
+            savePanel.title = "Save \(filename) as image"
+            savePanel.message = "Choose a location to save the image"
+            savePanel.nameFieldStringValue = "\(filename).png"
+            savePanel.begin { result in
+              if result == .OK, let url = savePanel.url {
+                if let tiffRepresentation = image.tiffRepresentation {
+                  let bitmapImage = NSBitmapImageRep(data: tiffRepresentation)
+                  let pngData = bitmapImage?.representation(using: .png, properties: [:])
+                  do {
+                    try pngData?.write(to: url)
+                  } catch {
+                    return
+                  }
+                }
+              }
+            }
+          }
+        }
+      }.keyboardShortcut(
+        "e", modifiers: EventModifiers([.command])
+      ).disabled(document == nil)
+    }
+
+    // Edit
+
+    CommandGroup(after: CommandGroupPlacement.pasteboard) {
+      Divider()
+      Button("Find...") {
+        withAnimation {
+          isSearching = isSearching != nil ? !isSearching! : true
+        }
+      }.keyboardShortcut(
+        "f", modifiers: EventModifiers([.command])
+      ).disabled(document == nil)
+    }
+
     // View
 
     CommandGroup(after: CommandGroupPlacement.toolbar) {
@@ -31,25 +85,25 @@ struct MapCommands: Commands {
           viewStyle = .vertical
         }.keyboardShortcut(
           "l", modifiers: EventModifiers([.command])
-        )
+        ).disabled(document == nil)
       } else {
         Button("Use Horizontal Layout") {
           viewStyle = .horizontal
         }.keyboardShortcut(
           "l", modifiers: EventModifiers([.command])
-        )
+        ).disabled(document == nil)
       }
       Divider()
       Button("Zoom In") {
         zoom = min(Constants.kMaxZoom, zoom + 0.1)
       }.keyboardShortcut(
         "+", modifiers: EventModifiers([.command])
-      )
+      ).disabled(document == nil)
       Button("Zoom Out") {
         zoom = max(Constants.kMinZoom, zoom - 0.1)
       }.keyboardShortcut(
         "-", modifiers: EventModifiers([.command])
-      )
+      ).disabled(document == nil)
       Divider()
     }