From 4d6692548ca29f5ec1e7bbef7e01aed48a2e5943 Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Wed, 5 Jun 2019 02:17:49 +0530 Subject: [PATCH] macOS: App menu > Quit shall show a prompt to quit or close window Signed-off-by: Roopesh Chander --- .../WireGuard/Base.lproj/Localizable.strings | 7 ++++- .../WireGuard/UI/macOS/AppDelegate.swift | 27 +++++++++++++++++++ WireGuard/WireGuard/UI/macOS/MainMenu.swift | 4 +-- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/WireGuard/WireGuard/Base.lproj/Localizable.strings b/WireGuard/WireGuard/Base.lproj/Localizable.strings index fead210..5d918bc 100644 --- a/WireGuard/WireGuard/Base.lproj/Localizable.strings +++ b/WireGuard/WireGuard/Base.lproj/Localizable.strings @@ -308,7 +308,6 @@ "macMenuHideApp" = "Hide WireGuard"; "macMenuHideOtherApps" = "Hide Others"; "macMenuShowAllApps" = "Show All"; -"macMenuQuitManagingTunnels" = "Quit Tunnel Manager"; "macMenuFile" = "File"; "macMenuCloseWindow" = "Close Window"; @@ -406,6 +405,12 @@ // Mac alert +"macConfirmAndQuitAlertMessage" = "Do you want to close the tunnels manager or quit WireGuard entirely?"; +"macConfirmAndQuitAlertInfo" = "If you close the tunnels manager, WireGuard will continue to be available from the menu bar icon."; +"macConfirmAndQuitInfoWithActiveTunnel (%@)" = "If you close the tunnels manager, WireGuard will continue to be available from the menu bar icon.\n\nNote that if you quit WireGuard entirely the currently active tunnel ('%@') will still remain active until you deactivate it from this application or through the Network panel in System Preferences."; +"macConfirmAndQuitAlertQuitWireGuard" = "Quit WireGuard"; +"macConfirmAndQuitAlertCloseWindow" = "Close Tunnels Manager"; + "macAppExitingWithActiveTunnelMessage" = "WireGuard is exiting with an active tunnel"; "macAppExitingWithActiveTunnelInfo" = "The tunnel will remain active after exiting. You may disable it by reopening this application or through the Network panel in System Preferences."; "macPrivacyNoticeMessage" = "Privacy notice: be sure you trust this configuration file"; diff --git a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift index ba2b0bd..da0c77c 100644 --- a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift +++ b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift @@ -78,6 +78,33 @@ class AppDelegate: NSObject, NSApplicationDelegate { return false } + @objc func confirmAndQuit() { + let alert = NSAlert() + alert.messageText = tr("macConfirmAndQuitAlertMessage") + if let currentTunnel = tunnelsTracker?.currentTunnel, currentTunnel.status == .active || currentTunnel.status == .activating { + alert.informativeText = tr(format: "macConfirmAndQuitInfoWithActiveTunnel (%@)", currentTunnel.name) + } else { + alert.informativeText = tr("macConfirmAndQuitAlertInfo") + } + alert.addButton(withTitle: tr("macConfirmAndQuitAlertCloseWindow")) + alert.addButton(withTitle: tr("macConfirmAndQuitAlertQuitWireGuard")) + + NSApp.activate(ignoringOtherApps: true) + if let manageWindow = manageTunnelsWindowObject { + manageWindow.orderFront(self) + alert.beginSheetModal(for: manageWindow) { response in + switch response { + case .alertFirstButtonReturn: + manageWindow.close() + case .alertSecondButtonReturn: + NSApp.terminate(nil) + default: + break + } + } + } + } + @objc func quit() { if let manageWindow = manageTunnelsWindowObject, manageWindow.attachedSheet != nil { NSApp.activate(ignoringOtherApps: true) diff --git a/WireGuard/WireGuard/UI/macOS/MainMenu.swift b/WireGuard/WireGuard/UI/macOS/MainMenu.swift index 27b674d..92fca4b 100644 --- a/WireGuard/WireGuard/UI/macOS/MainMenu.swift +++ b/WireGuard/WireGuard/UI/macOS/MainMenu.swift @@ -51,8 +51,8 @@ class MainMenu: NSMenu { menu.addItem(NSMenuItem.separator()) - menu.addItem(withTitle: tr("macMenuQuitManagingTunnels"), - action: #selector(NSWindow.performClose(_:)), keyEquivalent: "q") + menu.addItem(withTitle: tr("macMenuQuit"), + action: #selector(AppDelegate.confirmAndQuit), keyEquivalent: "q") return menu }