Skip to content

Commit

Permalink
feat: show warning and critical color for CPU temp. & Physical Memory
Browse files Browse the repository at this point in the history
Now TwoSensorGraph has `thresholds` attr, which determines the color shown in GraphText.
Besides, some configs is added, including:
temperatrueColor, warningColor, criticalColor in Apperance group;
CPU & Physical Memory Usage thresholds in Data group.
  • Loading branch information
Starrah committed Sep 26, 2022
1 parent 8f81526 commit ef29040
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 11 deletions.
36 changes: 36 additions & 0 deletions package/contents/config/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@
<entry name="networkSendingTotal" type="Double">
<default>100000.0</default>
</entry>
<entry name="thresholdWarningCpuTemp" type="Int">
<default>85</default>
</entry>
<entry name="thresholdCriticalCpuTemp" type="Int">
<default>105</default>
</entry>
<entry name="thresholdWarningMemory" type="Int">
<default>70</default>
</entry>
<entry name="thresholdCriticalMemory" type="Int">
<default>90</default>
</entry>
</group>

<group name="Appearance">
Expand Down Expand Up @@ -89,6 +101,12 @@
<default>top-right</default>
</entry>

<entry name="customTemperatureColor" type="Bool">
<default>false</default>
</entry>
<entry name="temperatureColor" type="Color">
</entry>

<entry name="customCpuColor" type="Bool">
<default>false</default>
</entry>
Expand All @@ -105,6 +123,11 @@
</entry>
<entry name="swapColor" type="Color">
</entry>
<entry name="customSwapTextColor" type="Bool">
<default>false</default>
</entry>
<entry name="swapTextColor" type="Color">
</entry>

<entry name="customNetDownColor" type="Bool">
<default>false</default>
Expand All @@ -116,6 +139,19 @@
</entry>
<entry name="netUpColor" type="Color">
</entry>

<entry name="customWarningColor" type="Bool">
<default>false</default>
</entry>
<entry name="warningColor" type="Color">
<default>#f6cd00</default>
</entry>
<entry name="customCriticalColor" type="Bool">
<default>false</default>
</entry>
<entry name="criticalColor" type="Color">
<default>#da4453</default>
</entry>
</group>

