From 4f79bf7b9be27b47d80d11e3ed75eadee66a095d Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 9 May 2019 18:39:06 +0530 Subject: [PATCH] macOS: Fix appearance of main menu when showing the manage window --- WireGuard/WireGuard/UI/macOS/AppDelegate.swift | 18 +++++++++++++++++- WireGuard/WireGuard/UI/macOS/StatusMenu.swift | 6 ++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift index 8dc5825..416de7a 100644 --- a/WireGuard/WireGuard/UI/macOS/AppDelegate.swift +++ b/WireGuard/WireGuard/UI/macOS/AppDelegate.swift @@ -100,11 +100,27 @@ class AppDelegate: NSObject, NSApplicationDelegate { } func applicationShouldTerminateAfterLastWindowClosed(_ application: NSApplication) -> Bool { - application.setActivationPolicy(.accessory) + AppDelegate.setAppAppearsInDock(false) return false } } +extension AppDelegate { + static func setAppAppearsInDock(_ enabled: Bool) { + if enabled { + if NSApp.activationPolicy() != .regular { + // It looks like we need to go from deactivated to activated with policy as .regular + // for our app's main menu to show up. + NSApp.deactivate() + NSApp.setActivationPolicy(.regular) + } + NSApp.activate(ignoringOtherApps: true) + } else { + NSApp.setActivationPolicy(.accessory) + } + } +} + extension AppDelegate { @objc func aboutClicked() { var appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "Unknown" diff --git a/WireGuard/WireGuard/UI/macOS/StatusMenu.swift b/WireGuard/WireGuard/UI/macOS/StatusMenu.swift index 4044d4e..b9f053c 100644 --- a/WireGuard/WireGuard/UI/macOS/StatusMenu.swift +++ b/WireGuard/WireGuard/UI/macOS/StatusMenu.swift @@ -151,15 +151,13 @@ class StatusMenu: NSMenu { } @objc func manageTunnelsClicked() { - NSApp.setActivationPolicy(.regular) - NSApp.activate(ignoringOtherApps: true) + AppDelegate.setAppAppearsInDock(true) guard let manageTunnelsWindow = windowDelegate?.manageTunnelsWindow() else { return } manageTunnelsWindow.makeKeyAndOrderFront(self) } @objc func importTunnelsClicked() { - NSApp.setActivationPolicy(.regular) - NSApp.activate(ignoringOtherApps: true) + AppDelegate.setAppAppearsInDock(true) guard let manageTunnelsWindow = windowDelegate?.manageTunnelsWindow() else { return } manageTunnelsWindow.makeKeyAndOrderFront(self) ImportPanelPresenter.presentImportPanel(tunnelsManager: tunnelsManager, sourceVC: manageTunnelsWindow.contentViewController)