aboutsummaryrefslogblamecommitdiffstats
path: root/WireGuard/WireGuard/UI/macOS/AppDelegate.swift
blob: 76ac0008a6728656bf4afa746a39d1dd611b6a0b (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                               
                                                             





                                                    

                                       
                                                   


                                                             
 
                                                                       

                                                                             

                                                     



                                                                      
 
                                                              
 
                                                                       
                                            
 
                                                             
                                                             






                                                                               
                                                            


         
 









                                                                                                                                
         
                                         
     
 
// SPDX-License-Identifier: MIT
// Copyright © 2018-2019 WireGuard LLC. All Rights Reserved.

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var tunnelsManager: TunnelsManager?
    var tunnelsTracker: TunnelsTracker?
    var statusItemController: StatusItemController?

    var manageTunnelsRootVC: ManageTunnelsRootViewController?
    var manageTunnelsWindowObject: NSWindow?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        Logger.configureGlobal(withFilePath: FileManager.appLogFileURL?.path)

        TunnelsManager.create { [weak self] result in
            guard let self = self else { return }
            if let error = result.error {
                ErrorPresenter.showErrorAlert(error: error, from: nil)
                return
            }

            let tunnelsManager: TunnelsManager = result.value!

            let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
            statusMenu.windowDelegate = self

            let statusItemController = StatusItemController()
            statusItemController.statusItem.menu = statusMenu

            let tunnelsTracker = TunnelsTracker(tunnelsManager: tunnelsManager)
            tunnelsTracker.statusMenu = statusMenu
            tunnelsTracker.statusItemController = statusItemController

            self.tunnelsManager = tunnelsManager
            self.tunnelsTracker = tunnelsTracker
            self.statusItemController = statusItemController
        }
    }
}

extension AppDelegate: StatusMenuWindowDelegate {
    func manageTunnelsWindow() -> NSWindow {
        if manageTunnelsWindowObject == nil {
            manageTunnelsRootVC = ManageTunnelsRootViewController(tunnelsManager: tunnelsManager!)
            let window = NSWindow(contentViewController: manageTunnelsRootVC!)
            window.title = tr("macWindowTitleManageTunnels")
            window.setContentSize(NSSize(width: 800, height: 480))
            window.setFrameAutosaveName(NSWindow.FrameAutosaveName("ManageTunnelsWindow")) // Auto-save window position and size
            manageTunnelsWindowObject = window
            tunnelsTracker?.manageTunnelsRootVC = manageTunnelsRootVC
        }
        return manageTunnelsWindowObject!
    }
}