-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaffeinationAdditions.swift
More file actions
69 lines (62 loc) · 2.59 KB
/
CaffeinationAdditions.swift
File metadata and controls
69 lines (62 loc) · 2.59 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
//
// CaffeinationAdditions.swift
// Caffeinator
//
// Created by aaplmath on 6/18/19.
// Copyright © 2019 aaplmath. All rights reserved.
//
import CaffeineKit
import Cocoa
extension Caffeination {
/// Starts the Caffeination, catches and handles (with an alert dialog) any errors that occur, and—if the Caffeination starts successfully—sets the menu bar icon to its active state
func handledStart() {
do {
try self.start()
(NSApp.delegate as! AppDelegate).updateIconForCafState(active: true)
} catch let err {
switch err {
case let err as CaffeinationError:
if err == CaffeinationError.caffeinateNotFound {
Notifier.showErrorMessage(withTitle: txt("AD.caffeinate-missing-dialog-title"), text: txt("AD.caffeinate-missing-dialog-msg"))
}
// ignore "already active" errors
default:
Notifier.showErrorMessage(withTitle: txt("AD.caffeinate-failure-title"), text: String(format: txt("AD.caffeinate-failure-msg"), err.localizedDescription))
}
self.opts = Caffeination.Opt.userDefaults
}
}
/// Starts the Caffeination with the provided options, catches and handles (with an alert dialog) any errors that occur, and—if the Caffeination starts successfully—sets the menu bar icon to its active state
func handledStart(withOpts opts: [Opt]) {
self.opts = opts
self.handledStart()
}
/// Starts the Caffeination if it is not active or stops it if its, catching and handling (with an alert dialog) any errors that occur and appropriately updating the menu bar icon upon start (use `terminationHandler` to deal with the stop update)
func quickToggle() {
if self.isActive {
self.stop()
} else {
self.handledStart()
}
}
/**
Starts the Caffeination for use in automation (e.g., scripting)—specifically, catches errors and converts success/fail to a boolean return state instead.
- Returns: `true` if successful, `false` if not.
*/
func handledStartForAutomation() -> Bool {
do {
try self.start()
(NSApp.delegate as! AppDelegate).updateIconForCafState(active: true)
return true
} catch {
return false
}
}
}
extension Caffeination.Opt {
static var userDefaults: [Caffeination.Opt] {
get {
return UserDefaults.standard.bool(forKey: "CaffeinateDisplay") ? Caffeination.Opt.defaults : [Caffeination.Opt.idle]
}
}
}