]> git.r.bdr.sh - rbdr/map/blob - Map/Views/SlowMapRender.swift
b7cd8423bcf0a5786bc2de3a79157ead7509e177
[rbdr/map] / Map / Views / SlowMapRender.swift
1 import Cocoa
2 import SwiftUI
3
4 class SlowMapRenderController: NSViewController {
5
6 var content: String
7 private var contentBinding: Binding<String> {
8 Binding(
9 get: { self.content },
10 set: { content in
11 self.content = content
12 }
13 )
14 }
15
16 var evolution: Stage
17 private var evolutionBinding: Binding<Stage> {
18 Binding(
19 get: { self.evolution },
20 set: { evolution in
21 self.evolution = evolution
22 }
23 )
24 }
25 private var colorSchemeEnvironment: ObservableColorSchemeEnvironment
26
27 private let debouncer: Debouncer = Debouncer(seconds: 0.1)
28
29 init(content: String, evolution: Stage, colorScheme: ColorScheme) {
30
31 self.content = content
32 self.evolution = evolution
33 self.colorSchemeEnvironment = ObservableColorSchemeEnvironment(colorScheme: colorScheme)
34 super.init(nibName: nil, bundle: nil)
35 }
36
37 required init?(coder: NSCoder) {
38 fatalError("init(coder:) has not been implemented")
39 }
40
41 override func loadView() {
42
43 let renderView = MapRenderView(content: self.contentBinding, evolution: self.evolutionBinding)
44 .environmentObject(self.colorSchemeEnvironment)
45 let hostingView = NSHostingView(rootView: renderView)
46
47 self.view = hostingView
48 }
49
50 func update(content: String, evolution: Stage, colorScheme: ColorScheme) {
51 self.debouncer.debounce {
52 DispatchQueue.main.async {
53 print("Updating: START")
54 self.content = content
55 self.evolution = evolution
56 self.colorSchemeEnvironment.colorScheme = colorScheme
57 print("Updating: END")
58 }
59 }
60 }
61
62 private class ObservableColorSchemeEnvironment: ObservableObject {
63 @Published var colorScheme: ColorScheme
64
65 init(colorScheme: ColorScheme) {
66 self.colorScheme = colorScheme
67 }
68 }
69 }
70
71 struct SlowMapRender: NSViewControllerRepresentable {
72
73 var content: String
74 var evolution: Stage
75 let colorScheme: ColorScheme
76
77 func makeNSViewController(
78 context: NSViewControllerRepresentableContext<SlowMapRender>
79 ) -> SlowMapRenderController {
80 return SlowMapRenderController(content: content, evolution: evolution, colorScheme: colorScheme)
81 }
82
83 func updateNSViewController(
84 _ nsViewController: SlowMapRenderController,
85 context: NSViewControllerRepresentableContext<SlowMapRender>
86 ) {
87 nsViewController.update(
88 content: content, evolution: evolution, colorScheme: context.environment.colorScheme)
89 }
90 }