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: Binding.constant(map.content ?? ""),
42 evolution: Binding.constant(state.selectedEvolution))
44 let view = NSHostingView(rootView: renderView)
45 window.contentView = view
47 let imageRepresentation = view.bitmapImageRepForCachingDisplay(in: view.bounds)!
48 view.cacheDisplay(in: view.bounds, to: imageRepresentation)
49 let image = NSImage(cgImage: imageRepresentation.cgImage!, size: view.bounds.size)
51 let dialog = NSSavePanel()
53 dialog.title = "Save Map"
54 dialog.showsResizeIndicator = false
55 dialog.canCreateDirectories = true
56 dialog.showsHiddenFiles = false
57 dialog.allowedContentTypes = [.png]
58 dialog.nameFieldStringValue = map.title ?? "Untitled Map"
60 if dialog.runModal() == NSApplication.ModalResponse.OK {
61 let result = dialog.url
65 image.writePNG(toURL: result!)
66 print("saved at \(result!)")
73 case .exportMapAsText(let map):
74 let dialog = NSSavePanel()
76 dialog.title = "Save Map Text"
77 dialog.showsResizeIndicator = false
78 dialog.canCreateDirectories = true
79 dialog.showsHiddenFiles = false
80 dialog.allowedContentTypes = [.text]
81 dialog.nameFieldStringValue = map.title ?? "Untitled Map"
83 if let content = map.content {
85 if dialog.runModal() == NSApplication.ModalResponse.OK {
86 let result = dialog.url
88 if let result = result {
89 try? content.write(to: result, atomically: true, encoding: String.Encoding.utf8)
95 case .deleteMap(let map):
96 let context = PersistenceController.shared.container.viewContext
103 typealias AppStore = Store<AppState, AppAction>