Skip to content

Commit

Permalink
Various bug fixes.
Browse files Browse the repository at this point in the history
* Catch and log exception when setting tray icon image.
* Show caught error in about page for easy reporting.
* Always save user settings as soon as they are updated.
* Minor tray icon font settings wording update.
  • Loading branch information
leonzhou-smokeball committed Dec 14, 2024
1 parent cf96477 commit 5e1f8ed
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 41 deletions.
19 changes: 15 additions & 4 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Windows;
using System.Windows.Media;
using Wpf.Ui;
using static Percentage.App.Properties.Settings;

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

Expand All @@ -28,9 +27,9 @@ public partial class App
internal const bool DefaultTrayIconFontBold = false;
internal const bool DefaultTrayIconFontUnderline = false;
internal const string Id = "f05f920a-c997-4817-84bd-c54d87e40625";

private static Exception _trayIconUpdateError;
internal static readonly FontFamily DefaultTrayIconFontFamily = new("Microsoft Sans Serif");
internal static readonly ISnackbarService SnackbarService = new SnackbarService();
internal static readonly FontFamily TrayIconFontFamily = new("Microsoft Sans Serif");

private readonly Mutex _appMutex;

Expand All @@ -49,6 +48,11 @@ public App()
if (!isNewInstance) Shutdown(1);
}

internal static Exception GetTrayIconUpdateError()
{
return _trayIconUpdateError;
}

private static void HandleException(object exception)
{
if (exception is OutOfMemoryException)
Expand Down Expand Up @@ -84,10 +88,17 @@ private static void HandleException(object exception)
}
}

internal static void SetTrayIconUpdateError(Exception e)
{
_trayIconUpdateError = e;
TrayIconUpdateErrorSet?.Invoke(e);
}

internal static event Action<Exception> TrayIconUpdateErrorSet;

protected override void OnExit(ExitEventArgs e)
{
// Save user settings when exiting the app.
Default.Save();
_appMutex.Dispose();
base.OnExit(e);
}
Expand Down
24 changes: 21 additions & 3 deletions App/Pages/AboutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:controls="clr-namespace:Percentage.App.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:data="clr-namespace:Codify.System.Windows.Data;assembly=Codify.System.Windows"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
d:DesignWidth="400">
Expand All @@ -19,9 +20,26 @@
<TextBlock Text="App info" />
</DockPanel>
</ui:CardExpander.Header>
<controls:ApplicationInformation Margin="40,0,0,0"
x:Name="ApplicationInformation"
x:FieldModifier="private" />
<StackPanel>
<controls:ApplicationInformation x:Name="ApplicationInformation"
x:FieldModifier="private" />
<ui:CardControl Icon="{ui:SymbolIcon Warning20}"
Visibility="{Binding TrayIconUpdateError, Converter={x:Static data:VisibilityConverter.Instance}}"
Background="{DynamicResource InfoBarWarningSeverityBackgroundBrush}">
<ui:CardControl.Header>
<StackPanel>
<ui:TextBlock
FontTypography="BodyStrong"
Text="The tray icon couldn't be updated" />
<ui:TextBlock
Text="Try restarting the app if the tray icon stops updating. Copy the full error details and report this issue if it blocks you from using the app." />
</StackPanel>
</ui:CardControl.Header>
<controls:CopyButton
TargetObject="{Binding TrayIconUpdateError}"
ToolTip="Copy full error details" />
</ui:CardControl>
</StackPanel>
</ui:CardExpander>
<ui:CardControl Icon="{ui:SymbolIcon Mail20}"
Header="Report an issue">
Expand Down
27 changes: 24 additions & 3 deletions App/Pages/AboutPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;

namespace Percentage.App.Pages;

