Rev 5 | Go to most recent revision | Details | 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() |
||
29 | let window = FirstBootWindowController(windowNibName: NSNib.Name ("FirstBootWindowController")) |
||
30 | |||
31 | 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 | |||
43 | func setupApp() { |
||
44 | //Add menuItem to menu |
||
45 | let menu = NSMenu() |
||
46 | |||
47 | let screenItem = NSMenuItem() |
||
48 | screenItem.title = "act_screen".localized |
||
49 | |||
50 | for (i,_) in NSScreen.screens.enumerated() { |
||
51 | screenMenu.addItem(withTitle: "\(1 + i)", action: #selector(AppDelegate.setActiveScreen(_:)), keyEquivalent: "") |
||
52 | } |
||
53 | |||
54 | var selected = UserDefaults.standard.integer(forKey: "activeScreen") |
||
55 | let maxScreens = NSScreen.screens.count - 1 |
||
56 | |||
57 | if maxScreens < selected { |
||
58 | selected = 0 |
||
59 | UserDefaults.standard.set(selected, forKey: "activeScreen") |
||
60 | } |
||
61 | |||
62 | screenMenu.item(at: selected)?.state = NSControl.StateValue.on |
||
63 | screenItem.submenu = screenMenu |
||
64 | |||
65 | menu.addItem(screenItem) |
||
66 | |||
67 | menu.addItem(withTitle: "exit".localized, action: #selector(AppDelegate.exitAction), keyEquivalent: "q") |
||
68 | statusBarItem.menu = menu |
||
69 | let icon = NSImage(named: NSImage.Name("statusIcon"))! |
||
70 | icon.isTemplate = true |
||
71 | statusBarItem.image = icon |
||
72 | |||
73 | mouseHandler = MouseHandler() |
||
74 | } |
||
75 | |||
76 | @objc func exitAction(){ |
||
77 | NSApp.terminate(self) |
||
78 | } |
||
79 | |||
80 | @objc func setActiveScreen(_ sender: NSMenuItem) { |
||
81 | for (i,screen) in screenMenu.items.enumerated() { |
||
82 | if screen == sender { |
||
83 | sender.state = NSControl.StateValue.on |
||
84 | UserDefaults.standard.set(i, forKey: "activeScreen") |
||
85 | } else { |
||
86 | screen.state = NSControl.StateValue.off |
||
87 | } |
||
88 | } |
||
89 | |||
90 | NotificationCenter.default.post(name: ActiveScreenDidChangeNotification, object: nil) |
||
91 | } |
||
92 | |||
93 | } |
||
94 |