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







                                                        

                                 
                                                                       

                                                                             


                                                                      
 


                                                                       

                                                           
                                                          
         
     

 








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

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var statusItem: NSStatusItem?

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

        TunnelsManager.create { [weak self] result in
            guard let self = self else { return }
            guard result.isSuccess else { return } // TODO: Show alert

            let tunnelsManager: TunnelsManager = result.value!
            let statusMenu = StatusMenu(tunnelsManager: tunnelsManager)
            self.statusItem = createStatusBarItem(with: statusMenu)

            tunnelsManager.tunnelsListDelegate = statusMenu
            tunnelsManager.activationDelegate = statusMenu
        }
    }
}

func createStatusBarItem(with statusMenu: StatusMenu) -> NSStatusItem {
    let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
    if let statusBarImage = NSImage(named: "WireGuardMacStatusBarIcon") {
        statusBarImage.isTemplate = true
        statusItem.button?.image = statusBarImage
    }
    statusItem.menu = statusMenu
    return statusItem
}