Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev battery info view #52

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0-windows10.0.22000.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework>
<RootNamespace>BatteryTracker.Tests.UnitTests</RootNamespace>
<Platforms>x86;x64;arm64</Platforms>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
3 changes: 2 additions & 1 deletion src/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<!-- Other merged dictionaries here -->
<ResourceDictionary Source="/Resources/Commands.xaml" />
<ResourceDictionary Source="/Styles/Button.xaml" />
<ResourceDictionary Source="/Styles/FontSizes.xaml" />
<ResourceDictionary Source="/Styles/Thickness.xaml" />
<ResourceDictionary Source="/Styles/FontIcon.xaml" />
<ResourceDictionary Source="/Styles/Button.xaml" />
<ResourceDictionary Source="/Styles/TextBlock.xaml" />
</ResourceDictionary.MergedDictionaries>

Expand Down
18 changes: 18 additions & 0 deletions src/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public App()
services.AddSingleton<ISettingsStorageService, AppLocalSettingsStorageService>();
services.AddSingleton<ISettingsService, SettingsService>();
services.AddTransient<INavigationViewService, NavigationViewService>();
services.AddTransient<IPowerService, PowerService>();

// Views and ViewModels
services.AddTransient<SettingsViewModel>();
Expand All @@ -87,6 +88,7 @@ public App()
services.AddTransient<ShellViewModel>();
services.AddTransient<AboutPage>();
services.AddTransient<AboutViewModel>();
services.AddTransient<BatteryInfoViewModel>();

// Taskbar icon
services.AddSingleton<BatteryIcon>();
Expand Down Expand Up @@ -193,10 +195,26 @@ private async Task InitializeTrayIconAsync()
var exitApplicationCommand = (XamlUICommand)Resources["ExitApplicationCommand"];
exitApplicationCommand.ExecuteRequested += ExitApplicationCommand_ExecuteRequested;

var displayBatteryInfoCommand = (XamlUICommand)Resources["DisplayBatteryInfoCommand"];
displayBatteryInfoCommand.ExecuteRequested += DisplayBatteryInfoCommand_ExecuteRequested;

_batteryIcon = GetService<BatteryIcon>();
await _batteryIcon.InitAsync(MainWindow.BatteryTrayIcon);
}

void DisplayBatteryInfoCommand_ExecuteRequested(XamlUICommand sender, ExecuteRequestedEventArgs args)
{
BatteryInfoWindow window = _batteryIcon!.BatteryInfoWindow;
if (window.Visible)
{
window.Hide();
}
else
{
window.Activate();
}
}

