- add an option to select the sensors widget layout (automatic/one row/two rows)

- improved translations
This commit is contained in:
Serhiy Mytrovtsiy
2020-10-31 00:04:01 +01:00
parent 59d0f9b935
commit ca98ce4ebb
13 changed files with 195 additions and 71 deletions

View File

@@ -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<Store>?
@@ -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..<num {
if !self.values.indices.contains(i*2) {
continue
}
var width: CGFloat = 0
var paddingLeft: CGFloat = 0
if self.values.indices.contains((i*2)+1) {
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
var i = 0
while i < self.values.count {
switch self.modeState {
case "automatic", "twoRows":
let firstSensor: SensorValue_t? = self.values[i]
let secondSensor: SensorValue_t? = self.values.indices.contains(i+1) ? self.values[i+1] : nil
let firstRowWidth = self.values[i*2].value.widthOfString(usingFont: font)
let secondRowWidth = self.values[(i*2)+1].value.widthOfString(usingFont: font)
width = max(20, max(firstRowWidth, secondRowWidth)).rounded(.up)
if self.iconState && (self.values[i*2].icon != nil || self.values[(i*2)+1].icon != nil) {
let iconSize: CGFloat = 8
if let icon = self.values[i*2].icon {
icon.draw(in: NSRect(x: x, y: rowHeight+((rowHeight-iconSize)/2), width: iconSize, height: iconSize))
}
if let icon = self.values[(i*2)+1].icon {
icon.draw(in: NSRect(x: x, y: (rowHeight-iconSize)/2, width: iconSize, height: iconSize))
}
paddingLeft = iconSize + (Constants.Widget.margin*3)
width += paddingLeft
var width: CGFloat = 0
if self.modeState == "automatic" && secondSensor == nil {
width += self.drawOneRow(firstSensor!, x: x)
} else {
width += self.drawTwoRows(topSensor: firstSensor, bottomSensor: secondSensor, x: x)
}
let attributes = [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: NSColor.textColor,
NSAttributedString.Key.paragraphStyle: style
]
x += width
totalWidth += width
var rect = CGRect(x: x+paddingLeft, y: rowHeight+1, width: width-paddingLeft, height: rowHeight)
var str = NSAttributedString.init(string: self.values[i*2].value, attributes: attributes)
str.draw(with: rect)
rect = CGRect(x: x+paddingLeft, y: 1, width: width-paddingLeft, height: rowHeight)
str = NSAttributedString.init(string: self.values[(i*2)+1].value, attributes: attributes)
str.draw(with: rect)
} else {
let font: NSFont = NSFont.systemFont(ofSize: 13, weight: .light)
width = self.values[i*2].value.widthOfString(usingFont: font).rounded(.up)
if let icon = self.values[i*2].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
if num != 1 && (i/2) != num {
x += Constants.Widget.margin
totalWidth += Constants.Widget.margin
}
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: self.values[i*2].value, attributes: [
NSAttributedString.Key.font: font,
NSAttributedString.Key.foregroundColor: NSColor.textColor,
NSAttributedString.Key.paragraphStyle: style
])
str.draw(with: rect)
i += 1
case "oneRow":
let width = self.drawOneRow(self.values[i], x: x)
x += width
totalWidth += width
// add margins between columns
if self.values.count != 1 && i != self.values.count {
x += Constants.Widget.margin
totalWidth += Constants.Widget.margin
}
default: break
}
x += width
totalWidth += width
// add margins between columns
if num != 1 && (i/2) != num {
x += Constants.Widget.margin
totalWidth += Constants.Widget.margin
}
i += 1
}
totalWidth += Constants.Widget.margin // closing space
@@ -154,6 +118,83 @@ public class SensorsWidget: Widget {
self.setWidth(totalWidth)
}
private func drawOneRow(_ sensor: SensorValue_t, x: CGFloat) -> 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)
}
}

View File

@@ -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";

View File

@@ -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";

View File

@@ -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";

View File

@@ -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" = "업로딩";

View File

@@ -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";

View File

@@ -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" = "Висилання";

View File

@@ -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";

View File

@@ -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" = "Вислання";

View File

@@ -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" = "上传";

View File

@@ -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" = "上傳";

View File

@@ -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 {

View File

@@ -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]