refactor time duration formatting

This commit is contained in:
end-4
2026-03-08 18:09:34 +01:00
parent 3da23ce176
commit 83baff7894
3 changed files with 22 additions and 32 deletions
@@ -24,4 +24,19 @@ Singleton {
targetDate.setDate(firstDayDate.getDate() + i);
return targetDate;
}
function formatDuration(seconds) {
const d = Math.floor(seconds / 86400);
const h = Math.floor((seconds % 86400) / 3600);
const m = Math.floor((seconds % 3600) / 60);
let str = "";
if (d > 0)
str += `${d}d`;
if (h > 0)
str += `${str ? ", " : ""}${h}h`;
if (m > 0 || !str)
str += `${str ? ", " : ""}${m}m`;
return str;
}
}
@@ -1,4 +1,5 @@
import qs.modules.common
import qs.modules.common.functions
import qs.modules.common.widgets
import qs.services
import QtQuick
@@ -27,18 +28,10 @@ StyledPopup {
icon: "schedule"
label: Battery.isCharging ? Translation.tr("Time to full:") : Translation.tr("Time to empty:")
value: {
function formatTime(seconds) {
var h = Math.floor(seconds / 3600);
var m = Math.floor((seconds % 3600) / 60);
if (h > 0)
return `${h}h, ${m}m`;
else
return `${m}m`;
}
if (Battery.isCharging)
return formatTime(Battery.timeToFull);
return DateUtils.formatDuration(Battery.timeToFull);
else
return formatTime(Battery.timeToEmpty);
return DateUtils.formatDuration(Battery.timeToEmpty);
}
}
@@ -54,13 +47,7 @@ StyledPopup {
return Translation.tr("Discharging:");
}
}
value: {
if (Battery.chargeState == 4) {
return "";
} else {
return `${Battery.energyRate.toFixed(2)}W`;
}
}
value: `${Battery.energyRate.toFixed(2)}W`
}
StyledPopupValueRow {