Skip to content

Commit 23e13d5

Browse files
committed
Refactor 1
1 parent 6ece2d9 commit 23e13d5

File tree

2 files changed

+51
-96
lines changed

2 files changed

+51
-96
lines changed

src/Files.App.Controls/Storage/RingShape/RingShape.Properties.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ protected virtual void OnEndAngleChanged(double oldValue, double newValue)
3838
protected virtual void OnSweepDirectionChanged(SweepDirection oldValue, SweepDirection newValue)
3939
{
4040
BeginUpdate();
41-
_sweepDirection = SweepDirection;
4241
EndUpdate();
4342
}
4443

@@ -73,7 +72,6 @@ protected virtual void OnRadiusHeightChanged(double oldValue, double newValue)
7372
protected virtual void OnIsCircleChanged(bool oldValue, bool newValue)
7473
{
7574
BeginUpdate();
76-
_isCircle = IsCircle;
7775
EndUpdate();
7876
}
7977
}

src/Files.App.Controls/Storage/RingShape/RingShape.cs

Lines changed: 51 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,14 @@ public partial class RingShape : Path
2121
// Fields
2222

2323
private bool _isUpdating; // Is True when path is updating
24-
private bool _isCircle; // When True, Width and Height are equalized
2524
private Size _equalSize; // Calculated where Width and Height are equal
2625
private double _equalRadius; // Calculated where RadiusWidth and RadiusHeight are equal
27-
private Point _centerPoint; // Center Point within Width and Height bounds
2826
private double _normalizedMinAngle; // Normalized MinAngle between -180 and 540
2927
private double _normalizedMaxAngle; // Normalized MaxAngle between 0 and 360
3028
private double _validStartAngle; // The validated StartAngle
3129
private double _validEndAngle; // The validated EndAngle
3230
private double _radiusWidth; // The radius Width
3331
private double _radiusHeight; // The radius Height
34-
private SweepDirection _sweepDirection; // The SweepDirection
3532

3633
// Constructor
3734

@@ -94,12 +91,12 @@ private void UpdatePath()
9491
// If the ring is closed and complete
9592
if (endAngle >= startAngle + 360)
9693
{
97-
Data = DrawEllipse(_isCircle, _centerPoint, _equalRadius, _radiusWidth, _radiusHeight);
94+
Data = DrawEllipse(IsCircle, Center, _equalRadius, _radiusWidth, _radiusHeight);
9895
}
9996
else
10097
{
10198
this.InvalidateArrange();
102-
this.Data = DrawArc(this, _sweepDirection, _isCircle, _centerPoint, startAngle, endAngle, _equalRadius, _radiusWidth, _radiusHeight);
99+
this.Data = DrawArc(this, SweepDirection, IsCircle, Center, startAngle, endAngle, _equalRadius, _radiusWidth, _radiusHeight);
103100
}
104101
}
105102

@@ -115,19 +112,18 @@ public void UpdateSizeAndStroke(DependencyObject d)
115112
{
116113
RingShape ringShape = (RingShape)d;
117114

118-
AdjustRadiusWidth( ringShape , ringShape.RadiusWidth , ringShape.StrokeThickness );
119-
AdjustRadiusHeight( ringShape , ringShape.RadiusHeight , ringShape.StrokeThickness );
115+
AdjustRadiusWidth();
116+
AdjustRadiusHeight();
120117

121-
_equalSize = CalculateEqualSize( new Size( ringShape.Width , ringShape.Height ) , ringShape.StrokeThickness );
122-
_equalRadius = CalculateEqualRadius( ringShape , ringShape.RadiusWidth , ringShape.RadiusHeight , ringShape.StrokeThickness );
118+
_equalSize = CalculateEqualSize(new(Width, Height));
119+
_equalRadius = CalculateEqualRadius();
123120

124-
_centerPoint = new Point( ringShape.Width / 2 , ringShape.Height / 2 );
125-
ringShape.Center = _centerPoint;
121+
Center = new ( ringShape.Width / 2 , ringShape.Height / 2 );
126122

127-
CalculateAndSetNormalizedAngles( ringShape , ringShape.MinAngle , ringShape.MaxAngle );
123+
CalculateAndSetNormalizedAngles();
128124

129-
ValidateAngle( ringShape , ringShape.StartAngle , true );
130-
ValidateAngle( ringShape , ringShape.EndAngle , false );
125+
_validStartAngle = ValidateAngle(StartAngle);
126+
_validEndAngle = ValidateAngle(EndAngle);
131127
}
132128

