forked from domzilla/Caffeine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
259 lines (211 loc) · 9.11 KB
/
Copy pathAppDelegate.swift
File metadata and controls
259 lines (211 loc) · 9.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//
// AppDelegate.swift
// Caffeine
//
// Created by Dominic Rodemer on 29.06.24.
//
import Cocoa
import IOKit.pwr_mgt
@main
class AppDelegate: NSObject, NSApplicationDelegate, NSMenuDelegate {
var isActive:Bool
var userSessionIsActive:Bool
var timer:Timer!
var timeoutTimer:Timer?
var sleepAssertionID:IOPMAssertionID?
var statusItem:NSStatusItem!
var statusItemMenuIcon:NSImage!
var statusItemMenuIconActive:NSImage!
@IBOutlet var menu:NSMenu!
@IBOutlet var infoMenuItem:NSMenuItem!
@IBOutlet var infoSeparatorItem:NSMenuItem!
var preferencesWindowController:PreferencesWindowController!
override init() {
self.isActive = false
self.userSessionIsActive = true
super.init()
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
timer = Timer.scheduledTimer(timeInterval: 10.0,
target: self,
selector: #selector(AppDelegate.timer(_:)),
userInfo: nil,
repeats: true)
NSWorkspace.shared.notificationCenter.addObserver(self,
selector: #selector(AppDelegate.workspaceSessionDidResignActiveNotification(_:)),
name: NSWorkspace.sessionDidResignActiveNotification,
object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self,
selector: #selector(AppDelegate.workspaceSessionDidBecomeActiveNotification(_:)),
name: NSWorkspace.sessionDidBecomeActiveNotification,
object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self,
selector: #selector(AppDelegate.workspaceWillSleepNotification(_:)),
name: NSWorkspace.willSleepNotification,
object: nil)
UserDefaults.standard.register(defaults: ["CASuppressLaunchMessage" : false])
preferencesWindowController = PreferencesWindowController(windowNibName: "PreferencesWindowController")
if !UserDefaults.standard.bool(forKey: "CASuppressLaunchMessage") {
self.showPreferences(nil)
}
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
self.showPreferences(nil)
return false
}
override func awakeFromNib() {
statusItemMenuIcon = NSImage(named: "inactive")
statusItemMenuIcon.isTemplate = true
statusItemMenuIconActive = NSImage(named: "active")
statusItemMenuIconActive.isTemplate = true
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
statusItem.button?.image = statusItemMenuIcon
statusItem.button?.sendAction(on: [.leftMouseUp, .rightMouseUp])
statusItem.button?.action = #selector(AppDelegate.statusItemAction(_:))
statusItem.button?.target = self
if UserDefaults.standard.bool(forKey: "CAActivateAtLaunch") {
self.activate()
}
}
// MARK: Actions
// MARK: ---
@IBAction func timer(_ sender:Timer) {
if isActive && userSessionIsActive {
if self.sleepAssertionID != nil {
IOPMAssertionRelease(self.sleepAssertionID!)
}
self.sleepAssertionID = IOPMAssertionID(0)
print(kIOPMAssertPreventUserIdleDisplaySleep)
IOPMAssertionCreateWithDescription(kIOPMAssertPreventUserIdleDisplaySleep as CFString,
"Caffeine prevents sleep" as CFString,
nil,
nil,
nil,
8, //Timeout assertion after 8 sec
nil,
&self.sleepAssertionID!)
}
}
@IBAction func timeoutReached(_ sender:Timer) {
self.deactivate()
}
@IBAction func statusItemAction(_ sender:Any?) {
let event = NSApp.currentEvent
if event?.type == .rightMouseUp {
statusItem.menu = menu
statusItem.button?.performClick(nil)
statusItem.menu = nil
} else {
toggleActive(sender)
}
}
@IBAction func activateWithTimeout(_ sender:NSMenuItem) {
let minutes = sender.tag
var seconds = minutes*60
if seconds == -60 {
seconds = 2
}
if minutes != 0 {
self.activate(withTimeoutDuration: TimeInterval(seconds))
} else {
self.activate()
}
}
@IBAction func showAbout(_ sender:Any?) {
NSApp.activate(ignoringOtherApps: true)
let credits = "© 2006 Tomas Franzén \n © 2018 Michael Jones \n © 2022 Dominic Rodemer \n\n Source code: \n https://github.caffeine-app.net"
NSApp.orderFrontStandardAboutPanel(options: [.credits : credits])
}
@IBAction func showPreferences(_ sender:Any?) {
NSApp.activate(ignoringOtherApps: true)
preferencesWindowController.showWindow(self)
}
// MARK: Public
// MARK: ---
func activate() {
self.activate(withTimeoutDuration: 0.0)
}
func activate(withTimeoutDuration interval:TimeInterval) {
if let timeoutTimer = self.timeoutTimer {
timeoutTimer.invalidate()
}
if interval > 0 {
timeoutTimer = Timer.scheduledTimer(timeInterval: interval,
target: self,
selector: #selector(AppDelegate.timeoutReached(_:)),
userInfo: nil,
repeats: false)
}
isActive = true
statusItem.button?.image = self.statusItemMenuIconActive
}
func deactivate() {
isActive = false
if let timeoutTimer = self.timeoutTimer {
timeoutTimer.invalidate()
}
timeoutTimer = nil
statusItem.button?.image = statusItemMenuIcon
}
func toggleActive(_ sender:Any?) {
if let timeoutTimer = self.timeoutTimer {
timeoutTimer.invalidate()
}
timeoutTimer = nil
if isActive {
self.deactivate()
} else {
let defaultMinutesDuration = UserDefaults.standard.integer(forKey: "CADefaultDuration")
var seconds = defaultMinutesDuration*60
if seconds == -60 {
seconds = 2
}
if defaultMinutesDuration == 0 {
self.activate(withTimeoutDuration: TimeInterval(seconds))
} else {
self.activate()
}
}
}
// MARK: NSMenuDelegate
// MARK: ---
func menuNeedsUpdate(_ menu: NSMenu) {
if isActive {
infoMenuItem.isHidden = false
infoSeparatorItem.isHidden = false
if let timeoutTimer = self.timeoutTimer {
let left = Int(timeoutTimer.fireDate.timeIntervalSinceNow)
if left >= 3600 {
infoMenuItem.title = String(format: "%02d:%02d", left/3600, (left%3600)/60)
} else if left > 60 {
infoMenuItem.title = String(format: NSLocalizedString("%d minutes", comment: "e.g. 5 minutes"), left/60)
} else {
infoMenuItem.title = String(format: NSLocalizedString("%d seconds", comment: "e.g. 54 seconds"), left)
}
} else {
infoMenuItem.title = NSLocalizedString("Caffeine is active", comment: "Indicate that Caffeine is active")
}
} else {
infoMenuItem.isHidden = true
infoSeparatorItem.isHidden = true
}
}
// MARK: SPUStandardUserDriverDelegate
// MARK: ---
func supportsGentleScheduledUpdateReminders() -> Bool {
return true
}
// MARK: NSWorkspace Notifications
// MARK: ---
@objc func workspaceSessionDidResignActiveNotification(_ notification:NSNotification) {
userSessionIsActive = false
}
@objc func workspaceSessionDidBecomeActiveNotification(_ notification:NSNotification) {
userSessionIsActive = true
}
@objc func workspaceWillSleepNotification(_ notification:NSNotification) {
if UserDefaults.standard.bool(forKey: "CADeactivateOnManualSleep") {
self.deactivate()
}
}
}