From f83f2c796436c13ae1cd253c947fdb23fceec71b Mon Sep 17 00:00:00 2001 From: John Colvin Date: Thu, 14 Jun 2018 19:10:14 -0400 Subject: [PATCH] SPARK-1348 time-based period filter requires textbox to lose focus to accept new value --- .../DataGridEx/Filters/Views/TimeFilter.cs | 18 ++- .../Controls/TickUpDown/TickUpDown.xaml.cs | 56 ++++----- .../view/Controls/Time/DateTimePicker.xaml | 9 +- .../view/Controls/Time/DateTimePicker.xaml.cs | 106 ++++++++++++++---- src/client/spark/view/Utility/TextUtils.cs | 10 ++ 5 files changed, 145 insertions(+), 54 deletions(-) diff --git a/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilter.cs b/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilter.cs index ba3a63d27..20155813d 100644 --- a/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilter.cs +++ b/src/client/spark/view/Controls/DataGridEx/Filters/Views/TimeFilter.cs @@ -54,8 +54,9 @@ namespace mbts.ui { public static TimeTextTuple FromTicks(long ticks) => FromDateTime(new DateTime(ticks)); - public static TimeTextTuple FromDateTime(DateTime date_time) => - new TimeTextTuple(DateTimeFormatInfo.CurrentInfo.AbbreviatedMonthNames[date_time.Month - 1], date_time.Day.ToString(), date_time.Year.ToString(), date_time.ToString("t")); + public static TimeTextTuple FromDateTime(DateTime date_time) => FromDateTime(date_time, "t"); + public static TimeTextTuple FromDateTime(DateTime date_time, string time_format) => + new TimeTextTuple(DateTimeFormatInfo.CurrentInfo.AbbreviatedMonthNames[date_time.Month - 1], date_time.Day.ToString(), date_time.Year.ToString(), date_time.ToString(time_format)); public TimeTextTuple(string month_text, string day_text, string year_text, string time_text) { _month_text = month_text; @@ -107,14 +108,23 @@ namespace mbts.ui { static DateTime? _parse(string month_text, string day_text, string year_text, string time_text) { - DateTime? t = _parse(time_text); + TimeSpan? t = _parse_time(time_text); if (t.HasValue) { DateTime? d = _parse($"{month_text}/{day_text}/{year_text}"); - if (d.HasValue) return d.Value.Date.AddDays(t.Value.TimeOfDay.TotalDays); + if (d.HasValue) return d.Value.Date.Add(t.Value); } return null; } + static TimeSpan? _parse_time(string text) { + DateTime? dt = _parse(text); + if (dt.HasValue) { + return dt.Value.TimeOfDay; + } else if (TimeSpan.TryParse(text, out TimeSpan result) && result < TimeSpan.FromDays(1)) { + return result; + } else return null; + } + static DateTime? _parse(string text) { if (!string.IsNullOrEmpty(text) && DateTime.TryParse(text, out DateTime result)) return result; diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml.cs index d40656662..a551f032c 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml.cs @@ -23,38 +23,42 @@ namespace mbts.ui.controls { } protected override void OnPreviewKeyDown(KeyEventArgs e) { - switch (Keyboard.Modifiers) { - case ModifierKeys.None: { - switch (e.Key) { - case Key.Up: - TickUpCommand.TryExecuteIfCan(TickUpParameter); - e.Handled = true; - break; - case Key.Down: - TickDownCommand.TryExecuteIfCan(TickDownParameter); - e.Handled = true; - break; + if (EnableArrowKeyTick) { + switch (Keyboard.Modifiers) { + case ModifierKeys.None: { + switch (e.Key) { + case Key.Up: + TickUpCommand.TryExecuteIfCan(TickUpParameter); + e.Handled = true; + break; + case Key.Down: + TickDownCommand.TryExecuteIfCan(TickDownParameter); + e.Handled = true; + break; + } } - } - break; - case ModifierKeys.Alt: { - // see: https://stackoverflow.com/questions/3099472/previewkeydown-is-not-seeing-alt-modifiers - switch (e.SystemKey) { - case Key.Up: - TickUpCommand.TryExecuteIfCan(TickUpAltParameter); - e.Handled = true; - break; - case Key.Down: - TickDownCommand.TryExecuteIfCan(TickDownAltParameter); - e.Handled = true; - break; + break; + case ModifierKeys.Alt: { + // see: https://stackoverflow.com/questions/3099472/previewkeydown-is-not-seeing-alt-modifiers + switch (e.SystemKey) { + case Key.Up: + TickUpCommand.TryExecuteIfCan(TickUpAltParameter); + e.Handled = true; + break; + case Key.Down: + TickDownCommand.TryExecuteIfCan(TickDownAltParameter); + e.Handled = true; + break; + } } - } - break; + break; + } } base.OnPreviewKeyDown(e); } + public bool EnableArrowKeyTick { get; set; } = true; + #region TickCommand public ICommand TickCommand { get { return (ICommand)GetValue(TickCommandProperty); } diff --git a/src/client/spark/view/Controls/Time/DateTimePicker.xaml b/src/client/spark/view/Controls/Time/DateTimePicker.xaml index a570f966a..ff6a256ef 100644 --- a/src/client/spark/view/Controls/Time/DateTimePicker.xaml +++ b/src/client/spark/view/Controls/Time/DateTimePicker.xaml @@ -31,8 +31,13 @@ - - + + + + + + + diff --git a/src/client/spark/view/Controls/Time/DateTimePicker.xaml.cs b/src/client/spark/view/Controls/Time/DateTimePicker.xaml.cs index 9767829c6..113dd9d03 100644 --- a/src/client/spark/view/Controls/Time/DateTimePicker.xaml.cs +++ b/src/client/spark/view/Controls/Time/DateTimePicker.xaml.cs @@ -3,6 +3,7 @@ using mbts.ui.shell; using System; using System.Collections.Generic; using System.ComponentModel; +using System.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Data; @@ -14,7 +15,9 @@ namespace mbts.ui.controls { /// Interaction logic for DateTimePicker.xaml /// public class DateTimePicker : Control { - TimeTupleVM _vm; + public static readonly RoutedCommand TickTimeComponentCommand = new RoutedCommand(); + + DTPController _controller; bool _suspend_notify_vm; static DateTimePicker() => @@ -24,8 +27,8 @@ namespace mbts.ui.controls { new FrameworkPropertyMetadata(default(TimeTextTuple), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTimeTupleChanged)); static void OnTimeTupleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var dtp = (DateTimePicker)d; - if (!dtp._suspend_notify_vm && dtp._vm != null) - dtp._vm.result = (TimeTextTuple)e.NewValue; + if (!dtp._suspend_notify_vm && dtp._controller != null) + dtp._controller.result = (TimeTextTuple)e.NewValue; } public TimeTextTuple TimeTuple { get => (TimeTextTuple)GetValue(TimeTupleProperty); set => SetValue(TimeTupleProperty, value); } @@ -41,59 +44,73 @@ namespace mbts.ui.controls { var time = this.assert_template_child("PART_time_tick"); var time_text = this.assert_template_child("PART_time_text"); - if (_vm != null) _vm.PropertyChanged -= _vm_PropertyChanged; - _vm = new TimeTupleVM(); - _vm.result = TimeTuple; - _vm.PropertyChanged += _vm_PropertyChanged; + if (_controller != null) _controller.PropertyChanged -= _vm_PropertyChanged; + _controller = new DTPController(time_text); + _controller.result = TimeTuple; + _controller.PropertyChanged += _vm_PropertyChanged; + time.CommandBindings.Add(new CommandBinding(TickTimeComponentCommand, (s, e) => { + _controller.tick_time_component((int)e.Parameter); + })); const int delay = 500; - time.SetBinding(TickUpDown.TickCommandProperty, new Binding("tick_time") { Source = _vm }); + time.SetBinding(TickUpDown.TickCommandProperty, new Binding("tick_time") { Source = _controller }); time.TickUpParameter = 1; time.TickDownParameter = -1; time.TickUpAltParameter = 60; time.TickDownAltParameter = -60; - time_text.SetBinding(TextBox.TextProperty, new Binding("time_text") { Source = _vm, Delay = delay, ValidatesOnExceptions = true }); - _hooks.Add(time_text.SubscribePreviewTextInput((s, e) => - e.Handled = !MatchAny(time_text.PeekText(e), RegexFilters.Time.StandardTime, RegexFilters.Time.MilitaryTime) - )); + time_text.SetBinding(TextBox.TextProperty, new Binding("time_text") { Source = _controller, Delay = delay, ValidatesOnExceptions = true }); + _hooks.Add(time_text.SubscribePreviewTextInput((s, e) => { + string preview = time_text.PeekText(e); + if (RegexFilters.Time.StandardTimeOptionalSeconds.IsMatch(preview)) { + _controller.use_military_time = false; + } else if (RegexFilters.Time.MilitaryTimeOptionalSeconds.IsMatch(preview)) { + _controller.use_military_time = true; + } else { + e.Handled = true; + } + })); - month.SetBinding(TickUpDown.TickCommandProperty, new Binding("tick_month") { Source = _vm }); + month.SetBinding(TickUpDown.TickCommandProperty, new Binding("tick_month") { Source = _controller }); month.TickUpParameter = 1; month.TickDownParameter = -1; - month_text.SetBinding(TextBox.TextProperty, new Binding("month_text") { Source = _vm, Delay = delay }); + month_text.SetBinding(TextBox.TextProperty, new Binding("month_text") { Source = _controller, Delay = delay }); _hooks.Add(month_text.SubscribePreviewTextInput((s, e) => e.Handled = !RegexFilters.Time.MonthAbbrev.IsMatch(month_text.PeekText(e)) )); - day.SetBinding(TickUpDown.TickCommandProperty, new Binding("tick_day") { Source = _vm }); + day.SetBinding(TickUpDown.TickCommandProperty, new Binding("tick_day") { Source = _controller }); day.TickUpParameter = 1; day.TickDownParameter = -1; - day_text.SetBinding(TextBox.TextProperty, new Binding("day_text") { Source = _vm, Delay = delay }); + day_text.SetBinding(TextBox.TextProperty, new Binding("day_text") { Source = _controller, Delay = delay }); _hooks.Add(day_text.SubscribePreviewTextInput((s, e) => e.Handled = !RegexFilters.Time.DaysInMonth.IsMatch(day_text.PeekText(e)) )); - year.SetBinding(TickUpDown.TickCommandProperty, new Binding("tick_year") { Source = _vm }); + year.SetBinding(TickUpDown.TickCommandProperty, new Binding("tick_year") { Source = _controller }); year.TickUpParameter = 1; year.TickDownParameter = -1; - year_text.SetBinding(TextBox.TextProperty, new Binding("year_text") { Source = _vm, Delay = delay }); + year_text.SetBinding(TextBox.TextProperty, new Binding("year_text") { Source = _controller, Delay = delay }); _hooks.Add(year_text.SubscribePreviewTextInput((s, e) => e.Handled = !RegexFilters.Time.Year.IsMatch(year_text.PeekText(e)) )); } void _vm_PropertyChanged(object sender, PropertyChangedEventArgs e) { - if (e.PropertyName == nameof(TimeTupleVM.result)) { + if (e.PropertyName == nameof(DTPController.result)) { _suspend_notify_vm = true; - TimeTuple = _vm.result; + TimeTuple = _controller.result; _suspend_notify_vm = false; } } - private class TimeTupleVM : ViewModelBase { + // This is not exactly a view-model, bc it accesses the time TextBox directly so it can get/set the CaretIndex, + // which is used to determine which time-componenet - hh:mm:ss AM/PM - should be incremented/decremented via the up/down arrow-keys. + class DTPController : ViewModelBase { + readonly TextBox _time_text_box; TimeTextTuple _result; - public TimeTupleVM() { + public DTPController(TextBox time_text_box) { + _time_text_box = time_text_box; tick_time = new DelegateCommand(_tick_time); tick_month = new DelegateCommand(_tick_month); tick_day = new DelegateCommand(_tick_day); @@ -135,12 +152,52 @@ namespace mbts.ui.controls { RaisePropertyChanged(nameof(month_text), nameof(day_text), nameof(year_text), nameof(time_text)); } } + + public bool use_military_time { get; set; } public ICommand tick_time { get; } public ICommand tick_month { get; } public ICommand tick_day { get; } public ICommand tick_year { get; } + string _time_format => use_military_time ? StringFormats.Time.MilitaryWithSeconds : StringFormats.Time.StandardWithSeconds; + public void tick_time_component(int increment) { + _ensure_time_text_up_to_date(); + var r = _result.result; + if (r.HasValue) { + int index = _time_text_box.CaretIndex; + using (detect_property_changes()) { + DateTime dt; + if (index <= 0) { + dt = r.Value.AddHours(increment); + } else { + int segment = _time_text_box.Text.Take(index).Count(c => c == ':'); + switch (segment) { + case 0: dt = r.Value.AddHours(increment); break; + case 1: dt = r.Value.AddMinutes(increment); break; + case 2: + if (_time_text_box.Text.Take(index).Contains(' ')) { + // toggle AM/PM + dt = r.Value.AddHours(r.Value.Hour < 12 ? 12 : -12); + } else { + dt = r.Value.AddSeconds(increment); + } + break; + default: + dt = r.Value.AddSeconds(increment); + break; + } + } + _result = TimeTextTuple.FromDateTime(dt, _time_format); + } + _time_text_box.CaretIndex = index; + } else { + using (detect_property_changes()) + _force_valid_inputs(); + } + } + void _tick_time(int increment) { + _ensure_time_text_up_to_date(); using (detect_property_changes()) { var r = _result.result; if (r.HasValue) { @@ -187,6 +244,11 @@ namespace mbts.ui.controls { } } } + + void _ensure_time_text_up_to_date() { + if (_time_text_box.IsKeyboardFocused) + _time_text_box.GetBindingExpression(TextBox.TextProperty)?.UpdateSource(); + } void _force_valid_inputs() { var dt = DateTime.Today.Date; diff --git a/src/client/spark/view/Utility/TextUtils.cs b/src/client/spark/view/Utility/TextUtils.cs index 9bc84615f..fd60462fc 100644 --- a/src/client/spark/view/Utility/TextUtils.cs +++ b/src/client/spark/view/Utility/TextUtils.cs @@ -130,12 +130,22 @@ namespace mbts.ui.utility.text { public static class Time { public static readonly Regex StandardTime = new Regex(@"^((0?[0-9])|(1?[0-2]?))(:?|:([0-5]?|[0-5][0-9]?)) ?(?i)((a|p)m?)?$"), + StandardTimeOptionalSeconds = new Regex(@"^((\d?|0\d|1[0-2])|((\d|0\d|1[0-2])(:?|(:([0-5]?|[0-5]\d(( ?|( ([aApP]?|[aApP][mM]))):?|:([0-5]?|([0-5]\d( ?|( ([aApP]?|([aApP][mM]))))))))))))$"), MilitaryTime = new Regex(@"^(([0-1]?[0-9])|(2?[0-3]?))(:?|:([0-5]?|[0-5][0-9]?))$"), + MilitaryTimeOptionalSeconds = new Regex(@"^((\d?|[0-1]\d|2[0-3])|((\d|[0-1]\d|2[0-3])(:?|(:([0-5]?|[0-5]\d(:?|:([0-5]?|[0-5]\d)))))))$"), DaysInMonth = new Regex(@"^([0-9]?|[0-2][0-9]|3[0-1])?$"), MonthAbbrev = new Regex(@"^(?i)(a(ug?|pr?)?|d(e|ec)?|f(e?b)?|j(an?|u[nl]?)?|m(a[ry]?)?|(o(ct?)?)|s(ep?)?|n(ov?)?)?$"), Year = new Regex(@"^\d{0,4}$"), TimeSpan = new Regex(@"^(\d+(:?|:([0-5]?|[0-5]\d(:?|:([0-5]?|[0-5]\d)))))?$"); } } + + public static class StringFormats { + public static class Time { + public const string + MilitaryWithSeconds = "HH:mm:ss", + StandardWithSeconds = "hh:mm:ss tt"; + } + } } } \ No newline at end of file