Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve notifyicon #53

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added source/SylphyHorn/.assets/tasktray-light.ico
Binary file not shown.
10 changes: 9 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,23 @@ 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),
#if DEBUG
new TaskTrayIconItem("Tasktray Icon Test", () => new TaskTrayTestWindow().Show()),
#endif
};

this._taskTrayIcon = new TaskTrayIcon(icon, menus);
this._taskTrayIcon = new TaskTrayIcon(icon, lightIcon, menus);
}

return this._taskTrayIcon;
Expand Down
12 changes: 12 additions & 0 deletions source/SylphyHorn/Interop/ColorHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Windows.Media;

namespace SylphyHorn.Interop
{
public static class ColorHelper
{
public static System.Drawing.Color ToGDIColor(this Color color)
{
return System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
}
}
}
68 changes: 60 additions & 8 deletions source/SylphyHorn/Interop/IconHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Drawing;
using System.Windows;
using System.IO;
using System.Windows.Media.Imaging;
using MetroRadiance.Interop;

namespace SylphyHorn.Interop
Expand All @@ -20,6 +21,35 @@ public static Icon GetIconFromResource(Uri uri)
}
}

public static RenderTargetBitmap ToRenderTargetBitmap(this System.Windows.Media.DrawingVisual drawingVisual, Size size, Dpi? dpi = null)
{
var imageDpi = dpi ?? new Dpi(96, 96);
var imageSize = ScaleSizeByDpi(size, imageDpi);
var renderTarget = new RenderTargetBitmap(imageSize.Width, imageSize.Height, imageDpi.X, imageDpi.Y, System.Windows.Media.PixelFormats.Default);
renderTarget.Render(drawingVisual);
return renderTarget;
}

public static Bitmap ToBitmap(this System.Windows.Media.DrawingVisual drawingVisual, Size size, Dpi? dpi = null, Color? transparentColor = null)
{
var renderTarget = drawingVisual.ToRenderTargetBitmap(size, dpi);
var encoder = new BmpBitmapEncoder();
var frame = BitmapFrame.Create(renderTarget);
encoder.Frames.Add(frame);

using (var stream = new MemoryStream())
{
encoder.Save(stream);

var bitmap = new Bitmap(stream, useIcm: true);
if (transparentColor.HasValue)
{
bitmap.MakeTransparent(transparentColor.Value);
}
return bitmap;
}
}

public static Icon ToIcon(this Bitmap bitmap)
{
var iconHandle = bitmap.GetHicon();
Expand All @@ -28,21 +58,43 @@ public static Icon ToIcon(this Bitmap bitmap)
return icon;
}

#if DEBUG
internal static BitmapSource ToBitmapSource(this Icon icon)
{
using (var bitmap = icon.ToBitmap())
using (var stream = new MemoryStream())
{
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Seek(0, SeekOrigin.Begin);

var bitmapSource = BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
return bitmapSource;
}
}
#endif

private static Icon ScaleIconToDpi(Icon targetIcon)
{
var dpi = GetMonitorDpi();
var dpi = GetSystemDpi();

return new Icon(targetIcon, new System.Drawing.Size((int)(16 * dpi.ScaleX), (int)(16 * dpi.ScaleY)));
return new Icon(targetIcon, new Size((int)(16 * dpi.ScaleX), (int)(16 * dpi.ScaleY)));
}

public static System.Windows.Size GetIconSize()
public static Size GetIconSize()
{
var dpi = GetMonitorDpi();
return new Size(
(int)System.Windows.SystemParameters.SmallIconWidth,
(int)System.Windows.SystemParameters.SmallIconHeight);
}

return new System.Windows.Size(SystemParameters.SmallIconWidth * dpi.ScaleX, SystemParameters.SmallIconHeight * dpi.ScaleY);
private static Size ScaleSizeByDpi(Size size, Dpi dpi)
{
return new Size(
(int)Math.Round(size.Width * dpi.ScaleX),
(int)Math.Round(size.Height * dpi.ScaleY));
}
private static Dpi GetMonitorDpi()

public static Dpi GetSystemDpi()
{
return PerMonitorDpi.GetDpi(IntPtr.Zero);
}
Expand Down
9 changes: 9 additions & 0 deletions source/SylphyHorn/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions source/SylphyHorn/Properties/Resources.ja.resx
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,7 @@
<data name="TaskTray_Menu_Settings" xml:space="preserve">
<value>設定</value>
</data>
<data name="TaskTray_TooltipText_DesktopCount" xml:space="preserve">
<value>デスクトップ {0}/{1}</value>
</data>
</root>
3 changes: 3 additions & 0 deletions source/SylphyHorn/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,7 @@
<data name="TaskTray_Menu_Settings" xml:space="preserve">
<value>&amp;Settings (S)</value>
</data>
<data name="TaskTray_TooltipText_DesktopCount" xml:space="preserve">
<value>Desktop {0}/{1}</value>
</data>
</root>
9 changes: 9 additions & 0 deletions source/SylphyHorn/SylphyHorn.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@
<Compile Include="Annotations\LightAttribute.cs" />
<Compile Include="ApplicationPreparation.cs" />
<Compile Include="CommandLineArgs.cs" />
<Compile Include="Interop\ColorHelper.cs" />
<Compile Include="Interop\DotnetVersion.cs" />
<Compile Include="Interop\IconHelper.cs" />
<Compile Include="Interop\IDesktopWallpaper.cs" />
Expand Down Expand Up @@ -282,6 +283,9 @@
<DependentUpon>SettingsWindow.xaml</DependentUpon>
</Compile>
<Compile Include="Interop\UwpBridge.cs" />
<Compile Include="UI\TaskTrayTestWindow.xaml.cs" Condition="'$(Configuration)' == 'Debug'">
<DependentUpon>TaskTrayTestWindow.xaml</DependentUpon>
</Compile>
<Page Include="Styles\Controls.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
Expand Down Expand Up @@ -324,6 +328,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="UI\TaskTrayTestWindow.xaml" Condition="'$(Configuration)' == 'Debug'">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Services\ShortcutKeyDetector.cs" />
Expand Down Expand Up @@ -367,6 +375,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
Loading