feat(battery): show estimated end time (#3117)

This commit is contained in:
Huỳnh Thiện Lộc
2026-08-19 20:44:58 -04:00
committed by GitHub
parent afe0dbb104
commit fde8bb6487
2 changed files with 28 additions and 10 deletions
@@ -175,7 +175,9 @@ DankPopout {
return I18n.tr("Power profile management available");
const time = BatteryService.formatTimeRemaining();
if (time !== "Unknown") {
return BatteryService.isCharging ? I18n.tr("Time until full: %1").arg(time) : I18n.tr("Time remaining: %1").arg(time);
const estimated = BatteryService.formatEstimatedTime();
const timeText = estimated ? `${time} (${estimated})` : time;
return BatteryService.isCharging ? I18n.tr("Time until full: %1").arg(timeText) : I18n.tr("Time remaining: %1").arg(timeText);
}
return "";
}
+25 -9
View File
@@ -405,23 +405,39 @@ Singleton {
return btDevices;
}
// Format time remaining for charge/discharge
function formatTimeRemaining() {
if (!batteryAvailable) {
return "Unknown";
}
function estimatedSeconds() {
if (!batteryAvailable)
return 0;
const rate = _smoothedChangeRate > 0 ? _smoothedChangeRate : changeRate;
const totalTime = (isCharging) ? ((batteryCapacity - batteryEnergy) / rate) : (batteryEnergy / rate);
const avgTime = Math.abs(totalTime * 3600);
if (!avgTime || avgTime <= 0 || avgTime > 86400)
const seconds = Math.abs(totalTime * 3600);
if (!seconds || seconds <= 0 || seconds > 86400)
return 0;
return seconds;
}
// Format time remaining for charge/discharge
function formatTimeRemaining() {
const seconds = estimatedSeconds();
if (!seconds)
return "Unknown";
const hours = Math.floor(avgTime / 3600);
const minutes = Math.floor((avgTime % 3600) / 60);
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
return hours > 0 ? `${hours}h ${minutes}m` : `${minutes}m`;
}
function formatEstimatedTime() {
const seconds = estimatedSeconds();
if (!seconds)
return "";
const target = new Date(Date.now() + seconds * 1000);
const use24Hour = SettingsData.use24HourClock !== false;
return target.toLocaleTimeString(Qt.locale(), use24Hour ? "HH:mm" : "h:mm AP");
}
function getBatteryIcon() {
if (!batteryAvailable) {
return "power";