6 var selectedMap: Map? = nil
7 var mapBeingDeleted: Map? = nil
11 case selectMap(map: Map?)
12 case deleteMap(map: Map)
13 case exportMapAsImage(map: Map, evolution: StageType)
14 case exportMapAsText(map: Map)
17 func appStateReducer(state: inout AppState, action: AppAction) {
21 case .selectMap(let map):
22 state.selectedMap = map
24 case .deleteMap(let map):
25 let context = PersistenceController.shared.container.viewContext
30 case .exportMapAsImage(let map, let evolution):
31 let window = NSWindow(
35 width: NSScreen.main!.frame.width,
36 height: NSScreen.main!.frame.height)),
37 styleMask: [.closable],
41 window.title = map.title ?? "Untitled Map"
42 window.isOpaque = true
44 window.isMovableByWindowBackground = true
45 window.makeKeyAndOrderFront(nil)
47 let renderView = MapRenderView(map: map, evolution: Stage.stages(evolution))
49 let view = NSHostingView(rootView: renderView)
50 window.contentView = view
52 let imageRepresentation = view.bitmapImageRepForCachingDisplay(in: view.bounds)!
53 view.cacheDisplay(in: view.bounds, to: imageRepresentation)
54 let image = NSImage(cgImage: imageRepresentation.cgImage!, size: view.bounds.size)
56 let dialog = NSSavePanel()
58 dialog.title = "Save Map"
59 dialog.showsResizeIndicator = false
60 dialog.canCreateDirectories = true
61 dialog.showsHiddenFiles = false
62 dialog.allowedFileTypes = ["png"]
63 dialog.nameFieldStringValue = map.title ?? "Untitled Map"
65 if dialog.runModal() == NSApplication.ModalResponse.OK {
66 let result = dialog.url
70 image.writePNG(toURL: result!)
71 print("saved at \(result!)")
78 case .exportMapAsText(let map):
79 let dialog = NSSavePanel()
81 dialog.title = "Save Map Text"
82 dialog.showsResizeIndicator = false
83 dialog.canCreateDirectories = true
84 dialog.showsHiddenFiles = false
85 dialog.allowedFileTypes = ["txt"]
86 dialog.nameFieldStringValue = map.title ?? "Untitled Map"
88 if let content = map.content {
90 if dialog.runModal() == NSApplication.ModalResponse.OK {
91 let result = dialog.url
93 if let result = result {
94 try? content.write(to: result, atomically: true, encoding: String.Encoding.utf8)
103 typealias AppStore = Store<AppState, AppAction>