From 7e357bbc40de6100e7f7faa77f21e830256e54b9 Mon Sep 17 00:00:00 2001 From: tfarcraw <123211521@163.com> Date: Tue, 10 Nov 2026 11:35:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E6=A0=87=E6=B3=A8=E6=A1=86=E4=B8=8A?= =?UTF-8?q?=E6=96=B9=E6=98=BE=E7=A4=BA=E9=94=81=E5=AE=9A=E7=9A=84=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E9=9B=B6=E4=BB=B6=E5=90=8D=E7=A7=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Training/AnnotationCanvas.cs | 36 +++++++++++++++++-- .../Training/TrainingPage.axaml | 2 +- tests/Avalonia.Smoke/Program.cs | 6 +++- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs b/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs index 3bd8fe7..21a92d3 100644 --- a/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs +++ b/src/SopHelper.Avalonia/Training/AnnotationCanvas.cs @@ -2,6 +2,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Media; +using System.Globalization; using SopHelper.Presentation.Imaging; namespace SopHelper.Training; @@ -9,14 +10,16 @@ public sealed class AnnotationCanvas : Control { public static readonly StyledProperty SourceProperty=AvaloniaProperty.Register(nameof(Source)); public static readonly StyledProperty BoxProperty=AvaloniaProperty.Register(nameof(Box)); + public static readonly StyledProperty TargetNameProperty=AvaloniaProperty.Register(nameof(TargetName), ""); public BitmapSource? Source {get=>GetValue(SourceProperty);set=>SetValue(SourceProperty,value);} public double[]? Box {get=>GetValue(BoxProperty);set=>SetValue(BoxProperty,value);} + public string TargetName {get=>GetValue(TargetNameProperty);set=>SetValue(TargetNameProperty,value);} public event Action? BoxDrawn; private Point? _start;private Point _end;private IPointer? _pointer; private double _zoom=1; private Vector _offset; private Point? _panStart; - static AnnotationCanvas(){AffectsRender(SourceProperty,BoxProperty);} + static AnnotationCanvas(){AffectsRender(SourceProperty,BoxProperty,TargetNameProperty);} public AnnotationCanvas(){ClipToBounds=true;Cursor=new Cursor(StandardCursorType.Cross);Focusable=true;} private Rect ImageRect { @@ -54,8 +57,35 @@ public sealed class AnnotationCanvas : Control dc.DrawRectangle(Brushes.Transparent,null,new Rect(Bounds.Size));var image=ImageRect;if(Source is null||image.Width<=0)return; dc.DrawImage(Source,image);var pen=new Pen(new SolidColorBrush(Color.FromRgb(0,220,220)),2); if(Box is { Length:4 } b) - {var scale=image.Width/Source.PixelWidth;dc.DrawRectangle(new SolidColorBrush(Color.FromArgb(30,0,220,220)),pen,new Rect(image.X+b[0]*scale,image.Y+b[1]*scale,(b[2]-b[0])*scale,(b[3]-b[1])*scale));} - if(_start is {} start)dc.DrawRectangle(null,new Pen(Brushes.Yellow,2),DragRect(start,_end)); + { + var scale=image.Width/Source.PixelWidth; + var rect=new Rect(image.X+b[0]*scale,image.Y+b[1]*scale,(b[2]-b[0])*scale,(b[3]-b[1])*scale); + dc.DrawRectangle(new SolidColorBrush(Color.FromArgb(30,0,220,220)),pen,rect); + if(_start is null)DrawTargetName(dc,rect,pen.Brush!); + } + if(_start is {} start) + { + var rect=DragRect(start,_end); + dc.DrawRectangle(null,new Pen(Brushes.Yellow,2),rect); + DrawTargetName(dc,rect,Brushes.Yellow); + } + } + private void DrawTargetName(DrawingContext dc,Rect box,IBrush foreground) + { + if(string.IsNullOrWhiteSpace(TargetName)||Bounds.Width<=8||Bounds.Height<=8|| + box.Right<0||box.Left>Bounds.Width||box.Bottom<0||box.Top>Bounds.Height)return; + const double padding=4; + var text=new FormattedText(TargetName,CultureInfo.CurrentCulture,FlowDirection.LeftToRight, + new Typeface("Microsoft YaHei, Noto Sans CJK SC"),14,foreground) + {MaxTextWidth=Math.Max(1,Bounds.Width-2*padding),MaxLineCount=1,Trimming=TextTrimming.CharacterEllipsis}; + var size=new Size(text.Width+2*padding,text.Height+2*padding); + var left=Math.Clamp(box.Left,0,Math.Max(0,Bounds.Width-size.Width)); + // Keep the label visible when the box touches the top edge or is panned partly off-screen. + var top=box.Top-size.Height-3; + if(top<0)top=box.Top+3; + top=Math.Clamp(top,0,Math.Max(0,Bounds.Height-size.Height)); + dc.DrawRectangle(new SolidColorBrush(Color.FromArgb(225,12,18,24)),null,new Rect(new Point(left,top),size)); + dc.DrawText(text,new Point(left+padding,top+padding)); } protected override void OnPointerPressed(PointerPressedEventArgs e) { diff --git a/src/SopHelper.Avalonia/Training/TrainingPage.axaml b/src/SopHelper.Avalonia/Training/TrainingPage.axaml index 9576287..8021a69 100644 --- a/src/SopHelper.Avalonia/Training/TrainingPage.axaml +++ b/src/SopHelper.Avalonia/Training/TrainingPage.axaml @@ -56,7 +56,7 @@ - +