Skip to content

Commit

Permalink
Customisable tray icon font size and other minor imporvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
leonzhou-smokeball committed Dec 24, 2024
1 parent 2ad4f2c commit 3be2a90
Show file tree
Hide file tree
Showing 13 changed files with 553 additions and 442 deletions.
15 changes: 8 additions & 7 deletions App/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<SupportedOSPlatformVersion>10.0.22000.0</SupportedOSPlatformVersion>
<ApplicationHighDpiMode>PerMonitorV2</ApplicationHighDpiMode>
<Platforms>AnyCPU;x64;ARM64;x86</Platforms>
<PackageVersion>2.1.2</PackageVersion>
<PackageVersion>2.1.3</PackageVersion>
<PackageProjectUrl>https://github.com/soleon/Percentage</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/soleon/Percentage?tab=MIT-1-ov-file</PackageLicenseUrl>
<PackageIcon>Icon.png</PackageIcon>
Expand All @@ -25,12 +25,13 @@
<Authors>Long Zhou</Authors>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Codify.System.Windows" Version="*" />
<PackageReference Include="System.Management" Version="*" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="*" />
<PackageReference Include="WPF-UI.Tray" Version="*" />
<PackageReference Include="System.Reactive" Version="*" />
<Resource Include="Icon.png" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="*"/>
<PackageReference Include="Codify.System.Windows" Version="*"/>
<PackageReference Include="System.Management" Version="*"/>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="*"/>
<PackageReference Include="WPF-UI.Tray" Version="*"/>
<PackageReference Include="System.Reactive" Version="*"/>
<Resource Include="Icon.png"/>
<Compile Update="Properties\Settings.Designer.cs">
<DesignTimeSharedInput>True</DesignTimeSharedInput>
<AutoGen>True</AutoGen>
Expand Down
2 changes: 1 addition & 1 deletion App/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
StartupUri="TrayIconWindow.xaml">
StartupUri="NotifyIconWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
4 changes: 2 additions & 2 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ internal static Exception GetTrayIconUpdateError()
return _trayIconUpdateError;
}

internal static TrayIconWindow GetTrayIconWindow()
internal static NotifyIconWindow GetTrayIconWindow()
{
return Current.Windows.OfType<TrayIconWindow>().FirstOrDefault();
return Current.Windows.OfType<NotifyIconWindow>().FirstOrDefault();
}

private static void HandleException(object exception)
Expand Down
59 changes: 59 additions & 0 deletions App/Extensions/BrushExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Windows;
using System.Windows.Media;
using Percentage.App.Properties;
using Wpf.Ui.Markup;

namespace Percentage.App.Extensions;

using static Settings;

internal static class BrushExtensions
{
internal static Brush GetBatteryChargingBrush()
{
return GetTargetBrush(Default.IsAutoBatteryChargingColour, Default.BatteryChargingColour,
new SolidColorBrush((Color)ColorConverter.ConvertFromString(App.DefaultBatteryChargingColour)!));
}

internal static Brush GetBatteryCriticalBrush()
{
return GetTargetBrush(Default.IsAutoBatteryCriticalColour, Default.BatteryCriticalColour,
new SolidColorBrush((Color)ColorConverter.ConvertFromString(App.DefaultBatteryCriticalColour)!));
}

internal static Brush GetBatteryLowBrush()
{
return GetTargetBrush(Default.IsAutoBatteryLowColour, Default.BatteryLowColour,
new SolidColorBrush((Color)ColorConverter.ConvertFromString(App.DefaultBatteryLowColour)!));
}

internal static Brush GetBatteryNormalBrush()
{
return GetTargetBrush(Default.IsAutoBatteryNormalColour, Default.BatteryNormalColour,
(Brush)Application.Current.FindResource(nameof(ThemeResource.TextFillColorPrimaryBrush)));
}


private static Brush GetBrushFromColourHexString(string hexString, Brush fallbackBrush)
{
object colour;
try
{
colour = ColorConverter.ConvertFromString(hexString);
}
catch (FormatException)
{
return fallbackBrush;
}

return colour == null ? fallbackBrush : new SolidColorBrush((Color)colour);
}

private static Brush GetTargetBrush(bool isUsingAutoColour, string targetColour, Brush fallbackBrush)
{
return isUsingAutoColour
? new SolidColorBrush((Color)Application.Current.FindResource(nameof(ThemeResource.TextFillColorPrimary))!)
: GetBrushFromColourHexString(targetColour, fallbackBrush);
}
}
51 changes: 51 additions & 0 deletions App/Extensions/NotifyIconExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wpf.Ui.Tray.Controls;

