aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/SideChains/MaterialDesignInXamlToolkit-master/MaterialDesignThemes.Wpf/TimePicker.cs
blob: 4fffadd9f124a03bf5168401c80a01b43ff64df1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Threading;
using MaterialDesignThemes.Wpf.Converters;

namespace MaterialDesignThemes.Wpf
{
	[TemplatePart(Name = ElementButton, Type = typeof(Button))]
	[TemplatePart(Name = ElementPopup, Type = typeof(Popup))]
	[TemplatePart(Name = ElementTextBox, Type = typeof(DatePickerTextBox))]
	public class TimePicker : Control
	{
		private const string ElementButton = "PART_Button";
		private const string ElementPopup = "PART_Popup";
		private const string ElementTextBox = "PART_TextBox";

		private readonly ContentControl _clockHostContentControl;
	    private readonly Clock _clock;
	    private TextBox _textBox;
	    private Popup _popup;
		private Button _dropDownButton;
		private bool _disablePopupReopen;
        private DateTime? _lastValidTime;
	    private bool _isManuallyMutatingText;

        static TimePicker()
		{
			DefaultStyleKeyProperty.OverrideMetadata(typeof(TimePicker), new FrameworkPropertyMetadata(typeof(TimePicker)));
		}

		public TimePicker()
		{
            _clock = new Clock
			{
				DisplayAutomation = ClockDisplayAutomation.ToMinutesOnly
			};
			_clockHostContentControl = new ContentControl
			{
				Content = _clock
			};
			InitializeClock();
		}

