mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
100 lines
3.1 KiB
Swift
100 lines
3.1 KiB
Swift
//
|
|
// CombinedView.swift
|
|
// Stats
|
|
//
|
|
// Created by Serhiy Mytrovtsiy on 09/01/2023
|
|
// Using Swift 5.0
|
|
// Running on macOS 13.1
|
|
//
|
|
// Copyright © 2023 Serhiy Mytrovtsiy. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
import Kit
|
|
|
|
class CombinedView {
|
|
private var menuBarItem: NSStatusItem? = nil
|
|
private var view: NSView = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: Constants.Widget.height))
|
|
|
|
private var status: Bool {
|
|
Store.shared.bool(key: "CombinedModules", defaultValue: false)
|
|
}
|
|
|
|
init() {
|
|
modules.forEach { (m: Module) in
|
|
m.menuBar.callback = { [weak self] in
|
|
if let s = self?.status, s {
|
|
DispatchQueue.main.async(execute: {
|
|
self?.recalculate()
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
if self.status {
|
|
self.enable()
|
|
}
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(listenForOneView), name: .toggleOneView, object: nil)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(listenForModuleRearrrange), name: .moduleRearrange, object: nil)
|
|
}
|
|
|
|
deinit {
|
|
NotificationCenter.default.removeObserver(self, name: .toggleOneView, object: nil)
|
|
}
|
|
|
|
public func enable() {
|
|
self.menuBarItem = NSStatusBar.system.statusItem(withLength: 0)
|
|
self.menuBarItem?.autosaveName = "CombinedModules"
|
|
self.menuBarItem?.button?.addSubview(self.view)
|
|
|
|
self.menuBarItem?.button?.target = self
|
|
self.menuBarItem?.button?.action = #selector(self.openSettings)
|
|
self.menuBarItem?.button?.sendAction(on: [.leftMouseDown, .rightMouseDown])
|
|
|
|
DispatchQueue.main.async(execute: {
|
|
self.recalculate()
|
|
})
|
|
}
|
|
|
|
public func disable() {
|
|
if let item = self.menuBarItem {
|
|
NSStatusBar.system.removeStatusItem(item)
|
|
}
|
|
self.menuBarItem = nil
|
|
}
|
|
|
|
private func recalculate() {
|
|
self.view.subviews.forEach({ $0.removeFromSuperview() })
|
|
|
|
var w: CGFloat = 0
|
|
var i: Int = 0
|
|
modules.filter({ $0.enabled }).sorted(by: { $0.oneViewPosition < $1.oneViewPosition }).forEach { (m: Module) in
|
|
self.view.addSubview(m.menuBar.view)
|
|
self.view.subviews[i].setFrameOrigin(NSPoint(x: w, y: 0))
|
|
w += m.menuBar.view.frame.width
|
|
i += 1
|
|
}
|
|
self.view.setFrameSize(NSSize(width: w, height: self.view.frame.height))
|
|
self.menuBarItem?.length = w
|
|
}
|
|
|
|
@objc private func openSettings() {
|
|
NotificationCenter.default.post(name: .toggleSettings, object: nil, userInfo: ["module": "Dashboard"])
|
|
}
|
|
|
|
@objc private func listenForOneView(_ notification: Notification) {
|
|
guard notification.userInfo?["module"] == nil else { return }
|
|
|
|
if self.status {
|
|
self.enable()
|
|
} else {
|
|
self.disable()
|
|
}
|
|
}
|
|
|
|
@objc private func listenForModuleRearrrange() {
|
|
self.recalculate()
|
|
}
|
|
}
|