M6 Windows: composizione condivisa Febe.App per Server e Desktop; Febe per Windows (WPF) con l'interfaccia in WebView2, area di notifica, istanza unica, «Browser di Febe» su WebView2 con CDP filtrato (WebView2CdpSession) e ripieghi di S2 (composizione, Edge), guscio nativo, avvisi, spostamento verificato della cartella dei dati, icona del marchio; un solo Febe.exe, installatore NSIS e zip portabile; guida docs/windows.md e prove dal vivo S1–S3
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
This commit is contained in:
73 files changed
+7914
-699
No files matched your search
@@ -0,0 +1,246 @@
|
||||
using System.Buffers.Binary;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Febe.Desktop.Core;
|
||||
|
||||
/// <summary>
|
||||
/// The brand mark of appendix C — a rounded square in the accent colour with a ring and a dot, a body and its moon —
|
||||
/// drawn with SkiaSharp at any size, the same way the SVG describes it:
|
||||
/// <code>
|
||||
/// <rect width="32" height="32" rx="8" fill="#0F5C8C"/>
|
||||
/// <circle cx="14.5" cy="17.5" r="7.4" fill="none" stroke="#FFFFFF" stroke-width="2.6"/>
|
||||
/// <circle cx="23.2" cy="8.8" r="3.1" fill="#FFFFFF"/>
|
||||
/// </code>
|
||||
/// </summary>
|
||||
public static class BrandMark
|
||||
{
|
||||
/// <summary>The sizes of the Windows icon: window, notification area, taskbar, Explorer, installer.</summary>
|
||||
public static IReadOnlyList<int> IconSizes { get; } = [16, 24, 32, 48, 64, 128, 256];
|
||||
|
||||
private static readonly SKColor Accent = new(0x0F, 0x5C, 0x8C);
|
||||
|
||||
/// <summary>The mark as a square bitmap of <paramref name="size"/> pixels, premultiplied BGRA, transparent corners.</summary>
|
||||
public static SKBitmap Render(int size)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(size, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfGreaterThan(size, 1024);
|
||||
var bitmap = new SKBitmap(new SKImageInfo(size, size, SKColorType.Bgra8888, SKAlphaType.Premul));
|
||||
using var canvas = new SKCanvas(bitmap);
|
||||
canvas.Clear(SKColors.Transparent);
|
||||
var scale = size / 32f;
|
||||
canvas.Scale(scale);
|
||||
using (var square = new SKPaint { Color = Accent, IsAntialias = true, Style = SKPaintStyle.Fill })
|
||||
{
|
||||
canvas.DrawRoundRect(new SKRect(0, 0, 32, 32), 8, 8, square);
|
||||
}
|
||||
|
||||
using (var ring = new SKPaint { Color = SKColors.White, IsAntialias = true, Style = SKPaintStyle.Stroke, StrokeWidth = 2.6f })
|
||||
{
|
||||
canvas.DrawCircle(14.5f, 17.5f, 7.4f, ring);
|
||||
}
|
||||
|
||||
using (var moon = new SKPaint { Color = SKColors.White, IsAntialias = true, Style = SKPaintStyle.Fill })
|
||||
{
|
||||
canvas.DrawCircle(23.2f, 8.8f, 3.1f, moon);
|
||||
}
|
||||
|
||||
canvas.Flush();
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
/// <summary>The multi-size Windows icon (<see cref="IconSizes"/>) of the mark, as the bytes of an .ico file.</summary>
|
||||
public static byte[] RenderIcon()
|
||||
{
|
||||
var images = new List<SKBitmap>();
|
||||
try
|
||||
{
|
||||
foreach (var size in IconSizes)
|
||||
{
|
||||
images.Add(Render(size));
|
||||
}
|
||||
|
||||
return IcoWriter.Write(images);
|
||||
}
|
||||
finally
|
||||
{
|
||||
foreach (var image in images)
|
||||
{
|
||||
image.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes Windows .ico files by hand (no package for it, spec §3): the 256-pixel image as PNG, the smaller ones as
|
||||
/// 32-bit DIBs (BGRA with alpha, bottom-up, plus the 1-bit AND mask), which every part of Windows reads — window icons,
|
||||
/// the notification area, Explorer, NSIS.
|
||||
/// </summary>
|
||||
public static class IcoWriter
|
||||
{
|
||||
/// <summary>Images at or above this size are stored as PNG (the Vista format); smaller ones as DIB.</summary>
|
||||
public const int PngFrom = 256;
|
||||
|
||||
private const int DirectoryHeader = 6;
|
||||
private const int DirectoryEntry = 16;
|
||||
private const int BitmapInfoHeader = 40;
|
||||
|
||||
/// <summary>The .ico bytes of square <paramref name="images"/> (1 to 256 pixels), in the order given.</summary>
|
||||
public static byte[] Write(IReadOnlyList<SKBitmap> images)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(images);
|
||||
if (images.Count is 0 or > ushort.MaxValue)
|
||||
{
|
||||
throw new ArgumentException("Un'icona contiene da 1 a 65535 immagini.", nameof(images));
|
||||
}
|
||||
|
||||
var payloads = new List<byte[]>(images.Count);
|
||||
foreach (var image in images)
|
||||
{
|
||||
if (image.Width != image.Height || image.Width is < 1 or > 256)
|
||||
{
|
||||
throw new ArgumentException("Le immagini di un'icona sono quadrate, da 1 a 256 pixel.", nameof(images));
|
||||
}
|
||||
|
||||
payloads.Add(image.Width >= PngFrom ? Png(image) : Dib(image));
|
||||
}
|
||||
|
||||
using var output = new MemoryStream();
|
||||
Span<byte> header = stackalloc byte[DirectoryHeader];
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(header, 0); // reserved
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(header[2..], 1); // 1 = icon
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(header[4..], (ushort)images.Count);
|
||||
output.Write(header);
|
||||
|
||||
var offset = DirectoryHeader + (DirectoryEntry * images.Count);
|
||||
Span<byte> entry = stackalloc byte[DirectoryEntry];
|
||||
for (var i = 0; i < images.Count; i++)
|
||||
{
|
||||
var size = images[i].Width;
|
||||
entry.Clear();
|
||||
entry[0] = (byte)(size >= 256 ? 0 : size); // 0 means 256
|
||||
entry[1] = (byte)(size >= 256 ? 0 : size);
|
||||
entry[2] = 0; // no palette
|
||||
entry[3] = 0; // reserved
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(entry[4..], 1); // colour planes
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(entry[6..], 32); // bits per pixel
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(entry[8..], (uint)payloads[i].Length);
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(entry[12..], (uint)offset);
|
||||
output.Write(entry);
|
||||
offset += payloads[i].Length;
|
||||
}
|
||||
|
||||
foreach (var payload in payloads)
|
||||
{
|
||||
output.Write(payload);
|
||||
}
|
||||
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>Reads the directory of an .ico file: size, bytes, whether the image is PNG (tests, checks).</summary>
|
||||
public static IReadOnlyList<IcoEntry> ReadDirectory(ReadOnlySpan<byte> ico)
|
||||
{
|
||||
if (ico.Length < DirectoryHeader || BinaryPrimitives.ReadUInt16LittleEndian(ico) != 0 || BinaryPrimitives.ReadUInt16LittleEndian(ico[2..]) != 1)
|
||||
{
|
||||
throw new InvalidDataException("Non è un file .ico.");
|
||||
}
|
||||
|
||||
var count = BinaryPrimitives.ReadUInt16LittleEndian(ico[4..]);
|
||||
var entries = new List<IcoEntry>(count);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var e = ico.Slice(DirectoryHeader + (i * DirectoryEntry), DirectoryEntry);
|
||||
var size = e[0] == 0 ? 256 : e[0];
|
||||
var length = (int)BinaryPrimitives.ReadUInt32LittleEndian(e[8..]);
|
||||
var offset = (int)BinaryPrimitives.ReadUInt32LittleEndian(e[12..]);
|
||||
if (offset < 0 || length < 0 || offset + length > ico.Length)
|
||||
{
|
||||
throw new InvalidDataException("Immagine fuori dal file .ico.");
|
||||
}
|
||||
|
||||
var data = ico.Slice(offset, length);
|
||||
var png = data.Length >= 8 && data[..8].SequenceEqual(PngSignature);
|
||||
entries.Add(new IcoEntry(size, BinaryPrimitives.ReadUInt16LittleEndian(e[6..]), offset, length, png));
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
|
||||
/// <summary>The image of an entry as a bitmap (DIB or PNG), for comparisons.</summary>
|
||||
public static SKBitmap Decode(ReadOnlySpan<byte> ico, IcoEntry entry)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(entry);
|
||||
var data = ico.Slice(entry.Offset, entry.Length);
|
||||
if (entry.IsPng)
|
||||
{
|
||||
return SKBitmap.Decode(data.ToArray()) ?? throw new InvalidDataException("PNG dell'icona non leggibile.");
|
||||
}
|
||||
|
||||
var size = entry.Size;
|
||||
var bitmap = new SKBitmap(new SKImageInfo(size, size, SKColorType.Bgra8888, SKAlphaType.Unpremul));
|
||||
var pixels = data[BitmapInfoHeader..];
|
||||
for (var y = 0; y < size; y++)
|
||||
{
|
||||
var row = pixels.Slice((size - 1 - y) * size * 4, size * 4);
|
||||
for (var x = 0; x < size; x++)
|
||||
{
|
||||
bitmap.SetPixel(x, y, new SKColor(row[(x * 4) + 2], row[(x * 4) + 1], row[x * 4], row[(x * 4) + 3]));
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
private static ReadOnlySpan<byte> PngSignature => [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A];
|
||||
|
||||
private static byte[] Png(SKBitmap image)
|
||||
{
|
||||
using var data = image.Encode(SKEncodedImageFormat.Png, 100) ?? throw new InvalidOperationException("Codifica PNG non riuscita.");
|
||||
return data.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>A 32-bit DIB: BITMAPINFOHEADER with twice the height (image + mask), BGRA rows bottom-up, AND mask.</summary>
|
||||
private static byte[] Dib(SKBitmap image)
|
||||
{
|
||||
var size = image.Width;
|
||||
var maskStride = ((size + 31) / 32) * 4;
|
||||
var pixelBytes = size * size * 4;
|
||||
var result = new byte[BitmapInfoHeader + pixelBytes + (maskStride * size)];
|
||||
var span = result.AsSpan();
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(span, BitmapInfoHeader);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(span[4..], size);
|
||||
BinaryPrimitives.WriteInt32LittleEndian(span[8..], size * 2);
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(span[12..], 1); // planes
|
||||
BinaryPrimitives.WriteUInt16LittleEndian(span[14..], 32); // bits per pixel
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(span[16..], 0); // BI_RGB
|
||||
BinaryPrimitives.WriteUInt32LittleEndian(span[20..], (uint)(pixelBytes + (maskStride * size)));
|
||||
|
||||
var pixels = span[BitmapInfoHeader..];
|
||||
var mask = span[(BitmapInfoHeader + pixelBytes)..];
|
||||
for (var y = 0; y < size; y++)
|
||||
{
|
||||
var row = pixels.Slice((size - 1 - y) * size * 4, size * 4);
|
||||
for (var x = 0; x < size; x++)
|
||||
{
|
||||
// GetPixel gives straight (not premultiplied) alpha, as icons want it.
|
||||
var c = image.GetPixel(x, y);
|
||||
row[x * 4] = c.Blue;
|
||||
row[(x * 4) + 1] = c.Green;
|
||||
row[(x * 4) + 2] = c.Red;
|
||||
row[(x * 4) + 3] = c.Alpha;
|
||||
if (c.Alpha == 0)
|
||||
{
|
||||
// Transparent pixels are also set in the AND mask, for the readers that ignore the alpha channel.
|
||||
var maskRow = mask.Slice((size - 1 - y) * maskStride, maskStride);
|
||||
maskRow[x / 8] |= (byte)(0x80 >> (x % 8));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One image of an .ico file.</summary>
|
||||
public sealed record IcoEntry(int Size, int BitsPerPixel, int Offset, int Length, bool IsPng);
|
||||
Reference in new issue
Block a user