Rev 1 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | pmbaty | 1 | // |
2 | // AppDelegate.swift |
||
3 | // GyroServer |
||
4 | // |
||
5 | // Created by Matteo Riva on 07/08/15. |
||
6 | // Copyright © 2015 Matteo Riva. All rights reserved. |
||
7 | // |
||
8 | |||
9 | import Cocoa |
||
10 | import CoreServices |
||
11 | |||
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 | |||
16 | extension String { |
||
17 | var localized: String { |
||
18 | return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "") |
||
19 | } |
||
20 | } |
||
21 | |||
22 | @NSApplicationMain |
||
23 | class AppDelegate: NSObject, NSApplicationDelegate { |
||
24 | |||
25 | var mouseHandler: MouseHandler! |
||
26 | |||
27 | let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) |
||
28 | let screenMenu = NSMenu() |
||
5 | pmbaty | 29 | //let window = FirstBootWindowController(windowNibName: NSNib.Name ("FirstBootWindowController")) |
1 | pmbaty | 30 | |
31 | func applicationDidFinishLaunching(_ aNotification: Notification) { |
||
5 | pmbaty | 32 | /* |
1 | pmbaty | 33 | if !UserDefaults.standard.bool(forKey: "firstBoot") { |
34 | window.completion = {[unowned self] in |
||
35 | self.setupApp() |
||
36 | } |
||
37 | window.showWindow(self) |
||
5 | pmbaty | 38 | } else {*/ |
1 | pmbaty | 39 | setupApp() |
5 | pmbaty | 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) |
||
1 | pmbaty | 62 | } |
63 | } |
||
64 | |||
65 | func setupApp() { |
||
66 | //Add menuItem to menu |
||
67 | let menu = NSMenu() |
||
68 | |||
69 | let screenItem = NSMenuItem() |
||
70 | screenItem.title = "act_screen".localized |
||
71 | |||
72 | for (i,_) in NSScreen.screens.enumerated() { |
||
73 | screenMenu.addItem(withTitle: "\(1 + i)", action: #selector(AppDelegate.setActiveScreen(_:)), keyEquivalent: "") |
||
74 | } |
||
75 | |||
76 | var selected = UserDefaults.standard.integer(forKey: "activeScreen") |
||
77 | let maxScreens = NSScreen.screens.count - 1 |
||
78 | |||
79 | if maxScreens < selected { |
||
80 | selected = 0 |
||
81 | UserDefaults.standard.set(selected, forKey: "activeScreen") |
||
82 | } |
||
83 | |||
84 | screenMenu.item(at: selected)?.state = NSControl.StateValue.on |
||
85 | screenItem.submenu = screenMenu |
||
86 | |||
87 | menu.addItem(screenItem) |
||
88 | |||
89 | menu.addItem(withTitle: "exit".localized, action: #selector(AppDelegate.exitAction), keyEquivalent: "q") |
||
90 | statusBarItem.menu = menu |
||
91 | let icon = NSImage(named: NSImage.Name("statusIcon"))! |
||
92 | icon.isTemplate = true |
||
93 | statusBarItem.image = icon |
||
94 | |||
95 | mouseHandler = MouseHandler() |
||
96 | } |
||
97 | |||
98 | @objc func exitAction(){ |
||
99 | NSApp.terminate(self) |
||
100 | } |
||
101 | |||
102 | @objc func setActiveScreen(_ sender: NSMenuItem) { |
||
103 | for (i,screen) in screenMenu.items.enumerated() { |
||
104 | if screen == sender { |
||
105 | sender.state = NSControl.StateValue.on |
||
106 | UserDefaults.standard.set(i, forKey: "activeScreen") |
||
107 | } else { |
||
108 | screen.state = NSControl.StateValue.off |
||
109 | } |
||
110 | } |
||
111 | |||
112 | NotificationCenter.default.post(name: ActiveScreenDidChangeNotification, object: nil) |
||
113 | } |
||
114 | |||
115 | } |
||
116 |