Skip to content

Commit

Permalink
Added estimated remaining time on battery
Browse files Browse the repository at this point in the history
Estimated time remaining is shown when hovering over tray icon or when the discharge/charge label is hovered over.
  • Loading branch information
The-Faulty committed Nov 25, 2024
1 parent d7202b1 commit 24965d2
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions app/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public partial class SettingsForm : RForm

bool isGpuSection = true;

bool rateMouseOver = false;
bool batteryMouseOver = false;
bool batteryFullMouseOver = false;

Expand Down Expand Up @@ -236,6 +237,8 @@ public SettingsForm()
labelCharge.MouseEnter += PanelBattery_MouseEnter;
labelCharge.MouseLeave += PanelBattery_MouseLeave;
labelBattery.Click += LabelBattery_Click;
labelBattery.MouseEnter += LabelBattery_MouseEnter;
labelBattery.MouseLeave += LabelBattery_MouseLeave;

buttonPeripheral1.Click += ButtonPeripheral_Click;
buttonPeripheral2.Click += ButtonPeripheral_Click;
Expand Down Expand Up @@ -292,6 +295,18 @@ private void LabelBattery_Click(object? sender, EventArgs e)
RefreshSensors(true);
}

private void LabelBattery_MouseEnter(object? sender, EventArgs e)
{
rateMouseOver = true;
RefreshSensors(true);
}

private void LabelBattery_MouseLeave(object? sender, EventArgs e)
{
rateMouseOver = false;
RefreshSensors(true);
}

private void ButtonDonate_Click(object? sender, EventArgs e)
{
AppConfig.Set("donate_click", AppConfig.Get("start_count"));
Expand Down Expand Up @@ -1412,6 +1427,22 @@ private void ButtonStopGPU_Click(object? sender, EventArgs e)
gpuControl.KillGPUApps();
}

private string BatteryTime()
{
decimal limit = 1;
if (!BatteryControl.chargeFull) limit = (decimal)AppConfig.Get("charge_limit") / 100;
decimal? remaining = HardwareControl.batteryRate < 0 ? HardwareControl.chargeCapacity : (HardwareControl.fullCapacity * limit) - HardwareControl.chargeCapacity;
try
{
double hoursRemaining = Math.Abs((double)((remaining / 1000) / HardwareControl.batteryRate));
return String.Format("Time Remaining: {0}h {1}m", Math.Floor(hoursRemaining), Math.Round((hoursRemaining % 1) * 60, 0));
}
catch
{
return "Time Remaining: Unknown";
}
}

public async void RefreshSensors(bool force = false)
{

Expand All @@ -1426,19 +1457,26 @@ public async void RefreshSensors(bool force = false)
HardwareControl.ReadSensors();
Task.Run((Action)PeripheralsProvider.RefreshBatteryForAllDevices);

string battTime = BatteryTime();

if (HardwareControl.cpuTemp > 0)
cpuTemp = ": " + Math.Round((decimal)HardwareControl.cpuTemp).ToString() + "°C";

if (HardwareControl.batteryCapacity > 0)
{
charge = Properties.Strings.BatteryCharge + ": " + HardwareControl.batteryCharge;
}

if (HardwareControl.batteryRate < 0)
battery = Properties.Strings.Discharging + ": " + Math.Round(-(decimal)HardwareControl.batteryRate, 1).ToString() + "W";
else if (HardwareControl.batteryRate > 0)
battery = Properties.Strings.Charging + ": " + Math.Round((decimal)HardwareControl.batteryRate, 1).ToString() + "W";

if (rateMouseOver)
{
battery = battTime;
}
else
{
if (HardwareControl.batteryRate < 0)
battery = Properties.Strings.Discharging + ": " + Math.Round(-(decimal)HardwareControl.batteryRate, 1).ToString() + "W";
else if (HardwareControl.batteryRate > 0)
battery = Properties.Strings.Charging + ": " + Math.Round((decimal)HardwareControl.batteryRate, 1).ToString() + "W";
}

if (HardwareControl.gpuTemp > 0)
{
Expand All @@ -1447,7 +1485,8 @@ public async void RefreshSensors(bool force = false)

string trayTip = "CPU" + cpuTemp + " " + HardwareControl.cpuFan;
if (gpuTemp.Length > 0) trayTip += "\nGPU" + gpuTemp + " " + HardwareControl.gpuFan;
if (battery.Length > 0) trayTip += "\n" + battery;
if (battery.Length > 0) trayTip += "\n" + battery + "\n" + battTime;


if (Program.settingsForm.IsHandleCreated)
Program.settingsForm.BeginInvoke(delegate
Expand Down

0 comments on commit 24965d2

Please sign in to comment.