From d820cccf8bb64a27b02cea4d9ad72a742dc4eb8f Mon Sep 17 00:00:00 2001 From: Dominique Louis Date: Thu, 8 Oct 2020 16:09:21 +0100 Subject: =?UTF-8?q?Refactor=20to=20have=20individual=20Date=20and=20Time?= =?UTF-8?q?=20Controls=20for=20better=20mapping=E2=80=A6=20(#756)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Refactor to have individual Date and Time Controls for better mapping for .Forms * Add default control mapping to the TypeMap * Call our initialise value method. * Use custom controls and converters for our custom Date/Time * Let's return the 'broken' string, if an error occurs, until they correct it. --- .../Controls/BaseDateTimeEditorControl.cs | 58 +++ .../Controls/DateEditorControl.cs | 27 ++ .../Controls/DateTimeEditorControl.cs | 55 +-- .../Controls/TimeEditorControl.cs | 27 ++ .../PropertyEditorSelector.cs | 3 +- .../MockControls/MockSampleControl.cs | 9 +- .../MainWindow.xaml.cs | 1 + .../DateEditorControl.cs | 12 + .../DateToTextConverter.cs | 32 ++ .../EditorPropertySelector.cs | 7 +- .../Themes/Resources.xaml | 34 ++ .../TimeEditorControl.cs | 12 + .../TimeToTextConverter.cs | 32 ++ .../Xamarin.PropertyEditing.Windows.csproj | 450 +++++++++++---------- Xamarin.PropertyEditing/Common/Date.cs | 60 +++ Xamarin.PropertyEditing/Common/Time.cs | 59 +++ .../ViewModels/PropertiesViewModel.cs | 2 + 17 files changed, 605 insertions(+), 275 deletions(-) create mode 100644 Xamarin.PropertyEditing.Mac/Controls/BaseDateTimeEditorControl.cs create mode 100644 Xamarin.PropertyEditing.Mac/Controls/DateEditorControl.cs create mode 100644 Xamarin.PropertyEditing.Mac/Controls/TimeEditorControl.cs create mode 100644 Xamarin.PropertyEditing.Windows/DateEditorControl.cs create mode 100644 Xamarin.PropertyEditing.Windows/DateToTextConverter.cs create mode 100644 Xamarin.PropertyEditing.Windows/TimeEditorControl.cs create mode 100644 Xamarin.PropertyEditing.Windows/TimeToTextConverter.cs create mode 100644 Xamarin.PropertyEditing/Common/Date.cs create mode 100644 Xamarin.PropertyEditing/Common/Time.cs diff --git a/Xamarin.PropertyEditing.Mac/Controls/BaseDateTimeEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/BaseDateTimeEditorControl.cs new file mode 100644 index 0000000..795c44e --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/BaseDateTimeEditorControl.cs @@ -0,0 +1,58 @@ +using System; +using AppKit; +using Xamarin.PropertyEditing.ViewModels; + +namespace Xamarin.PropertyEditing.Mac +{ + internal abstract class BaseDateTimeEditorControl : PropertyEditorControl> + { + public NSDatePicker DatePicker { get; } + + public BaseDateTimeEditorControl (IHostResourceProvider hostResources) + : base (hostResources) + { + DatePicker = new NSDatePicker { + ControlSize = NSControlSize.Small, + DatePickerElements = NSDatePickerElementFlags.HourMinuteSecond | NSDatePickerElementFlags.YearMonthDateDay, + DatePickerStyle = NSDatePickerStyle.TextFieldAndStepper, + Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)), + TimeZone = Foundation.NSTimeZone.FromAbbreviation ("UTC"), + TranslatesAutoresizingMaskIntoConstraints = false, + }; + + // update the value on keypress + DatePicker.Activated += Editor_Activated; + + AddSubview (DatePicker); + + AddConstraints (new[] { + NSLayoutConstraint.Create (DatePicker, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f), + NSLayoutConstraint.Create (DatePicker, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1f, 0f), + NSLayoutConstraint.Create (DatePicker, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, 0), + NSLayoutConstraint.Create (DatePicker, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1f, -6f), + }); + } + + protected abstract void Editor_Activated (object sender, EventArgs e); + + public override NSView FirstKeyView => DatePicker; + public override NSView LastKeyView => DatePicker; + + protected override void SetEnabled () + { + DatePicker.Enabled = ViewModel.Property.CanWrite; + } + + protected override void UpdateAccessibilityValues () + { + DatePicker.AccessibilityTitle = string.Format (Properties.Resources.AccessibilityDateTime, ViewModel.Property.Name); + DatePicker.Enabled = DatePicker.Enabled; + } + + protected override void Dispose (bool disposing) + { + DatePicker.Activated -= Editor_Activated; + base.Dispose (disposing); + } + } +} \ No newline at end of file diff --git a/Xamarin.PropertyEditing.Mac/Controls/DateEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/DateEditorControl.cs new file mode 100644 index 0000000..7249f0d --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/DateEditorControl.cs @@ -0,0 +1,27 @@ +using System; +using AppKit; +using Xamarin.PropertyEditing.Common; + +namespace Xamarin.PropertyEditing.Mac +{ + internal class DateEditorControl : BaseDateTimeEditorControl + { + + public DateEditorControl (IHostResourceProvider hostResources) + : base (hostResources) + { + DatePicker.DatePickerElements = NSDatePickerElementFlags.YearMonthDateDay; + } + + protected override void Editor_Activated (object sender, EventArgs e) + { + ViewModel.Value = new Date (DatePicker.DateValue.ToDateTime ()); + } + + protected override void UpdateValue () + { + if (ViewModel.Value != null) + DatePicker.DateValue = ViewModel.Value.DateTime.ToNSDate (); + } + } +} diff --git a/Xamarin.PropertyEditing.Mac/Controls/DateTimeEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/DateTimeEditorControl.cs index 36532e2..53874ae 100644 --- a/Xamarin.PropertyEditing.Mac/Controls/DateTimeEditorControl.cs +++ b/Xamarin.PropertyEditing.Mac/Controls/DateTimeEditorControl.cs @@ -1,65 +1,22 @@ using System; -using AppKit; -using Xamarin.PropertyEditing.ViewModels; namespace Xamarin.PropertyEditing.Mac { - internal class DateTimeEditorControl : PropertyEditorControl> - { - private readonly NSDatePicker datePicker; - + internal class DateTimeEditorControl : BaseDateTimeEditorControl + { public DateTimeEditorControl (IHostResourceProvider hostResources) : base (hostResources) - { - this.datePicker = new NSDatePicker { - ControlSize = NSControlSize.Small, - DatePickerElements = NSDatePickerElementFlags.HourMinuteSecond | NSDatePickerElementFlags.YearMonthDateDay, - DatePickerStyle = NSDatePickerStyle.TextFieldAndStepper, - Font = NSFont.SystemFontOfSize (NSFont.SystemFontSizeForControlSize (NSControlSize.Small)), - TranslatesAutoresizingMaskIntoConstraints = false - }; - - // update the value on keypress - this.datePicker.Activated += Editor_Activated; - - AddSubview (this.datePicker); - - AddConstraints (new[] { - NSLayoutConstraint.Create (this.datePicker, NSLayoutAttribute.CenterY, NSLayoutRelation.Equal, this, NSLayoutAttribute.CenterY, 1f, 0f), - NSLayoutConstraint.Create (this.datePicker, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, this, NSLayoutAttribute.Leading, 1f, 0f), - NSLayoutConstraint.Create (this.datePicker, NSLayoutAttribute.Width, NSLayoutRelation.Equal, this, NSLayoutAttribute.Width, 1f, 0), - NSLayoutConstraint.Create (this.datePicker, NSLayoutAttribute.Height, NSLayoutRelation.Equal, this, NSLayoutAttribute.Height, 1f, -6f), - }); - } - - private void Editor_Activated (object sender, EventArgs e) { - ViewModel.Value = this.datePicker.DateValue.ToDateTime (); } - - public override NSView FirstKeyView => this.datePicker; - public override NSView LastKeyView => this.datePicker; - protected override void UpdateValue () - { - this.datePicker.DateValue = ViewModel.Value.ToNSDate (); - } - - protected override void SetEnabled () + protected override void Editor_Activated (object sender, EventArgs e) { - this.datePicker.Enabled = ViewModel.Property.CanWrite; - } - - protected override void UpdateAccessibilityValues () - { - this.datePicker.AccessibilityTitle = string.Format (Properties.Resources.AccessibilityDateTime, ViewModel.Property.Name); - this.datePicker.Enabled = this.datePicker.Enabled; + ViewModel.Value = DatePicker.DateValue.ToDateTime (); } - protected override void Dispose (bool disposing) + protected override void UpdateValue () { - this.datePicker.Activated -= Editor_Activated; - base.Dispose (disposing); + DatePicker.DateValue = ViewModel.Value.ToNSDate (); } } } diff --git a/Xamarin.PropertyEditing.Mac/Controls/TimeEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/TimeEditorControl.cs new file mode 100644 index 0000000..f4ccd3b --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/TimeEditorControl.cs @@ -0,0 +1,27 @@ +using System; +using AppKit; +using Xamarin.PropertyEditing.Common; + +namespace Xamarin.PropertyEditing.Mac +{ + internal class TimeEditorControl : BaseDateTimeEditorControl