Skip to content

Commit c6262c0

Browse files
committed
fixed #441
1 parent c6763a4 commit c6262c0

4 files changed

Lines changed: 96 additions & 23 deletions

File tree

src/Shared/HandyControl_Shared/Controls/Block/RunningBlock.cs renamed to src/Shared/HandyControl_Shared/Controls/Block/RunningBlock/RunningBlock.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Windows;
33
using System.Windows.Controls;
4-
using System.Windows.Media;
54
using System.Windows.Media.Animation;
65
using HandyControl.Data;
76
using HandyControl.Expression.Drawing;
@@ -24,19 +23,32 @@ public class RunningBlock : ContentControl
2423

2524
public override void OnApplyTemplate()
2625
{
26+
if (_elementPanel != null)
27+
{
28+
_elementPanel.SizeChanged -= ElementPanel_SizeChanged;
29+
}
30+
2731
base.OnApplyTemplate();
2832

2933
_elementContent = GetTemplateChild(ElementContent) as FrameworkElement;
3034
_elementPanel = GetTemplateChild(ElementPanel) as Panel;
35+
36+
if (_elementPanel != null)
37+
{
38+
_elementPanel.SizeChanged += ElementPanel_SizeChanged;
39+
}
40+
3141
UpdateContent();
3242
}
3343

44+
private void ElementPanel_SizeChanged(object sender, SizeChangedEventArgs e) => UpdateContent();
45+
3446
public static readonly DependencyProperty RunawayProperty = DependencyProperty.Register(
3547
"Runaway", typeof(bool), typeof(RunningBlock), new FrameworkPropertyMetadata(ValueBoxes.TrueBox, FrameworkPropertyMetadataOptions.AffectsRender));
3648

3749
public bool Runaway
3850
{
39-
get => (bool) GetValue(RunawayProperty);
51+
get => (bool)GetValue(RunawayProperty);
4052
set => SetValue(RunawayProperty, ValueBoxes.BooleanBox(value));
4153
}
4254

@@ -45,7 +57,7 @@ public bool Runaway
4557

4658
public bool AutoRun
4759
{
48-
get => (bool) GetValue(AutoRunProperty);
60+
get => (bool)GetValue(AutoRunProperty);
4961
set => SetValue(AutoRunProperty, ValueBoxes.BooleanBox(value));
5062
}
5163

@@ -54,7 +66,7 @@ public bool AutoRun
5466

5567
public Orientation Orientation
5668
{
57-
get => (Orientation) GetValue(OrientationProperty);
69+
get => (Orientation)GetValue(OrientationProperty);
5870
set => SetValue(OrientationProperty, value);
5971
}
6072

@@ -63,7 +75,7 @@ public Orientation Orientation
6375

6476
public Duration Duration
6577
{
66-
get => (Duration) GetValue(DurationProperty);
78+
get => (Duration)GetValue(DurationProperty);
6779
set => SetValue(DurationProperty, value);
6880
}
6981

@@ -72,7 +84,7 @@ public Duration Duration
7284

7385
public double Speed
7486
{
75-
get => (double) GetValue(SpeedProperty);
87+
get => (double)GetValue(SpeedProperty);
7688
set => SetValue(SpeedProperty, value);
7789
}
7890

@@ -93,7 +105,7 @@ public double Speed
93105

94106
public bool IsRunning
95107
{
96-
get => (bool) GetValue(IsRunningProperty);
108+
get => (bool)GetValue(IsRunningProperty);
97109
set => SetValue(IsRunningProperty, ValueBoxes.BooleanBox(value));
98110
}
99111

@@ -102,60 +114,57 @@ public bool IsRunning
102114

103115
public bool AutoReverse
104116
{
105-
get => (bool) GetValue(AutoReverseProperty);
117+
get => (bool)GetValue(AutoReverseProperty);
106118
set => SetValue(AutoReverseProperty, ValueBoxes.BooleanBox(value));
107119
}
108120

