feat: added portal view (combined view mode) to the sensors module

This commit is contained in:
Serhiy Mytrovtsiy
2024-01-19 21:05:48 +01:00
parent c38e65c3ca
commit 4c35eafd00
5 changed files with 106 additions and 6 deletions

View File

@@ -16,6 +16,7 @@ public class Sensors: Module {
private let sensorsReader: SensorsReader
private let popupView: Popup
private let settingsView: Settings
private let portalView: Portal
private let notificationsView: Notifications
private var fanValueState: FanValue {
@@ -26,16 +27,19 @@ public class Sensors: Module {
self.sensorsReader = SensorsReader()
self.settingsView = Settings("Sensors", list: self.sensorsReader.list.sensors)
self.popupView = Popup()
self.portalView = Portal(.sensors)
self.notificationsView = Notifications(.sensors)
super.init(
popup: self.popupView,
settings: self.settingsView,
portal: self.portalView,
notifications: self.notificationsView
)
guard self.available else { return }
self.popupView.setup(self.sensorsReader.list.sensors)
self.portalView.setup(self.sensorsReader.list.sensors)
self.notificationsView.setup(self.sensorsReader.list.sensors)
self.settingsView.callback = { [weak self] in
@@ -49,6 +53,7 @@ public class Sensors: Module {
self?.sensorsReader.HIDCallback()
DispatchQueue.main.async {
self?.popupView.setup(self?.sensorsReader.list.sensors)
self?.portalView.setup(self?.sensorsReader.list.sensors)
self?.settingsView.setList(list: self?.sensorsReader.list.sensors ?? [])
self?.notificationsView.setup(self?.sensorsReader.list.sensors)
}
@@ -59,6 +64,7 @@ public class Sensors: Module {
self?.sensorsReader.unknownCallback()
DispatchQueue.main.async {
self?.popupView.setup(self?.sensorsReader.list.sensors)
self?.portalView.setup(self?.sensorsReader.list.sensors)
self?.settingsView.setList(list: self?.sensorsReader.list.sensors ?? [])
self?.notificationsView.setup(self?.sensorsReader.list.sensors)
}
@@ -117,6 +123,7 @@ public class Sensors: Module {
}
self.popupView.usageCallback(value.sensors)
self.portalView.usageCallback(value.sensors)
self.notificationsView.usageCallback(value.sensors)
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: Widget) in

View File

@@ -352,11 +352,11 @@ internal class ValueSensorView: NSStackView {
public var callback: (() -> Void)
private var labelView: LabelField = {
let view = LabelField(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
let view = LabelField(frame: NSRect.zero)
view.cell?.truncatesLastVisibleLine = true
return view
}()
private var valueView: ValueField = ValueField(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
private var valueView: ValueField = ValueField(frame: NSRect.zero)
public init(_ sensor: Sensor_p, width: CGFloat, callback: @escaping (() -> Void)) {
self.callback = callback

View File

@@ -0,0 +1,93 @@
//
// portal.swift
// Sensors
//
// Created by Serhiy Mytrovtsiy on 14/01/2024
// Using Swift 5.0
// Running on macOS 14.3
//
// Copyright © 2024 Serhiy Mytrovtsiy. All rights reserved.
//
import AppKit
import Kit
public class Portal: NSStackView, Portal_p {
public var name: String
private var initialized: Bool = false
private var container: ScrollableStackView = ScrollableStackView()
private var list: [String: NSView] = [:]
private var unknownSensorsState: Bool {
Store.shared.bool(key: "Sensors_unknown", defaultValue: false)
}
init(_ name: ModuleType) {
self.name = name.rawValue
super.init(frame: NSRect( x: 0, y: 0, width: Constants.Popup.width, height: Constants.Popup.portalHeight))
self.wantsLayer = true
self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
self.layer?.cornerRadius = 3
self.orientation = .vertical
self.distribution = .fillEqually
self.spacing = Constants.Popup.spacing*2
self.edgeInsets = NSEdgeInsets(
top: Constants.Popup.spacing*2,
left: Constants.Popup.spacing*2,
bottom: Constants.Popup.spacing*2,
right: Constants.Popup.spacing*2
)
self.container.stackView.spacing = 0
self.addArrangedSubview(PortalHeader(self.name))
self.addArrangedSubview(self.container)
self.heightAnchor.constraint(equalToConstant: Constants.Popup.portalHeight).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
public override func updateLayer() {
self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
}
public func setup(_ values: [Sensor_p]? = nil) {
guard var list = values else { return }
if !self.unknownSensorsState {
list = list.filter({ $0.group != .unknown })
}
if !self.list.isEmpty {
self.container.stackView.subviews.forEach({ $0.removeFromSuperview() })
self.list = [:]
}
let width: CGFloat = self.frame.width - self.edgeInsets.left - self.edgeInsets.right - Constants.Popup.margins
list.forEach { s in
let v = ValueSensorView(s, width: width, callback: {})
self.container.stackView.addArrangedSubview(v)
self.list[s.key] = v
}
}
public func usageCallback(_ values: [Sensor_p]) {
DispatchQueue.main.async(execute: {
if self.window?.isVisible ?? false {
values.forEach { (s: Sensor_p) in
if let v = self.list[s.key] as? ValueSensorView {
v.update(s.formattedPopupValue)
}
}
}
})
}
}