4 class SlowMapRenderController: NSViewController {
7 private var contentBinding: Binding<String> {
11 self.content = content
17 private var evolutionBinding: Binding<Stage> {
19 get: { self.evolution },
21 self.evolution = evolution
25 private var colorSchemeEnvironment: ObservableColorSchemeEnvironment
27 private let debouncer: Debouncer = Debouncer(seconds: 0.1)
29 init(content: String, evolution: Stage, colorScheme: ColorScheme) {
31 self.content = content
32 self.evolution = evolution
33 self.colorSchemeEnvironment = ObservableColorSchemeEnvironment(colorScheme: colorScheme)
34 super.init(nibName: nil, bundle: nil)
37 required init?(coder: NSCoder) {
38 fatalError("init(coder:) has not been implemented")
41 override func loadView() {
43 let renderView = MapRenderView(content: self.contentBinding, evolution: self.evolutionBinding)
44 .environmentObject(self.colorSchemeEnvironment)
45 let hostingView = NSHostingView(rootView: renderView)
47 self.view = hostingView
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")
62 private class ObservableColorSchemeEnvironment: ObservableObject {
63 @Published var colorScheme: ColorScheme
65 init(colorScheme: ColorScheme) {
66 self.colorScheme = colorScheme
71 struct SlowMapRender: NSViewControllerRepresentable {
75 let colorScheme: ColorScheme
77 func makeNSViewController(
78 context: NSViewControllerRepresentableContext<SlowMapRender>
79 ) -> SlowMapRenderController {
80 return SlowMapRenderController(content: content, evolution: evolution, colorScheme: colorScheme)
83 func updateNSViewController(
84 _ nsViewController: SlowMapRenderController,
85 context: NSViewControllerRepresentableContext<SlowMapRender>
87 nsViewController.update(
88 content: content, evolution: evolution, colorScheme: context.environment.colorScheme)