109121
private void UpdateContent()
110122
{
111123
if (_elementContent == null || _elementPanel == null) return;
124+
if (MathHelper.IsZero(_elementPanel.ActualWidth) || MathHelper.IsZero(_elementPanel.ActualHeight)) return;
112125

113126
_storyboard?.Stop();
114127

115-
_elementPanel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
116-
_elementPanel.Width = _elementPanel.DesiredSize.Width;
117-
_elementPanel.Height = _elementPanel.DesiredSize.Height;
118-
119128
double from;
120129
double to;
121130
PropertyPath propertyPath;
122131

123132
if (Orientation == Orientation.Horizontal)
124133
{
125-
if (AutoRun && _elementPanel.Width < ActualWidth)
134+
if (AutoRun && _elementPanel.ActualWidth < ActualWidth)
126135
{
127136
return;
128137
}
129138

130139
if (Runaway)
131140
{
132-
from = -_elementPanel.Width;
141+
from = -_elementPanel.ActualWidth;
133142
to = ActualWidth;
134143
}
135144
else
136145
{
137146
from = 0;
138-
to = ActualWidth - _elementPanel.Width;
147+
to = ActualWidth - _elementPanel.ActualWidth;
139148
SetCurrentValue(AutoReverseProperty, ValueBoxes.TrueBox);
140149
}
141150
propertyPath = new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.X)");
142151
}
143152
else
144153
{
145-
if (AutoRun && _elementPanel.Height < ActualHeight)
154+
if (AutoRun && _elementPanel.ActualHeight < ActualHeight)
146155
{
147156
return;
148157
}
149158

150159
if (Runaway)
151160
{
152-
from = -_elementPanel.Height;
161+
from = -_elementPanel.ActualHeight;
153162
to = ActualHeight;
154163
}
155164
else
156165
{
157166
from = 0;
158-
to = ActualHeight - _elementPanel.Height;
167+
to = ActualHeight - _elementPanel.ActualHeight;
159168
SetCurrentValue(AutoReverseProperty, ValueBoxes.TrueBox);
160169
}
161170
propertyPath = new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(TranslateTransform.Y)");
@@ -180,7 +189,5 @@ private void UpdateContent()
180189
_storyboard.Children.Add(animation);
181190
_storyboard.Begin();
182191
}
183-
184-
protected override void OnRender(DrawingContext drawingContext) => UpdateContent();
185192
}
186193
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Windows;
3+
using System.Windows.Controls;
4+
using HandyControl.Tools;
5+
6+
namespace HandyControl.Controls
7+
{
8+
public class RunningBorder : Border
9+
{
10+
private bool _test;
11+
12+
protected override Size MeasureOverride(Size constraint)
13+
{
14+
if (_test)
15+
{
16+
_test = false;
17+
return constraint;
18+
}
19+
20+
var child = Child;
21+
var borderThickness = BorderThickness;
22+
var padding = Padding;
23+
24+
if (UseLayoutRounding)
25+
{
26+
var dpiScaleX = DpiHelper.DeviceDpiX;
27+
var dpiScaleY = DpiHelper.DeviceDpiY;
28+
29+
borderThickness = new Thickness(
30+
DpiHelper.RoundLayoutValue(borderThickness.Left, dpiScaleX),
31+
DpiHelper.RoundLayoutValue(borderThickness.Top, dpiScaleY),
32+
DpiHelper.RoundLayoutValue(borderThickness.Right, dpiScaleX),
33+
DpiHelper.RoundLayoutValue(borderThickness.Bottom, dpiScaleY));
34+
}
35+
36+
var borderSize = ConvertThickness2Size(borderThickness);
37+
var paddingSize = ConvertThickness2Size(padding);
38+
var mySize = new Size();
39+
40+
if (child != null)
41+
{
42+
var combined = new Size(borderSize.Width + paddingSize.Width, borderSize.Height + paddingSize.Height);
43+
var borderConstraint = new Size(Math.Max(0.0, constraint.Width - combined.Width), Math.Max(0.0, constraint.Height - combined.Height));
44+
var childConstraint = new Size(Math.Max(0.0, double.PositiveInfinity - combined.Width), Math.Max(0.0, double.PositiveInfinity - combined.Height));
45+
46+
47+
child.Measure(borderConstraint);
48+
var childSize = child.DesiredSize;
49+
50+
mySize.Width = childSize.Width + combined.Width;
51+
mySize.Height = childSize.Height + combined.Height;
52+
53+
child.Measure(childConstraint);
54+
}
55+
else
56+
{
57+
mySize = new Size(borderSize.Width + paddingSize.Width, borderSize.Height + paddingSize.Height);
58+
}
59+
60+
return mySize;
61+
}
62+
63+
private static Size ConvertThickness2Size(Thickness th) => new Size(th.Left + th.Right, th.Top + th.Bottom);
64+
}
65+
}

src/Shared/HandyControl_Shared/HandyControl_Shared.projitems

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<Compile Include="$(MSBuildThisFileDirectory)Controls\Attach\TextBlockAttach.cs" />
2727
<Compile Include="$(MSBuildThisFileDirectory)Controls\Base\AdornerElement.cs" />
2828
<Compile Include="$(MSBuildThisFileDirectory)Controls\Block\FloatingBlock.cs" />
29+
<Compile Include="$(MSBuildThisFileDirectory)Controls\Block\RunningBlock\RunningBorder.cs" />
2930
<Compile Include="$(MSBuildThisFileDirectory)Controls\Button\SplitButton.cs" />
3031
<Compile Include="$(MSBuildThisFileDirectory)Controls\Carousel\CarouselItem.cs" />
3132
<Compile Include="$(MSBuildThisFileDirectory)Controls\Dialog\DialogContainer.cs" />
@@ -69,7 +70,7 @@
6970
<Compile Include="$(MSBuildThisFileDirectory)Controls\Slider\RangeSlider\RangeTrack.cs" />
7071
<Compile Include="$(MSBuildThisFileDirectory)Controls\Slider\RangeSlider\RangeThumb.cs" />
7172
<Compile Include="$(MSBuildThisFileDirectory)Controls\Slider\RangeSlider\TwoWayRangeBase.cs" />
72-
<Compile Include="$(MSBuildThisFileDirectory)Controls\Block\RunningBlock.cs" />
73+
<Compile Include="$(MSBuildThisFileDirectory)Controls\Block\RunningBlock\RunningBlock.cs" />
7374
<Compile Include="$(MSBuildThisFileDirectory)Controls\Text\SimpleText.cs" />
7475
<Compile Include="$(MSBuildThisFileDirectory)Controls\Window\GlowWindow.cs" />
7576
<Compile Include="$(MSBuildThisFileDirectory)Collections\DateTimeRangeComparer.cs" />

src/Shared/HandyControl_Shared/Themes/Styles/Base/RunningBlockBaseStyle.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<Setter Property="Template">
1717
<Setter.Value>
1818
<ControlTemplate TargetType="hc:RunningBlock">
19-
<Border CornerRadius="{Binding Path=(hc:BorderElement.CornerRadius),RelativeSource={RelativeSource TemplatedParent}}" Padding="{TemplateBinding Padding}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
19+
<hc:RunningBorder CornerRadius="{Binding Path=(hc:BorderElement.CornerRadius),RelativeSource={RelativeSource TemplatedParent}}" Padding="{TemplateBinding Padding}" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}">
2020
<hc:SimplePanel ClipToBounds="True">
2121
<hc:SimplePanel HorizontalAlignment="Left" x:Name="PART_Panel">
2222
<ContentPresenter x:Name="PART_ContentElement" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left">
@@ -28,7 +28,7 @@
2828
</ContentPresenter>
2929
</hc:SimplePanel>
3030
</hc:SimplePanel>
31-
</Border>
31+
</hc:RunningBorder>
3232
</ControlTemplate>
3333
</Setter.Value>
3434
</Setter>

0 commit comments

Comments
 (0)