diff options
Diffstat (limited to 'Map/Views/SlowMapRender.swift')
| -rw-r--r-- | Map/Views/SlowMapRender.swift | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/Map/Views/SlowMapRender.swift b/Map/Views/SlowMapRender.swift new file mode 100644 index 0000000..b7cd842 --- /dev/null +++ b/Map/Views/SlowMapRender.swift @@ -0,0 +1,90 @@ +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) + } +} |