diff --git a/AGENTS.md b/AGENTS.md index 2965975..a25d1dd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -120,6 +120,8 @@ a screenshot of each changed view: compiling is not working. ## Tools +- `tools/icon/make_icon.py`: renders the shared app icon with development-only Pillow 11.3.0. + Run `python -B tools/icon/make_icon.py`; test with `python -B tools/icon/test_make_icon.py`. - `tools/docs/check_docs.py`: the documentation check; its tests are `python -B tools/docs/test_check_docs.py`. - `tools/gitea.sh`: calls the Gitea API with the stored git credential or `GITEA_TOKEN`. diff --git a/src/FinalToken.App/AppIcon.cs b/src/FinalToken.App/AppIcon.cs new file mode 100644 index 0000000..070b5c1 --- /dev/null +++ b/src/FinalToken.App/AppIcon.cs @@ -0,0 +1,15 @@ +using Avalonia.Controls; +using Avalonia.Platform; + +namespace FinalToken.App; + +public static class AppIcon +{ + // Keep the ICO's multiple sizes on Windows; X11 uses the same artwork as a PNG (D8). + public static WindowIcon Load() + { + var extension = OperatingSystem.IsWindows() ? "ico" : "png"; + using var stream = AssetLoader.Open(new Uri($"avares://FinalToken/Assets/FinalToken.{extension}")); + return new WindowIcon(stream); + } +} diff --git a/src/FinalToken.App/Assets/FinalToken.ico b/src/FinalToken.App/Assets/FinalToken.ico new file mode 100644 index 0000000..e64dd3d Binary files /dev/null and b/src/FinalToken.App/Assets/FinalToken.ico differ diff --git a/src/FinalToken.App/Assets/FinalToken.png b/src/FinalToken.App/Assets/FinalToken.png new file mode 100644 index 0000000..5bc8312 Binary files /dev/null and b/src/FinalToken.App/Assets/FinalToken.png differ diff --git a/src/FinalToken.App/DesktopApp.cs b/src/FinalToken.App/DesktopApp.cs index 8da5ca7..9824f35 100644 --- a/src/FinalToken.App/DesktopApp.cs +++ b/src/FinalToken.App/DesktopApp.cs @@ -1,9 +1,6 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; -using Avalonia.Media; -using Avalonia.Media.Imaging; -using Avalonia.Themes.Fluent; using Avalonia.Threading; using FinalToken.Core; using FinalToken.Core.StreamDeck; @@ -75,11 +72,7 @@ public sealed class DesktopApp : Application var menu = new NativeMenu(); foreach (var (label, action) in new (string, Action)[] { ("Open", OpenPanel), ("Refresh now", () => Run(Refresh)), ("Settings", OpenSettings), ("Quit", () => Run(Quit)) }) { var item = new NativeMenuItem(label); item.Click += (_, _) => action(); menu.Items.Add(item); } - using var bitmap = new RenderTargetBitmap(new PixelSize(32, 32)); - using (var context = bitmap.CreateDrawingContext()) - { context.DrawRectangle(Brush.Parse("#3878D4"), null, new Rect(0, 0, 32, 32), 7, 7); context.DrawLine(new Pen(Brushes.White, 4), new(9, 9), new(23, 9)); context.DrawLine(new Pen(Brushes.White, 4), new(16, 9), new(16, 25)); } - using var stream = new MemoryStream(); bitmap.Save(stream, PngBitmapEncoderOptions.Default); stream.Position = 0; - tray = new TrayIcon { Icon = new WindowIcon(stream), ToolTipText = "FinalToken", Menu = menu, IsVisible = true }; + tray = new TrayIcon { Icon = AppIcon.Load(), ToolTipText = "FinalToken", Menu = menu, IsVisible = true }; tray.Clicked += (_, _) => OpenPanel(); TrayIcon.SetIcons(this, [tray]); } diff --git a/src/FinalToken.App/FinalToken.App.csproj b/src/FinalToken.App/FinalToken.App.csproj index 9704ede..c2c2124 100644 --- a/src/FinalToken.App/FinalToken.App.csproj +++ b/src/FinalToken.App/FinalToken.App.csproj @@ -5,8 +5,10 @@ true true true + Assets/FinalToken.ico + diff --git a/src/FinalToken.App/PanelWindow.cs b/src/FinalToken.App/PanelWindow.cs index b4d2c99..058757e 100644 --- a/src/FinalToken.App/PanelWindow.cs +++ b/src/FinalToken.App/PanelWindow.cs @@ -29,6 +29,7 @@ public sealed class PanelWindow : Window { this.model = model; this.clock = clock ?? TimeProvider.System; + Icon = AppIcon.Load(); Title = $"FinalToken {typeof(Program).Assembly.GetName().Version?.ToString(3)}"; Width = 1280; Height = 900; MinWidth = 960; MinHeight = 500; Ui.Theme(this); var brand = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 8, VerticalAlignment = VerticalAlignment.Center }; diff --git a/src/FinalToken.App/SettingsWindow.cs b/src/FinalToken.App/SettingsWindow.cs index ee2c654..0ab765b 100644 --- a/src/FinalToken.App/SettingsWindow.cs +++ b/src/FinalToken.App/SettingsWindow.cs @@ -10,6 +10,7 @@ public sealed class SettingsWindow : Window { public SettingsWindow(Settings initial, PluginInstallStatus status, Func save, Action? quit = null, Action? openFolder = null) { + Icon = AppIcon.Load(); Title = "FinalToken settings"; Width = 760; Height = 900; MinWidth = 600; MinHeight = 500; Ui.Theme(this); var body = new StackPanel { Spacing = 16, Margin = new Thickness(24) }; body.Children.Add(Ui.Text("Settings", 24)); diff --git a/tools/icon/make_icon.py b/tools/icon/make_icon.py new file mode 100644 index 0000000..7997726 --- /dev/null +++ b/tools/icon/make_icon.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +"""D8: render the shared usage gauge. Requires development-only Pillow 11.3.0.""" +import argparse +from pathlib import Path + +from PIL import Image, ImageDraw + +SIZES = (16, 20, 24, 32, 40, 48, 64, 128, 256) +ASSETS = Path(__file__).resolve().parents[2] / 'src' / 'FinalToken.App' / 'Assets' + + +def render(size): + scale = size * 8 + image = Image.new('RGBA', (scale, scale)) + draw = ImageDraw.Draw(image) + + def box(a, b, c, d): + return tuple(round(n * scale) for n in (a, b, c, d)) + + draw.rounded_rectangle(box(.025, .025, .975, .975), radius=round(.20 * scale), + fill='#1a1a19', outline='#52514e', width=round(.025 * scale)) + gauge = box(.17, .17, .83, .83) + width = round(.115 * scale) + draw.arc(gauge, 135, 405, fill='#383835', width=width) + draw.arc(gauge, 135, 265, fill='#d95926', width=width) + draw.arc(gauge, 267, 353, fill='#3987e5', width=width) + # One chunky pointer survives the 16 px downsample; there are no small-size hairlines. + draw.line(box(.50, .53, .64, .37), fill='#f0efec', width=round(.075 * scale)) + draw.ellipse(box(.445, .475, .555, .585), fill='#f0efec') + return image.resize((size, size), Image.Resampling.LANCZOS) + + +def write_assets(output): + output.mkdir(parents=True, exist_ok=True) + images = [render(size) for size in SIZES] + images[-1].save(output / 'FinalToken.ico', sizes=[(s, s) for s in SIZES], append_images=images[:-1]) + images[-1].save(output / 'FinalToken.png') + + +def contact_sheet(path): + sizes = (16, 24, 32, 48, 256) + sheet = Image.new('RGB', (512, 640)) + draw = ImageDraw.Draw(sheet) + for column, background in enumerate(('#f9f9f7', '#0d0d0d')): + left = column * 256 + draw.rectangle((left, 0, left + 255, 639), fill=background) + y = 24 + for size in sizes: + icon = render(size) + sheet.paste(icon, (left + (256 - size) // 2, y), icon) + y += size + 32 + path.parent.mkdir(parents=True, exist_ok=True) + sheet.save(path) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--out', type=Path, default=ASSETS) + parser.add_argument('--contact-sheet', type=Path) + args = parser.parse_args() + write_assets(args.out) + if args.contact_sheet: + contact_sheet(args.contact_sheet) + + +if __name__ == '__main__': + main() diff --git a/tools/icon/test_make_icon.py b/tools/icon/test_make_icon.py new file mode 100644 index 0000000..56ceb9f --- /dev/null +++ b/tools/icon/test_make_icon.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +"""Check all ICO frames and reproducible committed icon assets.""" +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +from PIL import Image +from make_icon import ASSETS, SIZES, render + + +class IconTests(unittest.TestCase): + def test_ico_contains_every_size_and_its_rendered_pixels(self): + with Image.open(ASSETS / 'FinalToken.ico') as icon: + self.assertEqual({(s, s) for s in SIZES}, icon.ico.sizes()) + for size in SIZES: + frame = icon.ico.getimage((size, size)) + self.assertEqual(render(size).tobytes(), frame.tobytes()) + + def test_fresh_run_reproduces_committed_files_byte_for_byte(self): + with tempfile.TemporaryDirectory() as temporary: + subprocess.run([sys.executable, '-B', str(Path(__file__).with_name('make_icon.py')), + '--out', temporary], check=True) + for name in ('FinalToken.ico', 'FinalToken.png'): + self.assertEqual((ASSETS / name).read_bytes(), (Path(temporary) / name).read_bytes(), name) + + +if __name__ == '__main__': + unittest.main()