mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added CPU, GPU, ANE, RAM, and PCI powers from IOReport (#2346)
This commit is contained in:
@@ -46,17 +46,16 @@ open class Settings: NSStackView, Settings_p {
|
||||
Store.shared.bool(key: "OneView", defaultValue: false)
|
||||
}
|
||||
private var oneViewState: Bool {
|
||||
get {
|
||||
return Store.shared.bool(key: "\(self.config.pointee.name)_oneView", defaultValue: false)
|
||||
}
|
||||
set {
|
||||
Store.shared.set(key: "\(self.config.pointee.name)_oneView", value: newValue)
|
||||
}
|
||||
get { Store.shared.bool(key: "\(self.config.pointee.name)_oneView", defaultValue: false) }
|
||||
set { Store.shared.set(key: "\(self.config.pointee.name)_oneView", value: newValue) }
|
||||
}
|
||||
|
||||
private var isPopupSettingsAvailable: Bool
|
||||
private var isNotificationsSettingsAvailable: Bool
|
||||
|
||||
private var previewView: NSView? = nil
|
||||
private var settingsView: NSView? = nil
|
||||
|
||||
init(config: UnsafePointer<module_c>, widgets: UnsafeMutablePointer<[SWidget]>, moduleSettings: Settings_v?, popupSettings: Popup_p?, notificationsSettings: NotificationsWrapper?) {
|
||||
self.config = config
|
||||
self.widgets = widgets.pointee
|
||||
@@ -80,11 +79,15 @@ open class Settings: NSStackView, Settings_p {
|
||||
right: Constants.Settings.margin
|
||||
)
|
||||
|
||||
let widgetSelector = WidgetSelectorView(module: self.config.pointee.name, widgets: self.widgets, stateCallback: self.loadWidget)
|
||||
let tabView = self.settingsView()
|
||||
let header = self.header()
|
||||
let settingsView = self.settings()
|
||||
self.settingsView = settingsView
|
||||
let previewView = self.preview()
|
||||
self.previewView = previewView
|
||||
|
||||
self.addArrangedSubview(widgetSelector)
|
||||
self.addArrangedSubview(tabView)
|
||||
self.addArrangedSubview(header)
|
||||
self.addArrangedSubview(settingsView)
|
||||
self.addArrangedSubview(previewView)
|
||||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(listenForOneView), name: .toggleOneView, object: nil)
|
||||
self.segmentedControl?.widthAnchor.constraint(equalTo: self.widthAnchor, constant: -(Constants.Settings.margin*2)).isActive = true
|
||||
@@ -102,7 +105,31 @@ open class Settings: NSStackView, Settings_p {
|
||||
toggleNSControlState(self.enableControl, state: newState ? .on : .off)
|
||||
}
|
||||
|
||||
private func settingsView() -> NSView {
|
||||
private func header() -> NSView {
|
||||
let view = NSStackView()
|
||||
view.orientation = .horizontal
|
||||
view.spacing = Constants.Settings.margin
|
||||
|
||||
let widgetSelector = WidgetSelectorView(module: self.config.pointee.name, widgets: self.widgets, stateCallback: self.loadWidget)
|
||||
// let button = ButtonSelectorView { [weak self] in
|
||||
// self?.toggleView()
|
||||
// }
|
||||
|
||||
view.addArrangedSubview(widgetSelector)
|
||||
// view.addArrangedSubview(button)
|
||||
|
||||
return view
|
||||
}
|
||||
|
||||
private func preview() -> NSView {
|
||||
let view = NSStackView()
|
||||
view.isHidden = true
|
||||
view.orientation = .vertical
|
||||
view.addArrangedSubview(EmptyView(height: 0, msg: localizedString("Preview is not available for that module")))
|
||||
return view
|
||||
}
|
||||
|
||||
private func settings() -> NSView {
|
||||
let view = NSStackView()
|
||||
view.orientation = .vertical
|
||||
view.spacing = Constants.Settings.margin
|
||||
@@ -268,6 +295,13 @@ open class Settings: NSStackView, Settings_p {
|
||||
self.oneViewBtn?.state = self.oneViewState ? .on : .off
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func toggleView() {
|
||||
guard let preview = self.previewView, let settings = self.settingsView else { return }
|
||||
|
||||
preview.isHidden = !preview.isHidden
|
||||
settings.isHidden = !settings.isHidden
|
||||
}
|
||||
}
|
||||
|
||||
private class WidgetSelectorView: NSStackView {
|
||||
@@ -622,3 +656,90 @@ private class WidgetSettings: NSStackView {
|
||||
return container
|
||||
}
|
||||
}
|
||||
|
||||
private class ButtonSelectorView: NSStackView {
|
||||
private var callback: () -> Void
|
||||
|
||||
private var background: NSVisualEffectView = {
|
||||
let view = NSVisualEffectView(frame: NSRect.zero)
|
||||
view.blendingMode = .withinWindow
|
||||
view.material = .contentBackground
|
||||
view.state = .active
|
||||
view.wantsLayer = true
|
||||
view.layer?.cornerRadius = 5
|
||||
return view
|
||||
}()
|
||||
|
||||
private var settingsIcon: NSImage {
|
||||
if #available(macOS 12.0, *), let icon = iconFromSymbol(name: "gear", scale: .large) {
|
||||
return icon
|
||||
}
|
||||
return NSImage(named: NSImage.Name("settings"))!
|
||||
}
|
||||
private var previewIcon: NSImage {
|
||||
if #available(macOS 12.0, *), let icon = iconFromSymbol(name: "command", scale: .large) {
|
||||
return icon
|
||||
}
|
||||
return NSImage(named: NSImage.Name("chart"))!
|
||||
}
|
||||
|
||||
private var button: NSButton? = nil
|
||||
private var isSettingsEnabled: Bool = false
|
||||
|
||||
fileprivate init(callback: @escaping () -> Void) {
|
||||
self.callback = callback
|
||||
|
||||
super.init(frame: NSRect.zero)
|
||||
|
||||
self.heightAnchor.constraint(equalToConstant: Constants.Widget.height + (Constants.Settings.margin*2)).isActive = true
|
||||
self.translatesAutoresizingMaskIntoConstraints = false
|
||||
self.edgeInsets = NSEdgeInsets(
|
||||
top: Constants.Settings.margin,
|
||||
left: Constants.Settings.margin,
|
||||
bottom: Constants.Settings.margin,
|
||||
right: Constants.Settings.margin
|
||||
)
|
||||
self.spacing = Constants.Settings.margin
|
||||
|
||||
self.addSubview(self.background, positioned: .below, relativeTo: .none)
|
||||
|
||||
let button = NSButton()
|
||||
button.toolTip = localizedString("Open module settings")
|
||||
button.bezelStyle = .regularSquare
|
||||
button.translatesAutoresizingMaskIntoConstraints = false
|
||||
button.imageScaling = .scaleNone
|
||||
button.image = self.settingsIcon
|
||||
button.contentTintColor = .secondaryLabelColor
|
||||
button.isBordered = false
|
||||
button.action = #selector(self.action)
|
||||
button.target = self
|
||||
button.focusRingType = .none
|
||||
button.widthAnchor.constraint(equalToConstant: Constants.Widget.height).isActive = true
|
||||
self.button = button
|
||||
|
||||
self.addArrangedSubview(button)
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func updateLayer() {
|
||||
self.background.setFrameSize(self.frame.size)
|
||||
}
|
||||
|
||||
@objc private func action() {
|
||||
guard let button = self.button else { return }
|
||||
self.callback()
|
||||
|
||||
self.isSettingsEnabled = !self.isSettingsEnabled
|
||||
|
||||
if self.isSettingsEnabled {
|
||||
button.image = self.previewIcon
|
||||
button.toolTip = localizedString("Close module settings")
|
||||
} else {
|
||||
button.image = self.settingsIcon
|
||||
button.toolTip = localizedString("Open module settings")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user