Subversion Repositories Mobile Apps.GyroMouse

Rev

Rev 5 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 5 Rev 7
Line 8... Line 8...
8
 
8
 
9
import Cocoa
9
import Cocoa
10
import CoreServices
10
import CoreServices
11
 
11
 
12
let ActiveScreenDidChangeNotification = Notification.Name("ActiveScreenDidChangeNotification")
12
let ActiveScreenDidChangeNotification = Notification.Name("ActiveScreenDidChangeNotification")
13
let yellow = NSColor(red: 255.0/255.0, green: 223.0/255.0, blue: 17.0/255.0, alpha: 1)
-
 
14
let blue = NSColor(red: 41.0/255.0, green: 95.0/255.0, blue: 196.0/255.0, alpha: 1)
-
 
15
 
13
 
16
extension String {
14
extension String {
17
    var localized: String {
15
    var localized: String {
18
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
16
        return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
19
    }
17
    }
Line 24... Line 22...
24
 
22
 
25
    var mouseHandler: MouseHandler!
23
    var mouseHandler: MouseHandler!
26
    
24
    
27
    let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
25
    let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
28
    let screenMenu = NSMenu()
26
    let screenMenu = NSMenu()
29
    //let window = FirstBootWindowController(windowNibName: NSNib.Name ("FirstBootWindowController"))
-
 
30
    
27
    
31
    func applicationDidFinishLaunching(_ aNotification: Notification) {
28
    func applicationDidFinishLaunching(_ aNotification: Notification) {
32
     /*
-
 
33
        if !UserDefaults.standard.bool(forKey: "firstBoot") {
-
 
34
            window.completion = {[unowned self] in
-
 
35
                self.setupApp()
-
 
36
            }
-
 
37
            window.showWindow(self)
-
 
38
        } else {*/
-
 
39
            setupApp()
-
 
40
       // }
-
 
41
        
-
 
42
        // attempt a keystroke ; if it fails, notify the user that he needs to allow the app
-
 
43
        let script = NSAppleScript(source: "tell app \"System Events\"\n" +
-
 
44
                                            "   key down shift\n" +
-
 
45
                                            "   key up shift\n" +
-
 
46
                                            "end tell")! // harmless keystroke
-
 
47
        var error: NSDictionary? = nil
-
 
48
        let resultMaybe = script.executeAndReturnError(&error) as NSAppleEventDescriptor?
-
 
49
        if (resultMaybe == nil)
-
 
50
        {
-
 
51
            let myStringDict = error as? [String:AnyObject]
-
 
52
            if (myStringDict?["NSAppleScriptErrorNumber"] as! Int16 == 1002) {
-
 
53
                let alert = NSAlert()
-
 
54
                alert.addButton(withTitle: "OK".localized)
-
 
55
                alert.messageText = "system_message".localized + "\n" + (myStringDict?["NSAppleScriptErrorBriefMessage"] as! String)
-
 
56
                alert.informativeText = "allow_perms".localized
-
 
57
                alert.alertStyle = .warning
-
 
58
                alert.runModal()
-
 
59
            }
-
 
60
            print (error as Any)
-
 
61
            exit (1)
-
 
62
        }
-
 
63
    }
-
 
64
    
29
 
65
    func setupApp() {
-
 
66
        //Add menuItem to menu
30
        //Add menuItem to menu
67
        let menu = NSMenu()
31
        let menu = NSMenu()
68
         
32
         
69
        let screenItem = NSMenuItem()
33
        let screenItem = NSMenuItem()
70
        screenItem.title = "act_screen".localized
34
        screenItem.title = "act_screen".localized
71
        
35
        
72
        for (i,_) in NSScreen.screens.enumerated() {
36
        for (i,_) in NSScreen.screens.enumerated() {
73
            screenMenu.addItem(withTitle: "\(1 + i)", action: #selector(AppDelegate.setActiveScreen(_:)), keyEquivalent: "")
37
            screenMenu.addItem(withTitle: "\(1 + i)", action: #selector(AppDelegate.setActiveScreen(_:)), keyEquivalent: "")
74
        }
38
        }
75
        
39
        
76
        var selected = UserDefaults.standard.integer(forKey: "activeScreen")
40
        var selected = UserDefaults.standard.integer(forKey: "activeScreen")
77
        let maxScreens =  NSScreen.screens.count - 1
41
        let maxScreens =  NSScreen.screens.count - 1
78
        
42
        
79
        if maxScreens < selected {
43
        if maxScreens < selected {
80
            selected = 0
44
            selected = 0
81
            UserDefaults.standard.set(selected, forKey: "activeScreen")
45
            UserDefaults.standard.set(selected, forKey: "activeScreen")
82
        }
46
        }
83
        
47
        
Line 91... Line 55...
91
        let icon = NSImage(named: NSImage.Name("statusIcon"))!
55
        let icon = NSImage(named: NSImage.Name("statusIcon"))!
92
        icon.isTemplate = true
56
        icon.isTemplate = true
93
        statusBarItem.image = icon
57
        statusBarItem.image = icon
94
        
58
        
95
        mouseHandler = MouseHandler()
59
        mouseHandler = MouseHandler()
-
 
60
 
-
 
61
        // attempt a keystroke ; if it fails, notify the user that he needs to allow the app
-
 
62
        let script = NSAppleScript(source: "tell app \"System Events\"\n" +
-
 
63
                                            "   key down shift\n" +
-
 
64
                                            "   key up shift\n" +
-
 
65
                                            "end tell")! // harmless keystroke
-
 
66
        var error: NSDictionary? = nil
-
 
67
        let resultMaybe = script.executeAndReturnError(&error) as NSAppleEventDescriptor?
-
 
68
        if (resultMaybe == nil)
-
 
69
        {
-
 
70
            let myStringDict = error as? [String:AnyObject]
-
 
71
            if (myStringDict?["NSAppleScriptErrorNumber"] as! Int16 == 1002) {
-
 
72
                let alert = NSAlert()
-
 
73
                alert.addButton(withTitle: "OK".localized)
-
 
74
                alert.messageText = "system_message".localized + "\n" + (myStringDict?["NSAppleScriptErrorBriefMessage"] as! String)
-
 
75
                alert.informativeText = "allow_perms".localized
-
 
76
                alert.alertStyle = .warning
-
 
77
                alert.runModal()
-
 
78
            }
-
 
79
            print (error as Any)
-
 
80
            exit (1)
-
 
81
        }
96
    }
82
    }
97
    
83
 
98
    @objc func exitAction(){
84
    @objc func exitAction(){
99
        NSApp.terminate(self)
85
        NSApp.terminate(self)
100
    }
86
    }
101
    
87
    
102
    @objc func setActiveScreen(_ sender: NSMenuItem) {
88
    @objc func setActiveScreen(_ sender: NSMenuItem) {