-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppIcon.cs
More file actions
60 lines (56 loc) · 2.13 KB
/
Copy pathAppIcon.cs
File metadata and controls
60 lines (56 loc) · 2.13 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
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.Imaging;
namespace Babelive;
/// <summary>
/// Builds the Babelive window/tray icon at runtime instead of shipping a
/// separate <c>.ico</c> asset. Used by:
/// <list type="bullet">
/// <item><see cref="TrayIconHost"/> — system-tray icon</item>
/// <item><see cref="MainWindow"/> — taskbar / Alt-Tab icon (the settings
/// window is the only one that shows in taskbar; lyric + API dialog
/// have <c>ShowInTaskbar=False</c>)</item>
/// </list>
/// </summary>
internal static class AppIcon
{
/// <summary>
/// Render a solid amber disc with a white "译" glyph and return as a
/// plain <see cref="Bitmap"/> usable as <c>Image.Source</c>. Rendered at
/// 64×64 so it reads crisply when displayed at 16-32 px (titlebar, tray
/// preview); the Avalonia renderer downscales.
/// </summary>
public static Bitmap BuildBitmap()
{
const int size = 64;
var bmp = new RenderTargetBitmap(new PixelSize(size, size), new Vector(96, 96));
using (var ctx = bmp.CreateDrawingContext())
{
var brush = new SolidColorBrush(Color.FromArgb(255, 0xFF, 0xB9, 0x38));
ctx.DrawEllipse(brush, null, new Rect(2, 2, size - 4, size - 4));
var text = new FormattedText(
"译",
System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface("Microsoft YaHei UI, Segoe UI", FontStyle.Normal, FontWeight.Bold),
size * 0.62,
Brushes.White);
var origin = new Point((size - text.Width) / 2, (size - text.Height) / 2);
ctx.DrawText(text, origin);
}
return bmp;
}
/// <summary>
/// Same icon wrapped as a <see cref="WindowIcon"/> for use by
/// <c>Window.Icon</c> (taskbar / Alt-Tab) and <c>TrayIcon.Icon</c>.
/// </summary>
public static WindowIcon Build()
{
var bmp = BuildBitmap();
var ms = new MemoryStream();
bmp.Save(ms);
ms.Position = 0;
return new WindowIcon(ms);
}
}