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

STHApplication

【MAUI】寄当家——安卓端交易管理系统

公开
关注 0 Fork 0 Star 0
UTF-8
using Microsoft.Maui.Controls.Shapes;

namespace JiDangJia.Views;

public static class Ui
{
    public static readonly Color Ink=Color.FromArgb("#2B211A"),Gold=Color.FromArgb("#C9A468"),Bronze=Color.FromArgb("#A07A3A"),Paper=Color.FromArgb("#F7F4EE"),Muted=Color.FromArgb("#938A7D"),Line=Color.FromArgb("#EDE7DC"),Green=Color.FromArgb("#387363"),Red=Color.FromArgb("#B9514D"),Cream=Color.FromArgb("#F2EADA");
    public static LinearGradientBrush GoldBrush => new(new GradientStopCollection { new(Color.FromArgb("#F3E0B4"),0),new(Color.FromArgb("#D1AB69"),1) },new Point(0,0),new Point(1,1));
    public static Label Text(string text,double size=14,Color? color=null,bool bold=false) => new() { Text=text,FontSize=size,TextColor=color??Ink,FontAttributes=bold?FontAttributes.Bold:FontAttributes.None,VerticalTextAlignment=TextAlignment.Center };
    public static VerticalStackLayout Stack(params View[] views) { var s=new VerticalStackLayout { Spacing=12 }; foreach(var v in views) s.Add(v); return s; }
    public static HorizontalStackLayout Row(params View[] views) { var s=new HorizontalStackLayout { Spacing=10,VerticalOptions=LayoutOptions.Center }; foreach(var v in views) s.Add(v); return s; }
    public static Grid Spread(View left,View right)
    {
        var g=new Grid { ColumnDefinitions=[new(GridLength.Star),new(GridLength.Auto)],ColumnSpacing=10 };
        g.Add(left); g.Add(right,1); right.HorizontalOptions=LayoutOptions.End; return g;
    }
    public static Border Card(View content,int radius=20,Thickness? padding=null,Color? background=null,bool shadow=false) {var card=new Border { Content=content,Stroke=Line,StrokeThickness=.7,StrokeShape=new RoundRectangle { CornerRadius=radius },Padding=padding??new Thickness(18),BackgroundColor=background??Colors.White };if(shadow)card.Shadow=new Shadow { Brush=Ink,Opacity=.04f,Radius=14,Offset=new Point(0,4) };return card;}
    public static BoxView Divider() => new() { Color=Line,HeightRequest=1,Margin=new Thickness(0,4) };
    public static void PawnOutline(Border card,string state)
    {
        if(state is not ("OVERDUE" or "DEAD" or "DUE_SOON"))return;
        var color=state=="OVERDUE"?Red:state=="DEAD"?Ink:Gold;card.Stroke=color;card.StrokeThickness=state=="DUE_SOON"?1:1.5;
        if(state!="DUE_SOON")card.Shadow=new Shadow{Brush=color,Opacity=.24f,Radius=12,Offset=new Point(0,2)};
    }
    public static GraphicsView Icon(string name,Color? color=null,int size=24) => new() { Drawable=new IconDrawable(name,color??Bronze),WidthRequest=size,HeightRequest=size,VerticalOptions=LayoutOptions.Center,HorizontalOptions=LayoutOptions.Center,InputTransparent=true };
    public static View Tap(View view,Func<Task> action,string? description=null)
    {
        view.InputTransparent=false;
        var tap=new TapGestureRecognizer(); tap.Tapped+=async(_,_)=>{ if(!view.IsEnabled)return; view.IsEnabled=false; try { await action(); } catch(Exception ex) { if(Application.Current?.Windows.FirstOrDefault()?.Page is { } p) await p.DisplayAlertAsync("操作未完成",ex.Message,"知道了"); } finally {view.IsEnabled=true;} }; view.GestureRecognizers.Add(tap);
        if(description is not null) SemanticProperties.SetDescription(view,description); return view;
    }
    public static Button Button(string text,Func<Task> action,bool primary=true)
    {
        var b=new Button { Text=text,FontSize=14,FontAttributes=FontAttributes.Bold,TextColor=primary?Ink:Bronze,CornerRadius=14,HeightRequest=46,Padding=new Thickness(18,0),Background=primary?GoldBrush:new SolidColorBrush(Cream),BorderWidth=0 };
        b.Clicked+=async(_,_)=> { if(!b.IsEnabled)return; b.IsEnabled=false; try { await action(); } catch(Exception ex) { if(Application.Current?.Windows.FirstOrDefault()?.Page is { } p) await p.DisplayAlertAsync("操作未完成",ex.Message,"知道了"); } finally { b.IsEnabled=true; } }; return b;
    }
    public static Border Pill(string text,Color? textColor=null,Color? bg=null) => Card(Text(text,11,textColor??Bronze),8,new Thickness(9,4),bg??Cream);
    public static View Section(string title,string? trailing=null,Func<Task>? action=null)
    {
        var left=Row(new BoxView { Color=Gold,WidthRequest=3,HeightRequest=17,CornerRadius=2 },Text(title,17,bold:true));
        if(trailing is null)return left;
        View link=Text(trailing,12,Muted); if(action is not null)link=Tap(link,action);return Spread(left,link);
    }
    public static View Empty(string title,string hint,Func<Task>? action=null,string? button=null)
    {
        var icon=Card(Icon("archive",Gold,30),18,new Thickness(15),Paper); icon.HorizontalOptions=LayoutOptions.Center;
        var label=Text(title,16,bold:true); label.HorizontalTextAlignment=TextAlignment.Center;
        var sub=Text(hint,12,Muted); sub.HorizontalTextAlignment=TextAlignment.Center;
        var stack=Stack(icon,label,sub);stack.Padding=new Thickness(20,28);stack.Spacing=10;
        if(action is not null&&button is not null) {var b=Button(button,action);b.HorizontalOptions=LayoutOptions.Center;stack.Add(b);}return stack;
    }
}

