|
| 1 | +using System; |
| 2 | +using System.Windows; |
| 3 | +using System.Windows.Controls; |
| 4 | +using System.Windows.Threading; |
| 5 | + |
| 6 | +namespace BionicCode.Controls.Net.Wpf |
| 7 | +{ |
| 8 | + public class Clock : Control |
| 9 | + { |
| 10 | + #region Dependency Properties |
| 11 | + |
| 12 | + #region ClockFace dependency property |
| 13 | + |
| 14 | + public static readonly DependencyProperty ClockFaceProperty = DependencyProperty.Register( |
| 15 | + "ClockFace", |
| 16 | + typeof(ClockFace), |
| 17 | + typeof(Clock), |
| 18 | + new PropertyMetadata(default)); |
| 19 | + |
| 20 | + public ClockFace ClockFace { get => (ClockFace) GetValue(Clock.ClockFaceProperty); set => SetValue(Clock.ClockFaceProperty, value); } |
| 21 | + |
| 22 | + #endregion ClockFace dependency property |
| 23 | + |
| 24 | + #region CurrentDateTime dependency property |
| 25 | + |
| 26 | + public static readonly DependencyProperty CurrentDateTimeProperty = DependencyProperty.Register( |
| 27 | + "CurrentDateTime", |
| 28 | + typeof(DateTime), |
| 29 | + typeof(Clock), |
| 30 | + new PropertyMetadata(default(DateTime), Clock.OnCurrentDateTimeChanged)); |
| 31 | + |
| 32 | + public DateTime CurrentDateTime { get => (DateTime) GetValue(Clock.CurrentDateTimeProperty); set => SetValue(Clock.CurrentDateTimeProperty, value); } |
| 33 | + |
| 34 | + #endregion CurrentDateTime dependency property |
| 35 | + |
| 36 | + #region TimeZone dependency property |
| 37 | + |
| 38 | + public static readonly DependencyProperty TimeZoneProperty = DependencyProperty.Register( |
| 39 | + "TimeZone", |
| 40 | + typeof(TimeZoneInfo), |
| 41 | + typeof(Clock), |
| 42 | + new PropertyMetadata(TimeZoneInfo.Local)); |
| 43 | + |
| 44 | + public TimeZoneInfo TimeZone { get => (TimeZoneInfo) GetValue(Clock.TimeZoneProperty); set => SetValue(Clock.TimeZoneProperty, value); } |
| 45 | + |
| 46 | + #endregion TimeZone dependency property |
| 47 | + |
| 48 | + #region Resolution dependency property |
| 49 | + |
| 50 | + public static readonly DependencyProperty ResolutionProperty = DependencyProperty.Register( |
| 51 | + "Resolution", |
| 52 | + typeof(TimeSpan), |
| 53 | + typeof(Clock), |
| 54 | + new PropertyMetadata(TimeSpan.FromSeconds(1))); |
| 55 | + |
| 56 | + public TimeSpan Resolution { get => (TimeSpan) GetValue(Clock.ResolutionProperty); set => SetValue(Clock.ResolutionProperty, value); } |
| 57 | + |
| 58 | + #endregion Resolution dependency property |
| 59 | + |
| 60 | + #region Precision dependency property |
| 61 | + |
| 62 | + public static readonly DependencyProperty PrecisionProperty = DependencyProperty.Register( |
| 63 | + "Precision", |
| 64 | + typeof(TimeSpan), |
| 65 | + typeof(Clock), |
| 66 | + new PropertyMetadata(TimeSpan.FromMilliseconds(50))); |
| 67 | + |
| 68 | + public TimeSpan Precision { get => (TimeSpan) GetValue(Clock.PrecisionProperty); set => SetValue(Clock.PrecisionProperty, value); } |
| 69 | + |
| 70 | + #endregion Precision dependency property |
| 71 | + |
| 72 | + #region DispatcherPriority dependency property |
| 73 | + |
| 74 | + public static readonly DependencyProperty DispatcherPriorityProperty = DependencyProperty.Register( |
| 75 | + "DispatcherPriority", |
| 76 | + typeof(DispatcherPriority), |
| 77 | + typeof(Clock), |
| 78 | + new PropertyMetadata(System.Windows.Threading.DispatcherPriority.Send)); |
| 79 | + |
| 80 | + public DispatcherPriority DispatcherPriority { get => (DispatcherPriority) GetValue(Clock.DispatcherPriorityProperty); set => SetValue(Clock.DispatcherPriorityProperty, value); } |
| 81 | + |
| 82 | + #endregion DispatcherPriority dependency property |
| 83 | + |
| 84 | + #region Meridiem read-only dependency property |
| 85 | + protected static readonly DependencyPropertyKey MeridiemPropertyKey = DependencyProperty.RegisterReadOnly( |
| 86 | + "Meridiem", |
| 87 | + typeof(Meridiem), |
| 88 | + typeof(Clock), |
| 89 | + new PropertyMetadata(default(Meridiem))); |
| 90 | + |
| 91 | + public static readonly DependencyProperty MeridiemProperty = Clock.MeridiemPropertyKey.DependencyProperty; |
| 92 | + |
| 93 | + public Meridiem Meridiem |
| 94 | + { |
| 95 | + get => (Meridiem) GetValue(Clock.MeridiemProperty); |
| 96 | + private set => SetValue(Clock.MeridiemPropertyKey, value); |
| 97 | + } |
| 98 | + |
| 99 | + #endregion Meridiem read-only dependency property |
| 100 | + |
| 101 | + #endregion Dependency Properties |
| 102 | + |
| 103 | + public bool IsDaylightSavingTime => this.TimeZone.IsDaylightSavingTime(this.CurrentDateTime); |
| 104 | + |
| 105 | + private DispatcherTimer Timer { get; } |
| 106 | + |
| 107 | + static Clock() |
| 108 | + { |
| 109 | + FrameworkElement.DefaultStyleKeyProperty |
| 110 | + .OverrideMetadata(typeof(Clock), new FrameworkPropertyMetadata(typeof(Clock))); |
| 111 | + } |
| 112 | + |
| 113 | + public Clock() |
| 114 | + { |
| 115 | + this.Timer = new DispatcherTimer(this.Precision, this.DispatcherPriority, OnTimerElapsed, this.Dispatcher); |
| 116 | + } |
| 117 | + |
| 118 | + #region OnCurrentDateTimeChanged dependency property changed handler |
| 119 | + |
| 120 | + private static void OnCurrentDateTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => |
| 121 | + (d as Clock).OnCurrentDateTimeChanged((DateTime) e.OldValue, (DateTime) e.NewValue); |
| 122 | + |
| 123 | + protected virtual void OnCurrentDateTimeChanged(DateTime oldValue, DateTime newValue) |
| 124 | + { |
| 125 | + this.Meridiem = newValue.TimeOfDay < new TimeSpan(11, 59, 59) |
| 126 | + ? Meridiem.AM |
| 127 | + : Meridiem.PM; |
| 128 | + } |
| 129 | + |
| 130 | + #endregion OnCurrentDateTimeChanged dependency property changed handler |
| 131 | + |
| 132 | + private void OnTimerElapsed(object sender, EventArgs e) => this.CurrentDateTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, this.TimeZone); |
| 133 | + } |
| 134 | +} |
0 commit comments