mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: added No Bluetooth devices are available message to the Bluetooth module settings and popup view
This commit is contained in:
@@ -14,9 +14,10 @@ import Kit
|
||||
|
||||
internal class Popup: NSStackView, Popup_p {
|
||||
public var sizeCallback: ((NSSize) -> Void)? = nil
|
||||
private let emptyView: EmptyView = EmptyView(height: 30, isHidden: false)
|
||||
|
||||
public init() {
|
||||
super.init(frame: NSRect(x: 0, y: 0, width: Constants.Popup.width, height: 0))
|
||||
super.init(frame: NSRect(x: 0, y: 0, width: Constants.Popup.width, height: 30))
|
||||
|
||||
self.orientation = .vertical
|
||||
self.spacing = Constants.Popup.margins
|
||||
@@ -27,6 +28,20 @@ internal class Popup: NSStackView, Popup_p {
|
||||
}
|
||||
|
||||
internal func batteryCallback(_ list: [BLEDevice]) {
|
||||
defer {
|
||||
if list.isEmpty && self.emptyView.superview == nil {
|
||||
self.addArrangedSubview(self.emptyView)
|
||||
} else if !list.isEmpty && self.emptyView.superview != nil {
|
||||
self.emptyView.removeFromSuperview()
|
||||
}
|
||||
|
||||
let h = self.arrangedSubviews.map({ $0.bounds.height + self.spacing }).reduce(0, +) - self.spacing
|
||||
if h > 0 && self.frame.size.height != h {
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: h))
|
||||
self.sizeCallback?(self.frame.size)
|
||||
}
|
||||
}
|
||||
|
||||
var views = self.subviews.filter{ $0 is BLEView }.map{ $0 as! BLEView }
|
||||
if list.count < views.count && !views.isEmpty {
|
||||
views.forEach{ $0.removeFromSuperview() }
|
||||
@@ -45,12 +60,6 @@ internal class Popup: NSStackView, Popup_p {
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
let h = self.arrangedSubviews.map({ $0.bounds.height + self.spacing }).reduce(0, +) - self.spacing
|
||||
if h > 0 && self.frame.size.height != h {
|
||||
self.setFrameSize(NSSize(width: self.frame.width, height: h))
|
||||
self.sizeCallback?(self.frame.size)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -258,8 +258,6 @@ internal class DevicesReader: Reader<[BLEDevice]>, CBCentralManagerDelegate, CBP
|
||||
|
||||
self.characteristicsDict[peripheral.identifier] = batteryCharacteristics
|
||||
peripheral.readValue(for: batteryCharacteristics)
|
||||
|
||||
debug("\(peripheral.identifier): discover battery service")
|
||||
}
|
||||
|
||||
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
|
||||
|
||||
@@ -16,6 +16,7 @@ internal class Settings: NSStackView, Settings_v {
|
||||
public var callback: (() -> Void) = {}
|
||||
|
||||
private var list: [String: Bool] = [:]
|
||||
private let emptyView: EmptyView = EmptyView()
|
||||
|
||||
public init() {
|
||||
super.init(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
|
||||
@@ -31,6 +32,7 @@ internal class Settings: NSStackView, Settings_v {
|
||||
self.spacing = Constants.Settings.margin
|
||||
|
||||
self.addArrangedSubview(NSView())
|
||||
self.addArrangedSubview(self.emptyView)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
@@ -41,10 +43,17 @@ internal class Settings: NSStackView, Settings_v {
|
||||
|
||||
internal func setList(_ list: [BLEDevice]) {
|
||||
if self.list.count != list.count && !self.list.isEmpty {
|
||||
self.subviews.forEach{ $0.removeFromSuperview() }
|
||||
self.subviews.filter({ $0 is NSStackView && ($0 as! NSStackView).identifier != NSUserInterfaceItemIdentifier(rawValue: "emptyView") }).forEach{ $0.removeFromSuperview() }
|
||||
self.list = [:]
|
||||
}
|
||||
|
||||
if list.isEmpty && self.emptyView.isHidden {
|
||||
self.emptyView.isHidden = false
|
||||
return
|
||||
} else if !list.isEmpty && !self.emptyView.isHidden {
|
||||
self.emptyView.isHidden = true
|
||||
}
|
||||
|
||||
list.forEach { (d: BLEDevice) in
|
||||
if self.list[d.id] == nil {
|
||||
let row: NSView = toggleSettingRow(
|
||||
@@ -75,3 +84,32 @@ internal class Settings: NSStackView, Settings_v {
|
||||
self.callback()
|
||||
}
|
||||
}
|
||||
|
||||
internal class EmptyView: NSStackView {
|
||||
public init(height: CGFloat = 120, isHidden: Bool = false) {
|
||||
super.init(frame: NSRect())
|
||||
|
||||
self.heightAnchor.constraint(equalToConstant: height).isActive = true
|
||||
|
||||
self.translatesAutoresizingMaskIntoConstraints = true
|
||||
self.orientation = .vertical
|
||||
self.distribution = .fillEqually
|
||||
self.isHidden = isHidden
|
||||
self.identifier = NSUserInterfaceItemIdentifier(rawValue: "emptyView")
|
||||
|
||||
let textView: NSTextView = NSTextView()
|
||||
textView.heightAnchor.constraint(equalToConstant: (height/2)+6).isActive = true
|
||||
textView.alignment = .center
|
||||
textView.isEditable = false
|
||||
textView.isSelectable = false
|
||||
textView.drawsBackground = false
|
||||
textView.string = localizedString("No Bluetooth devices are available")
|
||||
|
||||
self.addArrangedSubview(NSView())
|
||||
self.addArrangedSubview(textView)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user