public partial class AboutPage
public sealed partial class AboutPage : INotifyPropertyChanged
{
public AboutPage()
{
InitializeComponent();

Loaded += (_, _) => App.TrayIconUpdateErrorSet += OnTrayIconUpdateErrorSet;

Unloaded += (_, _) => App.TrayIconUpdateErrorSet -= OnTrayIconUpdateErrorSet;
}

public Exception TrayIconUpdateError => App.GetTrayIconUpdateError();

public event PropertyChangedEventHandler PropertyChanged;

private void OnDonationButtonClick(object sender, RoutedEventArgs e)
{
Helper.OpenDonationLocation();
}

private void OnFeedbackButtonClick(object sender, RoutedEventArgs e)
{
Helper.OpenFeedbackLocation();
}

private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

private void OnRatingButtonClick(object sender, RoutedEventArgs e)
{
Helper.ShowRatingView();
Expand All @@ -24,8 +45,8 @@ private void OnSourceCodeButtonClick(object sender, RoutedEventArgs e)
Helper.OpenSourceCodeLocation();
}

private void OnDonationButtonClick(object sender, RoutedEventArgs e)
private void OnTrayIconUpdateErrorSet(Exception obj)
{
Helper.OpenDonationLocation();
OnPropertyChanged(nameof(TrayIconUpdateError));
}
}
53 changes: 30 additions & 23 deletions App/Pages/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,31 +79,38 @@
FontTypography="BodyStrong"
Text="Tray icon font" />
<ui:TextBlock
Text="Set tray icon font family, size and styles" />
Text="Set tray icon font family and styles" />
</StackPanel>
</ui:CardExpander.Header>
<DockPanel>
<ToggleButton Content="{ui:SymbolIcon TextUnderline20}"
VerticalAlignment="Stretch"
IsChecked="{Binding TrayIconFontUnderline, Source={x:Static properties:Settings.Default}}"
DockPanel.Dock="Right"
Margin="5,0,0,0" />
<ToggleButton Content="{ui:SymbolIcon TextBold20}"
VerticalAlignment="Stretch"
IsChecked="{Binding TrayIconFontBold, Source={x:Static properties:Settings.Default}}"
DockPanel.Dock="Right" />
<ComboBox Margin="5,0"
SelectedItem="{Binding TrayIconFontFamily, Source={x:Static properties:Settings.Default}}"
ItemsSource="{x:Static Fonts.SystemFontFamilies}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock FontFamily="{Binding}"
Text="{Binding Source}"
VerticalAlignment="Center" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DockPanel>
<StackPanel>
<ui:InfoBar Severity="Informational"
IsOpen="True"
Margin="0,0,0,12"
Title="Why can't I make the tray icon text bigger?"
Message="Windows limits tray icons to be a 16 × 16 pixel square. Anything goes out of this box will be cut off." />
<DockPanel>
<ToggleButton Content="{ui:SymbolIcon TextUnderline20}"
VerticalAlignment="Stretch"
IsChecked="{Binding TrayIconFontUnderline, Source={x:Static properties:Settings.Default}}"
DockPanel.Dock="Right"
Margin="5,0,0,0" />
<ToggleButton Content="{ui:SymbolIcon TextBold20}"
VerticalAlignment="Stretch"
IsChecked="{Binding TrayIconFontBold, Source={x:Static properties:Settings.Default}}"
DockPanel.Dock="Right" />
<ComboBox Margin="5,0"
SelectedItem="{Binding TrayIconFontFamily, Source={x:Static properties:Settings.Default}}"
ItemsSource="{x:Static Fonts.SystemFontFamilies}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock FontFamily="{Binding}"
Text="{Binding Source}"
VerticalAlignment="Center" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</DockPanel>
</StackPanel>
</ui:CardExpander>

<!-- Tray icon colour -->
Expand Down
4 changes: 1 addition & 3 deletions App/Pages/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private void OnResetButtonClick(object sender, RoutedEventArgs e)
Default.BatteryLowColour = App.DefaultBatteryLowColour;
Default.BatteryChargingColour = App.DefaultBatteryChargingColour;
Default.BatteryNormalColour = App.DefaultBatteryNormalColour;
Default.TrayIconFontFamily = App.TrayIconFontFamily;
Default.TrayIconFontFamily = App.DefaultTrayIconFontFamily;
Default.TrayIconFontBold = App.DefaultTrayIconFontBold;
Default.TrayIconFontUnderline = App.DefaultTrayIconFontUnderline;
Default.BatteryCriticalNotificationValue = App.DefaultBatteryCriticalNotificationValue;
Expand All @@ -124,8 +124,6 @@ private void OnResetButtonClick(object sender, RoutedEventArgs e)
Default.BatteryCriticalNotification = App.DefaultBatteryCriticalNotification;
Default.HideAtStartup = App.DefaultHideAtStartup;

Default.Save();

_ = EnableAutoStart();
}

Expand Down
4 changes: 2 additions & 2 deletions App/TrayIconWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Loaded="OnLoaded"
ShowInTaskbar="False"
Left="-1000"
Top="-1000"
Left="-100"
Top="-100"
Width="0"
Height="0">
<tray:NotifyIcon x:Name="NotifyIcon"
Expand Down
31 changes: 28 additions & 3 deletions App/TrayIconWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Reactive.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Media.Imaging;
Expand All @@ -11,8 +12,8 @@
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.Win32;
using Percentage.App.Pages;
using Percentage.App.Properties;
using Wpf.Ui.Appearance;
using Wpf.Ui.Controls;
using Wpf.Ui.Markup;
using Application = System.Windows.Application;
using Brush = System.Windows.Media.Brush;
Expand Down Expand Up @@ -157,6 +158,9 @@ private void OnSettingsMenuItemClick(object sender, RoutedEventArgs e)

private void OnUserSettingsPropertyChanged(string propertyName)
{
// Always save settings change immediately in case the app crashes losing all changes.
Default.Save();

switch (propertyName)
{
case nameof(Default.RefreshSeconds):
Expand Down Expand Up @@ -192,7 +196,8 @@ private void SetNotifyIconText(string text, Brush foreground, string fontFamily
Text = text,
Foreground = foreground,
FontSize = 18,
Margin = new Thickness(-1.2)
TextAlignment = TextAlignment.Center,
Margin = new Thickness(-1, -0.4, 0, 0)
};

if (fontFamily != null) textBlock.FontFamily = new FontFamily(fontFamily);
Expand All @@ -202,7 +207,27 @@ private void SetNotifyIconText(string text, Brush foreground, string fontFamily

if (Default.TrayIconFontUnderline) textBlock.TextDecorations = TextDecorations.Underline;

NotifyIcon.Icon = GetImageSource(textBlock);
var iconImageSource = GetImageSource(textBlock);

// There's a chance that some native exception may be thrown when setting the notify icon's image.
// Catch any exception here and retry a few times then fail silently with logs.
for (var i = 0; i < 5; i++)
{
try
{
NotifyIcon.Icon = iconImageSource;
break;
}
catch (Exception e)
{
if (i == 4)
{
// Retried maximum number of times.
// Log error and continue.
App.SetTrayIconUpdateError(e);
}
}
}
}

private void UpdateBatteryStatus()
Expand Down

0 comments on commit 5e1f8ed

Please sign in to comment.