macos: simpler mechanism to detect no more config errors

Fixes #702
This commit is contained in:
Mitchell Hashimoto
2023-10-30 22:25:39 -07:00
parent 07c4692799
commit 0fbb5c8c70
3 changed files with 17 additions and 29 deletions

View File

@ -248,8 +248,8 @@ class AppDelegate: NSObject, ObservableObject, NSApplicationDelegate, GhosttyApp
// If we have configuration errors, we need to show them. // If we have configuration errors, we need to show them.
let c = ConfigurationErrorsController.sharedInstance let c = ConfigurationErrorsController.sharedInstance
c.model.errors = state.configErrors() c.errors = state.configErrors()
if (c.model.errors.count > 0) { if (c.errors.count > 0) {
if (c.window == nil || !c.window!.isVisible) { if (c.window == nil || !c.window!.isVisible) {
c.showWindow(self) c.showWindow(self)
} }

View File

@ -3,43 +3,31 @@ import Cocoa
import SwiftUI import SwiftUI
import Combine import Combine
class ConfigurationErrorsController: NSWindowController, NSWindowDelegate { class ConfigurationErrorsController: NSWindowController, NSWindowDelegate, ConfigurationErrorsViewModel {
/// Singleton for the errors view. /// Singleton for the errors view.
static let sharedInstance = ConfigurationErrorsController() static let sharedInstance = ConfigurationErrorsController()
override var windowNibName: NSNib.Name? { "ConfigurationErrors" } override var windowNibName: NSNib.Name? { "ConfigurationErrors" }
/// The data model for this view. Update this directly and the associated view will be updated, too. /// The data model for this view. Update this directly and the associated view will be updated, too.
let model = ConfigurationErrorsView.Model() @Published var errors: [String] = [] {
didSet {
private var cancellable: AnyCancellable? if (errors.count == 0) {
self.window?.performClose(nil)
}
}
}
//MARK: - NSWindowController //MARK: - NSWindowController
override func windowWillLoad() { override func windowWillLoad() {
shouldCascadeWindows = false shouldCascadeWindows = false
if let c = cancellable { c.cancel() }
cancellable = model.$errors.sink { newValue in
if (newValue.count == 0) {
self.window?.close()
}
}
} }
override func windowDidLoad() { override func windowDidLoad() {
guard let window = window else { return } guard let window = window else { return }
window.center() window.center()
window.level = .popUpMenu window.level = .popUpMenu
window.contentView = NSHostingView(rootView: ConfigurationErrorsView(model: model)) window.contentView = NSHostingView(rootView: ConfigurationErrorsView(model: self))
}
//MARK: - NSWindowDelegate
func windowWillClose(_ notification: Notification) {
if let cancellable = cancellable {
cancellable.cancel()
self.cancellable = nil
}
} }
} }

View File

@ -1,11 +1,11 @@
import SwiftUI import SwiftUI
struct ConfigurationErrorsView: View { protocol ConfigurationErrorsViewModel: ObservableObject {
class Model: ObservableObject { var errors: [String] { get set }
@Published var errors: [String] = [] }
}
struct ConfigurationErrorsView<ViewModel: ConfigurationErrorsViewModel>: View {
@ObservedObject var model: Model @ObservedObject var model: ViewModel
var body: some View { var body: some View {
VStack { VStack {