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 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 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? 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? 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