private void ProcessUnhandledException(Exception? e, bool showNotification)
{
_logger.LogCritical(e, "Unhandled exception");
Expand Down
33 changes: 24 additions & 9 deletions src/BatteryTracker.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows10.0.22000.0</TargetFramework>
<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework>
<TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
<RootNamespace>BatteryTracker</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
<Platforms>x86;x64;ARM64</Platforms>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<UseWinUI>true</UseWinUI>
<EnableMsixTooling>true</EnableMsixTooling>
Expand Down Expand Up @@ -98,26 +98,29 @@
<None Remove="Styles\FontSizes.xaml" />
<None Remove="Styles\TextBlock.xaml" />
<None Remove="Styles\Thickness.xaml" />
<None Remove="Styles\FontIcon.xaml" />
<None Remove="TrayIcon.xaml" />
<None Remove="Views\AboutPage.xaml" />
<None Remove="Views\SettingsPage.xaml" />
<None Remove="Views\ShellPage.xaml" />
<None Remove="Views\BatteryInfoWindow.xaml" />
<None Remove="Views\BatteryInfoPage.xaml" />
</ItemGroup>

<!-- Package references -->
<ItemGroup>
<PackageReference Include="CommunityToolkit.Labs.WinUI.SettingsControls" Version="0.0.17" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI" Version="7.1.2" />
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls" Version="7.1.2" />
<PackageReference Include="H.NotifyIcon" Version="2.0.108" />
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.0.108" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.3.230602002" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.756" />
<PackageReference Include="H.NotifyIcon" Version="2.0.123" />
<PackageReference Include="H.NotifyIcon.WinUI" Version="2.0.123" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231115000" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.2428" />
<PackageReference Include="Microsoft.Xaml.Behaviors.WinUI.Managed" Version="2.0.9" />
<PackageReference Include="Mii.Serilog.Extensions.Logging.File" Version="1.0.1" />
<PackageReference Include="WinUIEx" Version="2.2.0" />
<PackageReference Include="WinUIEx" Version="2.3.3" />
<Manifest Include="$(ApplicationManifest)" />
</ItemGroup>

Expand All @@ -144,7 +147,13 @@
<Page Update="MainWindow.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Views\BatteryInfoWindow.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Folder Include="Controls\SettingsGroup\" />
<Page Update="Views\BatteryInfoPage.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Update="Controls\SettingsGroup.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand All @@ -160,6 +169,9 @@
<Page Update="Views\ShellPage.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Views\BatteryInfoPage.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
</ItemGroup>

<!-- Styles -->
Expand All @@ -176,6 +188,9 @@
<Page Update="Styles\Thickness.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="Styles\FontIcon.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
</ItemGroup>

<!-- <ItemGroup> -->
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/Services/IPowerService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using BatteryTracker.Models;

namespace BatteryTracker.Contracts.Services;

public interface IPowerService
{
PowerMode GetPowerMode();

void SetPowerMode(PowerMode powerMode);
}
19 changes: 19 additions & 0 deletions src/Converters/PowerModeToIndexConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BatteryTracker.Models;
using Microsoft.UI.Xaml.Data;

namespace BatteryTracker.Converters;

public class PowerModeToIndexConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
=> (double)((PowerMode)value).Mode;

public object ConvertBack(object value, Type targetType, object parameter, string language)
=> (double)value switch
{
0 => PowerMode.BetterBattery,
1 => PowerMode.Balanced,
2 => PowerMode.BestPerformance,
_ => PowerMode.Balanced
};
}
21 changes: 21 additions & 0 deletions src/Helpers/PowerHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Attribute should be given to this StackOverflow post:
* https://stackoverflow.com/questions/61869347/control-windows-10s-power-mode-programmatically
*/

using System.Runtime.InteropServices;

namespace BatteryTracker.Helpers
{
internal static class PowerHelper
{
[DllImport("powrprof.dll", EntryPoint = "PowerSetActiveOverlayScheme")]
internal static extern uint PowerSetActiveOverlayScheme(Guid OverlaySchemeGuid);

[DllImport("powrprof.dll", EntryPoint = "PowerGetActualOverlayScheme")]
internal static extern uint PowerGetActualOverlayScheme(out Guid ActualOverlayGuid);

[DllImport("powrprof.dll", EntryPoint = "PowerGetEffectiveOverlayScheme")]
internal static extern uint PowerGetEffectiveOverlayScheme(out Guid EffectiveOverlayGuid);
}
}
47 changes: 47 additions & 0 deletions src/Helpers/Timer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Threading;

namespace BatteryTracker.Helpers;

class Timer
{
public Action? Action { get; set; }

System.Threading.Timer? _timer;

int _interval; // in milliseconds
bool _continue = true;

public async void StartTimer(int interval)
{
_continue = true;
_interval = interval;
if (_timer != null)
{
await _timer.DisposeAsync();
}
_timer = new System.Threading.Timer(Tick, null, interval, Timeout.Infinite);
}

public void StopTimer() => _continue = false;

void Tick(object state)
{
try
{
Action?.Invoke();
}
finally
{
if (_continue)
{
_timer?.Change(_interval, Timeout.Infinite);
}
}
}

~Timer()
{
_timer?.Dispose();
}
}

