Skip to content

Instantly share code, notes, and snippets.

@Zren
Created August 21, 2016 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Zren/4e5709d842965227088f6e1d3fd42016 to your computer and use it in GitHub Desktop.
Save Zren/4e5709d842965227088f6e1d3fd42016 to your computer and use it in GitHub Desktop.
import QtQuick 2.1
import QtQuick.Layouts 1.3
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponent
//import org.kde.kcoreaddons 1.0 as KCoreAddons
Item {
PlasmaCore.DataSource {
id: pmSource
engine: "powermanagement"
connectedSources: sources
onSourceAdded: {
disconnectSource(source)
connectSource(source)
}
onSourceRemoved: {
disconnectSource(source)
}
}
function getBatteryData(key, def) {
var value = pmSource.data.Battery[key]
if (typeof value === 'undefined') {
return def;
} else {
return value;
}
}
property bool hasBattery: getBatteryData('Has Battery', false)
property int remainingTime: getBatteryData('Remaining msec', 0)
PlasmaComponent.Label {
text: {
if (remainingTime > 0) {
// return KCoreAddons.Format.formatDuration(remainingTime, KCoreAddons.FormatTypes.HideSeconds);
return '' + Math.floor(remainingTime / (60 * 1000)) + 'm'
} else {
return '∞';
}
}
anchors.fill: parent
font.pixelSize: 1024
fontSizeMode: Text.Fit
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
color: {
if (remainingTime <= 15 * 60 * 1000) { // 15 minutes
return '#e33'
} else {
return theme.textColor
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment