|
|
@@ -32,13 +32,24 @@ public class PumpControl : Control
|
|
|
private double _rotationAngle;
|
|
|
private IDisposable? _timer;
|
|
|
|
|
|
- static PumpControl()
|
|
|
+ static PumpControl() { AffectsRender<PumpControl>(IsRunningProperty, SpeedProperty, FrequencyProperty, TextProperty, RunningColorProperty, StoppedColorProperty); }
|
|
|
+
|
|
|
+ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
+ {
|
|
|
+ base.OnAttachedToVisualTree(e);
|
|
|
+ ThemeHelper.ThemeChanged += OnThemeChanged;
|
|
|
+ if (IsRunning) StartAnim();
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
|
|
{
|
|
|
- AffectsRender<PumpControl>(IsRunningProperty, SpeedProperty, FrequencyProperty, TextProperty, RunningColorProperty, StoppedColorProperty);
|
|
|
+ ThemeHelper.ThemeChanged -= OnThemeChanged;
|
|
|
+ StopAnim();
|
|
|
+ base.OnDetachedFromVisualTree(e);
|
|
|
}
|
|
|
|
|
|
- protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); if (IsRunning) StartAnim(); }
|
|
|
- protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) { base.OnDetachedFromVisualTree(e); StopAnim(); }
|
|
|
+ private void OnThemeChanged() => InvalidateVisual();
|
|
|
+
|
|
|
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
|
|
|
{
|
|
|
base.OnPropertyChanged(change);
|
|
|
@@ -52,57 +63,46 @@ public class PumpControl : Control
|
|
|
{
|
|
|
base.Render(context);
|
|
|
var bounds = new Rect(Bounds.Size);
|
|
|
- var cx = bounds.Width / 2;
|
|
|
- var cy = bounds.Height / 2 - 5;
|
|
|
+ var cx = bounds.Width / 2; var cy = bounds.Height / 2 - 5;
|
|
|
var radius = Math.Min(bounds.Width, bounds.Height) / 2 - 15;
|
|
|
var typeface = new Typeface("Microsoft YaHei", FontStyle.Normal, FontWeight.Bold);
|
|
|
|
|
|
var statusColor = (IsRunning ? RunningColor : StoppedColor) ?? (IsRunning ? ThemeHelper.Success() : ThemeHelper.TextDisabled());
|
|
|
- var statusBrush = statusColor as SolidColorBrush ?? ThemeHelper.Success();
|
|
|
+ var statusBrush = statusColor as SolidColorBrush ?? (IsRunning ? ThemeHelper.Success() as SolidColorBrush : ThemeHelper.TextDisabled() as SolidColorBrush);
|
|
|
+ var statusColorValue = (statusBrush as SolidColorBrush)?.Color ?? Colors.Gray;
|
|
|
|
|
|
- // 鍙戝厜
|
|
|
- if (IsRunning)
|
|
|
- context.DrawEllipse(new SolidColorBrush((statusBrush as SolidColorBrush)?.Color ?? Colors.Gray, 0.2), null, new Rect(cx - radius - 8, cy - radius - 8, (radius + 8) * 2, (radius + 8) * 2));
|
|
|
+ if (IsRunning) context.DrawEllipse(new SolidColorBrush(statusColorValue, 0.2), null, new Rect(cx - radius - 8, cy - radius - 8, (radius + 8) * 2, (radius + 8) * 2));
|
|
|
|
|
|
- // 澶栧3
|
|
|
context.DrawEllipse(null, new Pen(ThemeHelper.Border(), 3), new Rect(cx - radius, cy - radius, radius * 2, radius * 2));
|
|
|
context.DrawEllipse(ThemeHelper.PanelBg(), null, new Rect(cx - radius + 3, cy - radius + 3, (radius - 3) * 2, (radius - 3) * 2));
|
|
|
|
|
|
- // 鍙剁墖
|
|
|
- var bladeCount = 6;
|
|
|
- var bladeLen = radius * 0.65;
|
|
|
- var bladeW = radius * 0.18;
|
|
|
- for (int i = 0; i < bladeCount; i++)
|
|
|
+ for (int i = 0; i < 6; i++)
|
|
|
{
|
|
|
- var angle = _rotationAngle + (360.0 / bladeCount) * i;
|
|
|
- var bx = cx + Math.Cos(angle * Math.PI / 180) * bladeLen * 0.5;
|
|
|
- var by = cy + Math.Sin(angle * Math.PI / 180) * bladeLen * 0.5;
|
|
|
- var bladeBrush = IsRunning ? new SolidColorBrush((statusBrush as SolidColorBrush)?.Color ?? Colors.Gray, 200) : ThemeHelper.TextDisabled();
|
|
|
+ var angle = _rotationAngle + 60.0 * i;
|
|
|
+ var bx = cx + Math.Cos(angle * Math.PI / 180) * radius * 0.65 * 0.5;
|
|
|
+ var by = cy + Math.Sin(angle * Math.PI / 180) * radius * 0.65 * 0.5;
|
|
|
+ var bladeBrush = IsRunning ? new SolidColorBrush(statusColorValue, 200) : ThemeHelper.TextDisabled();
|
|
|
var rad = angle * Math.PI / 180;
|
|
|
var cos = Math.Cos(rad); var sin = Math.Sin(rad);
|
|
|
var transform = new Matrix(cos, sin, -sin, cos, bx - bx * cos + by * sin, by - bx * sin - by * cos);
|
|
|
using (context.PushTransform(transform))
|
|
|
- context.DrawRectangle(bladeBrush, null, new Rect(bx - bladeW / 2, by - bladeLen / 2, bladeW, bladeLen));
|
|
|
+ context.DrawRectangle(bladeBrush, null, new Rect(bx - radius * 0.09, by - radius * 0.325, radius * 0.18, radius * 0.65));
|
|
|
}
|
|
|
|
|
|
- // 涓績
|
|
|
var cr = radius * 0.25;
|
|
|
context.DrawEllipse(IsRunning ? statusColor : ThemeHelper.Border(), null, new Rect(cx - cr, cy - cr, cr * 2, cr * 2));
|
|
|
context.DrawEllipse(new SolidColorBrush(Colors.White, 0.3), null, new Rect(cx - cr * 0.5, cy - cr * 0.5, cr, cr));
|
|
|
|
|
|
- // 鎸囩ず鐏
|
|
|
var indColor = IsRunning ? ThemeHelper.Success() : ThemeHelper.TextDisabled();
|
|
|
- if (IsRunning) context.DrawEllipse(new SolidColorBrush((statusBrush as SolidColorBrush)?.Color ?? Colors.Gray, 0.3), null, new Rect(bounds.Width - 23, 7, 16, 16));
|
|
|
+ if (IsRunning) context.DrawEllipse(new SolidColorBrush(statusColorValue, 0.3), null, new Rect(bounds.Width - 23, 7, 16, 16));
|
|
|
context.DrawEllipse(indColor, null, new Rect(bounds.Width - 20, 10, 10, 10));
|
|
|
|
|
|
- // 鏂囧瓧
|
|
|
if (!string.IsNullOrEmpty(Text))
|
|
|
{
|
|
|
var t = new FormattedText(Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 11, ThemeHelper.TextPrimary());
|
|
|
context.DrawText(t, new Point(cx - t.Width / 2, bounds.Bottom - t.Height - 5));
|
|
|
}
|
|
|
|
|
|
- // 棰戠巼
|
|
|
if (IsRunning)
|
|
|
{
|
|
|
var f = new FormattedText($"{Frequency:F0}Hz", CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("Microsoft YaHei"), 9, statusColor);
|