namespace Percentage.App.Extensions;

internal static class NotifyIconExtensions
{
private const double NotifyIconSize = 16;

internal static void SetIcon(this NotifyIcon notifyIcon, FrameworkElement textBlock)
{
// Measure the size of the element first.
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

// Use the desired size to work out the appropriate margin so that the element can be centre aligned in the
// tray icon's 16-by-16 region.
textBlock.Margin = new Thickness((NotifyIconSize - textBlock.DesiredSize.Width) / 2,
(NotifyIconSize - textBlock.DesiredSize.Height) / 2, 0, 0);

// Measure again for the correct desired size with the margin.
textBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
textBlock.Arrange(new Rect(textBlock.DesiredSize));

// Render the element with the correct DPI scale.
var dpiScale = VisualTreeHelper.GetDpi(textBlock);
var renderTargetBitmap = new RenderTargetBitmap(
(int)Math.Round(NotifyIconSize * dpiScale.DpiScaleX, MidpointRounding.AwayFromZero),
(int)Math.Round(NotifyIconSize * dpiScale.DpiScaleY, MidpointRounding.AwayFromZero),
dpiScale.PixelsPerInchX,
dpiScale.PixelsPerInchY,
PixelFormats.Default);
renderTargetBitmap.Render(textBlock);

notifyIcon.Icon = renderTargetBitmap;
}

internal static void SetBatteryFullIcon(this NotifyIcon notifyIcon)
{
notifyIcon.SetIcon(new TextBlock
{
Text = "\uf5fc",
Foreground = BrushExtensions.GetBatteryNormalBrush(),
FontFamily = new FontFamily("Segoe Fluent Icons"),
FontSize = 16
});
}
}
19 changes: 12 additions & 7 deletions App/TrayIconWindow.xaml → App/NotifyIconWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window x:Class="Percentage.App.TrayIconWindow"
<Window x:Class="Percentage.App.NotifyIconWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tray="http://schemas.lepo.co/wpfui/2022/xaml/tray"
Expand All @@ -17,19 +17,24 @@
<ContextMenu>
<ui:MenuItem Header="_Details"
Click="OnDetailsMenuItemClick"
Icon="{ui:SymbolIcon AppsListDetail20}" />
Icon="{ui:SymbolIcon AppsListDetail20}"
ToolTip="Show battery details" />
<ui:MenuItem Header="App _settings"
Click="OnAppSettingsMenuItemClick"
Icon="{ui:SymbolIcon Settings20}" />
<ui:MenuItem Header="System _power settings"
Icon="{ui:SymbolIcon Settings20}"
ToolTip="Open application settings" />
<ui:MenuItem Header="_Power &amp; battery settings"
Click="OnSystemSettingsMenuItemClick"
Icon="{ui:SymbolIcon FlashSettings20}" />
Icon="{ui:SymbolIcon Power20}"
ToolTip="Open Windows system power and battery settings" />
<ui:MenuItem Header="_About"
Click="OnAboutMenuItemClick"
Icon="{ui:SymbolIcon Info20}" />
Icon="{ui:SymbolIcon Info20}"
ToolTip="Show application information" />
<ui:MenuItem Header="E_xit"
Click="OnExitMenuItemClick"
Icon="{ui:SymbolIcon ArrowExit20}" />
Icon="{ui:SymbolIcon ArrowExit20}"
ToolTip="Exit application" />
</ContextMenu>
</tray:NotifyIcon.Menu>
</tray:NotifyIcon>
Expand Down
Loading

0 comments on commit 3be2a90

Please sign in to comment.