Skip to contentSkip to conversation

macOS: activate the app before showing a modal alert without a window

PATCH New Razvan Cremenescu 1 participant 2 messages Oct 5

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 macOS: activate the app before showing a modal alert without a window

WireGuard runs as a menu bar accessory app. When an error alert has no source window to attach to as a sheet - e.g. an activation failure while the Manage Tunnels window is closed - it falls back to NSAlert.runModal(). Shown while the app is inactive, the modal window comes up behind everything with no way to reach it, and the modal loop freezes the whole app until it is dismissed. Activate the app first so the alert is actually visible.

---
 Sources/WireGuardApp/UI/macOS/ErrorPresenter.swift | 5 +++++
 1 file changed, 5 insertions(+)
Sources/WireGuardApp/UI/macOS/ErrorPresenter.swift +5 -0
index dbab027..b3884ff 100644
@@ -16,6 +16,11 @@ class ErrorPresenter: ErrorPresenterProtocol {
                 onDismissal?()
             }
         } else {
+            // WireGuard runs as a menu bar (accessory) app, which is usually
+            // not the active app. A modal alert shown while inactive comes up
+            // behind other windows with no way to reach it, freezing the app
+            // until it is dismissed. Activate first so the alert is visible.
+            NSApp.activate(ignoringOtherApps: true)
             alert.runModal()
             onDismissal?()
         }
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 2/2 macOS: don't crash opening the editor for an unreadable tunnel

TunnelEditViewController.populateFields() force-unwraps the tunnel's configuration. When the saved configuration cannot be read - for instance when its keychain entry is inaccessible - that configuration is nil and opening the editor traps. Check that the configuration is readable before presenting the editor and report the failure instead.

---
 Sources/WireGuardApp/Base.lproj/Localizable.strings        | 1 +
 .../ViewController/TunnelDetailTableViewController.swift   | 7 +++++++
 2 files changed, 8 insertions(+)
Sources/WireGuardApp/Base.lproj/Localizable.strings +1 -0
index cacb316..23037ce 100644
@@ -385,6 +385,7 @@
 
 "macAlertNameIsEmpty" = "Name is required";
 "macAlertDuplicateName (%@)" = "Another tunnel already exists with the name ‘%@’.";
+"macAlertCannotReadConfigTitle" = "Unable to read configuration";
 
 "macAlertInvalidLine (%@)" = "Invalid line: ‘%@’.";
 
Sources/WireGuardApp/UI/macOS/ViewController/TunnelDetailTableViewController.swift +7 -0
index 41d2b15..f1526d7 100644
@@ -208,6 +208,13 @@ class TunnelDetailTableViewController: NSViewController {
     @objc func handleEditTunnelAction() {
         PrivateDataConfirmation.confirmAccess(to: tr("macViewPrivateData")) { [weak self] in
             guard let self = self else { return }
+            guard self.tunnel.tunnelConfiguration != nil else {
+                // The configuration could not be read (e.g. the keychain entry
+                // is inaccessible). Opening the editor would crash, so report
+                // the failure instead.
+                ErrorPresenter.showErrorAlert(title: tr("macAlertCannotReadConfigTitle"), message: tr("alertTunnelActivationSavedConfigFailureMessage"), from: self)
+                return
+            }
             let tunnelEditVC = TunnelEditViewController(tunnelsManager: self.tunnelsManager, tunnel: self.tunnel)
             tunnelEditVC.delegate = self
             self.presentAsSheet(tunnelEditVC)
Signature · 1 line
2.50.1 (Apple Git-155)