Repository navigation
Expand file tree
/
Copy pathAppDelegate.swift
More file actions
110 lines (93 loc) · 3.91 KB
/
Copy pathAppDelegate.swift
File metadata and controls
110 lines (93 loc) · 3.91 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
//
// AppDelegate.swift
// vanillaClone
//
// Created by Thanh Nguyen on 1/24/19.
// Copyright © 2019 Dwarves Foundation. All rights reserved.
//
import AppKit
import HotKey
import ServiceManagement
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate{
var statusBarController = StatusBarController()
var hotKey: HotKey? {
didSet {
guard let hotKey = hotKey else { return }
hotKey.keyDownHandler = { [weak self] in
self?.statusBarController.expandCollapseIfNeeded()
}
}
}
func applicationDidFinishLaunching(_ aNotification: Notification) {
guard ensureSingleInstance() else { return }
setupAutoStartApp()
registerDefaultValues()
setupHotKey()
openPreferencesIfNeeded()
detectLTRLang()
}
/// Hidden Bar is a menu bar utility with no visible window, so launching it a
/// second time looks like a no-op while silently spawning a duplicate instance.
/// If another instance with the same bundle ID is already running, activate it
/// and terminate this one instead.
private func ensureSingleInstance() -> Bool {
let current = ProcessInfo.processInfo.processIdentifier
let existing = NSWorkspace.shared.runningApplications.first {
guard $0.bundleIdentifier == Bundle.main.bundleIdentifier,
$0.processIdentifier != current else { return false }
#if DEBUG
// Let a dev build run next to the installed copy; only dedupe
// relaunches of the same bundle path.
return $0.bundleURL == Bundle.main.bundleURL
#else
return true
#endif
}
guard let existing else { return true }
existing.activate(options: [.activateAllWindows])
NSApp.terminate(nil)
return false
}
func openPreferencesIfNeeded() {
if Preferences.isShowPreference {
Util.showPrefWindow()
}
}
func setupAutoStartApp() {
removeLegacyLauncherLoginItem()
Util.setUpAutoStart(isAutoStart: Preferences.isAutoStart)
}
private func removeLegacyLauncherLoginItem() {
// Builds before the SMAppService migration registered a helper app in BTM;
// macOS never garbage-collects that record (TN3111), so deauthorize it once.
let migratedKey = "smAppServiceMigrated"
guard !UserDefaults.standard.bool(forKey: migratedKey) else { return }
SMLoginItemSetEnabled("com.dwarvesv.LauncherApplication" as CFString, false)
UserDefaults.standard.set(true, forKey: migratedKey)
}
// A plain value, separate from the side-effecting `register(defaults:)`
// call, so the "what are the defaults" behavior is testable without
// spinning up an AppDelegate (which creates real status bar items).
static let defaultPreferenceValues: [String: Any] = [
UserDefaults.Key.isAutoStart: false,
UserDefaults.Key.isShowPreference: true,
UserDefaults.Key.isAutoHide: true,
UserDefaults.Key.numberOfSecondForAutoHide: 10.0,
UserDefaults.Key.areSeparatorsHidden: false,
UserDefaults.Key.alwaysHiddenSectionEnabled: false,
UserDefaults.Key.notchOverflowEnabled: true
]
func registerDefaultValues() {
UserDefaults.standard.register(defaults: AppDelegate.defaultPreferenceValues)
}
func setupHotKey() {
guard let globalKey = Preferences.globalKey else {return}
hotKey = HotKey(keyCombo: KeyCombo(carbonKeyCode: globalKey.keyCode, carbonModifiers: globalKey.carbonFlags))
}
func detectLTRLang() {
// Languages like Arabic uses right to left (RTL) writing direction,
// so some behavier of the app needs to be changed in these cases
Constant.isUsingLTRLanguage = (NSApplication.shared.userInterfaceLayoutDirection == .leftToRight)
}
}