2020-06-23 00:03:00 +02:00
|
|
|
//
|
|
|
|
|
// popup.swift
|
2020-07-06 15:52:57 +02:00
|
|
|
// Sensors
|
2020-06-23 00:03:00 +02:00
|
|
|
//
|
|
|
|
|
// Created by Serhiy Mytrovtsiy on 22/06/2020.
|
|
|
|
|
// Using Swift 5.0.
|
|
|
|
|
// Running on macOS 10.15.
|
|
|
|
|
//
|
|
|
|
|
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Cocoa
|
2021-06-04 19:37:29 +02:00
|
|
|
import Kit
|
2020-06-23 00:03:00 +02:00
|
|
|
|
2020-10-31 19:34:28 +01:00
|
|
|
internal class Popup: NSView, Popup_p {
|
2020-06-23 00:03:00 +02:00
|
|
|
private var list: [String: NSTextField] = [:]
|
|
|
|
|
|
2020-10-31 19:34:28 +01:00
|
|
|
public var sizeCallback: ((NSSize) -> Void)? = nil
|
|
|
|
|
|
2020-06-23 00:03:00 +02:00
|
|
|
public init() {
|
|
|
|
|
super.init(frame: NSRect( x: 0, y: 0, width: Constants.Popup.width, height: 0))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-07 12:33:23 +03:00
|
|
|
internal func setup(_ values: [Sensor_p]?) {
|
2020-06-23 00:03:00 +02:00
|
|
|
guard values != nil else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-10 15:50:02 +02:00
|
|
|
var types: [SensorType] = []
|
2021-08-07 12:33:23 +03:00
|
|
|
values!.forEach { (s: Sensor_p) in
|
2020-07-16 19:43:43 +02:00
|
|
|
if !types.contains(s.type) {
|
|
|
|
|
types.append(s.type)
|
|
|
|
|
}
|
2020-06-23 00:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.subviews.forEach { (v: NSView) in
|
|
|
|
|
v.removeFromSuperview()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var y: CGFloat = 0
|
2021-04-10 15:50:02 +02:00
|
|
|
types.reversed().forEach { (typ: SensorType) in
|
2020-07-16 19:43:43 +02:00
|
|
|
let filtered = values!.filter{ $0.type == typ }
|
2021-04-10 15:50:02 +02:00
|
|
|
var groups: [SensorGroup] = []
|
2021-08-07 12:33:23 +03:00
|
|
|
filtered.forEach { (s: Sensor_p) in
|
2020-07-16 19:43:43 +02:00
|
|
|
if !groups.contains(s.group) {
|
|
|
|
|
groups.append(s.group)
|
|
|
|
|
}
|
2020-06-23 00:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let height: CGFloat = CGFloat((22*filtered.count)) + Constants.Popup.separatorHeight
|
|
|
|
|
let view: NSView = NSView(frame: NSRect(x: 0, y: y, width: self.frame.width, height: height))
|
2021-05-22 14:58:20 +02:00
|
|
|
let separator = separatorView(localizedString(typ.rawValue), origin: NSPoint(x: 0, y: view.frame.height - Constants.Popup.separatorHeight), width: self.frame.width)
|
2020-06-23 00:03:00 +02:00
|
|
|
view.addSubview(separator)
|
|
|
|
|
|
|
|
|
|
var i: CGFloat = 0
|
2021-04-10 15:50:02 +02:00
|
|
|
groups.reversed().forEach { (group: SensorGroup) in
|
2021-08-07 12:33:23 +03:00
|
|
|
filtered.reversed().filter{ $0.group == group }.forEach { (s: Sensor_p) in
|
2021-05-22 14:58:20 +02:00
|
|
|
let (key, value) = popupRow(view, n: i, title: "\(s.name):", value: s.formattedValue)
|
2020-12-10 19:55:46 +01:00
|
|
|
key.toolTip = s.key
|
|
|
|
|
self.list[s.key] = value
|
2020-06-23 00:03:00 +02:00
|
|
|
i += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.addSubview(view)
|
|
|
|
|
y += height
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.setFrameSize(NSSize(width: self.frame.width, height: y - Constants.Popup.margins))
|
2020-10-31 19:34:28 +01:00
|
|
|
self.sizeCallback?(self.frame.size)
|
2020-06-23 00:03:00 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-07 12:33:23 +03:00
|
|
|
internal func usageCallback(_ values: [Sensor_p]) {
|
2020-11-26 15:39:36 +01:00
|
|
|
DispatchQueue.main.async(execute: {
|
2021-05-22 14:58:20 +02:00
|
|
|
if self.window?.isVisible ?? false {
|
2021-08-07 12:33:23 +03:00
|
|
|
values.forEach { (s: Sensor_p) in
|
2020-11-26 15:39:36 +01:00
|
|
|
if self.list[s.key] != nil {
|
2020-06-23 00:03:00 +02:00
|
|
|
self.list[s.key]?.stringValue = s.formattedValue
|
|
|
|
|
}
|
2020-11-26 15:39:36 +01:00
|
|
|
}
|
2020-06-23 00:03:00 +02:00
|
|
|
}
|
2020-11-26 15:39:36 +01:00
|
|
|
})
|
2020-06-23 00:03:00 +02:00
|
|
|
}
|
|
|
|
|
}
|