feat: make text editable.
This commit is contained in:
@@ -26,7 +26,7 @@
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
CornerRadius="{TemplateBinding CornerRadius}" />
|
||||
<Grid ColumnDefinitions="*, Auto, * Auto">
|
||||
<Grid ColumnDefinitions="*, Auto, * Auto" Name="PART_PassThroughElement">
|
||||
<TextBox
|
||||
Name="{x:Static u:DateRangePicker.PART_StartTextBox}"
|
||||
Grid.Column="0"
|
||||
@@ -75,6 +75,7 @@
|
||||
</Grid>
|
||||
<Popup
|
||||
Name="{x:Static contracts:PartNames.PART_Popup}"
|
||||
OverlayInputPassThroughElement="{Binding #PART_PassThroughElement}"
|
||||
HorizontalOffset="-4"
|
||||
IsLightDismissEnabled="True"
|
||||
IsOpen="{TemplateBinding IsDropdownOpen,
|
||||
|
||||
@@ -129,26 +129,7 @@ public class DatePicker: DatePickerBase, IClearControl
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DateTime.TryParseExact(_textBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var date))
|
||||
{
|
||||
SetCurrentValue(SelectedDateProperty, date);
|
||||
if (_calendar is not null)
|
||||
{
|
||||
_calendar.ContextDate = _calendar.ContextDate.With(year: date.Year, month: date.Month);
|
||||
_calendar.UpdateDayButtons();
|
||||
}
|
||||
_calendar?.MarkDates(startDate: date, endDate: date);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetCurrentValue(SelectedDateProperty, null);
|
||||
if (!fromText)
|
||||
{
|
||||
_textBox?.SetValue(TextBox.TextProperty, null);
|
||||
}
|
||||
_calendar?.ClearSelection();
|
||||
}
|
||||
CommitInput(!fromText);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -192,6 +173,13 @@ public class DatePicker: DatePickerBase, IClearControl
|
||||
case Key.Tab:
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
return;
|
||||
case Key.Enter:
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
CommitInput(true);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
default:
|
||||
base.OnKeyDown(e);
|
||||
break;
|
||||
@@ -219,6 +207,7 @@ public class DatePicker: DatePickerBase, IClearControl
|
||||
{
|
||||
return;
|
||||
}
|
||||
CommitInput(true);
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
}
|
||||
|
||||
@@ -230,12 +219,33 @@ public class DatePicker: DatePickerBase, IClearControl
|
||||
|
||||
if (hasFocus)
|
||||
{
|
||||
|
||||
if (!wasFocused && _textBox != null)
|
||||
{
|
||||
_textBox.Focus();
|
||||
_textBox.SelectAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CommitInput(bool clearWhenInvalid)
|
||||
{
|
||||
if (DateTime.TryParseExact(_textBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var date))
|
||||
{
|
||||
SetCurrentValue(SelectedDateProperty, date);
|
||||
if (_calendar is not null)
|
||||
{
|
||||
_calendar.ContextDate = _calendar.ContextDate.With(year: date.Year, month: date.Month);
|
||||
_calendar.UpdateDayButtons();
|
||||
}
|
||||
_calendar?.MarkDates(startDate: date, endDate: date);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clearWhenInvalid)
|
||||
{
|
||||
SetCurrentValue(SelectedDateProperty, null);
|
||||
}
|
||||
_calendar?.ClearSelection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using Avalonia.Interactivity;
|
||||
using Irihi.Avalonia.Shared.Common;
|
||||
using Irihi.Avalonia.Shared.Contracts;
|
||||
using Irihi.Avalonia.Shared.Helpers;
|
||||
using Calendar = Avalonia.Controls.Calendar;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -53,6 +54,7 @@ public class DateRangePicker : DatePickerBase, IClearControl
|
||||
|
||||
static DateRangePicker()
|
||||
{
|
||||
FocusableProperty.OverrideDefaultValue<DateRangePicker>(true);
|
||||
SelectedStartDateProperty.Changed.AddClassHandler<DateRangePicker, DateTime?>((picker, args) =>
|
||||
picker.OnSelectionChanged(args));
|
||||
SelectedEndDateProperty.Changed.AddClassHandler<DateRangePicker, DateTime?>((picker, args) =>
|
||||
@@ -279,7 +281,6 @@ public class DateRangePicker : DatePickerBase, IClearControl
|
||||
|
||||
private void OnButtonClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
Focus(NavigationMethod.Pointer);
|
||||
SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
|
||||
_startTextBox?.Focus();
|
||||
// _start = true;
|
||||
@@ -456,9 +457,111 @@ public class DateRangePicker : DatePickerBase, IClearControl
|
||||
}
|
||||
return;
|
||||
}
|
||||
case Key.Enter:
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
CommitInput(true);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
default:
|
||||
base.OnKeyDown(e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnGotFocus(GotFocusEventArgs e)
|
||||
{
|
||||
base.OnGotFocus(e);
|
||||
FocusChanged(IsKeyboardFocusWithin);
|
||||
}
|
||||
|
||||
protected override void OnLostFocus(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLostFocus(e);
|
||||
FocusChanged(IsKeyboardFocusWithin);
|
||||
var top = TopLevel.GetTopLevel(this);
|
||||
var element = top?.FocusManager?.GetFocusedElement();
|
||||
if (element is Visual v && _popup?.IsInsidePopup(v)==true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (element == _startTextBox || element == _endTextBox)
|
||||
{
|
||||
return;
|
||||
}
|
||||
CommitInput(true);
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
}
|
||||
|
||||
private bool _isFocused;
|
||||
private void FocusChanged(bool hasFocus)
|
||||
{
|
||||
bool wasFocused = _isFocused;
|
||||
_isFocused = hasFocus;
|
||||
|
||||
if (hasFocus)
|
||||
{
|
||||
if (!wasFocused && _startTextBox != null)
|
||||
{
|
||||
_startTextBox.Focus();
|
||||
_start = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CommitInput(bool clearWhenInvalid)
|
||||
{
|
||||
DateTime? startDate = null;
|
||||
DateTime? endDate = null;
|
||||
if (DateTime.TryParseExact(_startTextBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var localStartDate))
|
||||
{
|
||||
startDate = localStartDate;
|
||||
SetCurrentValue(SelectedStartDateProperty, localStartDate);
|
||||
if (_startCalendar is not null)
|
||||
{
|
||||
_startCalendar.ContextDate = _startCalendar.ContextDate.With(year: localStartDate.Year, month: localStartDate.Month);
|
||||
_startCalendar.UpdateDayButtons();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clearWhenInvalid)
|
||||
{
|
||||
SetCurrentValue(SelectedStartDateProperty, null);
|
||||
}
|
||||
|
||||
}
|
||||
if (DateTime.TryParseExact(_endTextBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var localEndDate))
|
||||
{
|
||||
endDate = localEndDate;
|
||||
SetCurrentValue(SelectedEndDateProperty, localEndDate);
|
||||
if (_endCalendar is not null)
|
||||
{
|
||||
_endCalendar.ContextDate = _endCalendar.ContextDate.With(year: localEndDate.Year, month: localEndDate.Month);
|
||||
_endCalendar.UpdateDayButtons();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clearWhenInvalid)
|
||||
{
|
||||
SetCurrentValue(SelectedEndDateProperty, null);
|
||||
}
|
||||
}
|
||||
if (startDate is null || endDate is null)
|
||||
{
|
||||
_startCalendar?.ClearSelection();
|
||||
_endCalendar?.ClearSelection();
|
||||
}
|
||||
else
|
||||
{
|
||||
_startCalendar?.MarkDates(startDate: startDate, endDate: endDate);
|
||||
_endCalendar?.MarkDates(startDate: startDate, endDate: endDate);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -41,10 +41,12 @@ public class DateTimePicker : DatePickerBase
|
||||
private Button? _button;
|
||||
private CalendarView? _calendar;
|
||||
private TextBox? _textBox;
|
||||
private Popup? _popup;
|
||||
private TimePickerPresenter? _timePickerPresenter;
|
||||
|
||||
static DateTimePicker()
|
||||
{
|
||||
FocusableProperty.OverrideDefaultValue<DateTimePicker>(true);
|
||||
DisplayFormatProperty.OverrideDefaultValue<DateTimePicker>(CultureInfo.InvariantCulture.DateTimeFormat.FullDateTimePattern);
|
||||
SelectedDateProperty.Changed.AddClassHandler<DateTimePicker, DateTime?>((picker, args) =>
|
||||
picker.OnSelectionChanged(args));
|
||||
@@ -102,19 +104,17 @@ public class DateTimePicker : DatePickerBase
|
||||
base.OnApplyTemplate(e);
|
||||
GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _textBox);
|
||||
TextBox.TextChangedEvent.RemoveHandler(OnTextChanged, _textBox);
|
||||
PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _textBox);
|
||||
Button.ClickEvent.RemoveHandler(OnButtonClick, _button);
|
||||
CalendarView.DateSelectedEvent.RemoveHandler(OnDateSelected, _calendar);
|
||||
TimePickerPresenter.SelectedTimeChangedEvent.RemoveHandler(OnTimeSelectedChanged, _timePickerPresenter);
|
||||
_button = e.NameScope.Find<Button>(PART_Button);
|
||||
e.NameScope.Find<Popup>(PART_Popup);
|
||||
_popup = e.NameScope.Find<Popup>(PART_Popup);
|
||||
_textBox = e.NameScope.Find<TextBox>(PART_TextBox);
|
||||
_calendar = e.NameScope.Find<CalendarView>(PART_Calendar);
|
||||
_timePickerPresenter = e.NameScope.Find<TimePickerPresenter>(PART_TimePicker);
|
||||
Button.ClickEvent.AddHandler(OnButtonClick, RoutingStrategies.Bubble, true, _button);
|
||||
GotFocusEvent.AddHandler(OnTextBoxGetFocus, _textBox);
|
||||
TextBox.TextChangedEvent.AddHandler(OnTextChanged, _textBox);
|
||||
PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _textBox);
|
||||
CalendarView.DateSelectedEvent.AddHandler(OnDateSelected, RoutingStrategies.Bubble, true, _calendar);
|
||||
TimePickerPresenter.SelectedTimeChangedEvent.AddHandler(OnTimeSelectedChanged, _timePickerPresenter);
|
||||
SyncSelectedDateToText(SelectedDate);
|
||||
@@ -165,21 +165,10 @@ public class DateTimePicker : DatePickerBase
|
||||
|
||||
private void OnButtonClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
Focus(NavigationMethod.Pointer);
|
||||
SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
|
||||
}
|
||||
|
||||
private void OnTextBoxPointerPressed(object? sender, PointerPressedEventArgs e)
|
||||
{
|
||||
if (_calendar is not null)
|
||||
if (IsFocused)
|
||||
{
|
||||
var date = SelectedDate ?? DateTime.Now;
|
||||
_calendar.ContextDate = new CalendarContext(date.Year, date.Month);
|
||||
_calendar.UpdateDayButtons();
|
||||
_timePickerPresenter?.SyncTime(SelectedDate?.TimeOfDay);
|
||||
SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
|
||||
}
|
||||
|
||||
SetCurrentValue(IsDropdownOpenProperty, true);
|
||||
}
|
||||
|
||||
private bool _fromText = false;
|
||||
@@ -211,26 +200,7 @@ public class DateTimePicker : DatePickerBase
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DateTime.TryParseExact(_textBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var date))
|
||||
{
|
||||
SetCurrentValue(SelectedDateProperty, date);
|
||||
if (_calendar is not null)
|
||||
{
|
||||
_calendar.ContextDate = _calendar.ContextDate.With(date.Year, date.Month);
|
||||
_calendar.UpdateDayButtons();
|
||||
}
|
||||
|
||||
_calendar?.MarkDates(date.Date, date.Date);
|
||||
_timePickerPresenter?.SyncTime(date.TimeOfDay);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetCurrentValue(SelectedDateProperty, null);
|
||||
if (!fromText) _textBox?.SetValue(TextBox.TextProperty, null);
|
||||
_calendar?.ClearSelection();
|
||||
_timePickerPresenter?.SyncTime(null);
|
||||
}
|
||||
CommitInput(!fromText);
|
||||
}
|
||||
_fromText = temp;
|
||||
}
|
||||
@@ -244,15 +214,66 @@ public class DateTimePicker : DatePickerBase
|
||||
_calendar.UpdateDayButtons();
|
||||
_timePickerPresenter?.SyncTime(date.TimeOfDay);
|
||||
}
|
||||
|
||||
SetCurrentValue(IsDropdownOpenProperty, true);
|
||||
}
|
||||
|
||||
protected override void OnGotFocus(GotFocusEventArgs e)
|
||||
{
|
||||
base.OnGotFocus(e);
|
||||
FocusChanged(IsKeyboardFocusWithin);
|
||||
}
|
||||
|
||||
protected override void OnLostFocus(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLostFocus(e);
|
||||
// SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
SetSelectedDate();
|
||||
FocusChanged(IsKeyboardFocusWithin);
|
||||
var top = TopLevel.GetTopLevel(this);
|
||||
var element = top?.FocusManager?.GetFocusedElement();
|
||||
if (element is Visual v && _popup?.IsInsidePopup(v)==true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
CommitInput(true);
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
}
|
||||
|
||||
private bool _isFocused;
|
||||
private void FocusChanged(bool hasFocus)
|
||||
{
|
||||
bool wasFocused = _isFocused;
|
||||
_isFocused = hasFocus;
|
||||
|
||||
if (hasFocus)
|
||||
{
|
||||
if (!wasFocused && _textBox != null)
|
||||
{
|
||||
_textBox.Focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CommitInput(bool clearWhenInvalid)
|
||||
{
|
||||
if (DateTime.TryParseExact(_textBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var date))
|
||||
{
|
||||
SetCurrentValue(SelectedDateProperty, date);
|
||||
if (_calendar is not null)
|
||||
{
|
||||
_calendar.ContextDate = _calendar.ContextDate.With(date.Year, date.Month);
|
||||
_calendar.UpdateDayButtons();
|
||||
}
|
||||
|
||||
_calendar?.MarkDates(date.Date, date.Date);
|
||||
_timePickerPresenter?.SyncTime(date.TimeOfDay);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetCurrentValue(SelectedDateProperty, null);
|
||||
if (clearWhenInvalid) _textBox?.SetValue(TextBox.TextProperty, null);
|
||||
_calendar?.ClearSelection();
|
||||
_timePickerPresenter?.SyncTime(null);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -264,20 +285,17 @@ public class DateTimePicker : DatePickerBase
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Key == Key.Down)
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, true);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Key == Key.Tab)
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
return;
|
||||
}
|
||||
|
||||
base.OnKeyDown(e);
|
||||
}
|
||||
|
||||
|
||||
@@ -140,10 +140,17 @@ public class TimePicker : TimePickerBase, IClearControl
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Key == Key.Enter)
|
||||
{
|
||||
CommitInput(true);
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
base.OnKeyDown(e);
|
||||
}
|
||||
|
||||
|
||||
private void OnTextChanged(object? sender, TextChangedEventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(_textBox?.Text))
|
||||
@@ -156,9 +163,7 @@ public class TimePicker : TimePickerBase, IClearControl
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DateTime.TryParseExact(_textBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var time))
|
||||
_presenter?.SyncTime(time.TimeOfDay);
|
||||
CommitInput(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,7 +212,6 @@ public class TimePicker : TimePickerBase, IClearControl
|
||||
protected override void OnGotFocus(GotFocusEventArgs e)
|
||||
{
|
||||
base.OnGotFocus(e);
|
||||
// SetCurrentValue(IsDropdownOpenProperty, true);
|
||||
FocusChanged(IsKeyboardFocusWithin);
|
||||
}
|
||||
|
||||
@@ -219,6 +223,7 @@ public class TimePicker : TimePickerBase, IClearControl
|
||||
var element = top?.FocusManager?.GetFocusedElement();
|
||||
if (element is Visual v && _popup?.IsInsidePopup(v) == true) return;
|
||||
if (element == _textBox) return;
|
||||
CommitInput(true);
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
}
|
||||
|
||||
@@ -230,4 +235,22 @@ public class TimePicker : TimePickerBase, IClearControl
|
||||
if (!wasFocused && _textBox != null)
|
||||
_textBox.Focus();
|
||||
}
|
||||
|
||||
private void CommitInput(bool clearWhenInvalid)
|
||||
{
|
||||
if (DateTime.TryParseExact(_textBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var time))
|
||||
{
|
||||
SetCurrentValue(SelectedTimeProperty, time.TimeOfDay);
|
||||
_presenter?.SyncTime(time.TimeOfDay);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clearWhenInvalid)
|
||||
{
|
||||
SetCurrentValue(SelectedTimeProperty, null);
|
||||
}
|
||||
_presenter?.SyncTime(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Avalonia;
|
||||
using System.Globalization;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Metadata;
|
||||
using Avalonia.Controls.Primitives;
|
||||
@@ -45,31 +46,22 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
private Button? _button;
|
||||
private TimePickerPresenter? _endPresenter;
|
||||
private TextBox? _endTextBox;
|
||||
private bool _isFocused;
|
||||
private Popup? _popup;
|
||||
private TimePickerPresenter? _startPresenter;
|
||||
|
||||
private TextBox? _startTextBox;
|
||||
private bool _suppressTextPresenterEvent;
|
||||
|
||||
|
||||
static TimeRangePicker()
|
||||
{
|
||||
FocusableProperty.OverrideDefaultValue<TimeRangePicker>(true);
|
||||
StartTimeProperty.Changed.AddClassHandler<TimeRangePicker, TimeSpan?>((picker, args) =>
|
||||
picker.OnSelectionChanged(args));
|
||||
EndTimeProperty.Changed.AddClassHandler<TimeRangePicker, TimeSpan?>((picker, args) =>
|
||||
picker.OnSelectionChanged(args, false));
|
||||
DisplayFormatProperty.Changed.AddClassHandler<TimeRangePicker, string?>((picker, args) => picker.OnDisplayFormatChanged(args));
|
||||
}
|
||||
|
||||
private void OnDisplayFormatChanged(AvaloniaPropertyChangedEventArgs<string?> args)
|
||||
{
|
||||
if (_startTextBox is not null)
|
||||
{
|
||||
SyncTimeToText(StartTime);
|
||||
}
|
||||
if (_endTextBox is not null)
|
||||
{
|
||||
SyncTimeToText(EndTime, false);
|
||||
}
|
||||
DisplayFormatProperty.Changed.AddClassHandler<TimeRangePicker, string?>((picker, args) =>
|
||||
picker.OnDisplayFormatChanged(args));
|
||||
}
|
||||
|
||||
|
||||
@@ -104,6 +96,12 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
_endPresenter?.SyncTime(null);
|
||||
}
|
||||
|
||||
private void OnDisplayFormatChanged(AvaloniaPropertyChangedEventArgs<string?> args)
|
||||
{
|
||||
if (_startTextBox is not null) SyncTimeToText(StartTime);
|
||||
if (_endTextBox is not null) SyncTimeToText(EndTime, false);
|
||||
}
|
||||
|
||||
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<TimeSpan?> args, bool start = true)
|
||||
{
|
||||
SyncTimeToText(args.NewValue.Value, start);
|
||||
@@ -133,12 +131,12 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
base.OnApplyTemplate(e);
|
||||
|
||||
GotFocusEvent.RemoveHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox);
|
||||
PointerPressedEvent.RemoveHandler(OnTextBoxPointerPressed, _startTextBox, _endTextBox);
|
||||
Button.ClickEvent.RemoveHandler(OnButtonClick, _button);
|
||||
TimePickerPresenter.SelectedTimeChangedEvent.RemoveHandler(OnPresenterTimeChanged, _startPresenter,
|
||||
_endPresenter);
|
||||
TextBox.TextChangedEvent.RemoveHandler(OnTextChanged, _startTextBox, _endTextBox);
|
||||
|
||||
e.NameScope.Find<Popup>(PartNames.PART_Popup);
|
||||
_popup = e.NameScope.Find<Popup>(PartNames.PART_Popup);
|
||||
_startTextBox = e.NameScope.Find<TextBox>(PART_StartTextBox);
|
||||
_endTextBox = e.NameScope.Find<TextBox>(PART_EndTextBox);
|
||||
_startPresenter = e.NameScope.Find<TimePickerPresenter>(PART_StartPresenter);
|
||||
@@ -146,34 +144,71 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
_button = e.NameScope.Find<Button>(PART_Button);
|
||||
|
||||
GotFocusEvent.AddHandler(OnTextBoxGetFocus, _startTextBox, _endTextBox);
|
||||
PointerPressedEvent.AddHandler(OnTextBoxPointerPressed, RoutingStrategies.Tunnel, false, _startTextBox,
|
||||
_endTextBox);
|
||||
Button.ClickEvent.AddHandler(OnButtonClick, _button);
|
||||
TimePickerPresenter.SelectedTimeChangedEvent.AddHandler(OnPresenterTimeChanged, _startPresenter, _endPresenter);
|
||||
|
||||
TextBox.TextChangedEvent.AddHandler(OnTextChanged, _startTextBox, _endTextBox);
|
||||
|
||||
_startPresenter?.SyncTime(StartTime);
|
||||
_endPresenter?.SyncTime(EndTime);
|
||||
SyncTimeToText(StartTime);
|
||||
SyncTimeToText(EndTime, false);
|
||||
}
|
||||
|
||||
private void OnTextChanged(object? sender, TextChangedEventArgs e)
|
||||
{
|
||||
if (Equals(sender, _startTextBox))
|
||||
OnTextChangedInternal(_startTextBox, _startPresenter, StartTimeProperty, true);
|
||||
else if (Equals(sender, _endTextBox)) OnTextChangedInternal(_endTextBox, _endPresenter, EndTimeProperty, true);
|
||||
}
|
||||
|
||||
private void OnTextChangedInternal(TextBox? textBox, TimePickerPresenter? presenter, AvaloniaProperty property,
|
||||
bool fromText = false)
|
||||
{
|
||||
if (textBox?.Text is null || string.IsNullOrEmpty(textBox.Text))
|
||||
{
|
||||
SetCurrentValue(property, null);
|
||||
presenter?.SyncTime(null);
|
||||
}
|
||||
else if (DisplayFormat is null || DisplayFormat.Length == 0)
|
||||
{
|
||||
if (DateTime.TryParse(textBox.Text, out var defaultTime))
|
||||
{
|
||||
SetCurrentValue(property, defaultTime.TimeOfDay);
|
||||
presenter?.SyncTime(defaultTime.TimeOfDay);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (DateTime.TryParseExact(textBox.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var date))
|
||||
{
|
||||
SetCurrentValue(property, date.TimeOfDay);
|
||||
presenter?.SyncTime(date.TimeOfDay);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!fromText)
|
||||
{
|
||||
SetCurrentValue(property, null);
|
||||
textBox.SetValue(TextBox.TextProperty, null);
|
||||
presenter?.SyncTime(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnPresenterTimeChanged(object? sender, TimeChangedEventArgs e)
|
||||
{
|
||||
if (!IsInitialized) return;
|
||||
if (_suppressTextPresenterEvent) return;
|
||||
SetCurrentValue(Equals(sender, _startPresenter) ? StartTimeProperty : EndTimeProperty, e.NewTime);
|
||||
}
|
||||
|
||||
private void OnButtonClick(object? sender, RoutedEventArgs e)
|
||||
{
|
||||
Focus(NavigationMethod.Pointer);
|
||||
SetCurrentValue(IsDropdownOpenProperty, !IsDropdownOpen);
|
||||
}
|
||||
|
||||
private void OnTextBoxPointerPressed(object? sender, PointerPressedEventArgs e)
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, true);
|
||||
}
|
||||
|
||||
private void OnTextBoxGetFocus(object? sender, GotFocusEventArgs e)
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, true);
|
||||
@@ -201,6 +236,14 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Key == Key.Enter)
|
||||
{
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
CommitInput(true);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
base.OnKeyDown(e);
|
||||
}
|
||||
|
||||
@@ -217,4 +260,65 @@ public class TimeRangePicker : TimePickerBase, IClearControl
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
Focus();
|
||||
}
|
||||
|
||||
protected override void OnGotFocus(GotFocusEventArgs e)
|
||||
{
|
||||
base.OnGotFocus(e);
|
||||
FocusChanged(IsKeyboardFocusWithin);
|
||||
}
|
||||
|
||||
protected override void OnLostFocus(RoutedEventArgs e)
|
||||
{
|
||||
base.OnLostFocus(e);
|
||||
FocusChanged(IsKeyboardFocusWithin);
|
||||
var top = TopLevel.GetTopLevel(this);
|
||||
var element = top?.FocusManager?.GetFocusedElement();
|
||||
if (element is Visual v && _popup?.IsInsidePopup(v) == true) return;
|
||||
if (element == _startTextBox || element == _endTextBox) return;
|
||||
CommitInput(true);
|
||||
SetCurrentValue(IsDropdownOpenProperty, false);
|
||||
}
|
||||
|
||||
private void FocusChanged(bool hasFocus)
|
||||
{
|
||||
var wasFocused = _isFocused;
|
||||
_isFocused = hasFocus;
|
||||
if (hasFocus)
|
||||
if (!wasFocused && _startTextBox != null)
|
||||
_startTextBox.Focus();
|
||||
}
|
||||
|
||||
private void CommitInput(bool clearWhenInvalid)
|
||||
{
|
||||
if (DateTime.TryParseExact(_startTextBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture,
|
||||
DateTimeStyles.None,
|
||||
out var start))
|
||||
{
|
||||
_startPresenter?.SyncTime(start.TimeOfDay);
|
||||
SetCurrentValue(StartTimeProperty, start.TimeOfDay);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clearWhenInvalid)
|
||||
{
|
||||
_startTextBox?.SetValue(TextBox.TextProperty, null);
|
||||
_startPresenter?.SyncTime(null);
|
||||
}
|
||||
}
|
||||
|
||||
if (DateTime.TryParseExact(_endTextBox?.Text, DisplayFormat, CultureInfo.CurrentUICulture, DateTimeStyles.None,
|
||||
out var end))
|
||||
{
|
||||
_endPresenter?.SyncTime(end.TimeOfDay);
|
||||
SetCurrentValue(EndTimeProperty, end.TimeOfDay);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clearWhenInvalid)
|
||||
{
|
||||
_endTextBox?.SetValue(TextBox.TextProperty, null);
|
||||
_endPresenter?.SyncTime(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user