SoraV2Utils/SoraV2Utils_Agent/TrayIcon.cs
krjan02 26cde137c9
Some checks failed
Build and Relase / build-release (push) Failing after 38s
Build and Relase / create-release (push) Failing after 10s
Initial commit (1.0.0)
2025-01-13 16:27:29 +01:00

94 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
namespace SoraV2Utils_Agent
{
public class TrayIcon
{
public TrayIcon()
{
// Create a NotifyIcon instance
using (NotifyIcon trayIcon = new NotifyIcon())
{
// Set up the tray icon properties
trayIcon.Icon = new Icon(SystemIcons.WinLogo, 40, 40); // Choose your own icon
trayIcon.Visible = true;
// Create a context menu
var contextMenu = new ContextMenu();
// Add a non-clickable dynamic text item
var batteryLevelItem = new MenuItem("Battery Level: Loading...");
var batteryRuntimeItem = new MenuItem("Battery Runtime: Loading...");
batteryLevelItem.Enabled = false;
batteryRuntimeItem.Enabled = false;
// Add an exit menu item
contextMenu.MenuItems.Add("Exit", (sender, e) => ServiceIPC.Instance.Exit());
contextMenu.MenuItems.Add(batteryLevelItem);
contextMenu.MenuItems.Add(batteryRuntimeItem);
trayIcon.ContextMenu = contextMenu;
// Update the dynamic text in the context menu
ThreadPool.QueueUserWorkItem(state =>
{
while (true)
{
// Format battery level with charging status
string batteryLevelText = ServiceIPC.DeviceStatus.Charging == 1
? $"Battery Level: {ServiceIPC.DeviceStatus.Battery} (Charging...)"
: $"Battery Level: {ServiceIPC.DeviceStatus.Battery}";
// Format battery runtime (convert minutes to hours and minutes)
string batteryRuntimeText = FormatBatteryRuntime(ServiceIPC.BatteryRuntime);
// Update the context menu items
batteryLevelItem.Text = batteryLevelText;
batteryRuntimeItem.Text = $"Battery Runtime: {batteryRuntimeText}";
// Update every second
Thread.Sleep(1000);
}
});
Application.Run();
}
}
// Formats the runtime in hours and minutes.
private string FormatBatteryRuntime(double runtimeInMinutes)
{
if(runtimeInMinutes < 0)
{
return "No data yet";
}
int hours = (int)(runtimeInMinutes / 60);
int minutes = (int)(runtimeInMinutes % 60);
// If runtime is greater than 1 hour, display hours and minutes
if (hours > 0)
{
return $"{hours} hour{(hours > 1 ? "s" : "")} {minutes} minute{(minutes > 1 ? "s" : "")}";
}
else
{
return $"{minutes} minute{(minutes > 1 ? "s" : "")}";
}
}
public void Display()
{
}
}
}