From 68c45ab6049e26427bdb7ad8063e6d21e2adf2e6 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Fri, 25 Oct 2024 21:42:19 +0200 Subject: [PATCH] feat: added a new option to the Speed widget that specifies what to show in the widget (#2186). This new option removes the old "Reverse order" option since the new one includes it. --- .swiftlint.yml | 1 + Kit/Widgets/Speed.swift | 92 ++++++++++++++++++++++++++--------------- Kit/types.swift | 8 ++++ 3 files changed, 68 insertions(+), 33 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 028f8217..6ddda253 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -42,6 +42,7 @@ identifier_name: - _writeTS - LineChartHistory - SpeedPictogramColor + - SensorsWidgetValue line_length: 200 diff --git a/Kit/Widgets/Speed.swift b/Kit/Widgets/Speed.swift index e6f705ba..7cf37941 100644 --- a/Kit/Widgets/Speed.swift +++ b/Kit/Widgets/Speed.swift @@ -20,8 +20,8 @@ public class SpeedWidget: WidgetWrapper { private var iconColorState: String = "default" private var valueAlignmentState: String = "right" private var modeState: String = "twoRows" - private var reverseOrderState: Bool = false private var iconAlignmentState: String = "left" + private var displayValueState: String = "io" private var downloadColorState: SColor = .secondBlue private var uploadColorState: SColor = .secondRed @@ -37,6 +37,7 @@ public class SpeedWidget: WidgetWrapper { private var valueAlignmentView: NSPopUpButton? = nil private var iconAlignmentView: NSPopUpButton? = nil private var iconColorView: NSPopUpButton? = nil + private var displayModeView: NSPopUpButton? = nil private var downloadColor: (String) -> NSColor {{ state in if state == "none" { return .textColor } @@ -76,6 +77,10 @@ public class SpeedWidget: WidgetWrapper { DataSizeBase(rawValue: Store.shared.string(key: "\(self.title)_base", defaultValue: "byte")) ?? .byte } + private var reverseOrderState: Bool { + self.displayValueState == "oi" + } + public init(title: String, config: NSDictionary?, preview: Bool = false) { let widgetTitle: String = title if config != nil { @@ -104,9 +109,9 @@ public class SpeedWidget: WidgetWrapper { self.uploadColorState = SColor.fromString(Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_uploadColor", defaultValue: self.uploadColorState.key)) self.valueAlignmentState = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_valueAlignment", defaultValue: self.valueAlignmentState) self.modeState = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_mode", defaultValue: self.modeState) - self.reverseOrderState = Store.shared.bool(key: "\(self.title)_\(self.type.rawValue)_reverseOrder", defaultValue: self.reverseOrderState) self.iconAlignmentState = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_iconAlignment", defaultValue: self.iconAlignmentState) self.iconColorState = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_iconColor", defaultValue: self.iconColorState) + self.displayValueState = Store.shared.string(key: "\(self.title)_\(self.type.rawValue)_displayValue", defaultValue: self.displayValueState) } if preview { @@ -140,7 +145,7 @@ public class SpeedWidget: WidgetWrapper { private func drawOneRow() -> CGFloat { var width: CGFloat = Constants.Widget.margin.x - if self.reverseOrderState { + if self.displayValueState.first == "i" { width = self.drawRowItem( initWidth: width, symbol: self.symbols[1], @@ -148,14 +153,6 @@ public class SpeedWidget: WidgetWrapper { value: self.downloadValue, valueColor: self.downloadColor(self.valueColorState) ) - width += 4 - width = self.drawRowItem( - initWidth: width, - symbol: self.symbols[0], - iconColor: self.uploadColor(self.iconColorState), - value: self.uploadValue, - valueColor: self.uploadColor(self.valueColorState) - ) } else { width = self.drawRowItem( initWidth: width, @@ -164,14 +161,27 @@ public class SpeedWidget: WidgetWrapper { value: self.uploadValue, valueColor: self.uploadColor(self.valueColorState) ) - width += 4 - width = self.drawRowItem( - initWidth: width, - symbol: self.symbols[1], - iconColor: self.downloadColor(self.iconColorState), - value: self.downloadValue, - valueColor: self.downloadColor(self.valueColorState) - ) + } + + if self.displayValueState.count > 1 { + width += Constants.Widget.spacing*3 + if self.displayValueState.last == "i" { + width = self.drawRowItem( + initWidth: width, + symbol: self.symbols[1], + iconColor: self.downloadColor(self.iconColorState), + value: self.downloadValue, + valueColor: self.downloadColor(self.valueColorState) + ) + } else { + width = self.drawRowItem( + initWidth: width, + symbol: self.symbols[0], + iconColor: self.uploadColor(self.iconColorState), + value: self.uploadValue, + valueColor: self.uploadColor(self.valueColorState) + ) + } } return width + Constants.Widget.margin.x @@ -498,16 +508,21 @@ public class SpeedWidget: WidgetWrapper { valueColor.isEnabled = self.valueState self.valueColorView = valueColor + let displayMode = selectView( + action: #selector(self.changeDisplayMode), + items: SensorsWidgetMode.filter({ $0.key == "oneRow" || $0.key == "twoRows"}), + selected: self.modeState + ) + displayMode.isEnabled = self.displayValueState.count > 1 + self.displayModeView = displayMode + view.addArrangedSubview(PreferencesSection([ - PreferencesRow(localizedString("Display mode"), component: selectView( - action: #selector(self.changeDisplayMode), - items: SensorsWidgetMode.filter({ $0.key == "oneRow" || $0.key == "twoRows"}), - selected: self.modeState + PreferencesRow(localizedString("Value"), component: selectView( + action: #selector(self.changeDisplayValue), + items: SensorsWidgetValue, + selected: self.displayValueState )), - PreferencesRow(localizedString("Reverse order"), component: switchView( - action: #selector(self.toggleReverseOrder), - state: self.reverseOrderState - )) + PreferencesRow(localizedString("Display mode"), component: displayMode) ])) view.addArrangedSubview(PreferencesSection([ @@ -553,6 +568,23 @@ public class SpeedWidget: WidgetWrapper { return view } + @objc private func changeDisplayValue(_ sender: NSMenuItem) { + guard let key = sender.representedObject as? String else { return } + self.displayValueState = key + + if key.count == 1 { + if self.modeState != "oneRow" { + self.modeState = "oneRow" + Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_mode", value: self.modeState) + } + self.displayModeView?.selectItem(at: 0) + } + self.displayModeView?.isEnabled = key.count > 1 + + Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_displayValue", value: key) + self.display() + } + @objc private func changeDisplayMode(_ sender: NSMenuItem) { guard let key = sender.representedObject as? String else { return } self.modeState = key @@ -560,12 +592,6 @@ public class SpeedWidget: WidgetWrapper { self.display() } - @objc private func toggleReverseOrder(_ sender: NSControl) { - self.reverseOrderState = controlState(sender) - Store.shared.set(key: "\(self.title)_\(self.type.rawValue)_reverseOrder", value: self.reverseOrderState) - self.display() - } - @objc private func toggleValue(_ sender: NSControl) { self.valueState = controlState(sender) diff --git a/Kit/types.swift b/Kit/types.swift index a2304707..98d7a81b 100644 --- a/Kit/types.swift +++ b/Kit/types.swift @@ -102,6 +102,14 @@ internal enum StackMode: String { case twoRows = "twoRows" } +internal let SensorsWidgetValue: [KeyValue_t] = [ + KeyValue_t(key: "io", value: "Input/Output"), + KeyValue_t(key: "oi", value: "Output/Input"), + KeyValue_t(key: "separator", value: "separator"), + KeyValue_t(key: "i", value: "Input"), + KeyValue_t(key: "o", value: "Output") +] + internal let SensorsWidgetMode: [KeyValue_t] = [ KeyValue_t(key: StackMode.auto.rawValue, value: "Automatic"), KeyValue_t(key: "separator", value: "separator"),