</kcfg>
22 changes: 18 additions & 4 deletions package/contents/ui/components/TwoSensorGraph.qml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Item {
readonly property alias sensorsModel: sensorsModel
property var sensors: []
property var uplimits: [100, 100]
property var thresholds: [undefined, undefined]
property var textColors: [theme.textColor, theme.textColor]

// Aliases
readonly property alias textContainer: textContainer
Expand All @@ -36,7 +38,10 @@ Item {
// Graph properties
readonly property int historyAmount: plasmoid.configuration.historyAmount
readonly property int interval: plasmoid.configuration.updateInterval * 1000
readonly property color warningColor: plasmoid.configuration.customWarningColor ? plasmoid.configuration.warningColor : "#f6cd00"
readonly property color criticalColor: plasmoid.configuration.customCriticalColor ? plasmoid.configuration.criticalColor : "#da4453"
property var colors: [theme.highlightColor, theme.textColor]
property var thresholdColors: [warningColor, criticalColor]

// Text properties
property bool secondLabelWhenZero: true
Expand Down Expand Up @@ -148,20 +153,29 @@ Item {

// Update label
if (index === 0) { // is first line
firstLineLabel.visible = true
firstLineLabel.color = textColors[0]
if (typeof value === 'undefined') {
firstLineLabel.text = '...'
firstLineLabel.visible = true
} else {
firstLineLabel.text = data.formattedValue
firstLineLabel.visible = true
if (thresholds[0]) {
if (data.value >= thresholds[0][1]) firstLineLabel.color = thresholdColors[1]
else if (data.value >= thresholds[0][0]) firstLineLabel.color = thresholdColors[0]
}
}
} else if (index === 1) { // is second line
secondLineLabel.visible = secondLabelWhenZero
secondLineLabel.color = textColors[1]
if (typeof value === 'undefined') {
secondLineLabel.text = '...'
secondLineLabel.visible = secondLabelWhenZero
} else {
secondLineLabel.text = data.formattedValue
secondLineLabel.visible = data.value !== 0 || secondLabelWhenZero
secondLineLabel.visible = secondLineLabel.visible || data.value !== 0
if (thresholds[1]) {
if (data.value >= thresholds[1][1]) secondLineLabel.color = thresholdColors[1]
else if (data.value >= thresholds[1][0]) secondLineLabel.color = thresholdColors[0]
}
}
}
}
Expand Down
38 changes: 38 additions & 0 deletions package/contents/ui/config/ConfigAppearance.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ QtLayouts.ColumnLayout {
property string cfg_placement: ""
property string cfg_displayment: ""

property alias cfg_customTemperatureColor: temperatureColor.checked
property alias cfg_temperatureColor: temperatureColor.value
property alias cfg_customCpuColor: cpuColor.checked
property alias cfg_cpuColor: cpuColor.value
property alias cfg_customRamColor: ramColor.checked
property alias cfg_ramColor: ramColor.value
property alias cfg_customSwapColor: swapColor.checked
property alias cfg_swapColor: swapColor.value
property alias cfg_customSwapTextColor: swapTextColor.checked
property alias cfg_swapTextColor: swapTextColor.value
property alias cfg_customNetDownColor: netDownColor.checked
property alias cfg_netDownColor: netDownColor.value
property alias cfg_customNetUpColor: netUpColor.checked
property alias cfg_netUpColor: netUpColor.value
property alias cfg_customWarningColor: warningColor.checked
property alias cfg_warningColor: warningColor.value
property alias cfg_customCriticalColor: criticalColor.checked
property alias cfg_criticalColor: criticalColor.value

property color primaryColor: theme.highlightColor
property color negativeColor: theme.negativeTextColor
Expand Down Expand Up @@ -194,6 +202,14 @@ QtLayouts.ColumnLayout {
Kirigami.FormLayout {
id: colorsPage

RMControls.ColorSelector {
id: temperatureColor
Kirigami.FormData.label: i18n("Temperature text color:")

dialogTitle: i18n("Choose text color for temperature text (when in normal status)")
defaultColor: primaryColor
}

RMControls.ColorSelector {
id: cpuColor
Kirigami.FormData.label: i18n("CPU color:")
Expand All @@ -216,6 +232,13 @@ QtLayouts.ColumnLayout {
dialogTitle: i18n("Choose Swap graph color")
defaultColor: negativeColor
}
RMControls.ColorSelector {
id: swapTextColor
Kirigami.FormData.label: i18n("Swap Text color:")

dialogTitle: i18n("Choose Swap text color")
defaultColor: primaryColor
}

RMControls.ColorSelector {
id: netDownColor
Expand All @@ -231,6 +254,21 @@ QtLayouts.ColumnLayout {
dialogTitle: i18n("Choose network sending graph color")
defaultColor: negativeColor
}

RMControls.ColorSelector {
id: warningColor
Kirigami.FormData.label: i18n("Warning text color:")

dialogTitle: i18n("Choose text color when the value is in warning status")
defaultColor: "#f6cd00"
}
RMControls.ColorSelector {
id: criticalColor
Kirigami.FormData.label: i18n("Critical text color:")

dialogTitle: i18n("Choose text color when the value is in critical status")
defaultColor: "#da4453"
}
}
}
}
85 changes: 85 additions & 0 deletions package/contents/ui/config/ConfigData.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ QtLayouts.ColumnLayout {
readonly property var networkDialect: Functions.getNetworkDialectInfo(plasmoid.configuration.networkUnit)
property double cfg_networkReceivingTotal: 0.0
property double cfg_networkSendingTotal: 0.0
property alias cfg_thresholdWarningCpuTemp: thresholdWarningCpuTemp.value
property alias cfg_thresholdCriticalCpuTemp: thresholdCriticalCpuTemp.value
property alias cfg_thresholdWarningMemory: thresholdWarningMemory.value
property alias cfg_thresholdCriticalMemory: thresholdCriticalMemory.value

readonly property var networkSpeedOptions: [
{
Expand Down Expand Up @@ -63,6 +67,11 @@ QtLayouts.ColumnLayout {
iconSource: "preferences-system-network"
text: i18n("Network")
}
PlasmaComponents.TabButton {
tab: thresholdPage
iconSource: "dialog-warning"
text: i18n("Thresholds")
}
}

// Views
Expand Down Expand Up @@ -237,5 +246,81 @@ QtLayouts.ColumnLayout {
}
}
}

Kirigami.FormLayout {
id: thresholdPage
wideMode: true


QtLayouts.GridLayout {
QtLayouts.Layout.fillWidth: true
columns: 2
rowSpacing: Kirigami.Units.smallSpacing
columnSpacing: Kirigami.Units.largeSpacing

PlasmaComponents.Label {
text: i18n("Warning ")
font.pointSize: PlasmaCore.Theme.defaultFont.pointSize * 1.2
}
PlasmaComponents.Label {
text: i18n(" Critical")
font.pointSize: PlasmaCore.Theme.defaultFont.pointSize * 1.2
}
}

QtLayouts.GridLayout {
Kirigami.FormData.label: i18n("CPU Temperature:")
QtLayouts.Layout.fillWidth: true
columns: 2
rowSpacing: Kirigami.Units.smallSpacing
columnSpacing: Kirigami.Units.largeSpacing

RMControls.SpinBox {
id: thresholdWarningCpuTemp
Kirigami.FormData.label: i18n("Warning")
QtLayouts.Layout.fillWidth: true

textFromValue: function(value, locale) {
return value + " °C"
}
}
RMControls.SpinBox {
id: thresholdCriticalCpuTemp
Kirigami.FormData.label: i18n("Critical")
QtLayouts.Layout.fillWidth: true

textFromValue: function(value, locale) {
return value + " °C"
}
}
}

QtLayouts.GridLayout {
Kirigami.FormData.label: i18n("Physical Memory Usage:")
QtLayouts.Layout.fillWidth: true
columns: 2
rowSpacing: Kirigami.Units.smallSpacing
columnSpacing: Kirigami.Units.largeSpacing

RMControls.SpinBox {
id: thresholdWarningMemory
Kirigami.FormData.label: i18n("Warning")
QtLayouts.Layout.fillWidth: true

textFromValue: function(value, locale) {
return value + " %"
}
}
RMControls.SpinBox {
id: thresholdCriticalMemory
Kirigami.FormData.label: i18n("Critical")
QtLayouts.Layout.fillWidth: true

textFromValue: function(value, locale) {
return value + " %"
}
}
}
}
}
}
38 changes: 31 additions & 7 deletions package/contents/ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,17 @@ Item {
property color cpuColor: plasmoid.configuration.customCpuColor ? plasmoid.configuration.cpuColor : primaryColor
property color ramColor: plasmoid.configuration.customRamColor ? plasmoid.configuration.ramColor : primaryColor
property color swapColor: plasmoid.configuration.customSwapColor ? plasmoid.configuration.swapColor : negativeColor
property color swapTextColor: plasmoid.configuration.customSwapTextColor ? plasmoid.configuration.swapTextColor : primaryColor
property color netDownColor: plasmoid.configuration.customNetDownColor ? plasmoid.configuration.netDownColor : primaryColor
property color netUpColor: plasmoid.configuration.customNetUpColor ? plasmoid.configuration.netUpColor : negativeColor
readonly property color temperatureColor: plasmoid.configuration.customTemperatureColor ? plasmoid.configuration.temperatureColor : primaryColor
readonly property color warningColor: plasmoid.configuration.customWarningColor ? plasmoid.configuration.warningColor : "#f6cd00"
readonly property color criticalColor: plasmoid.configuration.customCriticalColor ? plasmoid.configuration.criticalColor : "#da4453"

property int thresholdWarningCpuTemp: plasmoid.configuration.thresholdWarningCpuTemp
property int thresholdCriticalCpuTemp: plasmoid.configuration.thresholdCriticalCpuTemp
property int thresholdWarningMemory: plasmoid.configuration.thresholdWarningMemory
property int thresholdCriticalMemory: plasmoid.configuration.thresholdCriticalMemory

// Component properties
property int containerCount: (showCpuMonitor?1:0) + (showRamMonitor?1:0) + (showNetMonitor?1:0)
Expand Down Expand Up @@ -114,6 +123,13 @@ Item {
}
}

onThresholdWarningMemoryChanged: {
maxMemoryQueryModel.enabled = true
}
onThresholdCriticalMemoryChanged: {
maxMemoryQueryModel.enabled = true
}

function keepInteger(str) {
var r = str.match(/^([.\d]*)(.*)/)
if (!(r && r[1])) return ""
Expand Down Expand Up @@ -141,10 +157,17 @@ Item {
to: 100
}

function getCpuTempColor(value) {
if (value >= thresholdCriticalCpuTemp) return criticalColor
else if (value >= thresholdWarningCpuTemp) return warningColor
else return temperatureColor
}

// Display first core frequency
onDataTick: {
if (canSeeValue(0) && showCpuTemperature) {
firstLineLeftLabel.text = keepInteger(cpuTempSensor.formattedValue)
firstLineLeftLabel.color = getCpuTempColor(cpuTempSensor.value)
}
if (canSeeValue(1)) {
secondLineLabel.text = cpuFrequencySensor.formattedValue
Expand All @@ -162,7 +185,10 @@ Item {
sensorId: "cpu/cpu0/temperature"
}
onShowValueWhenMouseMove: {
if (showCpuTemperature) firstLineLeftLabel.text = keepInteger(cpuTempSensor.formattedValue)
if (showCpuTemperature) {
firstLineLeftLabel.text = keepInteger(cpuTempSensor.formattedValue)
firstLineLeftLabel.color = getCpuTempColor(cpuTempSensor.value)
}
secondLineLabel.text = cpuFrequencySensor.formattedValue
secondLineLabel.visible = true
}
Expand All @@ -180,6 +206,7 @@ Item {
id: ramGraph
colors: [ramColor, swapColor]
secondLabelWhenZero: false
textColors: [theme.textColor, swapTextColor]

visible: showRamMonitor
width: itemWidth
Expand All @@ -197,7 +224,7 @@ Item {
// Get max y of graph
property var maxMemory: [-1, -1]
Sensors.SensorDataModel {
id: totalSensorsModel
id: maxMemoryQueryModel
sensors: ["memory/physical/total", "memory/swap/total"]
enabled: true

Expand All @@ -208,19 +235,16 @@ Item {
enabled = false
if (!showMemoryInPercent) {
ramGraph.uplimits = ramGraph.maxMemory
ramGraph.thresholds[0] = [ramGraph.maxMemory[0] * (thresholdWarningMemory / 100.0), ramGraph.maxMemory[0] * (thresholdCriticalMemory / 100.0)]
} else {
ramGraph.uplimits = [100, 100]
ramGraph.thresholds[0] = [thresholdWarningMemory, thresholdCriticalMemory]
}
ramGraph.updateSensors()
}
}
}

// Set the color of Swap
onShowValueWhenMouseMove: {
secondLineLabel.color = swapColor
}

function updateSensors() {
var suffix = showMemoryInPercent ? "Percent" : ""

Expand Down

0 comments on commit ef29040

Please sign in to comment.