From 0fbb5c8c70827554b17615c0b0f74e5ac0285cf1 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 30 Oct 2023 22:25:39 -0700 Subject: [PATCH] macos: simpler mechanism to detect no more config errors Fixes #702 --- macos/Sources/AppDelegate.swift | 4 +-- .../ConfigurationErrorsController.swift | 30 ++++++------------- .../Settings/ConfigurationErrorsView.swift | 12 ++++---- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/macos/Sources/AppDelegate.swift b/macos/Sources/AppDelegate.swift index edda1eaba..a9ea30eeb 100644 --- a/macos/Sources/AppDelegate.swift +++ b/macos/Sources/AppDelegate.swift @@ -248,8 +248,8 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp // If we have configuration errors, we need to show them. let c = ConfigurationErrorsController.sharedInstance - c.model.errors = state.configErrors() - if (c.model.errors.count > 0) { + c.errors = state.configErrors() + if (c.errors.count > 0) { if (c.window == nil || !c.window!.isVisible) { c.showWindow(self) } diff --git a/macos/Sources/Features/Settings/ConfigurationErrorsController.swift b/macos/Sources/Features/Settings/ConfigurationErrorsController.swift index fc74a2aad..b17ce5aab 100644 --- a/macos/Sources/Features/Settings/ConfigurationErrorsController.swift +++ b/macos/Sources/Features/Settings/ConfigurationErrorsController.swift @@ -3,43 +3,31 @@ import Cocoa import SwiftUI import Combine -class ConfigurationErrorsController: NSWindowController, NSWindowDelegate { +class ConfigurationErrorsController: NSWindowController, NSWindowDelegate, ConfigurationErrorsViewModel { /// Singleton for the errors view. static let sharedInstance = ConfigurationErrorsController() override var windowNibName: NSNib.Name? { "ConfigurationErrors" } /// The data model for this view. Update this directly and the associated view will be updated, too. - let model = ConfigurationErrorsView.Model() - - private var cancellable: AnyCancellable? + @Published var errors: [String] = [] { + didSet { + if (errors.count == 0) { + self.window?.performClose(nil) + } + } + } //MARK: - NSWindowController override func windowWillLoad() { shouldCascadeWindows = false - - if let c = cancellable { c.cancel() } - cancellable = model.$errors.sink { newValue in - if (newValue.count == 0) { - self.window?.close() - } - } } override func windowDidLoad() { guard let window = window else { return } window.center() window.level = .popUpMenu - window.contentView = NSHostingView(rootView: ConfigurationErrorsView(model: model)) - } - - //MARK: - NSWindowDelegate - - func windowWillClose(_ notification: Notification) { - if let cancellable = cancellable { - cancellable.cancel() - self.cancellable = nil - } + window.contentView = NSHostingView(rootView: ConfigurationErrorsView(model: self)) } } diff --git a/macos/Sources/Features/Settings/ConfigurationErrorsView.swift b/macos/Sources/Features/Settings/ConfigurationErrorsView.swift index 4d016d4e2..dabfd1a3b 100644 --- a/macos/Sources/Features/Settings/ConfigurationErrorsView.swift +++ b/macos/Sources/Features/Settings/ConfigurationErrorsView.swift @@ -1,11 +1,11 @@ import SwiftUI -struct ConfigurationErrorsView: View { - class Model: ObservableObject { - @Published var errors: [String] = [] - } - - @ObservedObject var model: Model +protocol ConfigurationErrorsViewModel: ObservableObject { + var errors: [String] { get set } +} + +struct ConfigurationErrorsView: View { + @ObservedObject var model: ViewModel var body: some View { VStack {