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.
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)
}

View File

@ -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))
}
}

View File

@ -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<ViewModel: ConfigurationErrorsViewModel>: View {
@ObservedObject var model: ViewModel
var body: some View {
VStack {