macOS: Minor refactor of StatusMenuWindowDelegate

Signed-off-by: Roopesh Chander <roop@roopc.net>
This commit is contained in:
Roopesh Chander 2019-05-15 16:16:00 +05:30
parent 586a592b68
commit 9bf304a9ac
2 changed files with 18 additions and 13 deletions

View File

@ -124,9 +124,13 @@ extension AppDelegate {
} }
extension AppDelegate: StatusMenuWindowDelegate { extension AppDelegate: StatusMenuWindowDelegate {
func manageTunnelsWindow() -> NSWindow { func showManageTunnelsWindow(completion: ((NSWindow?) -> Void)?) {
guard let tunnelsManager = tunnelsManager else {
completion?(nil)
return
}
if manageTunnelsWindowObject == nil { if manageTunnelsWindowObject == nil {
manageTunnelsRootVC = ManageTunnelsRootViewController(tunnelsManager: tunnelsManager!) manageTunnelsRootVC = ManageTunnelsRootViewController(tunnelsManager: tunnelsManager)
let window = NSWindow(contentViewController: manageTunnelsRootVC!) let window = NSWindow(contentViewController: manageTunnelsRootVC!)
window.title = tr("macWindowTitleManageTunnels") window.title = tr("macWindowTitleManageTunnels")
window.setContentSize(NSSize(width: 800, height: 480)) window.setContentSize(NSSize(width: 800, height: 480))
@ -134,7 +138,10 @@ extension AppDelegate: StatusMenuWindowDelegate {
manageTunnelsWindowObject = window manageTunnelsWindowObject = window
tunnelsTracker?.manageTunnelsRootVC = manageTunnelsRootVC tunnelsTracker?.manageTunnelsRootVC = manageTunnelsRootVC
} }
return manageTunnelsWindowObject! NSApp.setActivationPolicy(.regular)
NSApp.activate(ignoringOtherApps: true)
manageTunnelsWindowObject!.makeKeyAndOrderFront(self)
completion?(manageTunnelsWindowObject)
} }
} }

View File

@ -4,7 +4,7 @@
import Cocoa import Cocoa
protocol StatusMenuWindowDelegate: class { protocol StatusMenuWindowDelegate: class {
func manageTunnelsWindow() -> NSWindow func showManageTunnelsWindow(completion: ((NSWindow?) -> Void)?)
} }
class StatusMenu: NSMenu { class StatusMenu: NSMenu {
@ -151,18 +151,16 @@ class StatusMenu: NSMenu {
} }
@objc func manageTunnelsClicked() { @objc func manageTunnelsClicked() {
NSApp.setActivationPolicy(.regular) windowDelegate?.showManageTunnelsWindow(completion: nil)
NSApp.activate(ignoringOtherApps: true)
guard let manageTunnelsWindow = windowDelegate?.manageTunnelsWindow() else { return }
manageTunnelsWindow.makeKeyAndOrderFront(self)
} }
@objc func importTunnelsClicked() { @objc func importTunnelsClicked() {
NSApp.setActivationPolicy(.regular) windowDelegate?.showManageTunnelsWindow { [weak self] manageTunnelsWindow in
NSApp.activate(ignoringOtherApps: true) guard let self = self else { return }
guard let manageTunnelsWindow = windowDelegate?.manageTunnelsWindow() else { return } guard let manageTunnelsWindow = manageTunnelsWindow else { return }
manageTunnelsWindow.makeKeyAndOrderFront(self) ImportPanelPresenter.presentImportPanel(tunnelsManager: self.tunnelsManager,
ImportPanelPresenter.presentImportPanel(tunnelsManager: tunnelsManager, sourceVC: manageTunnelsWindow.contentViewController) sourceVC: manageTunnelsWindow.contentViewController)
}
} }
} }