Skip to content

Commit

Permalink
Improve notification icon on light theme.
Browse files Browse the repository at this point in the history
  • Loading branch information
mntone committed Aug 6, 2020
1 parent 79e200c commit 045d5ca
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
Binary file added source/SylphyHorn/.assets/tasktray-light.ico
Binary file not shown.
7 changes: 6 additions & 1 deletion source/SylphyHorn/ApplicationPreparation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using SylphyHorn.Services;
using SylphyHorn.UI;
using SylphyHorn.UI.Bindings;
using System.Runtime.InteropServices;
using Windows.System;

namespace SylphyHorn
{
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions source/SylphyHorn/SylphyHorn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<Resource Include=".assets\tasktray-light.ico" />
<Content Include="AppxManifest.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<SubType>Designer</SubType>
Expand Down
26 changes: 18 additions & 8 deletions source/SylphyHorn/UI/DynamicInfoTrayIcon.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Drawing;
using System.Windows.Controls;
using MetroRadiance.Platform;
using SylphyHorn.Interop;

namespace SylphyHorn.UI
Expand All @@ -12,24 +13,21 @@ 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);

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)
Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down
36 changes: 30 additions & 6 deletions source/SylphyHorn/UI/TaskTrayIcon.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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));
Expand All @@ -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();
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 045d5ca

Please sign in to comment.