From 5eb709ccf39aa5e30e0a070a72e4314ba22316a2 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Tue, 30 Mar 2021 20:34:50 +0200 Subject: [PATCH] feat: init `Label` widget (#376) feat: removed labels option from `Bar, Line and Pie chart` widgets feat: added `Label widget` to the all modules feat: removed another UnsafeMutablePointer --- ModuleKit/Widgets/BarChart.swift | 2 +- ModuleKit/Widgets/Label.swift | 71 +++++++++++++++++++++++++++++++ ModuleKit/Widgets/LineChart.swift | 2 +- ModuleKit/Widgets/PieChart.swift | 2 +- ModuleKit/settings.swift | 25 ++++++----- ModuleKit/widget.swift | 6 +++ Modules/Battery/config.plist | 13 ++++-- Modules/CPU/config.plist | 15 +++++-- Modules/Disk/config.plist | 19 ++++++--- Modules/Fans/config.plist | 9 +++- Modules/GPU/config.plist | 13 ++++-- Modules/Net/config.plist | 13 ++++-- Modules/RAM/config.plist | 17 +++++--- Modules/Sensors/config.plist | 9 +++- Stats.xcodeproj/project.pbxproj | 4 ++ Stats/Views/Settings.swift | 12 +++--- 16 files changed, 188 insertions(+), 44 deletions(-) create mode 100644 ModuleKit/Widgets/Label.swift diff --git a/ModuleKit/Widgets/BarChart.swift b/ModuleKit/Widgets/BarChart.swift index 302cdb82..453a6eef 100644 --- a/ModuleKit/Widgets/BarChart.swift +++ b/ModuleKit/Widgets/BarChart.swift @@ -13,7 +13,7 @@ import Cocoa import StatsKit public class BarChart: WidgetWrapper { - private var labelState: Bool = true + private var labelState: Bool = false private var boxState: Bool = true private var frameState: Bool = false private var colorState: widget_c = .systemAccent diff --git a/ModuleKit/Widgets/Label.swift b/ModuleKit/Widgets/Label.swift new file mode 100644 index 00000000..b6b8f6d0 --- /dev/null +++ b/ModuleKit/Widgets/Label.swift @@ -0,0 +1,71 @@ +// +// Label.swift +// ModuleKit +// +// Created by Serhiy Mytrovtsiy on 30/03/2021. +// Using Swift 5.0. +// Running on macOS 10.15. +// +// Copyright © 2021 Serhiy Mytrovtsiy. All rights reserved. +// + +import Cocoa +import StatsKit + +public class Label: WidgetWrapper { + private var label: String + + public init(title: String, config: NSDictionary, preview: Bool = false) { + if let title = config["Title"] as? String { + self.label = title + } else { + self.label = title + } + + super.init(.label, title: title, frame: CGRect( + x: 0, + y: Constants.Widget.margin.y, + width: 6 + (2*Constants.Widget.margin.x), + height: Constants.Widget.height - (2*Constants.Widget.margin.y) + )) + + self.canDrawConcurrently = true + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + public override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + + let size: CGSize = CGSize(width: 6, height: self.frame.height / 3) + var margin: CGPoint = CGPoint(x: Constants.Widget.margin.x, y: 0) + let style = NSMutableParagraphStyle() + style.alignment = .center + + let stringAttributes = [ + NSAttributedString.Key.font: NSFont.systemFont(ofSize: 7, weight: .regular), + NSAttributedString.Key.foregroundColor: NSColor.textColor, + NSAttributedString.Key.paragraphStyle: style + ] + + for char in String(self.label.prefix(3)).uppercased().reversed() { + let rect = CGRect(x: margin.x, y: margin.y, width: size.width, height: size.height) + let str = NSAttributedString.init(string: "\(char)", attributes: stringAttributes) + str.draw(with: rect) + margin.y += size.height + } + } + + public func setLabel(_ new: String) { + if self.label == new { + return + } + + self.label = new + DispatchQueue.main.async(execute: { + self.needsDisplay = true + }) + } +} diff --git a/ModuleKit/Widgets/LineChart.swift b/ModuleKit/Widgets/LineChart.swift index a320f82e..a3bdd30c 100644 --- a/ModuleKit/Widgets/LineChart.swift +++ b/ModuleKit/Widgets/LineChart.swift @@ -13,7 +13,7 @@ import Cocoa import StatsKit public class LineChart: WidgetWrapper { - private var labelState: Bool = true + private var labelState: Bool = false private var boxState: Bool = true private var frameState: Bool = false private var valueState: Bool = false diff --git a/ModuleKit/Widgets/PieChart.swift b/ModuleKit/Widgets/PieChart.swift index 067ff1d0..70325dfe 100644 --- a/ModuleKit/Widgets/PieChart.swift +++ b/ModuleKit/Widgets/PieChart.swift @@ -13,7 +13,7 @@ import Cocoa import StatsKit public class PieChart: WidgetWrapper { - private var labelState: Bool = true + private var labelState: Bool = false private var chart: PieChartView = PieChartView( frame: NSRect( diff --git a/ModuleKit/settings.swift b/ModuleKit/settings.swift index 571727af..a15e9cff 100644 --- a/ModuleKit/settings.swift +++ b/ModuleKit/settings.swift @@ -275,6 +275,9 @@ internal class WidgetPreview: NSStackView { private var widget: UnsafeMutablePointer private var size: CGFloat { get { + if self.widget.pointee.type == .label { + return Constants.Widget.spacing*2 + } return self.widget.pointee.isActive ? Constants.Widget.height + (Constants.Widget.spacing*3) + 1 : Constants.Widget.spacing*2 } } @@ -321,7 +324,7 @@ internal class WidgetPreview: NSStackView { container.addSubview(widget.pointee.preview) self.addArrangedSubview(container) - if self.widget.pointee.isActive { + if self.widget.pointee.isActive && self.widget.pointee.type != .label { self.addArrangedSubview(self.separator) if let button = self.button { self.addArrangedSubview(button) @@ -412,15 +415,17 @@ internal class WidgetPreview: NSStackView { self.widget.pointee.toggle() self.stateCallback() - if self.widget.pointee.isActive { - self.addArrangedSubview(self.separator) - if let button = self.button { - self.addArrangedSubview(button) - } - } else { - self.removeView(self.separator) - if let button = self.button { - self.removeView(button) + if self.widget.pointee.type != .label { + if self.widget.pointee.isActive { + self.addArrangedSubview(self.separator) + if let button = self.button { + self.addArrangedSubview(button) + } + } else { + self.removeView(self.separator) + if let button = self.button { + self.removeView(button) + } } } diff --git a/ModuleKit/widget.swift b/ModuleKit/widget.swift index 3ac838ed..47673025 100644 --- a/ModuleKit/widget.swift +++ b/ModuleKit/widget.swift @@ -24,6 +24,7 @@ public enum widget_t: String { case battery = "battery" case sensors = "sensors" case memory = "memory" + case label = "label" public func new(module: String, config: NSDictionary, defaultWidget: widget_t) -> Widget? { var preview: widget_p? = nil @@ -69,6 +70,10 @@ public enum widget_t: String { preview = MemoryWidget(title: module, config: widgetConfig, preview: true) item = MemoryWidget(title: module, config: widgetConfig, preview: false) break + case .label: + preview = Label(title: module, config: widgetConfig, preview: true) + item = Label(title: module, config: widgetConfig, preview: false) + break default: break } @@ -90,6 +95,7 @@ public enum widget_t: String { case .battery: return LocalizedString("Battery widget") case .sensors: return LocalizedString("Text widget") case .memory: return LocalizedString("Memory widget") + case .label: return LocalizedString("Label widget") default: return "" } } diff --git a/Modules/Battery/config.plist b/Modules/Battery/config.plist index d7ee3fc9..a069e497 100644 --- a/Modules/Battery/config.plist +++ b/Modules/Battery/config.plist @@ -8,6 +8,13 @@ Widgets + label + + Default + + Order + 0 + mini Default @@ -32,7 +39,7 @@ pressure Order - 0 + 1 bar_chart @@ -51,14 +58,14 @@ pressure Order - 1 + 2 battery Default Order - 2 + 3 diff --git a/Modules/CPU/config.plist b/Modules/CPU/config.plist index 423d2b48..8b8e3f05 100644 --- a/Modules/CPU/config.plist +++ b/Modules/CPU/config.plist @@ -8,6 +8,13 @@ Widgets + label + + Default + + Order + 0 + mini Default @@ -22,7 +29,7 @@ pressure Order - 0 + 1 line_chart @@ -35,7 +42,7 @@ pressure Order - 1 + 2 bar_chart @@ -53,14 +60,14 @@ pressure Order - 2 + 3 pie_chart Default Order - 3 + 4 diff --git a/Modules/Disk/config.plist b/Modules/Disk/config.plist index 9fb9bcd8..4cbcc3e9 100644 --- a/Modules/Disk/config.plist +++ b/Modules/Disk/config.plist @@ -8,6 +8,15 @@ Widgets + label + + Default + + Title + SSD + Order + 0 + mini Default @@ -26,7 +35,7 @@ pressure Order - 0 + 1 bar_chart @@ -54,7 +63,7 @@ pressure Order - 1 + 2 memory @@ -66,14 +75,12 @@ 47.85 GB, 184.84 GB Order - 2 + 3 speed Default - Order - 3 Icon characters Symbols @@ -81,6 +88,8 @@ W R + Order + 4 diff --git a/Modules/Fans/config.plist b/Modules/Fans/config.plist index 6e1c2902..075b0a92 100644 --- a/Modules/Fans/config.plist +++ b/Modules/Fans/config.plist @@ -8,6 +8,13 @@ Widgets + label + + Default + + Order + 0 + sensors Default @@ -18,7 +25,7 @@ 2160 RPM,1990 RPM Order - 0 + 1 diff --git a/Modules/GPU/config.plist b/Modules/GPU/config.plist index 3a6f8ee1..267b19c5 100644 --- a/Modules/GPU/config.plist +++ b/Modules/GPU/config.plist @@ -8,6 +8,13 @@ Widgets + label + + Default + + Order + 0 + mini Default @@ -22,7 +29,7 @@ pressure Order - 0 + 1 line_chart @@ -35,7 +42,7 @@ pressure Order - 1 + 2 bar_chart @@ -53,7 +60,7 @@ pressure Order - 2 + 3 diff --git a/Modules/Net/config.plist b/Modules/Net/config.plist index c511b266..d5cff8b2 100644 --- a/Modules/Net/config.plist +++ b/Modules/Net/config.plist @@ -8,24 +8,31 @@ Widgets + label + + Default + + Order + 0 + speed Default - Order - 0 Symbols U D + Order + 1 network_chart Default Order - 1 + 2 diff --git a/Modules/RAM/config.plist b/Modules/RAM/config.plist index fa7588f2..a042a36c 100644 --- a/Modules/RAM/config.plist +++ b/Modules/RAM/config.plist @@ -8,6 +8,13 @@ Widgets + label + + Default + + Order + 0 + mini Default @@ -18,7 +25,7 @@ 0.58 Order - 0 + 1 line_chart @@ -27,7 +34,7 @@ Color systemAccent Order - 1 + 2 bar_chart @@ -51,14 +58,14 @@ 0.48 Order - 2 + 3 pie_chart Default Order - 3 + 4 memory @@ -70,7 +77,7 @@ 7.75 GB, 8.25 GB Order - 4 + 5 diff --git a/Modules/Sensors/config.plist b/Modules/Sensors/config.plist index 195e9e57..f8da317d 100644 --- a/Modules/Sensors/config.plist +++ b/Modules/Sensors/config.plist @@ -8,6 +8,13 @@ Widgets + label + + Default + + Order + 0 + sensors Default @@ -18,7 +25,7 @@ 38°,41° Order - 0 + 1 diff --git a/Stats.xcodeproj/project.pbxproj b/Stats.xcodeproj/project.pbxproj index cfc27376..4038a1eb 100644 --- a/Stats.xcodeproj/project.pbxproj +++ b/Stats.xcodeproj/project.pbxproj @@ -38,6 +38,7 @@ 9A58DE9E24B363D800716A9F /* popup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A58DE9D24B363D800716A9F /* popup.swift */; }; 9A58DEA024B363F300716A9F /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A58DE9F24B363F300716A9F /* settings.swift */; }; 9A58DEA424B3647600716A9F /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A58DEA324B3647600716A9F /* settings.swift */; }; + 9A599701261121F00043560F /* Label.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A599700261121F00043560F /* Label.swift */; }; 9A5AF11B2469CE9B00684737 /* popup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A5AF11A2469CE9B00684737 /* popup.swift */; }; 9A65295825B78056005E2DE4 /* NetworkChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A65295725B78056005E2DE4 /* NetworkChart.swift */; }; 9A65654A253F20EF0096B607 /* settings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9A656549253F20EF0096B607 /* settings.swift */; }; @@ -403,6 +404,7 @@ 9A58DE9D24B363D800716A9F /* popup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = popup.swift; sourceTree = ""; }; 9A58DE9F24B363F300716A9F /* settings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = settings.swift; sourceTree = ""; }; 9A58DEA324B3647600716A9F /* settings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = settings.swift; sourceTree = ""; }; + 9A599700261121F00043560F /* Label.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Label.swift; sourceTree = ""; }; 9A5AF11A2469CE9B00684737 /* popup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = popup.swift; sourceTree = ""; }; 9A5F0503256A9135002FF75F /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = nb.lproj/Localizable.strings; sourceTree = ""; }; 9A65295725B78056005E2DE4 /* NetworkChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkChart.swift; sourceTree = ""; }; @@ -715,6 +717,7 @@ 9ABFF911248BF39500C9041A /* Battery.swift */, 9AE29AFD249A82B70071B02D /* Sensors.swift */, 9A41530B24ABC3AF00A2BDA7 /* Memory.swift */, + 9A599700261121F00043560F /* Label.swift */, ); path = Widgets; sourceTree = ""; @@ -1524,6 +1527,7 @@ 9A20E6DA2575555100AC2302 /* PieChart.swift in Sources */, 9A944D55244920690058F32A /* reader.swift in Sources */, 9A65295825B78056005E2DE4 /* NetworkChart.swift in Sources */, + 9A599701261121F00043560F /* Label.swift in Sources */, 9A7C61B42440DF810032695D /* Mini.swift in Sources */, 9AE29AFE249A82B70071B02D /* Sensors.swift in Sources */, 9A944D5D24492A8B0058F32A /* popup.swift in Sources */, diff --git a/Stats/Views/Settings.swift b/Stats/Views/Settings.swift index bce10e5f..bdc0555a 100644 --- a/Stats/Views/Settings.swift +++ b/Stats/Views/Settings.swift @@ -60,7 +60,7 @@ class SettingsWindow: NSWindow, NSWindowDelegate { } public func setModules() { - self.viewController.setModules(&modules) + self.viewController.setModules(modules) if modules.filter({ $0.enabled != false && $0.available != false }).count == 0 { self.setIsVisible(true) } @@ -95,7 +95,7 @@ private class SettingsViewController: NSViewController { super.viewDidLoad() } - public func setModules(_ list: UnsafeMutablePointer<[Module]>) { + public func setModules(_ list: [Module]) { self.settings.setModules(list) } @@ -105,7 +105,7 @@ private class SettingsViewController: NSViewController { } private class SettingsView: NSView { - private var modules: UnsafeMutablePointer<[Module]>? + private var modules: [Module] = [] private let sidebarWidth: CGFloat = 180 private let navigationHeight: CGFloat = 45 @@ -188,8 +188,8 @@ private class SettingsView: NSView { }) } - public func setModules(_ list: UnsafeMutablePointer<[Module]>) { - list.pointee.forEach { (m: Module) in + public func setModules(_ list: [Module]) { + list.forEach { (m: Module) in if !m.available { return } let n: Int = self.menuView.subviews.count - 1 let menu: NSView = MenuView(n: n, icon: m.config.icon, title: m.config.name) @@ -202,7 +202,7 @@ private class SettingsView: NSView { if let title = notification.userInfo?["module"] as? String { var view: NSView = NSView() - if let detectedModule = self.modules?.pointee.first(where: { $0.config.name == title }) { + if let detectedModule = self.modules.first(where: { $0.config.name == title }) { if let v = detectedModule.settings { view = v }