Subversion Repositories Mobile Apps.GyroMouse

Rev

Rev 1 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

//
//  AppDelegate.swift
//  GyroServer
//
//  Created by Matteo Riva on 07/08/15.
//  Copyright © 2015 Matteo Riva. All rights reserved.
//

import Cocoa
import CoreServices

let ActiveScreenDidChangeNotification = Notification.Name("ActiveScreenDidChangeNotification")
let yellow = NSColor(red: 255.0/255.0, green: 223.0/255.0, blue: 17.0/255.0, alpha: 1)
let blue = NSColor(red: 41.0/255.0, green: 95.0/255.0, blue: 196.0/255.0, alpha: 1)

extension String {
    var localized: String {
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
    }
}

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var mouseHandler: MouseHandler!
    
    let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
    let screenMenu = NSMenu()
    //let window = FirstBootWindowController(windowNibName: NSNib.Name ("FirstBootWindowController"))
    
    func applicationDidFinishLaunching(_ aNotification: Notification) {
     /*
        if !UserDefaults.standard.bool(forKey: "firstBoot") {
            window.completion = {[unowned self] in
                self.setupApp()
            }
            window.showWindow(self)
        } else {*/
            setupApp()
       // }
        
        // attempt a keystroke ; if it fails, notify the user that he needs to allow the app
        let script = NSAppleScript(source: "tell app \"System Events\"\n" +
                                            "   key down shift\n" +
                                            "   key up shift\n" +
                                            "end tell")! // harmless keystroke
        var error: NSDictionary? = nil
        let resultMaybe = script.executeAndReturnError(&error) as NSAppleEventDescriptor?
        if (resultMaybe == nil)
        {
            let myStringDict = error as? [String:AnyObject]
            if (myStringDict?["NSAppleScriptErrorNumber"] as! Int16 == 1002) {
                let alert = NSAlert()
                alert.addButton(withTitle: "OK".localized)
                alert.messageText = "system_message".localized + "\n" + (myStringDict?["NSAppleScriptErrorBriefMessage"] as! String)
                alert.informativeText = "allow_perms".localized
                alert.alertStyle = .warning
                alert.runModal()
            }
            print (error as Any)
            exit (1)
        }
    }
    
    func setupApp() {
        //Add menuItem to menu
        let menu = NSMenu()
         
        let screenItem = NSMenuItem()
        screenItem.title = "act_screen".localized
        
        for (i,_) in NSScreen.screens.enumerated() {
            screenMenu.addItem(withTitle: "\(1 + i)", action: #selector(AppDelegate.setActiveScreen(_:)), keyEquivalent: "")
        }
        
        var selected = UserDefaults.standard.integer(forKey: "activeScreen")
        let maxScreens =  NSScreen.screens.count - 1
        
        if maxScreens < selected {
            selected = 0
            UserDefaults.standard.set(selected, forKey: "activeScreen")
        }
        
        screenMenu.item(at: selected)?.state = NSControl.StateValue.on
        screenItem.submenu = screenMenu
        
        menu.addItem(screenItem)
        
        menu.addItem(withTitle: "exit".localized, action: #selector(AppDelegate.exitAction), keyEquivalent: "q")
        statusBarItem.menu = menu
        let icon = NSImage(named: NSImage.Name("statusIcon"))!
        icon.isTemplate = true
        statusBarItem.image = icon
        
        mouseHandler = MouseHandler()
    }
    
    @objc func exitAction(){
        NSApp.terminate(self)
    }
    
    @objc func setActiveScreen(_ sender: NSMenuItem) {
        for (i,screen) in screenMenu.items.enumerated() {
            if screen == sender {
                sender.state = NSControl.StateValue.on
                UserDefaults.standard.set(i, forKey: "activeScreen")
            } else {
                screen.state = NSControl.StateValue.off
            }
        }
        
        NotificationCenter.default.post(name: ActiveScreenDidChangeNotification, object: nil)
    }
    
}