133129
private static EllipseGeometry DrawEllipse(bool IsCircle, Point Center, double EqualRadius, double RadiusWidth, double RadiusHeight)
@@ -161,8 +157,8 @@ private static PathGeometry DrawArc(RingShape RingShape, SweepDirection SweepDir
161157
var pathGeometry = new PathGeometry();
162158
var pathFigure = new PathFigure()
163159
{
164-
pathFigure.IsClosed = false;
165-
pathFigure.IsFilled = false;
160+
pathFigure.IsClosed = false,
161+
pathFigure.IsFilled = false,
166162
};
167163

168164
var newCenter = Center;
@@ -178,7 +174,6 @@ private static PathGeometry DrawArc(RingShape RingShape, SweepDirection SweepDir
178174
// Start Point
179175
pathFigure.StartPoint = ArcStartPoint( SweepDirection , newCenter, StartAngle, radius, radius);
180176

181-
182177
// Arc Segment and End Point
183178
arcSegment = CreateArcSegment( SweepDirection , newCenter , StartAngle , EndAngle , radius , radius );
184179
}
@@ -206,35 +201,23 @@ private static PathGeometry DrawArc(RingShape RingShape, SweepDirection SweepDir
206201

207202
private static Point ArcStartPoint(SweepDirection SweepDirection , Point Center, double StartAngle , double RadiusWidth, double RadiusHeight)
208203
{
209-
var finalPoint = new Point();
210-
211-
// Counterclockwise
212-
if ( SweepDirection is SweepDirection.Counterclockwise)
213-
{
214-
finalPoint = new(
204+
return SweepDirection is SweepDirection.Counterclockwise
205+
? new(
215206
Center.X - Math.Sin(StartAngle * DegreesToRadians ) * RadiusWidth,
216207
Center.Y - Math.Cos(StartAngle * DegreesToRadians ) * RadiusHeight);
217-
}
218-
// Clockwise
219-
else
220-
{
221-
finalPoint = new(
208+
: new(
222209
Center.X + Math.Sin(StartAngle * DegreesToRadians) * RadiusWidth ,
223210
Center.Y - Math.Cos(StartAngle * DegreesToRadians) * RadiusHeight);
224-
}
225-
226-
return finalPoint;
227211
}
228212

229213
private static ArcSegment CreateArcSegment(SweepDirection SweepDirection , Point Center , double StartAngle, double EndAngle , double RadiusWidth , double RadiusHeight)
230214
{
231215
var finalArcSegment = new ArcSegment();
232216

233-
// Counterclockwise
234217
if (SweepDirection is SweepDirection.Counterclockwise)
235218
{
236219
finalArcSegment.Point =
237-
new Point(
220+
new(
238221
Center.X - Math.Sin(EndAngle * DegreesToRadians) * RadiusWidth,
239222
Center.Y - Math.Cos(EndAngle * DegreesToRadians) * RadiusHeight);
240223

@@ -249,11 +232,10 @@ private static ArcSegment CreateArcSegment(SweepDirection SweepDirection , Point
249232
finalArcSegment.SweepDirection = SweepDirection.Counterclockwise;
250233
}
251234
}
252-
// Clockwise
253235
else
254236
{
255237
finalArcSegment.Point =
256-
new Point(
238+
new(
257239
Center.X + Math.Sin(EndAngle * DegreesToRadians) * RadiusWidth,
258240
Center.Y - Math.Cos(EndAngle * DegreesToRadians) * RadiusHeight);
259241

@@ -280,31 +262,22 @@ private static ArcSegment CreateArcSegment(SweepDirection SweepDirection , Point
280262

281263
#region Value Calculations
282264

283-
private static Size CalculateEqualSize(Size size, double strokeThickness)
265+
private static Size CalculateEqualSize(Size size)
284266
{
285-
var adjWidth = size.Width;
286-
var adjHeight = size.Height;
287-
288-
var smaller = Math.Min(adjWidth, adjHeight);
289-
if (smaller > strokeThickness * 2)
267+
var smaller = Math.Min(size.Width, size.Height);
268+
if (smaller > StrokeThickness * 2)
290269
return new(smaller, smaller);
291270
else
292-
return new(strokeThickness * 2, strokeThickness * 2);
271+
return new(StrokeThickness * 2, StrokeThickness * 2);
293272
}
294273

295-
private static double CalculateEqualRadius(DependencyObject d, double radiusWidth, double radiusHeight, double strokeThickness)
274+
private static double CalculateEqualRadius()
296275
{
297-
RingShape ringShape = (RingShape)d;
298-
299-
double adjWidth = radiusWidth;
300-
double adjHeight = radiusHeight;
301-
302-
var smaller = Math.Min(adjWidth, adjHeight);
303-
304-
if (smaller <= strokeThickness)
305-
return strokeThickness;
306-
else if (smaller >= ((_equalSize.Width / 2) - (strokeThickness / 2)))
307-
return (_equalSize.Width / 2.0) - (strokeThickness / 2.0);
276+
var smaller = Math.Min(RadiusWidth, RadiusHeight);
277+
if (smaller <= StrokeThickness)
278+
return StrokeThickness;
279+
else if (smaller >= ((_equalSize.Width / 2) - (StrokeThickness / 2)))
280+
return (_equalSize.Width / 2.0) - (StrokeThickness / 2.0);
308281
else
309282
return smaller;
310283
}
@@ -315,15 +288,15 @@ private static Point CalculateCenter(double Width, double Height)
315288
return calculatedCenter;
316289
}
317290

318-
private static void CalculateAndSetNormalizedAngles(DependencyObject d, double minAngle, double maxAngle)
291+
private static void CalculateAndSetNormalizedAngles()
319292
{
320-
var result = CalculateModulus(minAngle, 360);
293+
var result = CalculateModulus(MinAngle, 360);
321294
if (result >= 180)
322295
result = result - 360;
323296

324297
_normalizedMinAngle = result;
325298

326-
result = CalculateModulus(maxAngle, 360);
299+
result = CalculateModulus(MaxAngle, 360);
327300

328301
if (result < 180)
329302
result = result + 360;
@@ -344,55 +317,39 @@ private static double CalculateModulus(double number, double divider)
344317
return result;
345318
}
346319

347-
private void ValidateAngle(DependencyObject d, double angle, bool isStart)
320+
private double ValidateAngle(double angle)
348321
{
349-
if (angle >= _normalizedMaxAngle)
350-
{
351-
if (isStart)
352-
_validStartAngle = _normalizedMaxAngle;
353-
else
354-
_validEndAngle = _normalizedMaxAngle;
355-
}
356-
else if (angle <= _normalizedMinAngle)
357-
{
358-
if (isStart)
359-
_validStartAngle = _normalizedMinAngle;
360-
else
361-
_validEndAngle = _normalizedMinAngle;
362-
}
363-
else
364-
{
365-
if (isStart)
366-
_validStartAngle = angle;
367-
else
368-
_validEndAngle = angle;
369-
}
322+
return angle >= _normalizedMaxAngle
323+
? _normalizedMaxAngle
324+
: angle <= _normalizedMinAngle
325+
? _normalizedMinAngle
326+
: angle;
370327
}
371328

372-
private void AdjustRadiusWidth(DependencyObject d, double radiusWidth, double strokeThickness)
329+
private void AdjustRadiusWidth()
373330
{
374-
var maxValue = (ringShape.Width / 2) - (ringShape.StrokeThickness / 2);
375-
var threshold = strokeThickness;
331+
var maxValue = (Width / 2) - (StrokeThickness / 2);
332+
var threshold = StrokeThickness;
376333

377-
if (radiusWidth >= maxValue)
378-
_radiusWidth = maxValue;
379-
else if (radiusWidth <= maxValue && radiusWidth >= threshold)
380-
_radiusWidth = radiusWidth;
334+
if (RadiusWidth >= maxValue)
335+
RadiusWidth = maxValue;
336+
else if (RadiusWidth <= maxValue && RadiusWidth >= threshold)
337+
RadiusWidth = RadiusWidth;
381338
else
382-
_radiusWidth = threshold;
339+
RadiusWidth = threshold;
383340
}
384341

385-
private void AdjustRadiusHeight(DependencyObject d, double radiusHeight, double strokeThickness)
342+
private void AdjustRadiusHeight()
386343
{
387344
var maxValue = (Height / 2) - (StrokeThickness / 2);
388-
var threshold = strokeThickness;
345+
var threshold = StrokeThickness;
389346

390-
if (radiusHeight >= maxValue)
391-
_radiusHeight = maxValue;
392-
else if (radiusHeight <= maxValue && radiusHeight >= threshold)
393-
_radiusHeight = radiusHeight;
347+
if (RadiusHeight >= maxValue)
348+
RadiusHeight = maxValue;
349+
else if (RadiusHeight <= maxValue && RadiusHeight >= threshold)
350+
RadiusHeight = RadiusHeight;
394351
else
395-
_radiusHeight = threshold;
352+
RadiusHeight = threshold;
396353
}
397354

398355
#endregion

0 commit comments

Comments
 (0)