forked from Shinonome/dots-hyprland
refactor time duration formatting
This commit is contained in:
@@ -24,4 +24,19 @@ Singleton {
|
|||||||
targetDate.setDate(firstDayDate.getDate() + i);
|
targetDate.setDate(firstDayDate.getDate() + i);
|
||||||
return targetDate;
|
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
|
||||||
|
import qs.modules.common.functions
|
||||||
import qs.modules.common.widgets
|
import qs.modules.common.widgets
|
||||||
import qs.services
|
import qs.services
|
||||||
import QtQuick
|
import QtQuick
|
||||||
@@ -27,18 +28,10 @@ StyledPopup {
|
|||||||
icon: "schedule"
|
icon: "schedule"
|
||||||
label: Battery.isCharging ? Translation.tr("Time to full:") : Translation.tr("Time to empty:")
|
label: Battery.isCharging ? Translation.tr("Time to full:") : Translation.tr("Time to empty:")
|
||||||
value: {
|
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)
|
if (Battery.isCharging)
|
||||||
return formatTime(Battery.timeToFull);
|
return DateUtils.formatDuration(Battery.timeToFull);
|
||||||
else
|
else
|
||||||
return formatTime(Battery.timeToEmpty);
|
return DateUtils.formatDuration(Battery.timeToEmpty);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,13 +47,7 @@ StyledPopup {
|
|||||||
return Translation.tr("Discharging:");
|
return Translation.tr("Discharging:");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
value: {
|
value: `${Battery.energyRate.toFixed(2)}W`
|
||||||
if (Battery.chargeState == 4) {
|
|
||||||
return "";
|
|
||||||
} else {
|
|
||||||
return `${Battery.energyRate.toFixed(2)}W`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
StyledPopupValueRow {
|
StyledPopupValueRow {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ pragma Singleton
|
|||||||
pragma ComponentBehavior: Bound
|
pragma ComponentBehavior: Bound
|
||||||
import qs
|
import qs
|
||||||
import qs.modules.common
|
import qs.modules.common
|
||||||
|
import qs.modules.common.functions
|
||||||
import QtQuick
|
import QtQuick
|
||||||
import Quickshell
|
import Quickshell
|
||||||
import Quickshell.Io
|
import Quickshell.Io
|
||||||
@@ -10,6 +11,7 @@ import Quickshell.Io
|
|||||||
* A nice wrapper for date and time strings.
|
* A nice wrapper for date and time strings.
|
||||||
*/
|
*/
|
||||||
Singleton {
|
Singleton {
|
||||||
|
id: root
|
||||||
property var clock: SystemClock {
|
property var clock: SystemClock {
|
||||||
id: clock
|
id: clock
|
||||||
precision: {
|
precision: {
|
||||||
@@ -33,21 +35,7 @@ Singleton {
|
|||||||
fileUptime.reload();
|
fileUptime.reload();
|
||||||
const textUptime = fileUptime.text();
|
const textUptime = fileUptime.text();
|
||||||
const uptimeSeconds = Number(textUptime.split(" ")[0] ?? 0);
|
const uptimeSeconds = Number(textUptime.split(" ")[0] ?? 0);
|
||||||
|
root.uptime = DateUtils.formatDuration(uptimeSeconds)
|
||||||
// Convert seconds to days, hours, and minutes
|
|
||||||
const days = Math.floor(uptimeSeconds / 86400);
|
|
||||||
const hours = Math.floor((uptimeSeconds % 86400) / 3600);
|
|
||||||
const minutes = Math.floor((uptimeSeconds % 3600) / 60);
|
|
||||||
|
|
||||||
// Build the formatted uptime string
|
|
||||||
let formatted = "";
|
|
||||||
if (days > 0)
|
|
||||||
formatted += `${days}d`;
|
|
||||||
if (hours > 0)
|
|
||||||
formatted += `${formatted ? ", " : ""}${hours}h`;
|
|
||||||
if (minutes > 0 || !formatted)
|
|
||||||
formatted += `${formatted ? ", " : ""}${minutes}m`;
|
|
||||||
uptime = formatted;
|
|
||||||
interval = Config.options?.resources?.updateInterval ?? 3000;
|
interval = Config.options?.resources?.updateInterval ?? 3000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user