TimeControl.axaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Markup.Xaml;
  4. using System;
  5. namespace ShakerApp.Views;
  6. public partial class TimeControl : UserControl
  7. {
  8. public TimeControl()
  9. {
  10. InitializeComponent();
  11. }
  12. private bool changed = false;
  13. public static readonly StyledProperty<float> TimeProperty = AvaloniaProperty.Register<TimeControl, float>(nameof(Time), defaultBindingMode: Avalonia.Data.BindingMode.TwoWay, defaultValue: 0, coerce: static (obj, val) =>
  14. {
  15. if (obj is TimeControl control)
  16. {
  17. float time = val;
  18. if (control.changed) return val;
  19. control.changed = true;
  20. control.SetValue(HourProperty!, (uint)time / 3600);
  21. control.SetValue(MinuteProperty!, (byte)((uint)time / 60 % 60));
  22. control.SetValue(SecondProperty!, MathF.Floor((time - (control.Hour * 3600 + control.Minute * 60)) / control.Increment) * control.Increment);
  23. control.changed = false;
  24. }
  25. return val;
  26. });
  27. public float Time
  28. {
  29. get => GetValue(TimeProperty);
  30. set
  31. {
  32. var val = GetValue(TimeProperty);
  33. if (val == value) return;
  34. SetValue(TimeProperty, value);
  35. }
  36. }
  37. public static readonly StyledProperty<uint> HourProperty = AvaloniaProperty.Register<TimeControl, uint>(nameof(Hour), defaultValue: 0, coerce: (obj, val) =>
  38. {
  39. if (obj is TimeControl control)
  40. {
  41. control.SetValue(TimeProperty, control.Hour * 3600 + control.Minute * 60 + control.Second);
  42. }
  43. return val;
  44. });
  45. public uint Hour
  46. {
  47. get => GetValue(HourProperty);
  48. set
  49. {
  50. var val = GetValue(HourProperty);
  51. if (val == value) return;
  52. SetValue(HourProperty, value);
  53. }
  54. }
  55. public static readonly StyledProperty<byte> MinuteProperty = AvaloniaProperty.Register<TimeControl, byte>(nameof(Minute), defaultValue: 0, coerce:(obj,val)=>
  56. {
  57. byte temp = Math.Clamp(val, (byte)0, (byte)59);
  58. var control = (TimeControl)obj;
  59. control.SetValue(TimeProperty, control.Hour * 3600 + temp * 60 + control.Second);
  60. return temp;
  61. });
  62. public byte Minute
  63. {
  64. get => GetValue(MinuteProperty);
  65. set
  66. {
  67. var val = GetValue(MinuteProperty);
  68. if (val == value) return;
  69. SetValue(MinuteProperty, value);
  70. }
  71. }
  72. public static readonly StyledProperty<float> SecondProperty = AvaloniaProperty.Register<TimeControl, float>(nameof(Second), defaultValue: 0, coerce: (obj, val) =>
  73. {
  74. var control = (TimeControl)obj;
  75. float temp = Math.Clamp(val, 0f, 59f - control.Increment);
  76. control.SetValue(TimeProperty, control.Hour * 3600 + control.Minute * 60 + temp);
  77. return temp;
  78. });
  79. public float Second
  80. {
  81. get => GetValue(SecondProperty);
  82. set
  83. {
  84. var val = GetValue(SecondProperty);
  85. if (val == value) return;
  86. SetValue(SecondProperty, value);
  87. }
  88. }
  89. public static readonly StyledProperty<float> IncrementProperty = AvaloniaProperty.Register<TimeControl, float>(nameof(Increment), defaultValue:0.01f );
  90. public float Increment
  91. {
  92. get => GetValue(IncrementProperty);
  93. set => SetValue(IncrementProperty, value);
  94. }
  95. }