-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogManager.swift
More file actions
165 lines (140 loc) · 4.79 KB
/
DialogManager.swift
File metadata and controls
165 lines (140 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// DialogManager.swift
// MistTray
//
import Cocoa
import UniformTypeIdentifiers
class DialogManager: NSObject, NSWindowDelegate {
static let shared = DialogManager()
private override init() { super.init() }
/// Activate the app and ensure the alert renders as a proper foreground window.
/// Required because we show alerts from a .nonActivatingPanel where the app
/// isn't the active application, causing default buttons to render without accent color.
private func activateForAlert(_ alert: NSAlert) {
NSApp.activate(ignoringOtherApps: true)
alert.window.level = .modalPanel
alert.window.makeKeyAndOrderFront(nil)
}
func windowWillClose(_ notification: Notification) {
NSApp.stopModal(withCode: .cancel)
}
// MARK: - Alert Dialogs
func showSuccessAlert(title: String, message: String) {
let alert = NSAlert()
alert.messageText = title
alert.informativeText = message
alert.alertStyle = .informational
alert.addButton(withTitle: "OK")
activateForAlert(alert)
alert.runModal()
}
func showErrorAlert(title: String, message: String) {
let alert = NSAlert()
alert.messageText = title
alert.informativeText = message
alert.alertStyle = .critical
alert.addButton(withTitle: "OK")
activateForAlert(alert)
alert.runModal()
}
func showWarningAlert(
title: String, message: String, confirmButtonTitle: String = "Continue",
cancelButtonTitle: String = "Cancel"
) -> Bool {
let alert = NSAlert()
alert.messageText = title
alert.informativeText = message
alert.alertStyle = .warning
alert.addButton(withTitle: confirmButtonTitle)
alert.addButton(withTitle: cancelButtonTitle)
activateForAlert(alert)
return alert.runModal() == .alertFirstButtonReturn
}
func showInfoAlert(title: String, message: String) {
let alert = NSAlert()
alert.messageText = title
alert.informativeText = message
alert.alertStyle = .informational
alert.addButton(withTitle: "OK")
activateForAlert(alert)
alert.runModal()
}
// MARK: - Configuration Backup & Restore
func showBackupConfigurationDialog(completion: @escaping (URL?) -> Void) {
let savePanel = NSSavePanel()
savePanel.allowedContentTypes = [UTType.json]
savePanel.nameFieldStringValue = "mistserver-backup.json"
let response = savePanel.runModal()
completion(response == .OK ? savePanel.url : nil)
}
func showRestoreConfigurationDialog(completion: @escaping (URL?) -> Void) {
let openPanel = NSOpenPanel()
openPanel.allowedContentTypes = [UTType.json]
openPanel.canChooseFiles = true
openPanel.canChooseDirectories = false
openPanel.allowsMultipleSelection = false
let response = openPanel.runModal()
completion(response == .OK ? openPanel.url : nil)
}
func showExportConfigurationDialog(completion: @escaping (URL?) -> Void) {
let savePanel = NSSavePanel()
savePanel.allowedContentTypes = [UTType.json]
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd-HHmmss"
savePanel.nameFieldStringValue =
"mistserver-export-\(dateFormatter.string(from: Date())).json"
let response = savePanel.runModal()
completion(response == .OK ? savePanel.url : nil)
}
func confirmRestoreConfiguration() -> Bool {
return showWarningAlert(
title: "Restore Configuration",
message:
"This will replace the current configuration. The server will restart. Are you sure?",
confirmButtonTitle: "Restore")
}
func confirmFactoryReset() -> Bool {
return showWarningAlert(
title: "Factory Reset",
message:
"This will reset ALL configuration to factory defaults. This action cannot be undone.",
confirmButtonTitle: "Reset")
}
// MARK: - Confirmation Dialogs
func showConfirmationAlert(
title: String, message: String, confirmButtonTitle: String = "OK", isDestructive: Bool = false,
completion: @escaping (Bool) -> Void
) {
let alert = NSAlert()
alert.messageText = title
alert.informativeText = message
alert.alertStyle = isDestructive ? .warning : .informational
alert.addButton(withTitle: confirmButtonTitle)
alert.addButton(withTitle: "Cancel")
if isDestructive, let confirmButton = alert.buttons.first {
confirmButton.hasDestructiveAction = true
}
activateForAlert(alert)
let response = alert.runModal()
completion(response == .alertFirstButtonReturn)
}
}
// MARK: - Configuration Data Structures
struct StreamConfiguration {
let name: String
let source: String
}
struct PushConfiguration {
let streamName: String
let targetURL: String
}
struct PreferencesSettings {
let autoUpdateEnabled: Bool
let startServerOnLaunch: Bool
let showNotifications: Bool
}
enum AutoPushRuleAction {
case addRule
case deleteRule(String)
case close
}