6 changes: 4 additions & 2 deletions src/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

<tb:TaskbarIcon
x:Name="TrayIcon"
ContextMenuMode="PopupMenu" NoLeftClickDelay="True"
ToolTipText="Battery Tracker" Visibility="Visible">
ContextMenuMode="PopupMenu"
LeftClickCommand="{StaticResource DisplayBatteryInfoCommand}"
NoLeftClickDelay="True" ToolTipText="Battery Tracker"
Visibility="Visible">
<tb:TaskbarIcon.IconSource>
<tb:GeneratedIconSource
BackgroundType="Rectangle" FontFamily="Arial" FontWeight="SemiBold"
Expand Down
14 changes: 14 additions & 0 deletions src/Models/BatteryInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Windows.System.Power;

namespace BatteryTracker.Models;

public struct BatteryInfo
{
public int ChargePercent;
public BatteryStatus BatteryStatus;
public PowerSupplyStatus PowerSupplyStatus;
public int DesignedCapacity;
public int MaxCapacity;
public int RemainingCapacity;
public int ChargingRate;
}
6 changes: 6 additions & 0 deletions src/Models/Exceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace BatteryTracker.Models;

public sealed class RuntimeException : ApplicationException
{
public RuntimeException(string message) : base(message) { }
}
55 changes: 55 additions & 0 deletions src/Models/PowerMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Attribute should be given to FluentFlyouts3:
* https://github.com/FireCubeStudios/FluentFlyouts3
*/

namespace BatteryTracker.Models;

public enum PowerModeEnum
{
BetterBattery = 0,
Balanced = 1,
BestPerformance = 2
}

public static class PowerModeIds
{
public static readonly Guid BetterBattery = new("961cc777-2547-4f9d-8174-7d86181b8a7a");
public static readonly Guid Balanced = new("00000000-0000-0000-0000-000000000000");
public static readonly Guid BestPerformance = new("ded574b5-45a0-4f42-8737-46345c09c238");
}

public sealed record PowerMode(PowerModeEnum Mode, Guid Guid)
{
public static readonly PowerMode BetterBattery =
new(PowerModeEnum.BetterBattery, PowerModeIds.BetterBattery);

public static readonly PowerMode Balanced =
new(PowerModeEnum.Balanced, PowerModeIds.Balanced);

public static readonly PowerMode BestPerformance =
new(PowerModeEnum.BestPerformance, PowerModeIds.BestPerformance);

public static PowerMode GuidToPowerMode(Guid guid)
{
if (guid == PowerModeIds.BetterBattery)
{
return BetterBattery;
}
if (guid == PowerModeIds.Balanced)
{
return Balanced;
}
if (guid == PowerModeIds.BestPerformance)
{
return BestPerformance;
}

throw new ArgumentOutOfRangeException($"Power mode guid {guid} is invalid!");
}

public override string ToString()
{
return $"{Mode}";
}
}
20 changes: 8 additions & 12 deletions src/Resources/Commands.xaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tb="using:H.NotifyIcon"
>
xmlns:tb="using:H.NotifyIcon">
<XamlUICommand
x:Key="OpenSettingsCommand"
x:Uid="OpenSettingsCommand"
Description="Open settings window"
>
</XamlUICommand>
x:Key="OpenSettingsCommand" x:Uid="OpenSettingsCommand"
Description="Open settings window" />
<XamlUICommand
x:Key="ExitApplicationCommand"
x:Uid="ExitApplicationCommand"
Description="Exit"
>
</XamlUICommand>
x:Key="ExitApplicationCommand" x:Uid="ExitApplicationCommand"
Description="Exit" />
<XamlUICommand
x:Key="DisplayBatteryInfoCommand" x:Uid="DisplayBatteryInfoCommand"
Description="Open battery info window" />
</ResourceDictionary>
Loading