From ca98ce4ebbb024dc1fd648907f2c75dc32d528df Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 31 Oct 2020 00:04:01 +0100 Subject: [PATCH] - add an option to select the sensors widget layout (automatic/one row/two rows) - improved translations --- ModuleKit/Widgets/Sensors.swift | 195 +++++++++++------- .../de.lproj/Localizable.strings | 6 + .../en.lproj/Localizable.strings | 6 + .../es.lproj/Localizable.strings | 6 + .../ko.lproj/Localizable.strings | 6 + .../pl.lproj/Localizable.strings | 6 + .../ru.lproj/Localizable.strings | 6 + .../tr.lproj/Localizable.strings | 6 + .../uk.lproj/Localizable.strings | 6 + .../zh-Hans.lproj/Localizable.strings | 8 +- .../zh-Hant.lproj/Localizable.strings | 6 + StatsKit/extensions.swift | 2 +- StatsKit/helpers.swift | 7 + 13 files changed, 195 insertions(+), 71 deletions(-) diff --git a/ModuleKit/Widgets/Sensors.swift b/ModuleKit/Widgets/Sensors.swift index 02e079fd..0e8db024 100644 --- a/ModuleKit/Widgets/Sensors.swift +++ b/ModuleKit/Widgets/Sensors.swift @@ -23,6 +23,7 @@ public struct SensorValue_t { } public class SensorsWidget: Widget { + private var modeState: String = "automatic" private var iconState: Bool = false private let store: UnsafePointer? @@ -49,6 +50,7 @@ public class SensorsWidget: Widget { self.canDrawConcurrently = true if self.store != nil { + self.modeState = store!.pointee.string(key: "\(self.title)_\(self.type.rawValue)_mode", defaultValue: self.modeState) self.iconState = store!.pointee.bool(key: "\(self.title)_\(self.type.rawValue)_icon", defaultValue: self.iconState) } } @@ -66,85 +68,47 @@ public class SensorsWidget: Widget { } let num: Int = Int(round(Double(self.values.count) / 2)) - let rowHeight: CGFloat = self.frame.height / 2 var totalWidth: CGFloat = Constants.Widget.margin // opening space var x: CGFloat = Constants.Widget.margin - for i in 0.. CGFloat { + var width: CGFloat = 0 + var paddingLeft: CGFloat = 0 + + let font: NSFont = NSFont.systemFont(ofSize: 13, weight: .light) + width = sensor.value.widthOfString(usingFont: font).rounded(.up) + 2 + + if let icon = sensor.icon, self.iconState { + let iconSize: CGFloat = 11 + icon.draw(in: NSRect(x: x, y: ((Constants.Widget.height-iconSize)/2)-2, width: iconSize, height: iconSize)) + paddingLeft = iconSize + (Constants.Widget.margin*3) + width += paddingLeft + } + + let style = NSMutableParagraphStyle() + style.alignment = .center + let rect = CGRect(x: x+paddingLeft, y: (Constants.Widget.height-13)/2, width: width-paddingLeft, height: 13) + let str = NSAttributedString.init(string: sensor.value, attributes: [ + NSAttributedString.Key.font: font, + NSAttributedString.Key.foregroundColor: NSColor.textColor, + NSAttributedString.Key.paragraphStyle: style + ]) + str.draw(with: rect) + + return width + } + + private func drawTwoRows(topSensor: SensorValue_t?, bottomSensor: SensorValue_t?, x: CGFloat) -> CGFloat { + var width: CGFloat = 0 + var paddingLeft: CGFloat = 0 + let rowHeight: CGFloat = self.frame.height / 2 + + var font: NSFont = NSFont.systemFont(ofSize: 9, weight: .light) + if #available(OSX 10.15, *) { + font = NSFont.monospacedSystemFont(ofSize: 9, weight: .light) + } + let style = NSMutableParagraphStyle() + style.alignment = .right + + let firstRowWidth = topSensor?.value.widthOfString(usingFont: font) + let secondRowWidth = bottomSensor?.value.widthOfString(usingFont: font) + width = max(20, max(firstRowWidth ?? 0, secondRowWidth ?? 0)).rounded(.up) + + if self.iconState && (topSensor?.icon != nil || bottomSensor?.icon != nil) { + let iconSize: CGFloat = 8 + if let icon = topSensor?.icon { + icon.draw(in: NSRect(x: x, y: rowHeight+((rowHeight-iconSize)/2), width: iconSize, height: iconSize)) + } + if let icon = bottomSensor?.icon { + icon.draw(in: NSRect(x: x, y: (rowHeight-iconSize)/2, width: iconSize, height: iconSize)) + } + + paddingLeft = iconSize + (Constants.Widget.margin*3) + width += paddingLeft + } + + let attributes = [ + NSAttributedString.Key.font: font, + NSAttributedString.Key.foregroundColor: NSColor.textColor, + NSAttributedString.Key.paragraphStyle: style + ] + + if topSensor != nil { + let rect = CGRect(x: x+paddingLeft, y: rowHeight+1, width: width-paddingLeft, height: rowHeight) + let str = NSAttributedString.init(string: topSensor!.value, attributes: attributes) + str.draw(with: rect) + } + + if bottomSensor != nil { + let rect = CGRect(x: x+paddingLeft, y: 1, width: width-paddingLeft, height: rowHeight) + let str = NSAttributedString.init(string: bottomSensor!.value, attributes: attributes) + str.draw(with: rect) + } + + return width + } + public override func settings(superview: NSView) { let rowHeight: CGFloat = 30 let height: CGFloat = ((rowHeight + Constants.Settings.margin) * 1) + Constants.Settings.margin @@ -162,12 +203,20 @@ public class SensorsWidget: Widget { let view: NSView = NSView(frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: superview.frame.width - (Constants.Settings.margin*2), height: superview.frame.height - (Constants.Settings.margin*2))) view.addSubview(ToggleTitleRow( - frame: NSRect(x: 0, y: 0, width: view.frame.width, height: rowHeight), + frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 1, width: view.frame.width, height: rowHeight), title: LocalizedString("Pictogram"), action: #selector(toggleIcom), state: self.iconState )) + view.addSubview(SelectRow( + frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 0, width: view.frame.width, height: rowHeight), + title: LocalizedString("Display mode"), + action: #selector(changeMode), + items: SensorsWidgetMode, + selected: self.modeState + )) + superview.addSubview(view) } @@ -190,4 +239,12 @@ public class SensorsWidget: Widget { self.store?.pointee.set(key: "\(self.title)_\(self.type.rawValue)_icon", value: self.iconState) self.display() } + + @objc private func changeMode(_ sender: NSMenuItem) { + guard let key = sender.representedObject as? String else { + return + } + self.modeState = key + store?.pointee.set(key: "\(self.title)_\(self.type.rawValue)_mode", value: key) + } } diff --git a/Stats/Supporting Files/de.lproj/Localizable.strings b/Stats/Supporting Files/de.lproj/Localizable.strings index 5e708278..281cdd8d 100644 --- a/Stats/Supporting Files/de.lproj/Localizable.strings +++ b/Stats/Supporting Files/de.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "Nicht verfügbar"; "Yes" = "Ja"; "No" = "Nein"; +"Automatic" = "Automatisch"; // Alerts "New version available" = "Neue Version verfügbar"; @@ -61,6 +62,9 @@ "Additional information" = "Zusätzliche Informationen"; "Reverse values order" = "Umgekehrte Reihenfolge der Werte"; "Base" = "Basis"; +"Display mode" = "Anzeigemodus"; +"One row" = "Eine reihe"; +"Two rows" = "Zwei reihen"; // Module Kit "Open module settings" = "Module Einstellungen öffnen"; @@ -106,6 +110,8 @@ // Sensors "Temperature unit" = "Temperatureinheit"; +"Celsius" = "Celsius"; +"Fahrenheit" = "Fahrenheit"; // Network "Uploading" = "Upload"; diff --git a/Stats/Supporting Files/en.lproj/Localizable.strings b/Stats/Supporting Files/en.lproj/Localizable.strings index 08c37686..abc5260b 100644 --- a/Stats/Supporting Files/en.lproj/Localizable.strings +++ b/Stats/Supporting Files/en.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "Unavailable"; "Yes" = "Yes"; "No" = "No"; +"Automatic" = "Automatic"; // Alerts "New version available" = "New version available"; @@ -61,6 +62,9 @@ "Additional information" = "Additional information"; "Reverse values order" = "Reverse values order"; "Base" = "Base"; +"Display mode" = "View mode"; +"One row" = "One row"; +"Two rows" = "Two rows"; // Module Kit "Open module settings" = "Open module settings"; @@ -106,6 +110,8 @@ // Sensors "Temperature unit" = "Temperature unit"; +"Celsius" = "Celsius"; +"Fahrenheit" = "Fahrenheit"; // Network "Uploading" = "Upload"; diff --git a/Stats/Supporting Files/es.lproj/Localizable.strings b/Stats/Supporting Files/es.lproj/Localizable.strings index 188ab11d..52307fdb 100644 --- a/Stats/Supporting Files/es.lproj/Localizable.strings +++ b/Stats/Supporting Files/es.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "No disponible"; "Yes" = "Si"; "No" = "No"; +"Automatic" = "Automático"; // Alerts "New version available" = "Nueva versión disponible"; @@ -61,6 +62,9 @@ "Additional information" = "Información adicional"; "Reverse values order" = "Invertir el order de los valores"; "Base" = "Base"; +"Display mode" = "Modo de visualización"; +"One row" = "Una fila"; +"Two rows" = "Dos filas"; // Module Kit "Open module settings" = "Abrir la configruación del módulo"; @@ -103,6 +107,8 @@ // Sensors "Temperature unit" = "Unidad de temperatura"; +"Celsius" = "Celsius"; +"Fahrenheit" = "Fahrenheit"; // Network "Uploading" = "Subiendo"; diff --git a/Stats/Supporting Files/ko.lproj/Localizable.strings b/Stats/Supporting Files/ko.lproj/Localizable.strings index b93c03fd..0bcc8124 100644 --- a/Stats/Supporting Files/ko.lproj/Localizable.strings +++ b/Stats/Supporting Files/ko.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "사용할 수 없음"; "Yes" = "예"; "No" = "아니요"; +"Automatic" = "자동적 인"; // Alerts "New version available" = "새로운 버전을 이용할 수 있습니다"; @@ -61,6 +62,9 @@ "Additional information" = "추가 정보"; "Reverse values order" = "값 순서 변경"; "Base" = "Base"; +"Display mode" = "디스플레이 모드"; +"One row" = "한 줄"; +"Two rows" = "두 줄"; // Module Kit "Open module settings" = "모듈 설정 열기"; @@ -106,6 +110,8 @@ // Sensors "Temperature unit" = "온도 단위"; +"Celsius" = "섭씨"; +"Fahrenheit" = "화씨"; // Network "Uploading" = "업로딩"; diff --git a/Stats/Supporting Files/pl.lproj/Localizable.strings b/Stats/Supporting Files/pl.lproj/Localizable.strings index 58c6f1d3..fb94cf0f 100644 --- a/Stats/Supporting Files/pl.lproj/Localizable.strings +++ b/Stats/Supporting Files/pl.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "Niedostępny"; "Yes" = "Tak"; "No" = "Nie"; +"Automatic" = "Automatyczny"; // Alerts "New version available" = "Nowa wersja dostępna"; @@ -61,6 +62,9 @@ "Additional information" = "Informacja dodatkowa"; "Reverse values order" = "Zmień kolejność wyświetlania"; "Base" = "Podstawa"; +"Display mode" = "Tryb wyświetlania"; +"One row" = "Jeden wiersz"; +"Two rows" = "Dwa wierszy"; // Module Kit "Open module settings" = "Otwórz ustawienie modulu"; @@ -106,6 +110,8 @@ // Sensors "Temperature unit" = "Jednostka temperatury"; +"Celsius" = "Celsjusz"; +"Fahrenheit" = "Fahrenheit"; // Network "Uploading" = "Wysyłka"; diff --git a/Stats/Supporting Files/ru.lproj/Localizable.strings b/Stats/Supporting Files/ru.lproj/Localizable.strings index 61663838..51e90d8d 100644 --- a/Stats/Supporting Files/ru.lproj/Localizable.strings +++ b/Stats/Supporting Files/ru.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "Недоступно"; "Yes" = "Да"; "No" = "Нет"; +"Automatic" = "Автоматический"; // Alerts "New version available" = "Доступна новая версия"; @@ -61,6 +62,9 @@ "Additional information" = "Дополнительная информация"; "Reverse values order" = "Изменить порядок сортировки"; "Base" = "Основа"; +"Display mode" = "Режим отображения"; +"One row" = "Один ряд"; +"Two rows" = "Два ряда"; // Module Kit "Open module settings" = "Открыть настройки модуля"; @@ -106,6 +110,8 @@ // Sensors "Temperature unit" = "Единица измерения температуры"; +"Celsius" = "Цельсия"; +"Fahrenheit" = "Фаренгейта"; // Network "Uploading" = "Висилання"; diff --git a/Stats/Supporting Files/tr.lproj/Localizable.strings b/Stats/Supporting Files/tr.lproj/Localizable.strings index bee4b7f8..314f369c 100644 --- a/Stats/Supporting Files/tr.lproj/Localizable.strings +++ b/Stats/Supporting Files/tr.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "Devre Dışı"; "Yes" = "Evet"; "No" = "Hayır"; +"Automatic" = "Otomatik"; // Alerts "New version available" = "Yeni versiyon indirilebilir"; @@ -61,6 +62,9 @@ "Additional information" = "Ek bilgi"; "Reverse values order" = "Değerler sırasını tersine çevir"; "Base" = "Temel"; +"Display mode" = "Ekran modu"; +"One row" = "Bir sıra"; +"Two rows" = "İki sıra"; // Module Kit "Open module settings" = "Modül ayarlarını aç"; @@ -106,6 +110,8 @@ // Sensors "Temperature unit" = "Sıcaklık birimi"; +"Celsius" = "Santigrat"; +"Fahrenheit" = "Fahrenheit"; // Network "Uploading" = "Yükleme"; diff --git a/Stats/Supporting Files/uk.lproj/Localizable.strings b/Stats/Supporting Files/uk.lproj/Localizable.strings index e2692929..65de4bc4 100644 --- a/Stats/Supporting Files/uk.lproj/Localizable.strings +++ b/Stats/Supporting Files/uk.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "Недоступно"; "Yes" = "Так"; "No" = "Ні"; +"Automatic" = "Автоматичний"; // Alerts "New version available" = "Доступна нова версія"; @@ -61,6 +62,9 @@ "Additional information" = "Додаткова інформація"; "Reverse values order" = "Змінити порядок сортування"; "Base" = "Основа"; +"Display mode" = "Режим відображення"; +"One row" = "Один ряд"; +"Two rows" = "Два ряди"; // Module Kit "Open module settings" = "Відкрити налаштування модуля"; @@ -106,6 +110,8 @@ // Sensors "Temperature unit" = "Одиниця виміру температури"; +"Celsius" = "Цельсія"; +"Fahrenheit" = "Фаренгейта"; // Network "Uploading" = "Вислання"; diff --git a/Stats/Supporting Files/zh-Hans.lproj/Localizable.strings b/Stats/Supporting Files/zh-Hans.lproj/Localizable.strings index 74468a51..72ed9f0e 100644 --- a/Stats/Supporting Files/zh-Hans.lproj/Localizable.strings +++ b/Stats/Supporting Files/zh-Hans.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "不可用"; "Yes" = "是"; "No" = "否"; +"Automatic" = "自动"; // Alerts "New version available" = "新版本可用"; @@ -61,6 +62,9 @@ "Additional information" = "附加信息"; "Reverse values order" = "数值反序"; "Base" = "基础"; +"Display mode" = "显示模式"; +"One row" = "一排"; +"Two rows" = "两排"; // Module Kit "Open module settings" = "打开模块设置"; @@ -105,7 +109,9 @@ "Disk to show" = "显示的磁盘"; // Sensors -"Temperature unit" = "Temperature unit"; +"Temperature unit" = "温度单位"; +"Celsius" = "摄氏温度"; +"Fahrenheit" = "华氏温度"; // Network "Uploading" = "上传"; diff --git a/Stats/Supporting Files/zh-Hant.lproj/Localizable.strings b/Stats/Supporting Files/zh-Hant.lproj/Localizable.strings index 2d93a86b..59aa9996 100644 --- a/Stats/Supporting Files/zh-Hant.lproj/Localizable.strings +++ b/Stats/Supporting Files/zh-Hant.lproj/Localizable.strings @@ -23,6 +23,7 @@ "Unavailable" = "不可用"; "Yes" = "是"; "No" = "否"; +"Automatic" = "自動"; // Alerts "New version available" = "新版本可用"; @@ -60,6 +61,9 @@ "Additional information" = "附加資訊"; "Reverse values order" = "上下顛倒數值"; "Base" = "基本"; +"Display mode" = "顯示模式"; +"One row" = "一排"; +"Two rows" = "兩排"; // Module Kit "Open module settings" = "打開 module 設定"; @@ -102,6 +106,8 @@ // Sensors "Temperature unit" = "溫度單位"; +"Celsius" = "攝氏溫度"; +"Fahrenheit" = "華氏溫度"; // Network "Uploading" = "上傳"; diff --git a/StatsKit/extensions.swift b/StatsKit/extensions.swift index 817e2027..b5fa6e0d 100644 --- a/StatsKit/extensions.swift +++ b/StatsKit/extensions.swift @@ -360,7 +360,7 @@ public extension NSView { if item.key.contains("separator") { menu.addItem(NSMenuItem.separator()) } else { - let interfaceMenu = NSMenuItem(title: item.value, action: nil, keyEquivalent: "") + let interfaceMenu = NSMenuItem(title: LocalizedString(item.value), action: nil, keyEquivalent: "") interfaceMenu.representedObject = item.key menu.addItem(interfaceMenu) if selected == item.key { diff --git a/StatsKit/helpers.swift b/StatsKit/helpers.swift index 11dacd31..dfb426d1 100644 --- a/StatsKit/helpers.swift +++ b/StatsKit/helpers.swift @@ -51,6 +51,13 @@ public let SpeedBase: [KeyValue_t] = [ KeyValue_t(key: "byte", value: "Byte", additional: DataSizeBase.byte) ] +public let SensorsWidgetMode: [KeyValue_t] = [ + KeyValue_t(key: "automatic", value: "Automatic"), + KeyValue_t(key: "separator", value: "separator"), + KeyValue_t(key: "oneRow", value: "One row"), + KeyValue_t(key: "twoRows", value: "Two rows"), +] + public let ReaderUpdateIntervals: [Int] = [1, 2, 3, 5, 10, 15, 30] public let NumbersOfProcesses: [Int] = [3, 5, 8, 10, 15]