From 6039d146e8a7642d79601acfb89f5ef205f3735b Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Mon, 6 Feb 2023 17:59:40 +0100 Subject: [PATCH] feat: added an option to set a spacing between modules in the combined view (#1084) --- .swiftlint.yml | 1 + Kit/module/module.swift | 2 +- Kit/types.swift | 12 +++++++++ Stats/Views/AppSettings.swift | 45 +++++++++++++++++++++++----------- Stats/Views/CombinedView.swift | 7 ++++-- 5 files changed, 50 insertions(+), 17 deletions(-) diff --git a/.swiftlint.yml b/.swiftlint.yml index 6e7f4b50..78070f28 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -34,6 +34,7 @@ identifier_name: - _uuidAddress - AppleSiliconSensorsList - FanValues + - CombinedModulesSpacings line_length: 200 diff --git a/Kit/module/module.swift b/Kit/module/module.swift index 58e5df75..1d4a4656 100644 --- a/Kit/module/module.swift +++ b/Kit/module/module.swift @@ -82,7 +82,7 @@ open class Module: Module_p { public var name: String { config.name } - public var oneViewPosition: Int { + public var combinedPosition: Int { get { Store.shared.int(key: "\(self.name)_position", defaultValue: 0) } diff --git a/Kit/types.swift b/Kit/types.swift index acc2d47a..226bfbc9 100644 --- a/Kit/types.swift +++ b/Kit/types.swift @@ -54,6 +54,18 @@ public let TemperatureUnits: [KeyValue_t] = [ KeyValue_t(key: "fahrenheit", value: "Fahrenheit", additional: UnitTemperature.fahrenheit) ] +public let CombinedModulesSpacings: [KeyValue_t] = [ + KeyValue_t(key: "none", value: "None"), + KeyValue_t(key: "1", value: "1", additional: 1), + KeyValue_t(key: "2", value: "2", additional: 2), + KeyValue_t(key: "3", value: "3", additional: 3), + KeyValue_t(key: "4", value: "4", additional: 4), + KeyValue_t(key: "5", value: "5", additional: 5), + KeyValue_t(key: "6", value: "6", additional: 6), + KeyValue_t(key: "7", value: "7", additional: 7), + KeyValue_t(key: "8", value: "8", additional: 8) +] + public enum DataSizeBase: String { case bit case byte diff --git a/Stats/Views/AppSettings.swift b/Stats/Views/AppSettings.swift index 31c49af9..cf1cc575 100644 --- a/Stats/Views/AppSettings.swift +++ b/Stats/Views/AppSettings.swift @@ -19,7 +19,7 @@ class ApplicationSettings: NSStackView { private var temperatureUnitsValue: String { get { - return Store.shared.string(key: "temperature_units", defaultValue: "system") + Store.shared.string(key: "temperature_units", defaultValue: "system") } set { Store.shared.set(key: "temperature_units", value: newValue) @@ -34,6 +34,14 @@ class ApplicationSettings: NSStackView { Store.shared.set(key: "CombinedModules", value: newValue) } } + private var combinedModulesSpacing: String { + get { + Store.shared.string(key: "CombinedModules_spacing", defaultValue: "none") + } + set { + Store.shared.set(key: "CombinedModules_spacing", value: newValue) + } + } private let updateWindow: UpdateWindow = UpdateWindow() private let moduleSelector: ModuleSelectorView = ModuleSelectorView() @@ -190,6 +198,14 @@ class ApplicationSettings: NSStackView { state: self.combinedModulesState, text: localizedString("Combined modules") )]) + grid.addRow(with: [ + self.titleView(localizedString("Combined module spacing")), + selectView( + action: #selector(self.toggleCombinedModulesSpacing), + items: CombinedModulesSpacings, + selected: self.combinedModulesSpacing + ) + ]) view.addArrangedSubview(self.moduleSelector) view.addArrangedSubview(grid) @@ -270,7 +286,7 @@ class ApplicationSettings: NSStackView { // MARK: - actions - @objc func updateAction(_ sender: NSObject) { + @objc private func updateAction(_ sender: NSObject) { updater.check(force: true, completion: { result, error in if error != nil { debug("error updater.check(): \(error!.localizedDescription)") @@ -290,21 +306,16 @@ class ApplicationSettings: NSStackView { } @objc private func toggleUpdateInterval(_ sender: NSMenuItem) { - guard let key = sender.representedObject as? String else { - return - } - + guard let key = sender.representedObject as? String else { return } Store.shared.set(key: "update-interval", value: key) } @objc private func toggleTemperatureUnits(_ sender: NSMenuItem) { - guard let key = sender.representedObject as? String else { - return - } + guard let key = sender.representedObject as? String else { return } self.temperatureUnitsValue = key } - @objc func toggleDock(_ sender: NSButton) { + @objc private func toggleDock(_ sender: NSButton) { let state = sender.state Store.shared.set(key: "dockIcon", value: state == NSControl.StateValue.on) @@ -315,14 +326,14 @@ class ApplicationSettings: NSStackView { } } - @objc func toggleLaunchAtLogin(_ sender: NSButton) { + @objc private func toggleLaunchAtLogin(_ sender: NSButton) { LaunchAtLogin.isEnabled = sender.state == NSControl.StateValue.on if !Store.shared.exist(key: "runAtLoginInitialized") { Store.shared.set(key: "runAtLoginInitialized", value: true) } } - @objc func resetSettings(_ sender: NSObject) { + @objc private func resetSettings(_ sender: NSObject) { let alert = NSAlert() alert.messageText = localizedString("Reset settings") alert.informativeText = localizedString("Reset settings text") @@ -359,6 +370,12 @@ class ApplicationSettings: NSStackView { self.moduleSelector.isHidden = !self.combinedModulesState NotificationCenter.default.post(name: .toggleOneView, object: nil, userInfo: nil) } + + @objc private func toggleCombinedModulesSpacing(_ sender: NSMenuItem) { + guard let key = sender.representedObject as? String else { return } + self.combinedModulesSpacing = key + NotificationCenter.default.post(name: .moduleRearrange, object: nil, userInfo: nil) + } } private class ModuleSelectorView: NSStackView { @@ -385,7 +402,7 @@ private class ModuleSelectorView: NSStackView { }() var w = self.spacing - modules.filter({ $0.available }).sorted(by: { $0.oneViewPosition < $1.oneViewPosition }).forEach { (m: Module) in + modules.filter({ $0.available }).sorted(by: { $0.combinedPosition < $1.combinedPosition }).forEach { (m: Module) in let v = ModulePreview(id: m.name, icon: m.config.icon) self.addArrangedSubview(v) w += v.frame.width + self.spacing @@ -464,7 +481,7 @@ private class ModuleSelectorView: NSStackView { for (i, v) in self.views(in: .leading).compactMap({$0 as? ModulePreview}).enumerated() { if let m = modules.first(where: { $0.name == v.identifier?.rawValue }) { - m.oneViewPosition = i + m.combinedPosition = i } } } diff --git a/Stats/Views/CombinedView.swift b/Stats/Views/CombinedView.swift index 993408fb..0ebbda03 100644 --- a/Stats/Views/CombinedView.swift +++ b/Stats/Views/CombinedView.swift @@ -19,6 +19,9 @@ class CombinedView { private var status: Bool { Store.shared.bool(key: "CombinedModules", defaultValue: false) } + private var spacing: CGFloat { + CGFloat(Int(Store.shared.string(key: "CombinedModules_spacing", defaultValue: "")) ?? 0) + } init() { modules.forEach { (m: Module) in @@ -69,10 +72,10 @@ class CombinedView { var w: CGFloat = 0 var i: Int = 0 - modules.filter({ $0.enabled }).sorted(by: { $0.oneViewPosition < $1.oneViewPosition }).forEach { (m: Module) in + modules.filter({ $0.enabled }).sorted(by: { $0.combinedPosition < $1.combinedPosition }).forEach { (m: Module) in self.view.addSubview(m.menuBar.view) self.view.subviews[i].setFrameOrigin(NSPoint(x: w, y: 0)) - w += m.menuBar.view.frame.width + w += m.menuBar.view.frame.width + self.spacing i += 1 } self.view.setFrameSize(NSSize(width: w, height: self.view.frame.height))