Skip to content

Commit f808dd4

Browse files
authored
Fixing stack overflow when the text was being set (#2312)
1 parent 7deb1a2 commit f808dd4

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

MainDemo.Wpf/Domain/SimpleDateValidationRule.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ public class SimpleDateValidationRule : ValidationRule
88
{
99
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
1010
{
11-
DateTime time;
1211
return DateTime.TryParse((value ?? "").ToString(),
1312
CultureInfo.CurrentCulture,
1413
DateTimeStyles.AssumeLocal | DateTimeStyles.AllowWhiteSpaces,
15-
out time)
14+
out _)
1615
? ValidationResult.ValidResult
1716
: new ValidationResult(false, "Invalid date");
1817
}

MaterialDesignThemes.Wpf/TimePicker.cs

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ private static void TextPropertyChangedCallback(DependencyObject dependencyObjec
7777
{
7878
var timePicker = (TimePicker)dependencyObject;
7979
if (!timePicker._isManuallyMutatingText)
80+
{
8081
timePicker.SetSelectedTime();
82+
}
83+
8184
if (timePicker._textBox != null)
8285
{
8386
timePicker.UpdateTextBoxText(dependencyPropertyChangedEventArgs.NewValue as string ?? "");
@@ -173,13 +176,7 @@ public bool Is24Hours
173176
private static object OnCoerceIsDropDownOpen(DependencyObject d, object baseValue)
174177
{
175178
var timePicker = (TimePicker)d;
176-
177-
if (!timePicker.IsEnabled)
178-
{
179-
return false;
180-
}
181-
182-
return baseValue;
179+
return timePicker.IsEnabled ? baseValue : false;
183180
}
184181

185182
/// <summary>
@@ -326,7 +323,9 @@ private void TextBoxOnLostFocus(object sender, RoutedEventArgs routedEventArgs)
326323
UpdateTextBoxTextIfNeeded(text!);
327324
}
328325
else // Invalid time, jump back to previous good time
326+
{
329327
SetInvalidTime();
328+
}
330329
}
331330

332331
private void SetInvalidTime()
@@ -352,9 +351,7 @@ private void SetInvalidTime()
352351
}
353352

354353
private void TextBoxOnKeyDown(object sender, KeyEventArgs keyEventArgs)
355-
{
356-
keyEventArgs.Handled = ProcessKey(keyEventArgs) || keyEventArgs.Handled;
357-
}
354+
=> keyEventArgs.Handled = ProcessKey(keyEventArgs) || keyEventArgs.Handled;
358355

359356
private bool ProcessKey(KeyEventArgs keyEventArgs)
360357
{
@@ -394,19 +391,23 @@ private void TextBoxOnTextChanged(object sender, TextChangedEventArgs textChange
394391
if (_textBox is { } textBox &&
395392
(_popup?.IsOpen == true || IsInvalidTextAllowed))
396393
{
394+
_isManuallyMutatingText = true;
397395
SetCurrentValue(TextProperty, textBox.Text);
396+
_isManuallyMutatingText = false;
398397
}
399398

400399
if (_popup?.IsOpen == false)
400+
{
401401
SetSelectedTime(true);
402+
}
402403
}
403404

404405
private void UpdateTextBoxText(string? text)
405406
{
406407
// Save and restore the cursor position
407408
if (_textBox is { } textBox)
408409
{
409-
var caretIndex = textBox.CaretIndex;
410+
int caretIndex = textBox.CaretIndex;
410411
textBox.Text = text;
411412
textBox.CaretIndex = caretIndex;
412413
}
@@ -418,26 +419,31 @@ private void UpdateTextBoxTextIfNeeded(string lastText)
418419
{
419420
string? formattedText = DateTimeToString(SelectedTime);
420421
if (formattedText != lastText)
422+
{
421423
UpdateTextBoxText(formattedText);
424+
}
422425
}
423426
}
424427

425428
private void SetSelectedTime(in DateTime time)
426-
{
427-
SetCurrentValue(SelectedTimeProperty, (SelectedTime?.Date ?? DateTime.Today).Add(time.TimeOfDay));
428-
}
429+
=> SetCurrentValue(SelectedTimeProperty, (SelectedTime?.Date ?? DateTime.Today).Add(time.TimeOfDay));
429430

430431
private void SetSelectedTime(bool beCautious = false)
431432
{
432-
var currentText = _textBox?.Text;
433+
string? currentText = _textBox?.Text;
433434
if (!string.IsNullOrEmpty(currentText))
434435
{
435436
ParseTime(currentText!, t =>
436437
{
437438
if (!beCautious || DateTimeToString(t) == currentText)
439+
{
438440
SetSelectedTime(t);
441+
}
442+
439443
if (!beCautious)
444+
{
440445
UpdateTextBoxTextIfNeeded(currentText!);
446+
}
441447
});
442448
}
443449
else
@@ -449,7 +455,9 @@ private void SetSelectedTime(bool beCautious = false)
449455
private void ParseTime(string s, Action<DateTime> successContinuation)
450456
{
451457
if (IsTimeValid(s, out DateTime time))
458+
{
452459
successContinuation(time);
460+
}
453461
}
454462

455463
private bool IsTimeValid(string s, out DateTime time)
@@ -500,7 +508,7 @@ private string DateTimeToString(DateTime datetime, DatePickerFormat format)
500508

501509
private void PopupOnPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
502510
{
503-
if (!(sender is Popup popup) || popup.StaysOpen) return;
511+
if (sender is not Popup popup || popup.StaysOpen) return;
504512

505513
if (_dropDownButton?.InputHitTest(mouseButtonEventArgs.GetPosition(_dropDownButton)) != null)
506514
{
@@ -573,11 +581,15 @@ private void DropDownButtonOnClick(object sender, RoutedEventArgs routedEventArg
573581
private void TogglePopup()
574582
{
575583
if (IsDropDownOpen)
584+
{
576585
SetCurrentValue(IsDropDownOpenProperty, false);
586+
}
577587
else
578588
{
579589
if (_disablePopupReopen)
590+
{
580591
_disablePopupReopen = false;
592+
}
581593
else
582594
{
583595
SetSelectedTime();

0 commit comments

Comments
 (0)