6 var selectedEvolution: StageType = .general
10 case selectEvolution(evolution: StageType)
11 case exportMapAsImage(map: Map)
12 case exportMapAsText(map: Map)
13 case deleteMap(map: Map)
16 func appStateReducer(state: inout AppState, action: AppAction) {
20 case .selectEvolution(let evolution):
21 state.selectedEvolution = evolution
23 case .exportMapAsImage(let map):
24 let window = NSWindow(
28 width: NSScreen.main!.frame.width,
29 height: NSScreen.main!.frame.height)),
30 styleMask: [.closable],
34 window.title = map.title ?? "Untitled Map"
35 window.isOpaque = true
37 window.isMovableByWindowBackground = true
38 window.makeKeyAndOrderFront(nil)
40 let renderView = MapRenderView(
41 content: map.content ?? "", evolution: Stage.stages(state.selectedEvolution))
43 let view = NSHostingView(rootView: renderView)
44 window.contentView = view
46 let imageRepresentation = view.bitmapImageRepForCachingDisplay(in: view.bounds)!
47 view.cacheDisplay(in: view.bounds, to: imageRepresentation)
48 let image = NSImage(cgImage: imageRepresentation.cgImage!, size: view.bounds.size)
50 let dialog = NSSavePanel()
52 dialog.title = "Save Map"
53 dialog.showsResizeIndicator = false
54 dialog.canCreateDirectories = true
55 dialog.showsHiddenFiles = false
56 dialog.allowedFileTypes = ["png"]
57 dialog.nameFieldStringValue = map.title ?? "Untitled Map"
59 if dialog.runModal() == NSApplication.ModalResponse.OK {
60 let result = dialog.url
64 image.writePNG(toURL: result!)
65 print("saved at \(result!)")
72 case .exportMapAsText(let map):
73 let dialog = NSSavePanel()
75 dialog.title = "Save Map Text"
76 dialog.showsResizeIndicator = false
77 dialog.canCreateDirectories = true
78 dialog.showsHiddenFiles = false
79 dialog.allowedFileTypes = ["txt"]
80 dialog.nameFieldStringValue = map.title ?? "Untitled Map"
82 if let content = map.content {
84 if dialog.runModal() == NSApplication.ModalResponse.OK {
85 let result = dialog.url
87 if let result = result {
88 try? content.write(to: result, atomically: true, encoding: String.Encoding.utf8)
94 case .deleteMap(map: let map):
95 let context = PersistenceController.shared.container.viewContext
102 typealias AppStore = Store<AppState, AppAction>