From 045d5cac5259906bc9c53faef1f619c1f27adcb1 Mon Sep 17 00:00:00 2001 From: mntone Date: Thu, 6 Aug 2020 22:27:41 +0900 Subject: [PATCH] Improve notification icon on light theme. --- source/SylphyHorn/.assets/tasktray-light.ico | Bin 0 -> 12926 bytes source/SylphyHorn/ApplicationPreparation.cs | 7 +++- source/SylphyHorn/SylphyHorn.csproj | 1 + source/SylphyHorn/UI/DynamicInfoTrayIcon.cs | 26 +++++++++----- source/SylphyHorn/UI/TaskTrayIcon.cs | 36 +++++++++++++++---- 5 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 source/SylphyHorn/.assets/tasktray-light.ico diff --git a/source/SylphyHorn/.assets/tasktray-light.ico b/source/SylphyHorn/.assets/tasktray-light.ico new file mode 100644 index 0000000000000000000000000000000000000000..d30ab0829cf130c3f9a7818984e52cbfe72e8dd7 GIT binary patch literal 12926 zcmeHNyKWRg5Nu04WSPK$B^+4d$jBgs%o6`VOh`t61c8HdNXTap5)vWA2OuIu1pWgq zK-d=2F4L?^S8mtr%whpPJY$Wzr@E?UuD#ydJK1c_F5Bj&r8u)km&_iR+1A#uetpI4 zO~SUfhxPMyv!@$oJ3GVr(>1d$ac8q({WAG{ylHkTd89Ns4tNT-n|#js=+pUXX6|LU zwlpFK`?)UGlovSAVLeq3e$y|z%)MBmCo|@Fj_E9}om4GX;IIx=)UZ}@j04Y7RKEWN z>(fbn9$Yp1dT4h0o7v;we?+FCQ<`J*$mV9VlHBv3|-R0M4%+ zip%@CkK*L~!DSC%(8M)x6(bkE%#9qb#XNGG-|zi?(brY!AD=T`19&Yef(t#?%vBiI zIj`aJUNt%UU%0&Q7wJ7`m!n=jfg!So!nj__l^z%(dnk471#hr+mC%9S1%B6}!|>!n=jfg!So!nj__ zl^z%(dnkyhW;pWaWMF?W`!xbjxmRNow5c{gl@JCHtWEO;K- z+QR30e{4SQ^~Z0gOz7al-k$sCnpv8=Y2tIuzW+l;8?6Ja1GOE<-Jhr8wfjZh)O~XN z=&ROI>l|Q;UxTq8IbzfUNAx=9kzccBJ#xgTSGdBWZfvde3Ud#ID{O4PSW|k1wf$TF z)pen;c#Vy%m0n@(`fJypx-JwpcD==#(kqOwKdy5gy=&I2M~)cvz!CXOkz2b~UW}D| wz&&d$?USpYbzkUn{ta&(xX>L)-xbKwnazE^n|JqpzMpqbrcCJIBR5IlPs2>sv;Y7A literal 0 HcmV?d00001 diff --git a/source/SylphyHorn/ApplicationPreparation.cs b/source/SylphyHorn/ApplicationPreparation.cs index 830f1b6..a649706 100644 --- a/source/SylphyHorn/ApplicationPreparation.cs +++ b/source/SylphyHorn/ApplicationPreparation.cs @@ -11,6 +11,8 @@ using SylphyHorn.Services; using SylphyHorn.UI; using SylphyHorn.UI.Bindings; +using System.Runtime.InteropServices; +using Windows.System; namespace SylphyHorn { @@ -114,17 +116,20 @@ public TaskTrayIcon CreateTaskTrayIcon() if (this._taskTrayIcon == null) { const string iconUri = "pack://application:,,,/SylphyHorn;Component/.assets/tasktray.ico"; + const string lightIconUri = "pack://application:,,,/SylphyHorn;Component/.assets/tasktray-light.ico"; if (!Uri.TryCreate(iconUri, UriKind.Absolute, out var uri)) return null; + if (!Uri.TryCreate(lightIconUri, UriKind.Absolute, out var lightUri)) return null; var icon = IconHelper.GetIconFromResource(uri); + var lightIcon = IconHelper.GetIconFromResource(lightUri); var menus = new[] { new TaskTrayIconItem(Resources.TaskTray_Menu_Settings, ShowSettings, () => Application.Args.CanSettings), new TaskTrayIconItem(Resources.TaskTray_Menu_Exit, this._shutdownAction), }; - this._taskTrayIcon = new TaskTrayIcon(icon, menus); + this._taskTrayIcon = new TaskTrayIcon(icon, lightIcon, menus); } return this._taskTrayIcon; diff --git a/source/SylphyHorn/SylphyHorn.csproj b/source/SylphyHorn/SylphyHorn.csproj index a2a7cce..c7bdb6c 100644 --- a/source/SylphyHorn/SylphyHorn.csproj +++ b/source/SylphyHorn/SylphyHorn.csproj @@ -367,6 +367,7 @@ + Always Designer diff --git a/source/SylphyHorn/UI/DynamicInfoTrayIcon.cs b/source/SylphyHorn/UI/DynamicInfoTrayIcon.cs index d4604f2..9c333a8 100644 --- a/source/SylphyHorn/UI/DynamicInfoTrayIcon.cs +++ b/source/SylphyHorn/UI/DynamicInfoTrayIcon.cs @@ -1,6 +1,7 @@ using System; using System.Drawing; using System.Windows.Controls; +using MetroRadiance.Platform; using SylphyHorn.Interop; namespace SylphyHorn.UI @@ -12,11 +13,12 @@ public class DynamicInfoTrayIcon : IDisposable private const float _verticalFontSize = 6; private readonly FontFamily _fontFamily; - private readonly Brush _brush; + private readonly Color? _color; private Font _font; + private Brush _brush; private Orientation _lastOrientation; - public DynamicInfoTrayIcon(int totalDesktopCount, FontFamily fontFamily = null, Color? color = null) + public DynamicInfoTrayIcon(int totalDesktopCount, Theme theme, FontFamily fontFamily = null, Color? color = null) { var currentOrientation = this._lastOrientation = GetOrientation(totalDesktopCount); var fontSize = GetFontSize(currentOrientation); @@ -24,12 +26,8 @@ public DynamicInfoTrayIcon(int totalDesktopCount, FontFamily fontFamily = null, this._fontFamily = fontFamily ?? new FontFamily(_defaultFontFamilyName); this._font = new Font(this._fontFamily, fontSize, FontStyle.Bold); - if (!color.HasValue) - { - color = Color.White; - } - - this._brush = new SolidBrush(color.Value); + this._color = color; + this._brush = new SolidBrush(color.GetValueOrDefault(GetForegroundColor(theme))); } public Icon GetDesktopInfoIcon(int currentDesktop, int totalDesktopCount) @@ -59,6 +57,12 @@ private void UpdateFontSize(Orientation newOrientation) this._font = new Font(this._fontFamily, fontSize, FontStyle.Bold); } + public void UpdateBrush(Theme theme) + { + this._brush?.Dispose(); + this._brush = new SolidBrush(this._color.GetValueOrDefault(GetForegroundColor(theme))); + } + // consolidate two methods below? private Bitmap DrawHorizontalInfo(int currentDesktop, int totalDesktopCount) { @@ -71,6 +75,7 @@ private Bitmap DrawHorizontalInfo(int currentDesktop, int totalDesktopCount) using (var graphics = Graphics.FromImage(bitmap)) { + graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; graphics.DrawString(stringToDraw, this._font, this._brush, offset); } @@ -114,6 +119,11 @@ private static float GetFontSize(Orientation orientation) return orientation == Orientation.Horizontal ? _horizontalFontSize : _verticalFontSize; } + private static Color GetForegroundColor(Theme theme) + { + return theme == Theme.Light ? Color.Black : Color.White; + } + private static PointF GetHorizontalStringOffset() { return new PointF(-2, 0); diff --git a/source/SylphyHorn/UI/TaskTrayIcon.cs b/source/SylphyHorn/UI/TaskTrayIcon.cs index 4000305..278d1aa 100644 --- a/source/SylphyHorn/UI/TaskTrayIcon.cs +++ b/source/SylphyHorn/UI/TaskTrayIcon.cs @@ -1,7 +1,9 @@ using System; using System.Drawing; using System.Linq; +using System.Windows; using System.Windows.Forms; +using MetroRadiance.Platform; using SylphyHorn.Properties; using SylphyHorn.Serialization; using WindowsDesktop; @@ -12,19 +14,22 @@ public class TaskTrayIcon : IDisposable { private Icon _icon; private readonly Icon _defaultIcon; + private readonly Icon _lightIcon; private readonly TaskTrayIconItem[] _items; private NotifyIcon _notifyIcon; private DynamicInfoTrayIcon _infoIcon; public string Text { get; set; } = ProductInfo.Title; - public TaskTrayIcon(Icon icon, TaskTrayIconItem[] items) + public TaskTrayIcon(Icon icon, Icon lightIcon, TaskTrayIconItem[] items) { this._defaultIcon = icon; + this._lightIcon = lightIcon; - this._icon = icon; + this._icon = WindowsTheme.SystemTheme.Current == Theme.Light ? this._lightIcon : this._defaultIcon; this._items = items; + WindowsTheme.SystemTheme.Changed += this.OnSystemThemeChanged; VirtualDesktop.CurrentChanged += this.OnCurrentDesktopChanged; } @@ -65,12 +70,14 @@ public void Reload(VirtualDesktop desktop = null) { this.UpdateWithDesktopInfo(desktop ?? VirtualDesktop.Current); } - else if (this._icon != this._defaultIcon) + else if (this._icon != this._defaultIcon && this._icon != this._lightIcon) { this._infoIcon?.Dispose(); this._infoIcon = null; - this.ChangeIcon(this._defaultIcon); + this.ChangeIcon(WindowsTheme.SystemTheme.Current == Theme.Light + ? this._lightIcon + : this._defaultIcon); } } @@ -82,7 +89,7 @@ private void UpdateWithDesktopInfo(VirtualDesktop currentDesktop) if (this._infoIcon == null) { - this._infoIcon = new DynamicInfoTrayIcon(totalDesktopCount); + this._infoIcon = new DynamicInfoTrayIcon(totalDesktopCount, WindowsTheme.SystemTheme.Current); } this.ChangeIcon(this._infoIcon.GetDesktopInfoIcon(currentDesktopIndex, totalDesktopCount)); @@ -93,9 +100,24 @@ private void OnCurrentDesktopChanged(object sender, VirtualDesktopChangedEventAr this.Reload(e.NewDesktop); } + private void OnSystemThemeChanged(object sender, Theme e) + { + if (Settings.General.TrayShowDesktop) + { + this._infoIcon.UpdateBrush(e); + this.UpdateWithDesktopInfo(VirtualDesktop.Current); + } + else + { + this.ChangeIcon(e == Theme.Light + ? this._lightIcon + : this._defaultIcon); + } + } + private void ChangeIcon(Icon newIcon) { - if (this._icon != this._defaultIcon) + if (this._icon != this._defaultIcon && this._icon != this._lightIcon) { this._icon?.Dispose(); } @@ -107,8 +129,10 @@ private void ChangeIcon(Icon newIcon) public void Dispose() { this._notifyIcon?.Dispose(); + this._lightIcon?.Dispose(); this._icon?.Dispose(); + WindowsTheme.SystemTheme.Changed -= this.OnSystemThemeChanged; VirtualDesktop.CurrentChanged -= this.OnCurrentDesktopChanged; } }