-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmodule.swift
More file actions
341 lines (284 loc) · 12.5 KB
/
module.swift
File metadata and controls
341 lines (284 loc) · 12.5 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// module.swift
// Kit
//
// Created by Serhiy Mytrovtsiy on 09/04/2020.
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
//
import Cocoa
public struct module_c {
public var name: String = ""
public var icon: NSImage?
public var defaultState: Bool = false
internal var defaultWidget: widget_t = .unknown
internal var availableWidgets: [widget_t] = []
internal var widgetsConfig: NSDictionary = NSDictionary()
internal var settingsConfig: NSDictionary = NSDictionary()
init(in path: String) {
let dict: NSDictionary = NSDictionary(contentsOfFile: path)!
if let name = dict["Name"] as? String {
self.name = name
}
if let state = dict["State"] as? Bool {
self.defaultState = state
}
if let symbol = dict["Symbol"] as? String {
self.icon = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)
}
if self.icon == nil, let symbol = dict["AlternativeSymbol"] as? String {
self.icon = NSImage(systemSymbolName: symbol, accessibilityDescription: nil)
}
if let widgetsDict = dict["Widgets"] as? NSDictionary {
var list: [String: Int] = [:]
self.widgetsConfig = widgetsDict
for widgetName in widgetsDict.allKeys {
if let widget = widget_t(rawValue: widgetName as! String) {
let widgetDict = widgetsDict[widgetName as! String] as! NSDictionary
if widgetDict["Default"] as! Bool {
self.defaultWidget = widget
}
var order = 0
if let o = widgetDict["Order"] as? Int {
order = o
}
list[widgetName as! String] = order
}
}
self.availableWidgets = list.sorted(by: { $0.1 < $1.1 }).map{ (widget_t(rawValue: $0.key) ?? .unknown) }
}
if let settingsDict = dict["Settings"] as? NSDictionary {
self.settingsConfig = settingsDict
}
}
}
open class Module {
public var config: module_c
public var available: Bool = false
public var enabled: Bool = false
public var menuBar: MenuBar
public var settings: Settings_p? = nil
public let portal: Portal_p?
public var name: String { config.name }
public var combinedPosition: Int {
get { Store.shared.int(key: "\(self.name)_position", defaultValue: 0) }
set { Store.shared.set(key: "\(self.name)_position", value: newValue) }
}
public var userDefaults: UserDefaults? = UserDefaults(suiteName: "\(Bundle.main.object(forInfoDictionaryKey: "TeamId") as! String).eu.exelban.Stats.widgets")
public var popupKeyboardShortcut: [UInt16] { self.popupView?.keyboardShortcut ?? [] }
private var moduleType: ModuleType
private var settingsView: Settings_v? = nil
private var popup: PopupWindow? = nil
private var popupView: Popup_p? = nil
private var notificationsView: NotificationsWrapper? = nil
private let log: NextLog
private var readers: [Reader_p] = []
private var pauseState: Bool {
get { Store.shared.bool(key: "pause", defaultValue: false) }
set { Store.shared.set(key: "pause", value: newValue) }
}
public init(moduleType: ModuleType, popup: Popup_p? = nil, settings: Settings_v? = nil, portal: Portal_p? = nil, notifications: NotificationsWrapper? = nil) {
self.moduleType = moduleType
self.portal = portal
self.config = module_c(in: Bundle(for: type(of: self)).path(forResource: "config", ofType: "plist")!)
self.log = NextLog.shared.copy(category: self.config.name)
self.settingsView = settings
self.popupView = popup
self.notificationsView = notifications
self.menuBar = MenuBar(moduleName: self.config.name)
self.available = self.isAvailable()
self.enabled = Store.shared.bool(key: "\(self.config.name)_state", defaultValue: self.config.defaultState)
self.userDefaults?.set(self.enabled, forKey: "\(self.config.name)_state")
if !self.available {
debug("Module is not available", log: self.log)
if self.enabled {
self.enabled = false
Store.shared.set(key: "\(self.config.name)_state", value: false)
}
return
} else if self.pauseState {
self.disable()
}
NotificationCenter.default.addObserver(self, selector: #selector(listenForMouseDownInSettings), name: .clickInSettings, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(listenForModuleToggle), name: .toggleModule, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(listenForPopupToggle), name: .togglePopup, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(listenForToggleWidget), name: .toggleWidget, object: nil)
// swiftlint:disable empty_count
if self.config.widgetsConfig.count != 0 {
// swiftlint:enable empty_count
self.initWidgets()
} else {
debug("Module started without widget", log: self.log)
}
self.settings = Settings(
config: &self.config,
widgets: &self.menuBar.widgets,
moduleSettings: self.settingsView,
popupSettings: self.popupView,
notificationsSettings: self.notificationsView
)
self.popup = PopupWindow(title: self.config.name, module: self.moduleType, view: self.popupView, visibilityCallback: self.visibilityCallback)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
// load function which call when app start
public func mount() {
guard self.enabled else { return }
self.readers.forEach { (reader: Reader_p) in
reader.initStoreValues(title: self.config.name)
reader.start()
}
self.menuBar.enable()
}
// disable module
public func unmount() {
self.enabled = false
self.available = false
}
// terminate function which call before app termination
public func terminate() {
self.willTerminate()
self.readers.forEach{
$0.stop()
$0.terminate()
}
self.menuBar.disable()
debug("Module terminated", log: self.log)
}
// function to call before module terminate
open func willTerminate() {}
// set module state to enabled
public func enable() {
guard self.available else { return }
self.enabled = true
Store.shared.set(key: "\(self.config.name)_state", value: true)
self.userDefaults?.set(true, forKey: "\(self.config.name)_state")
self.readers.forEach { (reader: Reader_p) in
reader.initStoreValues(title: self.config.name)
reader.start()
}
self.menuBar.enable()
self.settings?.setState(self.enabled)
debug("Module enabled", log: self.log)
}
// set module state to disabled
public func disable() {
guard self.available else { return }
self.enabled = false
if !self.pauseState { // omit saving the disable state when toggle by pause, need for resume state restoration
Store.shared.set(key: "\(self.config.name)_state", value: false)
self.userDefaults?.set(false, forKey: "\(self.config.name)_state")
}
self.readers.forEach{ $0.stop() }
self.menuBar.disable()
self.settings?.setState(self.enabled)
self.popup?.setIsVisible(false)
debug("Module disabled", log: self.log)
}
public func setReaders(_ list: [Reader_p?]) {
self.readers = list.filter({ $0 != nil }).map({ $0! as Reader_p })
}
// determine if module is available (can be overrided in module)
open func isAvailable() -> Bool { return true }
// load the widget and set up. Calls when module init
private func initWidgets() {
guard self.available else { return }
self.config.availableWidgets.forEach { (widgetType: widget_t) in
if let widget = widgetType.new(
module: self.config.name,
config: self.config.widgetsConfig,
defaultWidget: self.config.defaultWidget
) {
self.menuBar.append(widget)
}
}
}
// call when popup appear/disappear
private func visibilityCallback(_ state: Bool) {
let popupReaders = self.readers.filter { $0.popup }
if state {
popupReaders.forEach { (reader: Reader_p) in
reader.unlock()
reader.start()
}
self.readers.filter { !$0.popup }.forEach { (reader: Reader_p) in
reader.replay()
}
return
}
popupReaders.forEach { (reader: Reader_p) in
reader.pause()
reader.lock()
}
}
@objc private func listenForPopupToggle(_ notification: Notification) {
guard let popup = self.popup,
let name = notification.userInfo?["module"] as? String,
let buttonOrigin = notification.userInfo?["origin"] as? CGPoint,
let buttonCenter = notification.userInfo?["center"] as? CGFloat,
self.config.name == name else {
return
}
let openedWindows = NSApplication.shared.windows.filter{ $0 is NSPanel }
openedWindows.forEach{ $0.setIsVisible(false) }
var reopen: Bool = false
if let widget = notification.userInfo?["widget"] as? widget_t {
reopen = popup.openedBy != nil && popup.openedBy != widget
popup.openedBy = widget
}
if popup.occlusionState.rawValue == 8192 || reopen {
NSApplication.shared.activate(ignoringOtherApps: true)
popup.contentView?.invalidateIntrinsicContentSize()
let windowCenter = popup.contentView!.intrinsicContentSize.width / 2
var x = buttonOrigin.x - windowCenter + buttonCenter
let y = buttonOrigin.y - popup.contentView!.intrinsicContentSize.height - 3
let maxWidth = NSScreen.screens.map{ $0.frame.width }.reduce(0, +)
if x + popup.contentView!.intrinsicContentSize.width > maxWidth {
x = maxWidth - popup.contentView!.intrinsicContentSize.width - 3
}
popup.setFrameOrigin(NSPoint(x: x, y: y))
popup.setIsVisible(true)
} else {
popup.locked = false
popup.openedBy = nil
popup.setIsVisible(false)
}
}
@objc private func listenForModuleToggle(_ notification: Notification) {
if let name = notification.userInfo?["module"] as? String {
if name == self.config.name {
if let state = notification.userInfo?["state"] as? Bool {
if state && !self.enabled {
self.enable()
} else if !state && self.enabled {
self.disable()
}
} else {
if self.enabled {
self.disable()
} else {
self.enable()
}
}
}
if self.pauseState == true {
self.pauseState = false
NotificationCenter.default.post(name: .pause, object: nil, userInfo: ["state": false])
}
}
}
@objc private func listenForMouseDownInSettings() {
if let popup = self.popup, popup.isVisible && !popup.locked {
self.popup?.setIsVisible(false)
}
}
@objc private func listenForToggleWidget(_ notification: Notification) {
guard let name = notification.userInfo?["module"] as? String, name == self.config.name else {
return
}
let isEmpty = self.menuBar.widgets.filter({ $0.isActive }).isEmpty
if !isEmpty && !self.enabled {
NotificationCenter.default.post(name: .toggleModule, object: nil, userInfo: ["module": self.config.name, "state": true])
}
}
}