-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppIconProvider.cs
More file actions
95 lines (84 loc) · 2.9 KB
/
Copy pathAppIconProvider.cs
File metadata and controls
95 lines (84 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Runtime.InteropServices;
namespace PrimeDictate;
internal enum TrayVisualState
{
Ready,
AlwaysListening,
Recording,
Processing,
Error
}
internal static class AppIconProvider
{
private const string IconFileName = "PrimeDictate.ico";
public static Icon LoadWindowIcon()
{
foreach (var candidate in EnumerateIconCandidates())
{
if (!File.Exists(candidate))
{
continue;
}
try
{
return new Icon(candidate);
}
catch (ArgumentException)
{
}
catch (IOException)
{
}
}
return SystemIcons.Application;
}
public static Icon CreateTrayIcon(TrayVisualState state)
{
var color = state switch
{
TrayVisualState.Ready => Color.FromArgb(34, 122, 255),
// Bright yellow so wake-on is obvious in the Windows tray (orange was too easy to miss).
TrayVisualState.AlwaysListening => Color.FromArgb(255, 214, 10),
TrayVisualState.Recording => Color.FromArgb(220, 53, 69),
TrayVisualState.Processing => Color.FromArgb(32, 164, 112),
TrayVisualState.Error => Color.FromArgb(245, 158, 11),
_ => Color.FromArgb(34, 122, 255)
};
// 32px reads clearly on Win11 / high-DPI trays; Windows scales down as needed.
const int size = 32;
using var bitmap = new Bitmap(size, size);
using (var graphics = Graphics.FromImage(bitmap))
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.Clear(Color.Transparent);
using var fillBrush = new SolidBrush(color);
using var borderPen = new Pen(Color.FromArgb(40, 40, 40), 2f);
using var centerBrush = new SolidBrush(Color.White);
graphics.FillEllipse(fillBrush, 2, 2, size - 4, size - 4);
graphics.DrawEllipse(borderPen, 2, 2, size - 4, size - 4);
graphics.FillEllipse(centerBrush, 12, 12, 8, 8);
}
var handle = bitmap.GetHicon();
try
{
using var unmanagedIcon = Icon.FromHandle(handle);
return (Icon)unmanagedIcon.Clone();
}
finally
{
_ = DestroyIcon(handle);
}
}
private static IEnumerable<string> EnumerateIconCandidates()
{
yield return Path.Combine(AppContext.BaseDirectory, IconFileName);
yield return Path.Combine(Directory.GetCurrentDirectory(), IconFileName);
yield return Path.Combine(Directory.GetCurrentDirectory(), "installer", "wix", "assets", IconFileName);
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool DestroyIcon(IntPtr hIcon);
}