|
6 | 6 | // |
7 | 7 |
|
8 | 8 | #if canImport(UIKit) |
9 | | - import UIKit |
10 | | - |
11 | | - public class AlertController { |
12 | | - |
13 | | - private var onTextChange: ((String) -> Void)? |
14 | | - |
15 | | - internal var rootController: UIViewController? { |
16 | | - guard |
17 | | - let scene = UIApplication.shared.connectedScenes.first(where: { |
18 | | - $0.activationState == .foregroundActive |
19 | | - }) as? UIWindowScene, |
20 | | - let window = scene.windows.first(where: { $0.isKeyWindow }) |
21 | | - else { |
22 | | - return nil |
23 | | - } |
24 | | - |
25 | | - var root = window.rootViewController |
26 | | - while let presentedViewController = root?.presentedViewController { |
27 | | - root = presentedViewController |
28 | | - } |
29 | | - return root |
30 | | - } |
| 9 | + import UIKit |
| 10 | + |
| 11 | + public class AlertController { |
| 12 | + |
| 13 | + private var onTextChange: ((String) -> Void)? |
| 14 | + |
| 15 | + internal var rootController: UIViewController? { |
| 16 | + guard |
| 17 | + let scene = UIApplication.shared.connectedScenes.first(where: { |
| 18 | + $0.activationState == .foregroundActive |
| 19 | + }) as? UIWindowScene, |
| 20 | + let window = scene.windows.first(where: { $0.isKeyWindow }) |
| 21 | + else { |
| 22 | + return nil |
| 23 | + } |
| 24 | + |
| 25 | + var root = window.rootViewController |
| 26 | + while let presentedViewController = root?.presentedViewController { |
| 27 | + root = presentedViewController |
| 28 | + } |
| 29 | + return root |
| 30 | + } |
31 | 31 |
|
32 | | - func showAlert( |
33 | | - title: String, |
34 | | - message: String, |
35 | | - placeholder: String? = nil, |
36 | | - defaultText: String? = nil, |
37 | | - onTextChange: ((String) -> Void)? = nil, |
38 | | - completion: @escaping (String?) -> Void |
39 | | - ) { |
40 | | - // Store the onTextChange closure |
41 | | - self.onTextChange = onTextChange |
42 | | - |
43 | | - // Create the alert controller |
44 | | - let alert = UIAlertController( |
45 | | - title: title, message: message, preferredStyle: .alert) |
46 | | - |
47 | | - // Add a text field |
48 | | - alert.addTextField { textField in |
49 | | - textField.placeholder = placeholder |
50 | | - textField.text = defaultText |
51 | | - |
52 | | - // Add a target to handle text changes |
53 | | - if onTextChange != nil { |
54 | | - textField.addTarget( |
55 | | - self, action: #selector(self.textFieldDidChange(_:)), |
56 | | - for: .editingChanged) |
57 | | - } |
58 | | - } |
59 | | - |
60 | | - alert.addAction( |
61 | | - UIAlertAction( |
62 | | - title: "OK", style: .default, |
63 | | - handler: { [weak alert] _ in |
64 | | - let textFieldText = alert?.textFields?.first?.text |
65 | | - completion(textFieldText) |
66 | | - })) |
67 | | - alert.addAction( |
68 | | - UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) |
69 | | - |
70 | | - // Present the alert |
71 | | - rootController?.present(alert, animated: true) |
| 32 | + func showAlert( |
| 33 | + title: String, |
| 34 | + message: String, |
| 35 | + placeholder: String? = nil, |
| 36 | + defaultText: String? = nil, |
| 37 | + onTextChange: ((String) -> Void)? = nil, |
| 38 | + completion: @escaping (String?) -> Void |
| 39 | + ) { |
| 40 | + // Store the onTextChange closure |
| 41 | + self.onTextChange = onTextChange |
| 42 | + |
| 43 | + // Create the alert controller |
| 44 | + let alert = UIAlertController( |
| 45 | + title: title, message: message, preferredStyle: .alert) |
| 46 | + |
| 47 | + // Add a text field |
| 48 | + alert.addTextField { textField in |
| 49 | + textField.placeholder = placeholder |
| 50 | + textField.text = defaultText |
| 51 | + |
| 52 | + // Add a target to handle text changes |
| 53 | + if onTextChange != nil { |
| 54 | + textField.addTarget( |
| 55 | + self, action: #selector(self.textFieldDidChange(_:)), |
| 56 | + for: .editingChanged) |
72 | 57 | } |
| 58 | + } |
| 59 | + |
| 60 | + alert.addAction( |
| 61 | + UIAlertAction( |
| 62 | + title: "OK", style: .default, |
| 63 | + handler: { [weak alert] _ in |
| 64 | + let textFieldText = alert?.textFields?.first?.text |
| 65 | + completion(textFieldText) |
| 66 | + })) |
| 67 | + alert.addAction( |
| 68 | + UIAlertAction(title: "Cancel", style: .cancel, handler: nil)) |
| 69 | + |
| 70 | + // Present the alert |
| 71 | + rootController?.present(alert, animated: true) |
| 72 | + } |
73 | 73 |
|
74 | | - @objc private func textFieldDidChange(_ textField: UITextField) { |
75 | | - onTextChange?(textField.text ?? "") |
76 | | - } |
| 74 | + @objc private func textFieldDidChange(_ textField: UITextField) { |
| 75 | + onTextChange?(textField.text ?? "") |
| 76 | + } |
77 | 77 |
|
78 | | - func showAlert( |
79 | | - title: String, |
80 | | - message: String, |
81 | | - onOk: (() -> Void)? = nil, |
82 | | - onCancel: (() -> Void)? = nil |
83 | | - ) { |
84 | | - // Create the alert controller |
85 | | - let alert = UIAlertController( |
86 | | - title: title, message: message, preferredStyle: .alert) |
87 | | - |
88 | | - alert.addAction( |
89 | | - UIAlertAction( |
90 | | - title: "OK", style: .default, |
91 | | - handler: { _ in |
92 | | - onOk?() |
93 | | - })) |
94 | | - alert.addAction( |
95 | | - UIAlertAction( |
96 | | - title: "Cancel", style: .cancel, |
97 | | - handler: { _ in |
98 | | - onCancel?() |
99 | | - })) |
100 | | - |
101 | | - // Present the alert |
102 | | - rootController?.present(alert, animated: true) |
103 | | - } |
| 78 | + func showAlert( |
| 79 | + title: String, |
| 80 | + message: String, |
| 81 | + onOk: (() -> Void)? = nil, |
| 82 | + onCancel: (() -> Void)? = nil |
| 83 | + ) { |
| 84 | + // Create the alert controller |
| 85 | + let alert = UIAlertController( |
| 86 | + title: title, message: message, preferredStyle: .alert) |
| 87 | + |
| 88 | + alert.addAction( |
| 89 | + UIAlertAction( |
| 90 | + title: "OK", style: .default, |
| 91 | + handler: { _ in |
| 92 | + onOk?() |
| 93 | + })) |
| 94 | + alert.addAction( |
| 95 | + UIAlertAction( |
| 96 | + title: "Cancel", style: .cancel, |
| 97 | + handler: { _ in |
| 98 | + onCancel?() |
| 99 | + })) |
| 100 | + |
| 101 | + // Present the alert |
| 102 | + rootController?.present(alert, animated: true) |
104 | 103 | } |
| 104 | + } |
105 | 105 | #endif |
106 | 106 |
|
107 | 107 | #if canImport(AppKit) |
108 | | - import AppKit |
109 | | - public class AlertController { |
| 108 | + import AppKit |
| 109 | + public class AlertController { |
110 | 110 |
|
111 | | - private var onTextChange: ((String) -> Void)? |
| 111 | + private var onTextChange: ((String) -> Void)? |
112 | 112 |
|
113 | | - // Root Window or ViewController |
114 | | - internal var rootWindow: NSWindow? { |
115 | | - return NSApp.mainWindow |
116 | | - } |
| 113 | + // Root Window or ViewController |
| 114 | + internal var rootWindow: NSWindow? { |
| 115 | + return NSApp.mainWindow |
| 116 | + } |
117 | 117 |
|
118 | | - // Show alert with a text field and real-time text change closure |
119 | | - func showAlert( |
120 | | - title: String, |
121 | | - message: String, |
122 | | - placeholder: String? = nil, |
123 | | - defaultText: String? = nil, |
124 | | - onTextChange: ((String) -> Void)? = nil, |
125 | | - completion: @escaping (String?) -> Void |
126 | | - ) { |
127 | | - // Store the onTextChange closure |
128 | | - self.onTextChange = onTextChange |
129 | | - |
130 | | - // Create the alert |
131 | | - let alert = NSAlert() |
132 | | - alert.messageText = title |
133 | | - alert.informativeText = message |
134 | | - alert.alertStyle = .informational |
135 | | - |
136 | | - // Create an input text field |
137 | | - let textField = NSTextField( |
138 | | - frame: NSRect(x: 0, y: 0, width: 200, height: 24)) |
139 | | - textField.placeholderString = placeholder |
140 | | - textField.stringValue = defaultText ?? "" |
141 | | - alert.accessoryView = textField |
142 | | - |
143 | | - // Show real-time text updates |
144 | | - if let onTextChange = onTextChange { |
145 | | - textField.target = self |
146 | | - textField.action = #selector(self.textFieldDidChange(_:)) |
147 | | - } |
148 | | - |
149 | | - // Add the OK and Cancel buttons |
150 | | - alert.addButton(withTitle: "OK") |
151 | | - alert.addButton(withTitle: "Cancel") |
152 | | - |
153 | | - // Show the alert |
154 | | - let response = alert.runModal() |
155 | | - |
156 | | - // Handle completion based on the response |
157 | | - if response == .alertFirstButtonReturn { |
158 | | - completion(textField.stringValue) |
159 | | - } else { |
160 | | - completion(nil) |
161 | | - } |
162 | | - } |
| 118 | + // Show alert with a text field and real-time text change closure |
| 119 | + func showAlert( |
| 120 | + title: String, |
| 121 | + message: String, |
| 122 | + placeholder: String? = nil, |
| 123 | + defaultText: String? = nil, |
| 124 | + onTextChange: ((String) -> Void)? = nil, |
| 125 | + completion: @escaping (String?) -> Void |
| 126 | + ) { |
| 127 | + // Store the onTextChange closure |
| 128 | + self.onTextChange = onTextChange |
| 129 | + |
| 130 | + // Create the alert |
| 131 | + let alert = NSAlert() |
| 132 | + alert.messageText = title |
| 133 | + alert.informativeText = message |
| 134 | + alert.alertStyle = .informational |
| 135 | + |
| 136 | + // Create an input text field |
| 137 | + let textField = NSTextField( |
| 138 | + frame: NSRect(x: 0, y: 0, width: 200, height: 24)) |
| 139 | + textField.placeholderString = placeholder |
| 140 | + textField.stringValue = defaultText ?? "" |
| 141 | + alert.accessoryView = textField |
| 142 | + |
| 143 | + // Show real-time text updates |
| 144 | + if onTextChange != nil { |
| 145 | + textField.target = self |
| 146 | + textField.action = #selector(self.textFieldDidChange(_:)) |
| 147 | + } |
| 148 | + |
| 149 | + // Add the OK and Cancel buttons |
| 150 | + alert.addButton(withTitle: "OK") |
| 151 | + alert.addButton(withTitle: "Cancel") |
| 152 | + |
| 153 | + // Show the alert |
| 154 | + let response = alert.runModal() |
| 155 | + |
| 156 | + // Handle completion based on the response |
| 157 | + if response == .alertFirstButtonReturn { |
| 158 | + completion(textField.stringValue) |
| 159 | + } else { |
| 160 | + completion(nil) |
| 161 | + } |
| 162 | + } |
163 | 163 |
|
164 | | - @objc private func textFieldDidChange(_ textField: NSTextField) { |
165 | | - // Call the closure with the updated text |
166 | | - onTextChange?(textField.stringValue) |
167 | | - } |
| 164 | + @objc private func textFieldDidChange(_ textField: NSTextField) { |
| 165 | + // Call the closure with the updated text |
| 166 | + onTextChange?(textField.stringValue) |
| 167 | + } |
168 | 168 |
|
169 | | - // Show a simple alert with OK and Cancel actions |
170 | | - func showAlert( |
171 | | - title: String, |
172 | | - message: String, |
173 | | - onOk: (() -> Void)? = nil, |
174 | | - onCancel: (() -> Void)? = nil |
175 | | - ) { |
176 | | - let alert = NSAlert() |
177 | | - alert.messageText = title |
178 | | - alert.informativeText = message |
179 | | - alert.alertStyle = .informational |
180 | | - alert.addButton(withTitle: "OK") |
181 | | - alert.addButton(withTitle: "Cancel") |
182 | | - |
183 | | - // Show the alert |
184 | | - let response = alert.runModal() |
185 | | - |
186 | | - // Handle actions based on the response |
187 | | - if response == .alertFirstButtonReturn { |
188 | | - onOk?() |
189 | | - } else { |
190 | | - onCancel?() |
191 | | - } |
192 | | - } |
| 169 | + // Show a simple alert with OK and Cancel actions |
| 170 | + func showAlert( |
| 171 | + title: String, |
| 172 | + message: String, |
| 173 | + onOk: (() -> Void)? = nil, |
| 174 | + onCancel: (() -> Void)? = nil |
| 175 | + ) { |
| 176 | + let alert = NSAlert() |
| 177 | + alert.messageText = title |
| 178 | + alert.informativeText = message |
| 179 | + alert.alertStyle = .informational |
| 180 | + alert.addButton(withTitle: "OK") |
| 181 | + alert.addButton(withTitle: "Cancel") |
| 182 | + |
| 183 | + // Show the alert |
| 184 | + let response = alert.runModal() |
| 185 | + |
| 186 | + // Handle actions based on the response |
| 187 | + if response == .alertFirstButtonReturn { |
| 188 | + onOk?() |
| 189 | + } else { |
| 190 | + onCancel?() |
| 191 | + } |
193 | 192 | } |
| 193 | + } |
194 | 194 | #endif |
0 commit comments