forked from kas/percentage
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Customisable tray icon font size and other minor imporvements.
- Loading branch information
1 parent
2ad4f2c
commit 3be2a90
Showing
13 changed files
with
553 additions
and
442 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.