123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430 |
- using Syncfusion.Windows.Shared;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media;
- using Comal.Classes;
- using InABox.DynamicGrid;
- namespace PRS.Shared
- {
- public class CoreTimeEditorControl : DynamicEnclosedEditorControl<TimeBlock, CoreTimeEditor>
- {
-
- static CoreTimeEditorControl()
- {
- //DynamicEditorControlFactory.Register<CoreTimeEditorControl, CoreTimeEditor>();
- }
-
- private DockPanel Docker;
- private TextBox StartLabel;
- private DateTimeEdit Start;
- private TextBox DurationLabel;
- private TimeSpanEdit Duration;
- private TextBox FinishLabel;
- private DateTimeEdit Finish;
- private TimeBlock Time = new();
- public override void Configure()
- {
- }
- protected override FrameworkElement CreateEditor()
- {
- Docker = new DockPanel();
- Docker.LastChildFill = false;
- CreateTimeEdit("From:", 0, StartChanged, false, out StartLabel, out Start);
- CreateTimeSpanEdit("Dur:", 4, DurationChanged, true, out DurationLabel, out Duration);
- CreateTimeEdit("To:", 8, FinishChanged, true, out FinishLabel, out Finish);
-
- return Docker;
- }
- private void StartChanged(TimeSpan start)
- {
- Time.Start = start;
- UpdateEditors();
- }
- private void DurationChanged(TimeSpan duration)
- {
- Time.Duration = duration;
- UpdateEditors();
- }
- private void FinishChanged(TimeSpan finish)
- {
- Time.Finish = finish;
- UpdateEditors();
- }
- private void UpdateEditors()
- {
- SetValue(Start, Time.Start);
- SetValue(Duration, Time.Duration);
- SetValue(Finish, Time.Finish);
- CheckChanged();
- }
- public override void SetEnabled(bool enabled)
- {
- base.SetEnabled(enabled);
- Start.IsEnabled = enabled;
- Duration.IsEnabled = enabled;
- Finish.IsEnabled = enabled;
- SetColor(enabled ? Color : Colors.WhiteSmoke);
- }
- private void CreateTimeSpanEdit(string header, int column, Action<TimeSpan> onChanged, bool leftMargin, out TextBox label, out TimeSpanEdit editor)
- {
-
- DockPanel dock = new DockPanel();
- dock.SetValue(DockPanel.DockProperty, Dock.Left);
- dock.Margin = new Thickness(leftMargin ? 5 : 0, 0, 0, 0);
- dock.Width = 150;
-
- var edit = new TimeSpanEdit
- {
- Format = "h:mm",
- MinValue = new TimeSpan(),
- MaxValue = TimeSpan.MaxValue,
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalContentAlignment = HorizontalAlignment.Center,
- ShowArrowButtons = false
- };
- edit.SetValue(DockPanel.DockProperty, Dock.Left);
- var brush = edit.BorderBrush;
- var thickness = edit.BorderThickness.Left;
- edit.BorderThickness = new Thickness(0, thickness, thickness, thickness);
-
- label = new TextBox
- {
- Text = header,
- Margin = new Thickness(0),
- BorderBrush = brush,
- BorderThickness = new Thickness(thickness,thickness,0,thickness),
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalContentAlignment = HorizontalAlignment.Left,
- IsReadOnly = true,
- Padding = new Thickness(2,0,0,0),
- Width = 40
- };
- label.SetValue(DockPanel.DockProperty, Dock.Left);
-
- edit.PreviewKeyDown += (o, e) =>
- {
- var separator = edit.Text.IndexOf(":");
- if (e.Key == Key.OemPeriod)
- {
- edit.Select(separator + 1, 2);
- e.Handled = true;
- }
- else if (e.Key == Key.Back)
- {
- if (string.Equals(edit.SelectedText, "00"))
- edit.Select(0, separator);
- else
- edit.SelectedText = "00";
- e.Handled = true;
- }
- else if (e.Key == Key.Tab)
- {
- if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.LeftShift))
- {
- if (edit.SelectionStart > separator)
- {
- edit.Select(0, separator);
- e.Handled = true;
- }
- }
- else
- {
- if (edit.SelectionLength != edit.Text.Length && edit.SelectionStart < separator)
- {
- edit.Select(separator + 1, 2);
- e.Handled = true;
- }
- }
- }
- };
- var changed = false;
- edit.ValueChanged += (o, e) =>
- {
- changed = true;
- };
- edit.LostFocus += (o, e) =>
- {
- if (changed)
- onChanged(GetValue(edit));
- };
- var less = new Button
- {
- Content = "<",
- Width = 23,
- Margin = new Thickness(2, 0, 0, 0),
- Focusable = false
- };
- less.SetValue(DockPanel.DockProperty, Dock.Right);
- less.Click += (o, e) =>
- {
- edit.Value = edit.Value.HasValue && edit.Value >= new TimeSpan(0, 15, 0)
- ? edit.Value.Value.Subtract(new TimeSpan(0, 15, 0))
- : new TimeSpan(0);
- onChanged(GetValue(edit));
- };
- var more = new Button
- {
- Content = ">",
- Width = 23,
- Margin = new Thickness(2, 0, 0, 0),
- Focusable = false
- };
- more.SetValue(DockPanel.DockProperty, Dock.Right);
- more.Click += (o, e) =>
- {
- edit.Value = edit.Value.HasValue ? edit.Value.Value.Add(new TimeSpan(0, 15, 0)) : new TimeSpan(0, 15, 0);
- onChanged(GetValue(edit));
- };
- dock.Children.Add(label);
- dock.Children.Add(more);
- dock.Children.Add(less);
- dock.Children.Add(edit);
- Docker.Children.Add(dock);
- editor = edit;
- }
- private void CreateTimeEdit(string header, int column, Action<TimeSpan> onChanged, bool leftMargin, out TextBox label, out DateTimeEdit editor)
- {
- DockPanel dock = new DockPanel();
- dock.SetValue(DockPanel.DockProperty, Dock.Left);
- dock.Margin = new Thickness(leftMargin ? 5 : 0, 0, 0, 0);
- dock.Width = 150;
-
- var edit = new DateTimeEdit
- {
- Pattern = DateTimePattern.CustomPattern,
- CustomPattern = "HH:mm", //DateTimeFormat.Custom,
- //FormatString = "HH:mm",
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalContentAlignment = HorizontalAlignment.Center,
- //TimeInterval = new TimeSpan(0, 15, 0),
- //ShowButtonSpinner = false
- DropDownView = DropDownViews.Clock,
- BorderThickness = new Thickness(0, 0.75, 0.75, 0.75),
- BorderBrush = new SolidColorBrush(Colors.Gray),
- IsButtonPopUpEnabled = false,
- };
- edit.SetValue(DockPanel.DockProperty, Dock.Left);
-
- label = new TextBox
- {
- Text = header,
- Margin = new Thickness(0),
- BorderBrush = new SolidColorBrush(Colors.Gray),
- BorderThickness = new Thickness(0.75, 0.75, 0, 0.75),
- VerticalContentAlignment = VerticalAlignment.Center,
- HorizontalContentAlignment = HorizontalAlignment.Left,
- Padding = new Thickness(2,0,0,0),
- IsReadOnly = true,
- Width = 40
- };
- label.SetValue(DockPanel.DockProperty, Dock.Left);
- edit.PreviewKeyDown += (o, e) =>
- {
- var separator = edit.Text.IndexOf(":");
- if (e.Key == Key.OemPeriod)
- {
- edit.Select(separator + 1, 2);
- e.Handled = true;
- }
- else if (e.Key == Key.Back)
- {
- if (string.Equals(edit.SelectedText, "00"))
- edit.Select(0, separator);
- else
- edit.SelectedText = "00";
- e.Handled = true;
- }
- else if (e.Key == Key.Tab)
- {
- if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.LeftShift))
- {
- if (edit.SelectionStart > separator)
- {
- edit.Select(0, separator);
- e.Handled = true;
- }
- }
- else
- {
- if (edit.SelectionLength != edit.Text.Length && edit.SelectionStart < separator)
- {
- edit.Select(separator + 1, 2);
- e.Handled = true;
- }
- }
- }
- };
- var changed = false;
- edit.DateTimeChanged += (o, e) =>
- {
- changed = true;
- };
- edit.LostFocus += (o, e) =>
- {
- if (changed)
- {
- onChanged(GetValue(edit));
- }
- };
- var less = new Button
- {
- Content = "<",
- Width = 23,
- Margin = new Thickness(2, 0, 0, 0),
- Focusable = false
- };
- less.SetValue(DockPanel.DockProperty, Dock.Right);
- less.Click += (o, e) =>
- {
- edit.DateTime = edit.DateTime.HasValue && edit.DateTime.Value.TimeOfDay >= new TimeSpan(0, 15, 0)
- ? edit.DateTime.Value.Subtract(new TimeSpan(0, 15, 0))
- : edit.DateTime;
- onChanged(GetValue(edit));
- };
- var more = new Button
- {
- Content = ">",
- Width = 23,
- Margin = new Thickness(2, 0, 0, 0),
- Focusable = false
- };
- more.SetValue(DockPanel.DockProperty, Dock.Right);
- more.Click += (o, e) =>
- {
- edit.DateTime = edit.DateTime.HasValue && edit.DateTime.Value.TimeOfDay < new TimeSpan(23, 45, 0)
- ? edit.DateTime.Value.Add(new TimeSpan(0, 15, 0))
- : edit.DateTime;
- onChanged(GetValue(edit));
- };
- dock.Children.Add(label);
- dock.Children.Add(more);
- dock.Children.Add(less);
- dock.Children.Add(edit);
- Docker.Children.Add(dock);
-
- editor = edit;
- }
- private static TimeSpan GetValue(DateTimeEdit edit)
- {
- var result = new TimeSpan(0);
- if (edit.DateTime.HasValue)
- result = edit.DateTime.Value.TimeOfDay;
- result = new TimeSpan(result.Hours, result.Minutes, 0);
- return result;
- }
- private static void SetValue(DateTimeEdit edit, TimeSpan value)
- {
- if (value.Ticks > 0)
- edit.DateTime = DateTime.MinValue.Add(value);
- else
- edit.DateTime = null;
- }
- private static TimeSpan GetValue(TimeSpanEdit edit)
- {
- var result = new TimeSpan(0);
- if (edit.Value.HasValue)
- result = edit.Value.Value;
- return result;
- }
- private static void SetValue(TimeSpanEdit edit, TimeSpan value)
- {
- edit.Value = value;
- }
- public override int DesiredHeight()
- {
- return 25;
- }
- public override int DesiredWidth()
- {
- return int.MaxValue;
- }
- protected override IEnumerable<KeyValuePair<string, object?>> GetChildValues()
- {
- yield return new("Start", Time.Start);
- yield return new("Duration", Time.Duration);
- yield return new("Finish", Time.Finish);
- }
- protected override object? GetChildValue(string property)
- {
- if (property == "Start") return Time.Start;
- if (property == "Duration") return Time.Duration;
- if (property == "Finish") return Time.Finish;
- return null;
- }
- protected override void SetChildValue(string property, object? value)
- {
- if (value is not TimeSpan timeSpan) return;
- if (property == "Start") Time.Start = timeSpan;
- else if (property == "Duration") Time.Duration = timeSpan;
- else if (property == "Finish") Time.Finish = timeSpan;
- UpdateEditors();
- }
- public override void SetColor(Color color)
- {
- StartLabel.Background = new SolidColorBrush(color);
- Start.Background = new SolidColorBrush(color);
- DurationLabel.Background = new SolidColorBrush(color);
- Duration.Background = new SolidColorBrush(color);
- FinishLabel.Background = new SolidColorBrush(color);
- Finish.Background = new SolidColorBrush(color);
- }
- public override void SetFocus()
- {
- Start.Focus();
- }
- }
- }
|