		public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
            nameof(Text), typeof (string), typeof (TimePicker), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, TextPropertyChangedCallback));

		private static void TextPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
		{
			var timePicker = (TimePicker) dependencyObject;
            if (!timePicker._isManuallyMutatingText)
			    timePicker.SetSelectedTime();
			if (timePicker._textBox != null)
				timePicker._textBox.Text = dependencyPropertyChangedEventArgs.NewValue as string;
		}

		public string Text
		{
			get { return (string) GetValue(TextProperty); }
			set { SetValue(TextProperty, value); }
		}

		public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register(
            nameof(SelectedTime), typeof (DateTime?), typeof (TimePicker), new FrameworkPropertyMetadata(default(DateTime?), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, SelectedTimePropertyChangedCallback));

		private static void SelectedTimePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
		{
			var timePicker = (TimePicker) dependencyObject;
		    timePicker._isManuallyMutatingText = true;
			timePicker.SetCurrentValue(TextProperty, timePicker.DateTimeToString(timePicker.SelectedTime));
            timePicker._isManuallyMutatingText = false;
            timePicker._lastValidTime = timePicker.SelectedTime;
        }

        public DateTime? SelectedTime
		{
			get { return (DateTime?) GetValue(SelectedTimeProperty); }
			set { SetValue(SelectedTimeProperty, value); }
		}

		public static readonly DependencyProperty SelectedTimeFormatProperty = DependencyProperty.Register(
            nameof(SelectedTimeFormat), typeof (DatePickerFormat), typeof (TimePicker), new PropertyMetadata(DatePickerFormat.Short));

		public DatePickerFormat SelectedTimeFormat
		{
			get { return (DatePickerFormat) GetValue(SelectedTimeFormatProperty); }
			set { SetValue(SelectedTimeFormatProperty, value); }
		}

		public static readonly DependencyProperty IsDropDownOpenProperty = DependencyProperty.Register(
            nameof(IsDropDownOpen), typeof (bool), typeof (TimePicker),
			new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged, OnCoerceIsDropDownOpen));

		public bool IsDropDownOpen
		{
			get { return (bool) GetValue(IsDropDownOpenProperty); }
			set { SetValue(IsDropDownOpenProperty, value); }
		}

	    public static readonly DependencyProperty Is24HoursProperty = DependencyProperty.Register(
            nameof(Is24Hours), typeof (bool), typeof (TimePicker), new PropertyMetadata(default(bool)));

	    public bool Is24Hours
	    {
	        get { return (bool) GetValue(Is24HoursProperty); }
	        set { SetValue(Is24HoursProperty, value); }
	    }

		private static object OnCoerceIsDropDownOpen(DependencyObject d, object baseValue)
		{
			var timePicker = (TimePicker)d;
		
			if (!timePicker.IsEnabled)
			{
				return false;
			}

			return baseValue;
		}

		/// <summary> 
		/// IsDropDownOpenProperty property changed handler.
		/// </summary> 
		/// <param name="d">DatePicker that changed its IsDropDownOpen.</param> 
		/// <param name="e">DependencyPropertyChangedEventArgs.</param>
		private static void OnIsDropDownOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var timePicker = (TimePicker)d;

			var newValue = (bool) e.NewValue;
			if (timePicker._popup == null || timePicker._popup.IsOpen == newValue) return;

			timePicker._popup.IsOpen = newValue;
			if (newValue)
			{
				//TODO set time
				//dp._originalSelectedDate = dp.SelectedDate;

				timePicker.Dispatcher.BeginInvoke(DispatcherPriority.Input, (Action) delegate()
				{
					timePicker._clock.Focus();
				});
			}
		}

		public static readonly DependencyProperty ClockStyleProperty = DependencyProperty.Register(
            nameof(ClockStyle), typeof (Style), typeof (TimePicker), new PropertyMetadata(default(Style)));

		public Style ClockStyle
		{
			get { return (Style) GetValue(ClockStyleProperty); }
			set { SetValue(ClockStyleProperty, value); }
		}

		public static readonly DependencyProperty ClockHostContentControlStyleProperty = DependencyProperty.Register(
            nameof(ClockHostContentControlStyle), typeof (Style), typeof (TimePicker), new PropertyMetadata(default(Style)));

	    public Style ClockHostContentControlStyle
		{
			get { return (Style) GetValue(ClockHostContentControlStyleProperty); }
			set { SetValue(ClockHostContentControlStyleProperty, value); }
		}

	    public static readonly DependencyProperty IsInvalidTextAllowedProperty = DependencyProperty.Register(
	        "IsInvalidTextAllowed", typeof (bool), typeof (TimePicker), new PropertyMetadata(default(bool)));

        /// <summary>
        /// Set to true to stop invalid text reverting back to previous valid value. Useful in cases where you
        /// want to display validation messages and allow the user to correct the data without it reverting.
        /// </summary>
	    public bool IsInvalidTextAllowed
	    {
	        get { return (bool) GetValue(IsInvalidTextAllowedProperty); }
	        set { SetValue(IsInvalidTextAllowedProperty, value); }
	    }

	    public override void OnApplyTemplate()
		{
			if (_popup != null)
			{
				_popup.RemoveHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopupOnPreviewMouseLeftButtonDown));
				_popup.Opened -= PopupOnOpened;
				_popup.Closed -= PopupOnClosed;
				_popup.Child = null;
			}
			if (_dropDownButton != null)
			{
				_dropDownButton.Click -= DropDownButtonOnClick;
			}
			if (_textBox != null)
			{
				_textBox.RemoveHandler(KeyDownEvent, new KeyEventHandler(TextBoxOnKeyDown));
				_textBox.RemoveHandler(TextBoxBase.TextChangedEvent, new TextChangedEventHandler(TextBoxOnTextChanged));
				_textBox.AddHandler(LostFocusEvent, new RoutedEventHandler(TextBoxOnLostFocus));
			}

			_textBox = GetTemplateChild(ElementTextBox) as TextBox;
			if (_textBox != null)
			{
				_textBox.AddHandler(KeyDownEvent, new KeyEventHandler(TextBoxOnKeyDown));
				_textBox.AddHandler(TextBoxBase.TextChangedEvent, new TextChangedEventHandler(TextBoxOnTextChanged));
				_textBox.AddHandler(LostFocusEvent, new RoutedEventHandler(TextBoxOnLostFocus));
			    _textBox.Text = Text;
			}

			_popup = GetTemplateChild(ElementPopup) as Popup;
			if (_popup != null)
			{
				_popup.AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(PopupOnPreviewMouseLeftButtonDown));
                _popup.Opened += PopupOnOpened;
				_popup.Closed += PopupOnClosed;
                _popup.Child = _clockHostContentControl; 
				if (IsDropDownOpen)
				{
					_popup.IsOpen = true;
				}
			}

			_dropDownButton = GetTemplateChild(ElementButton) as Button;
			if (_dropDownButton != null)
			{
				_dropDownButton.Click += DropDownButtonOnClick;
            }

			base.OnApplyTemplate();
		}

		private void TextBoxOnLostFocus(object sender, RoutedEventArgs routedEventArgs)
		{
		    if (string.IsNullOrEmpty(_textBox?.Text))
            {
                SetCurrentValue(SelectedTimeProperty, null);
                return;
            }

            DateTime time;
		    if (IsTimeValid(_textBox.Text, out time))
		        SetCurrentValue(SelectedTimeProperty, time);

		    else // Invalid time, jump back to previous good time
		        SetInvalidTime();
		}

        private void SetInvalidTime()
        {
            if (IsInvalidTextAllowed) return;

            if (_lastValidTime != null)
            {
                SetCurrentValue(SelectedTimeProperty, _lastValidTime.Value);
                _textBox.Text = _lastValidTime.Value.ToString(_lastValidTime.Value.Hour % 12 > 9 ? "hh:mm tt" : "h:mm tt");
            }

            else
            {
                SetCurrentValue(SelectedTimeProperty, null);
                _textBox.Text = "";
            }

        }

        private void TextBoxOnKeyDown(object sender, KeyEventArgs keyEventArgs)
		{
			keyEventArgs.Handled = ProcessKey(keyEventArgs) || keyEventArgs.Handled;
		}

		private bool ProcessKey(KeyEventArgs keyEventArgs)
		{
			switch (keyEventArgs.Key)
			{
				case Key.System:
					{
						switch (keyEventArgs.SystemKey)
						{
							case Key.Down:
								{
									if ((Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
									{
										TogglePopup();
										return true;
									}

									break;
								}
						}

						break;
					}

				case Key.Enter:
					{
						SetSelectedTime();
						return true;
					}
			}

			return false;
		}

		private void TextBoxOnTextChanged(object sender, TextChangedEventArgs textChangedEventArgs)
		{
            if (_popup?.IsOpen == true || IsInvalidTextAllowed)
			    SetCurrentValue(TextProperty, _textBox.Text);

            if (_popup?.IsOpen == false)
                SetSelectedTime(true);
        }

	    private void SetSelectedTime(bool beCautious = false)
        {
            if (!string.IsNullOrEmpty(_textBox?.Text))
			{
                ParseTime(_textBox.Text, t =>
                {
                    if (!beCautious || DateTimeToString(t) == _textBox.Text)
                        SetCurrentValue(SelectedTimeProperty, t);
                });
            }
            else
            {
                SetCurrentValue(SelectedTimeProperty, null);
            }
        }

		private static void ParseTime(string s, Action<DateTime> successContinuation)
		{
			DateTime time;
            if (IsTimeValid(s, out time))
				successContinuation(time);
		}

        private static bool IsTimeValid(string s, out DateTime time)
        {
            return DateTime.TryParse(s,
                                     CultureInfo.CurrentCulture,
                                     DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces,
                                     out time);
        }

        private string DateTimeToString(DateTime? d)
		{
			return d.HasValue ? DateTimeToString(d.Value) : null;
		}

		private string DateTimeToString(DateTime d)
		{
			var dtfi = CultureInfo.CurrentCulture.GetDateFormat(); 

			switch (SelectedTimeFormat)
			{
				case DatePickerFormat.Short:
					return string.Format(CultureInfo.CurrentCulture, d.ToString(dtfi.ShortTimePattern, dtfi));					
				case DatePickerFormat.Long:
					return string.Format(CultureInfo.CurrentCulture, d.ToString(dtfi.LongTimePattern, dtfi));					
			}

			return null;
		}

		private void PopupOnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
		{
			var popup = sender as Popup;
			if (popup == null || popup.StaysOpen) return;

		    if (_dropDownButton?.InputHitTest(mouseButtonEventArgs.GetPosition(_dropDownButton)) != null)
			{
				// This popup is being closed by a mouse press on the drop down button 
				// The following mouse release will cause the closed popup to immediately reopen. 
				// Raise a flag to block reopeneing the popup
				_disablePopupReopen = true;
			}
		}

		private void PopupOnClosed(object sender, EventArgs eventArgs)
		{
			if (IsDropDownOpen)
			{
				SetCurrentValue(IsDropDownOpenProperty, false);
			}

			if (_clock.IsKeyboardFocusWithin)
			{
				MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
			}

			//TODO Clock closed event
			//OnCalendarClosed(new RoutedEventArgs());
		}

		private void PopupOnOpened(object sender, EventArgs eventArgs)
		{
			if (!IsDropDownOpen)
			{
				SetCurrentValue(IsDropDownOpenProperty, true);
			}

			if (_clock != null)
			{
				_clock.DisplayMode = ClockDisplayMode.Hours;
				_clock.MoveFocus(new TraversalRequest(FocusNavigationDirection.First));
			}

			//TODO ClockOpenedEvent
			//this.OnCalendarOpened(new RoutedEventArgs());
		}

		private void InitializeClock()
		{
			_clock.AddHandler(Clock.ClockChoiceMadeEvent, new ClockChoiceMadeEventHandler(ClockChoiceMadeHandler));
            _clock.SetBinding(ForegroundProperty, GetBinding(ForegroundProperty));
			_clock.SetBinding(StyleProperty, GetBinding(ClockStyleProperty));
			_clock.SetBinding(Clock.TimeProperty, GetBinding(SelectedTimeProperty, new NullableDateTimeToCurrentDateConverter()));
		    _clock.SetBinding(Clock.Is24HoursProperty, GetBinding(Is24HoursProperty));
			_clockHostContentControl.SetBinding(StyleProperty, GetBinding(ClockHostContentControlStyleProperty));
		}

        private void ClockChoiceMadeHandler(object sender, ClockChoiceMadeEventArgs clockChoiceMadeEventArgs)
        {
            if (clockChoiceMadeEventArgs.Mode == ClockDisplayMode.Minutes)
            {
                TogglePopup();
                if (SelectedTime == null)
                {
                    SelectedTime = _clock.Time;
                }
            }
        }

		private void DropDownButtonOnClick(object sender, RoutedEventArgs routedEventArgs)
		{
			TogglePopup();
		}

		private void TogglePopup()
		{
			if (IsDropDownOpen)
				SetCurrentValue(IsDropDownOpenProperty, false);
			else
			{
				if (_disablePopupReopen)
					_disablePopupReopen = false;
				else
				{
					SetSelectedTime();
					SetCurrentValue(IsDropDownOpenProperty, true);
				}
			}
		}

		private BindingBase GetBinding(DependencyProperty property, IValueConverter converter = null)
		{
		    var binding = new Binding(property.Name)
		    {
		        Source = this,
		        Converter = converter
		    };
		    return binding;
		}
	}
}