XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESystem

【Rust】XFE系统,基于Rust和汇编

公开
关注 0 Fork 0 Star 0
UTF-8
namespace XFESystem.Desktop;

/// <summary>A quiet, theme-derived wallpaper drawn directly at the current display size.</summary>
public static class DesktopWallpaper
{
    public static uint RowColor(DesktopTheme theme, int y, int height)
    {
        // Integer interpolation gives one color per scanline and no large
        // bitmap, random texture, blur pass or per-frame image allocation.
        int strength = theme.IsDark ? 16 : 8;
        long denominator = 100L * Math.Max(1, height - 1);
        long weight = (long)strength * Math.Max(0, height - 1 - Math.Clamp(y, 0, Math.Max(0, height - 1)));
        uint result = 0;
        for (int shift = 0; shift <= 16; shift += 8)
            result |= (uint)(((theme.Background >> shift & 255) * (denominator - weight) +
                              (theme.Accent >> shift & 255) * weight + denominator / 2) / denominator) << shift;
        return result;
    }

    public static void Draw(IDesktopCanvas canvas, DesktopTheme theme, DesktopBounds damage, int workHeight)
    {
        int top = Math.Clamp(damage.Y, 0, canvas.Height);
        int bottom = Math.Clamp(damage.Y + damage.Height, top, canvas.Height);
        int left = Math.Clamp(damage.X, 0, canvas.Width);
        int width = Math.Clamp(damage.Width, 0, canvas.Width - left);
        // Coalesce equal adjacent colors so even a full-screen restore makes
        // only a small number of clipped fills.
        for (int y = top; y < bottom;)
        {
            uint color = RowColor(theme, y, workHeight);
            int end = y + 1;
            while (end < bottom && RowColor(theme, end, workHeight) == color) end++;
            canvas.Fill(color, left, y, width, end - y);
            y = end;
        }
        const string name = "XFESystem";
        uint ink = DesktopTheme.Mix(theme.Background, theme.IsDark ? theme.AccentText : theme.Text, theme.IsDark ? 42 : 52);
        if (canvas is IDesktopWebCanvas web)
        {
            const double size = 44;
            double measured = web.MeasureWebText(name, size, "Lato, sans-serif", 700);
            web.WebText(name, ink, Math.Max(132, (canvas.Width - measured) / 2), Math.Max(16, workHeight / 2d - 25), size, "Lato, sans-serif", 700);
        }
        else canvas.Text(name, ink, Math.Max(132, (canvas.Width - canvas.TextWidth(name)) / 2), Math.Max(16, workHeight / 2 - 8));
    }
}