Skip to content

Commit

Permalink
Various imporvements.
Browse files Browse the repository at this point in the history
* Added auto colour option for all configurable colours.
* Update AccentColourPicker to custom control.
* Added black and white colour to available colours.
* Debounced all battery status update calls to further minimise back pressure problem.
  • Loading branch information
leonzhou-smokeball committed Dec 16, 2024
1 parent 298d54a commit 3d1ba4c
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 384 deletions.
34 changes: 29 additions & 5 deletions App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,29 @@
using System.Windows;
using System.Windows.Media;
using Wpf.Ui;
using Wpf.Ui.Markup;
using static Percentage.App.Properties.Settings;

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

namespace Percentage.App;

public partial class App
{
internal const string DefaultBatteryChargingColour = "#10893E";
internal const string DefaultBatteryCriticalColour = "#E81123";
internal const string DefaultBatteryChargingColour = "#FF10893E";
internal const string DefaultBatteryCriticalColour = "#FFE81123";
internal const bool DefaultBatteryCriticalNotification = true;
internal const int DefaultBatteryCriticalNotificationValue = 10;
internal const bool DefaultBatteryFullNotification = true;
internal const bool DefaultBatteryHighNotification = true;
internal const int DefaultBatteryHighNotificationValue = 80;
internal const string DefaultBatteryLowColour = "#CA5010";
internal const string DefaultBatteryLowColour = "#FFCA5010";
internal const bool DefaultBatteryLowNotification = true;
internal const int DefaultBatteryLowNotificationValue = 20;
internal const string DefaultBatteryNormalColour = null;
internal const bool DefaultHideAtStartup = false;
internal const bool DefaultIsAutoBatteryChargingColour = false;
internal const bool DefaultIsAutoBatteryCriticalColour = false;
internal const bool DefaultIsAutoBatteryLowColour = false;
internal const bool DefaultIsAutoBatteryNormalColour = true;
internal const int DefaultRefreshSeconds = 60;
internal const bool DefaultTrayIconFontBold = false;
Expand All @@ -38,6 +42,8 @@ public App()
{
_appMutex = new Mutex(true, Id, out var isNewInstance);

if (!isNewInstance) Shutdown(1);

DispatcherUnhandledException += (_, e) => HandleException(e.Exception);

AppDomain.CurrentDomain.UnhandledException += (_, e) => HandleException(e.ExceptionObject);
Expand All @@ -46,7 +52,8 @@ public App()

InitializeComponent();

if (!isNewInstance) Shutdown(1);
// User settings migration for backward compatibility.
MigrateUserSettings();
}

internal static Exception GetTrayIconUpdateError()
Expand Down Expand Up @@ -97,6 +104,23 @@ internal static void SetTrayIconUpdateError(Exception e)

internal static event Action<Exception> TrayIconUpdateErrorSet;

private void MigrateUserSettings()
{
if (Default.RefreshSeconds < 5) Default.RefreshSeconds = 5;

Default.BatteryNormalColour ??=
((Brush)FindResource(nameof(ThemeResource.TextFillColorPrimaryBrush)))!.ToString();

if (Default.BatteryLowColour is { Length: 7 } lowColourHexValue)
Default.BatteryLowColour = lowColourHexValue.Insert(1, "FF");

if (Default.BatteryCriticalColour is { Length: 7 } criticalColourHexValue)
Default.BatteryCriticalColour = criticalColourHexValue.Insert(1, "FF");

if (Default.BatteryChargingColour is { Length: 7 } chargingColourHexValue)
Default.BatteryChargingColour = chargingColourHexValue.Insert(1, "FF");
}

protected override void OnExit(ExitEventArgs e)
{
// Save user settings when exiting the app.
Expand Down
81 changes: 81 additions & 0 deletions App/Controls/AccentColourPicker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System.Windows;
using System.Windows.Controls;

namespace Percentage.App.Controls;

public class AccentColourPicker : Control
{
public static readonly string[] AccentBrushes =
[
"#FF000000", // Black
"#FFFFFFFF", // White
"#FFFFB900", // Gold
"#FFFF8C00", // Dark Orange
"#FFF7630C", // Orange
"#FFCA5010", // Burnt Orange
"#FFDA3B01", // Red-Orange
"#FFEF6950", // Salmon
"#FFD13438", // Red
"#FFFF4343", // Bright Red
"#FFE74856", // Light Red
"#FFE81123", // Crimson Red
"#FFEA005E", // Pink
"#FFC30052", // Magenta
"#FFE3008C", // Fuchsia
"#FFBF0077", // Dark Magenta
"#FFC239B3", // Purple
"#FF9A0089", // Dark Purple
"#FF0078D7", // Blue
"#FF0063B1", // Royal Blue
"#FF8E8CD8", // Lavender
"#FF6B69D6", // Indigo
"#FF8764B8", // Violet
"#FF744DA9", // Purple Indigo
"#FFB146C2", // Orchid
"#FF0099BC", // Teal
"#FF2D7D9A", // Peacock Blue
"#FF00B7C3", // Light Aqua
"#FF038387", // Dark Cyan
"#FF00CC6A", // Light Green
"#FF10893E", // Green
"#FF7A7574", // Taupe
"#FF5D5A58", // Dark Gray
"#FF68768A", // Cool Gray
"#FF515C6B" // Slate
];

public static readonly DependencyProperty IsAutoColourProperty = DependencyProperty.Register(
nameof(IsAutoColour), typeof(bool), typeof(AccentColourPicker),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

public static readonly DependencyProperty LabelProperty = DependencyProperty.Register(
nameof(Label), typeof(string), typeof(AccentColourPicker));

public static readonly DependencyProperty SelectedColourProperty = DependencyProperty.Register(
nameof(SelectedColour), typeof(object), typeof(AccentColourPicker),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

static AccentColourPicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(AccentColourPicker),
new FrameworkPropertyMetadata(typeof(AccentColourPicker)));
}

public bool IsAutoColour
{
get => (bool)GetValue(IsAutoColourProperty);
set => SetValue(IsAutoColourProperty, value);
}

public string Label
{
get => (string)GetValue(LabelProperty);
set => SetValue(LabelProperty, value);
}

public object SelectedColour
{
get => GetValue(SelectedColourProperty);
set => SetValue(SelectedColourProperty, value);
}
}
Loading

0 comments on commit 3d1ba4c

Please sign in to comment.