public sealed class IconDrawable(string name,Color color) : IDrawable
{
    public void Draw(ICanvas c,RectF bounds)
    {
        c.SaveState();c.Scale(bounds.Width/24,bounds.Height/24);c.StrokeColor=color;c.FillColor=color;c.StrokeSize=1.65f;c.StrokeLineCap=LineCap.Round;c.StrokeLineJoin=LineJoin.Round;
        void Path(params float[] p) {var path=new PathF();path.MoveTo(p[0],p[1]);for(int i=2;i<p.Length;i+=2)path.LineTo(p[i],p[i+1]);c.DrawPath(path);}
        switch(name)
        {
            case "gem": Path(3,8,7,3,17,3,21,8,12,21,3,8,21,8);Path(7,3,10,8,12,21,14,8,17,3);break;
            case "camera":c.DrawRoundedRectangle(2,6,20,15,2);Path(7,6,9,3,15,3,17,6);c.DrawCircle(12,13,4);break;
            case "video":c.DrawRoundedRectangle(2,5,13,14,2);Path(15,9,22,5,22,19,15,15);break;
            case "home": Path(3,10,12,3,21,10);Path(5,9,5,21,19,21,19,9);Path(10,21,10,14,14,14,14,21);break;
            case "grid": c.DrawRoundedRectangle(3,3,7,7,1.8f);c.DrawRoundedRectangle(14,3,7,7,1.8f);c.DrawRoundedRectangle(3,14,7,7,1.8f);c.DrawRoundedRectangle(14,14,7,7,1.8f);break;
            case "archive": Path(3,7,12,3,21,7,21,18,12,22,3,18,3,7,12,11,21,7);Path(12,11,12,22);Path(7,5,16,9);break;
            case "finance": c.DrawCircle(12,12,9);Path(8,7,12,12,16,7);Path(8,12,16,12);Path(8,15,16,15);Path(12,12,12,19);break;
            case "people": c.DrawCircle(9,8,3);c.DrawCircle(18,9,2);Path(3,20,3,17,5,14,13,14,15,17,15,20);Path(17,15,20,15,22,18,22,20);break;
            case "person": c.DrawCircle(12,7,4);Path(4,21,4,18,7,14,17,14,20,18,20,21);break;
            case "bell": Path(5,17,7,14,7,9,9,5,15,5,17,9,17,14,19,17,5,17);Path(10,20,14,20);break;
            case "settings": c.DrawCircle(12,12,3);c.DrawCircle(12,12,8);for(int i=0;i<8;i++){float a=(float)(i*Math.PI/4);c.DrawLine(12+8*(float)Math.Cos(a),12+8*(float)Math.Sin(a),12+11*(float)Math.Cos(a),12+11*(float)Math.Sin(a));}break;
            case "search": c.DrawCircle(10,10,6);Path(14,14,21,21);break;
            case "plus": Path(5,12,19,12);Path(12,5,12,19);break;
            case "refresh": Path(19,4,19,9,14,9);Path(19,9,16,5,10,4,5,7,3,12,5,18,11,20,17,18,20,14);break;
            case "help": c.DrawCircle(12,12,9);Path(9,8,10,6,14,6,16,8,15,10,12,12,12,14);c.FillCircle(12,17,1);break;
            case "support": c.DrawArc(4,3,16,18,0,180,false,false);c.DrawRoundedRectangle(3,11,4,8,1.5f);c.DrawRoundedRectangle(17,11,4,8,1.5f);Path(19,19,16,22,12,22);break;
            case "clock": c.DrawCircle(12,12,9);Path(12,6,12,12,16,14);break;
            case "chevron": Path(9,5,16,12,9,19);break;
            case "phone": Path(6,3,3,6,4,12,9,18,15,21,19,21,21,17,17,14,14,17,10,14,7,10,10,7,7,3,6,3);break;
            case "shield": Path(12,2,21,6,20,15,17,19,12,22,7,19,4,15,3,6,12,2);Path(8,12,11,15,17,9);break;
            case "document": Path(5,2,15,2,20,7,20,22,5,22,5,2);Path(15,2,15,7,20,7);Path(8,12,17,12);Path(8,16,17,16);break;
            case "gold": Path(6,7,18,7,22,18,2,18,6,7);Path(7,11,17,11);break;
            default: c.DrawCircle(12,12,8);Path(8,12,16,12);break;
        }
        c.RestoreState();
    }
}