+import Cocoa
+import SwiftUI
+
+class SlowMapRenderController: NSViewController {
+
+ var content: String
+ private var contentBinding: Binding<String> {
+ Binding(
+ get: { self.content },
+ set: { content in
+ self.content = content
+ }
+ )
+ }
+
+ var evolution: Stage
+ private var evolutionBinding: Binding<Stage> {
+ Binding(
+ get: { self.evolution },
+ set: { evolution in
+ self.evolution = evolution
+ }
+ )
+ }
+ private var colorSchemeEnvironment: ObservableColorSchemeEnvironment
+
+ private let debouncer: Debouncer = Debouncer(seconds: 0.1)
+
+ init(content: String, evolution: Stage, colorScheme: ColorScheme) {
+
+ self.content = content
+ self.evolution = evolution
+ self.colorSchemeEnvironment = ObservableColorSchemeEnvironment(colorScheme: colorScheme)
+ super.init(nibName: nil, bundle: nil)
+ }
+
+ required init?(coder: NSCoder) {
+ fatalError("init(coder:) has not been implemented")
+ }
+
+ override func loadView() {
+
+ let renderView = MapRenderView(content: self.contentBinding, evolution: self.evolutionBinding)
+ .environmentObject(self.colorSchemeEnvironment)
+ let hostingView = NSHostingView(rootView: renderView)
+
+ self.view = hostingView
+ }
+
+ func update(content: String, evolution: Stage, colorScheme: ColorScheme) {
+ self.debouncer.debounce {
+ DispatchQueue.main.async {
+ print("Updating: START")
+ self.content = content
+ self.evolution = evolution
+ self.colorSchemeEnvironment.colorScheme = colorScheme
+ print("Updating: END")
+ }
+ }
+ }
+
+ private class ObservableColorSchemeEnvironment: ObservableObject {
+ @Published var colorScheme: ColorScheme
+
+ init(colorScheme: ColorScheme) {
+ self.colorScheme = colorScheme
+ }
+ }
+}
+
+struct SlowMapRender: NSViewControllerRepresentable {
+
+ var content: String
+ var evolution: Stage
+ let colorScheme: ColorScheme
+
+ func makeNSViewController(
+ context: NSViewControllerRepresentableContext<SlowMapRender>
+ ) -> SlowMapRenderController {
+ return SlowMapRenderController(content: content, evolution: evolution, colorScheme: colorScheme)
+ }
+
+ func updateNSViewController(
+ _ nsViewController: SlowMapRenderController,
+ context: NSViewControllerRepresentableContext<SlowMapRender>
+ ) {
+ nsViewController.update(
+ content: content, evolution: evolution, colorScheme: context.environment.colorScheme)
+ }
+}