From ba981cd8acb48acf34354c2da4e71ae76661586b Mon Sep 17 00:00:00 2001 From: rabbitism Date: Thu, 19 Dec 2024 21:56:03 +0800 Subject: [PATCH 1/3] feat: fix date range start selection when tab into the control. --- .../DateTimePicker/DateRangePicker.cs | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs b/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs index 157a2f4..775d66b 100644 --- a/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs +++ b/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs @@ -457,5 +457,32 @@ public class DateRangePicker : DatePickerBase, IClearControl break; } } - + + protected override void OnGotFocus(GotFocusEventArgs e) + { + base.OnGotFocus(e); + FocusChanged(IsKeyboardFocusWithin); + } + + private bool _isFocused; + private void FocusChanged(bool hasFocus) + { + // The OnGotFocus & OnLostFocus are asynchronously and cannot + // reliably tell you that have the focus. All they do is let you + // know that the focus changed sometime in the past. To determine + // if you currently have the focus you need to do consult the + // FocusManager. + + bool wasFocused = _isFocused; + _isFocused = hasFocus; + + if (hasFocus) + { + + if (!wasFocused && _start == null) + { + _start = true; + } + } + } } \ No newline at end of file From bed5f2046e84586fd3ad141c892ef4fba5432245 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Thu, 19 Dec 2024 22:04:47 +0800 Subject: [PATCH 2/3] feat: delete useless comment. --- src/Ursa/Controls/DateTimePicker/DateRangePicker.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs b/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs index 775d66b..72dffbb 100644 --- a/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs +++ b/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs @@ -467,12 +467,6 @@ public class DateRangePicker : DatePickerBase, IClearControl private bool _isFocused; private void FocusChanged(bool hasFocus) { - // The OnGotFocus & OnLostFocus are asynchronously and cannot - // reliably tell you that have the focus. All they do is let you - // know that the focus changed sometime in the past. To determine - // if you currently have the focus you need to do consult the - // FocusManager. - bool wasFocused = _isFocused; _isFocused = hasFocus; From 6eff00d9d496a1c4e33c852792937a1f22c1a9db Mon Sep 17 00:00:00 2001 From: rabbitism Date: Mon, 23 Dec 2024 23:51:39 +0800 Subject: [PATCH 3/3] fix: rework tab behavior. --- .../DateTimePicker/DateRangePicker.cs | 62 +++++++------------ 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs b/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs index 72dffbb..56968f7 100644 --- a/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs +++ b/src/Ursa/Controls/DateTimePicker/DateRangePicker.cs @@ -120,7 +120,7 @@ public class DateRangePicker : DatePickerBase, IClearControl protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); - GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _startTextBox); + GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox); TextBox.TextChangedEvent.RemoveHandler(OnTextChanged, _startTextBox, _endTextBox); PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _startTextBox, _endTextBox); Button.ClickEvent.RemoveHandler(OnButtonClick, _button); @@ -248,7 +248,7 @@ public class DateRangePicker : DatePickerBase, IClearControl if (SelectedEndDate < e.Date) SelectedEndDate = null; SetCurrentValue(SelectedStartDateProperty, e.Date); _startTextBox?.SetValue(TextBox.TextProperty, e.Date?.ToString(DisplayFormat ?? "yyyy-MM-dd")); - _start = false; + //_start = false; _previewStart = null; _previewEnd = null; _startCalendar?.MarkDates(SelectedStartDate, SelectedEndDate, _previewStart, _previewEnd); @@ -267,7 +267,7 @@ public class DateRangePicker : DatePickerBase, IClearControl _endCalendar?.MarkDates(SelectedStartDate, SelectedEndDate, _previewStart, _previewEnd); if (SelectedStartDate is null) { - _start = true; + //_start = true; _startTextBox?.Focus(); } else @@ -281,14 +281,15 @@ public class DateRangePicker : DatePickerBase, IClearControl { Focus(NavigationMethod.Pointer); SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen); - _start = true; + _startTextBox?.Focus(); + // _start = true; } private void OnTextBoxPointerPressed(object? sender, PointerPressedEventArgs e) { if (Equals(sender, _startTextBox)) { - _start = true; + //_start = true; if (_startCalendar is not null) { var date = SelectedStartDate ?? DateTime.Today; @@ -312,7 +313,7 @@ public class DateRangePicker : DatePickerBase, IClearControl } else if (Equals(sender, _endTextBox)) { - _start = false; + //_start = false; if (_endCalendar is not null) { var date = SelectedEndDate ?? DateTime.Today; @@ -415,23 +416,22 @@ public class DateRangePicker : DatePickerBase, IClearControl private void OnTextBoxGetFocus(object? sender, GotFocusEventArgs e) { - if (_startCalendar is not null && _startCalendar?.Mode == CalendarViewMode.Month) + if (sender == _startTextBox) { - //_startCalendar.ContextDate = new CalendarContext(date.Year, date.Month); - //_startCalendar.UpdateDayButtons(); + _start = true; + } + else if (sender == _endTextBox) + { + _start = false; } - if (_endCalendar is not null && _endCalendar?.Mode == CalendarViewMode.Month) + if (IsDropdownOpen == false) { - var date2 = SelectedStartDate ?? DateTime.Today; - date2 = date2.AddMonths(1); - //_endCalendar.ContextDate = new CalendarContext(date2.Year, date2.Month); - //_endCalendar.UpdateDayButtons(); + SetCurrentValue(IsDropdownOpenProperty, true); } - - SetCurrentValue(IsDropdownOpenProperty, true); + } - + protected override void OnKeyDown(KeyEventArgs e) { switch (e.Key) @@ -446,7 +446,11 @@ public class DateRangePicker : DatePickerBase, IClearControl return; case Key.Tab: { - if(_endTextBox?.IsFocused == true) + if(_endTextBox?.IsFocused == true && e.KeyModifiers == KeyModifiers.None) + { + SetCurrentValue(IsDropdownOpenProperty, false); + } + else if (_startTextBox?.IsFocused == true && e.KeyModifiers == KeyModifiers.Shift) { SetCurrentValue(IsDropdownOpenProperty, false); } @@ -457,26 +461,4 @@ public class DateRangePicker : DatePickerBase, IClearControl break; } } - - protected override void OnGotFocus(GotFocusEventArgs e) - { - base.OnGotFocus(e); - FocusChanged(IsKeyboardFocusWithin); - } - - private bool _isFocused; - private void FocusChanged(bool hasFocus) - { - bool wasFocused = _isFocused; - _isFocused = hasFocus; - - if (hasFocus) - { - - if (!wasFocused && _start == null) - { - _start = true; - } - } - } } \ No newline at end of file