Rev 5 | 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 | |||
14 | extension String { |
||
15 | var localized: String { |
||
16 | return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "") |
||
17 | } |
||
18 | } |
||
19 | |||
20 | @NSApplicationMain |
||
21 | class AppDelegate: NSObject, NSApplicationDelegate { |
||
22 | |||
23 | var mouseHandler: MouseHandler! |
||
24 | |||
25 | let statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) |
||
26 | let screenMenu = NSMenu() |
||
27 | |||
28 | func applicationDidFinishLaunching(_ aNotification: Notification) { |
||
7 | pmbaty | 29 | |
1 | pmbaty | 30 | //Add menuItem to menu |
31 | let menu = NSMenu() |
||
32 | |||
33 | let screenItem = NSMenuItem() |
||
34 | screenItem.title = "act_screen".localized |
||
35 | |||
36 | for (i,_) in NSScreen.screens.enumerated() { |
||
37 | screenMenu.addItem(withTitle: "\(1 + i)", action: #selector(AppDelegate.setActiveScreen(_:)), keyEquivalent: "") |
||
38 | } |
||
39 | |||
40 | var selected = UserDefaults.standard.integer(forKey: "activeScreen") |
||
41 | let maxScreens = NSScreen.screens.count - 1 |
||
42 | |||
43 | if maxScreens < selected { |
||
44 | selected = 0 |
||
45 | UserDefaults.standard.set(selected, forKey: "activeScreen") |
||
46 | } |
||
47 | |||
48 | screenMenu.item(at: selected)?.state = NSControl.StateValue.on |
||
49 | screenItem.submenu = screenMenu |
||
50 | |||
51 | menu.addItem(screenItem) |
||
52 | |||
53 | menu.addItem(withTitle: "exit".localized, action: #selector(AppDelegate.exitAction), keyEquivalent: "q") |
||
54 | statusBarItem.menu = menu |
||
55 | let icon = NSImage(named: NSImage.Name("statusIcon"))! |
||
56 | icon.isTemplate = true |
||
57 | statusBarItem.image = icon |
||
58 | |||
59 | mouseHandler = MouseHandler() |
||
7 | pmbaty | 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 | } |
||
1 | pmbaty | 82 | } |
7 | pmbaty | 83 | |
1 | pmbaty | 84 | @objc func exitAction(){ |
85 | NSApp.terminate(self) |
||
86 | } |
||
87 | |||
88 | @objc func setActiveScreen(_ sender: NSMenuItem) { |
||
89 | for (i,screen) in screenMenu.items.enumerated() { |
||
90 | if screen == sender { |
||
91 | sender.state = NSControl.StateValue.on |
||
92 | UserDefaults.standard.set(i, forKey: "activeScreen") |
||
93 | } else { |
||
94 | screen.state = NSControl.StateValue.off |
||
95 | } |
||
96 | } |
||
97 | |||
98 | NotificationCenter.default.post(name: ActiveScreenDidChangeNotification, object: nil) |
||
99 | } |
||
100 | |||
101 | } |
||
102 |