From 2e1b776bb6111ebb992b378a07d60cda3599988c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E5=8A=BC?= Date: Mon, 3 Jun 2024 16:11:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=20=E7=9B=B8=E8=BE=83=E7=94=BB?= =?UTF-8?q?=E5=B8=83=E7=9A=84=E6=9C=80=E5=B0=8F=E7=BC=A9=E6=94=BE=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=20=E4=BD=9C=E4=B8=BA=20property=20=E6=9A=B4=E9=9C=B2?= =?UTF-8?q?=E7=BB=99=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/Ursa.Demo/Pages/ImageViewerDemo.axaml | 3 +- src/Ursa/Controls/ImageViewer/ImageViewer.cs | 46 +++++++++++++++----- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/demo/Ursa.Demo/Pages/ImageViewerDemo.axaml b/demo/Ursa.Demo/Pages/ImageViewerDemo.axaml index 106de69..85db8e6 100644 --- a/demo/Ursa.Demo/Pages/ImageViewerDemo.axaml +++ b/demo/Ursa.Demo/Pages/ImageViewerDemo.axaml @@ -20,7 +20,8 @@ SetAndRaise(ScaleProperty, ref _scale, value); } + public static readonly DirectProperty MinScaleProperty = AvaloniaProperty.RegisterDirect( + nameof(MinScale), o => o.MinScale, (o, v) => o.MinScale = v, unsetValue: 0.1); + + public double MinScale + { + get => _minScale; + set => SetAndRaise(ScaleProperty, ref _minScale, value); + } + private double _minScale = 1; + private double _translateX; public static readonly DirectProperty TranslateXProperty = AvaloniaProperty.RegisterDirect( @@ -101,9 +111,7 @@ public class ImageViewer: TemplatedControl set => SetValue(StretchProperty, value); } - private double _minScale = 0.5; - - private const double defaultMinScale = 0.1; + private double _sourceMinScale = 0.1; static ImageViewer() { @@ -113,6 +121,7 @@ public class ImageViewer: TemplatedControl TranslateXProperty.Changed.AddClassHandler((o,e)=>o.OnTranslateXChanged(e)); TranslateYProperty.Changed.AddClassHandler((o, e) => o.OnTranslateYChanged(e)); StretchProperty.Changed.AddClassHandler((o, e) => o.OnStretchChanged(e)); + MinScaleProperty.Changed.AddClassHandler((o, e) => o.OnMinScaleChanged(e)); } private void OnTranslateYChanged(AvaloniaPropertyChangedEventArgs args) @@ -165,7 +174,7 @@ public class ImageViewer: TemplatedControl _image.Height = size.Height; } Scale = GetScaleRatio(width/size.Width, height/size.Height, this.Stretch); - _minScale = Math.Min(width * defaultMinScale / size.Width, height * defaultMinScale / size.Height); + _sourceMinScale = Math.Min(width * MinScale / size.Width, height * MinScale / size.Height); } private void OnStretchChanged(AvaloniaPropertyChangedEventArgs args) @@ -174,14 +183,31 @@ public class ImageViewer: TemplatedControl Scale = GetScaleRatio(Width / _image!.Width, Height / _image!.Height, stretch); if(_image is { }) { - _minScale = Math.Min(Width * defaultMinScale / _image.Width, Height * defaultMinScale / _image.Height); + _sourceMinScale = Math.Min(Width * MinScale / _image.Width, Height * MinScale / _image.Height); } else { - _minScale = defaultMinScale; + _sourceMinScale = MinScale; } } - + + private void OnMinScaleChanged(AvaloniaPropertyChangedEventArgs args) + { + var newMinScale = args.GetNewValue(); + if (newMinScale > Scale) + { + Scale = newMinScale; + } + if (_image is { }) + { + _sourceMinScale = Math.Min(Width * MinScale / _image.Width, Height * MinScale / _image.Height); + } + else + { + _sourceMinScale = MinScale; + } + } + private double GetScaleRatio(double widthRatio, double heightRatio, Stretch stretch) { return stretch switch @@ -216,11 +242,11 @@ public class ImageViewer: TemplatedControl _image.Width = size.Width; _image.Height = size.Height; Scale = GetScaleRatio(width/size.Width, height/size.Height, this.Stretch); - _minScale = Math.Min(width * defaultMinScale / size.Width, height * defaultMinScale / size.Height); + _sourceMinScale = Math.Min(width * MinScale / size.Width, height * MinScale / size.Height); } else { - _minScale = defaultMinScale; + _sourceMinScale = MinScale; } } @@ -236,7 +262,7 @@ public class ImageViewer: TemplatedControl { var scale = Scale; scale /= 1.1; - if (scale < _minScale) scale = _minScale; + if (scale < _sourceMinScale) scale = _sourceMinScale; Scale = scale; } }