From c6262c0ebdcbd53b6fdff3495068adfa9af6f8da Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Tue, 1 Sep 2020 00:50:21 +0800 Subject: [PATCH] fixed #441 --- .../Block/{ => RunningBlock}/RunningBlock.cs | 47 ++++++++------ .../Block/RunningBlock/RunningBorder.cs | 65 +++++++++++++++++++ .../HandyControl_Shared.projitems | 3 +- .../Styles/Base/RunningBlockBaseStyle.xaml | 4 +- 4 files changed, 96 insertions(+), 23 deletions(-) rename src/Shared/HandyControl_Shared/Controls/Block/{ => RunningBlock}/RunningBlock.cs (82%) create mode 100644 src/Shared/HandyControl_Shared/Controls/Block/RunningBlock/RunningBorder.cs diff --git a/src/Shared/HandyControl_Shared/Controls/Block/RunningBlock.cs b/src/Shared/HandyControl_Shared/Controls/Block/RunningBlock/RunningBlock.cs similarity index 82% rename from src/Shared/HandyControl_Shared/Controls/Block/RunningBlock.cs rename to src/Shared/HandyControl_Shared/Controls/Block/RunningBlock/RunningBlock.cs index 7020e843..fc7e905c 100644 --- a/src/Shared/HandyControl_Shared/Controls/Block/RunningBlock.cs +++ b/src/Shared/HandyControl_Shared/Controls/Block/RunningBlock/RunningBlock.cs @@ -1,7 +1,6 @@ using System; using System.Windows; using System.Windows.Controls; -using System.Windows.Media; using System.Windows.Media.Animation; using HandyControl.Data; using HandyControl.Expression.Drawing; @@ -24,19 +23,32 @@ namespace HandyControl.Controls public override void OnApplyTemplate() { + if (_elementPanel != null) + { + _elementPanel.SizeChanged -= ElementPanel_SizeChanged; + } + base.OnApplyTemplate(); _elementContent = GetTemplateChild(ElementContent) as FrameworkElement; _elementPanel = GetTemplateChild(ElementPanel) as Panel; + + if (_elementPanel != null) + { + _elementPanel.SizeChanged += ElementPanel_SizeChanged; + } + UpdateContent(); } + private void ElementPanel_SizeChanged(object sender, SizeChangedEventArgs e) => UpdateContent(); + public static readonly DependencyProperty RunawayProperty = DependencyProperty.Register( "Runaway", typeof(bool), typeof(RunningBlock), new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.AffectsRender)); public bool Runaway { - get => (bool) GetValue(RunawayProperty); + get => (bool)GetValue(RunawayProperty); set => SetValue(RunawayProperty, ValueBoxes.BooleanBox(value)); } @@ -45,7 +57,7 @@ namespace HandyControl.Controls public bool AutoRun { - get => (bool) GetValue(AutoRunProperty); + get => (bool)GetValue(AutoRunProperty); set => SetValue(AutoRunProperty, ValueBoxes.BooleanBox(value)); } @@ -54,7 +66,7 @@ namespace HandyControl.Controls public Orientation Orientation { - get => (Orientation) GetValue(OrientationProperty); + get => (Orientation)GetValue(OrientationProperty); set => SetValue(OrientationProperty, value); } @@ -63,7 +75,7 @@ namespace HandyControl.Controls public Duration Duration { - get => (Duration) GetValue(DurationProperty); + get => (Duration)GetValue(DurationProperty); set => SetValue(DurationProperty, value); } @@ -72,7 +84,7 @@ namespace HandyControl.Controls public double Speed { - get => (double) GetValue(SpeedProperty); + get => (double)GetValue(SpeedProperty); set => SetValue(SpeedProperty, value); } @@ -93,7 +105,7 @@ namespace HandyControl.Controls public bool IsRunning { - get => (bool) GetValue(IsRunningProperty); + get => (bool)GetValue(IsRunningProperty); set => SetValue(IsRunningProperty, ValueBoxes.BooleanBox(value)); } @@ -102,60 +114,57 @@ namespace HandyControl.Controls public bool AutoReverse { - get => (bool) GetValue(AutoReverseProperty); + get => (bool)GetValue(AutoReverseProperty); set => SetValue(AutoReverseProperty, ValueBoxes.BooleanBox(value)); } private void UpdateContent() { if (_elementContent == null || _elementPanel == null) return; + if (MathHelper.IsZero(_elementPanel.ActualWidth) || MathHelper.IsZero(_elementPanel.ActualHeight)) return; _storyboard?.Stop(); - _elementPanel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); - _elementPanel.Width = _elementPanel.DesiredSize.Width; - _elementPanel.Height = _elementPanel.DesiredSize.Height; - double from; double to; PropertyPath propertyPath; if (Orientation == Orientation.Horizontal) { - if (AutoRun && _elementPanel.Width < ActualWidth) + if (AutoRun && _elementPanel.ActualWidth < ActualWidth) { return; } if (Runaway) { - from = -_elementPanel.Width; + from = -_elementPanel.ActualWidth; to = ActualWidth; } else { from = 0; - to = ActualWidth - _elementPanel.Width; + to = ActualWidth - _elementPanel.ActualWidth; SetCurrentValue(AutoReverseProperty, ValueBoxes.TrueBox); } propertyPath = new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)"); } else { - if (AutoRun && _elementPanel.Height < ActualHeight) + if (AutoRun && _elementPanel.ActualHeight < ActualHeight) { return; } if (Runaway) { - from = -_elementPanel.Height; + from = -_elementPanel.ActualHeight; to = ActualHeight; } else { from = 0; - to = ActualHeight - _elementPanel.Height; + to = ActualHeight - _elementPanel.ActualHeight; SetCurrentValue(AutoReverseProperty, ValueBoxes.TrueBox); } propertyPath = new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)"); @@ -180,7 +189,5 @@ namespace HandyControl.Controls _storyboard.Children.Add(animation); _storyboard.Begin(); } - - protected override void OnRender(DrawingContext drawingContext) => UpdateContent(); } } \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/Controls/Block/RunningBlock/RunningBorder.cs b/src/Shared/HandyControl_Shared/Controls/Block/RunningBlock/RunningBorder.cs new file mode 100644 index 00000000..5966ad75 --- /dev/null +++ b/src/Shared/HandyControl_Shared/Controls/Block/RunningBlock/RunningBorder.cs @@ -0,0 +1,65 @@ +using System; +using System.Windows; +using System.Windows.Controls; +using HandyControl.Tools; + +namespace HandyControl.Controls +{ + public class RunningBorder : Border + { + private bool _test; + + protected override Size MeasureOverride(Size constraint) + { + if (_test) + { + _test = false; + return constraint; + } + + var child = Child; + var borderThickness = BorderThickness; + var padding = Padding; + + if (UseLayoutRounding) + { + var dpiScaleX = DpiHelper.DeviceDpiX; + var dpiScaleY = DpiHelper.DeviceDpiY; + + borderThickness = new Thickness( + DpiHelper.RoundLayoutValue(borderThickness.Left, dpiScaleX), + DpiHelper.RoundLayoutValue(borderThickness.Top, dpiScaleY), + DpiHelper.RoundLayoutValue(borderThickness.Right, dpiScaleX), + DpiHelper.RoundLayoutValue(borderThickness.Bottom, dpiScaleY)); + } + + var borderSize = ConvertThickness2Size(borderThickness); + var paddingSize = ConvertThickness2Size(padding); + var mySize = new Size(); + + if (child != null) + { + var combined = new Size(borderSize.Width + paddingSize.Width, borderSize.Height + paddingSize.Height); + var borderConstraint = new Size(Math.Max(0.0, constraint.Width - combined.Width), Math.Max(0.0, constraint.Height - combined.Height)); + var childConstraint = new Size(Math.Max(0.0, double.PositiveInfinity - combined.Width), Math.Max(0.0, double.PositiveInfinity - combined.Height)); + + + child.Measure(borderConstraint); + var childSize = child.DesiredSize; + + mySize.Width = childSize.Width + combined.Width; + mySize.Height = childSize.Height + combined.Height; + + child.Measure(childConstraint); + } + else + { + mySize = new Size(borderSize.Width + paddingSize.Width, borderSize.Height + paddingSize.Height); + } + + return mySize; + } + + private static Size ConvertThickness2Size(Thickness th) => new Size(th.Left + th.Right, th.Top + th.Bottom); + } +} \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems index 43a2d51c..da81d0fa 100644 --- a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems +++ b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems @@ -26,6 +26,7 @@ + @@ -69,7 +70,7 @@ - + diff --git a/src/Shared/HandyControl_Shared/Themes/Styles/Base/RunningBlockBaseStyle.xaml b/src/Shared/HandyControl_Shared/Themes/Styles/Base/RunningBlockBaseStyle.xaml index 3b61ada6..69b569c8 100644 --- a/src/Shared/HandyControl_Shared/Themes/Styles/Base/RunningBlockBaseStyle.xaml +++ b/src/Shared/HandyControl_Shared/Themes/Styles/Base/RunningBlockBaseStyle.xaml @@ -16,7 +16,7 @@ - + @@ -28,7 +28,7 @@ - +