diff options
author | Dominique Louis <dominique@Dominiques-MacBook-Pro-2.local> | 2018-11-23 01:39:31 +0300 |
---|---|---|
committer | Dominique Louis <dominique@Dominiques-MacBook-Pro-2.local> | 2018-11-23 13:08:25 +0300 |
commit | fdc5ee8e221dbcc6b7c466c7e1768c45d02db5cb (patch) | |
tree | 835a4f1532572d5269f04f39a28ab5c4668a0cd1 | |
parent | 9ea098e431e87513f474c21553b3de6f90678cd1 (diff) |
[Mac] Example of Creating a Control from scratch.dominique-DTExampleForJose
8 files changed, 226 insertions, 0 deletions
diff --git a/Xamarin.PropertyEditing.Mac/Controls/DateTimeEditorControl.cs b/Xamarin.PropertyEditing.Mac/Controls/DateTimeEditorControl.cs new file mode 100644 index 0000000..72a4265 --- /dev/null +++ b/Xamarin.PropertyEditing.Mac/Controls/DateTimeEditorControl.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections; +using AppKit; +using CoreGraphics; +using Foundation; +using Xamarin.PropertyEditing.Drawing; +using Xamarin.PropertyEditing.Mac.Resources; +using Xamarin.PropertyEditing.ViewModels; + +namespace Xamarin.PropertyEditing.Mac +{ + internal class DateTimeEditorControl : PropertyEditorControl<PropertyViewModel<CommonDateTime>> + { + internal NSDatePicker datePicker; + + public override NSView FirstKeyView => this.datePicker; + public override NSView LastKeyView => this.datePicker; + + public DateTimeEditorControl () + { + this.datePicker = new NSDatePicker (new CGRect (4, 0, 170, 24)) { + DatePickerStyle = NSDatePickerStyle.TextFieldAndStepper, + }; + + this.datePicker.Activated += OnInputUpdated; + + AddSubview (this.datePicker); + + /* Some controle may require constraints this.DoConstraints (new[] { + this.datePicker.ConstraintTo (this, (xe, c) => xe.Width == 170), + this.datePicker.ConstraintTo (this, (xe, c) => xe.Height == DefaultControlHeight), + }); */ + + UpdateTheme (); + } + + protected override void HandleErrorsChanged (object sender, System.ComponentModel.DataErrorsChangedEventArgs e) + { + UpdateErrorsDisplayed (ViewModel.GetErrors (ViewModel.Property.Name)); + } + + protected override void UpdateErrorsDisplayed (IEnumerable errors) + { + if (ViewModel.HasErrors) { + SetErrors (errors); + } else { + SetErrors (null); + SetEnabled (); + } + } + + protected override void SetEnabled () + { + this.datePicker.Enabled = ViewModel.Property.CanWrite; + } + + protected override void UpdateAccessibilityValues () + { + this.datePicker.AccessibilityEnabled = this.datePicker.Enabled; + // TODO With Correct Localization - this.datePicker.AccessibilityTitle = string.Format (LocalizationResources.AccessibilityXEditor, ViewModel.Property.Name); + } + + protected override void UpdateValue () + { + this.datePicker.DateValue = NSDate.FromTimeIntervalSinceReferenceDate (ViewModel.Value.Ticks); // From* may need tweaking. + } + + protected void OnInputUpdated (object sender, EventArgs e) + { + NSDateComponents dateComponents = NSCalendar.CurrentCalendar.Components (NSCalendarUnit.Year | NSCalendarUnit.Month | NSCalendarUnit.Day | NSCalendarUnit.Hour | NSCalendarUnit.Minute | NSCalendarUnit.Second, this.datePicker.DateValue); + ViewModel.Value = new CommonDateTime ((int)dateComponents.Year, (int)dateComponents.Month, (int)dateComponents.Day, (int)dateComponents.Hour, (int)dateComponents.Minute, (int)dateComponents.Second); + } + } +} diff --git a/Xamarin.PropertyEditing.Mac/PropertyEditorSelector.cs b/Xamarin.PropertyEditing.Mac/PropertyEditorSelector.cs index f3f2afd..c660a53 100644 --- a/Xamarin.PropertyEditing.Mac/PropertyEditorSelector.cs +++ b/Xamarin.PropertyEditing.Mac/PropertyEditorSelector.cs @@ -54,6 +54,7 @@ namespace Xamarin.PropertyEditing.Mac {typeof (RatioViewModel), typeof (RatioEditorControl<CommonRatio>)}, {typeof (ThicknessPropertyViewModel), typeof (CommonThicknessEditorControl) }, {typeof (PropertyGroupViewModel), typeof (GroupEditorControl)}, + {typeof (DateTimePropertyViewModel), typeof (DateTimeEditorControl) }, }; } } diff --git a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj index c2dc90d..d82bf33 100644 --- a/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj +++ b/Xamarin.PropertyEditing.Mac/Xamarin.PropertyEditing.Mac.csproj @@ -143,6 +143,7 @@ <Compile Include="PropertyInlinePreviewSelector.cs" />
<Compile Include="Controls\EditorContainer.cs" />
<Compile Include="Controls\Layout.cs" />
+ <Compile Include="Controls\DateTimeEditorControl.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controls\" />
diff --git a/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs b/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs index c724cc1..95f38f3 100644 --- a/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs +++ b/Xamarin.PropertyEditing.Tests/MockControls/MockSampleControl.cs @@ -80,6 +80,8 @@ namespace Xamarin.PropertyEditing.Tests.MockControls valueSources: ValueSources.Default | ValueSources.Local | ValueSources.Resource); AddProperty<CommonColor> (this.colorPropertyInfo); + AddProperty<CommonDateTime> ("DateTime", ReadWrite); + AddEvents ("Click", "Hover", "Focus"); } diff --git a/Xamarin.PropertyEditing/Drawing/CommonDateTime.cs b/Xamarin.PropertyEditing/Drawing/CommonDateTime.cs new file mode 100644 index 0000000..f63f920 --- /dev/null +++ b/Xamarin.PropertyEditing/Drawing/CommonDateTime.cs @@ -0,0 +1,59 @@ +using System; + +namespace Xamarin.PropertyEditing.Drawing +{ + [Serializable] + public struct CommonDateTime : IEquatable<CommonDateTime> + { + private readonly DateTime dateTime; + + public int Year { get => this.dateTime.Year; } + public int Month { get => this.dateTime.Month; } + public int Day { get => this.dateTime.Day; } + + public int Hour { get => this.dateTime.Hour; } + public int Minute { get => this.dateTime.Minute; } + public int Second { get => this.dateTime.Second; } + + public long Ticks { get { return this.dateTime.Ticks; } } + + public CommonDateTime (int year = 0, int month = 0, int day = 0, int hour = 0, int minute = 0, int second = 0) + { + this.dateTime = new DateTime (year, month, day, hour, minute, second); + } + + public CommonDateTime (long ticks) + { + this.dateTime = new DateTime (ticks); + } + + public override bool Equals (object obj) + { + return obj is CommonPoint && Equals ((CommonPoint)obj); + } + + public bool Equals (CommonDateTime other) + { + return this.Year == other.Year + && this.Month == other.Month + && this.Day == other.Day + && this.Hour == other.Hour + && this.Minute == other.Minute + && this.Second == other.Second; + } + + public override int GetHashCode () + { + var hashCode = 1861411796; + unchecked { + hashCode = hashCode * -1521134296 + Year.GetHashCode (); + hashCode = hashCode * -1521134296 + Month.GetHashCode (); + hashCode = hashCode * -1521134296 + Day.GetHashCode (); + hashCode = hashCode * -1521134296 + Hour.GetHashCode (); + hashCode = hashCode * -1521134296 + Minute.GetHashCode (); + hashCode = hashCode * -1521134296 + Second.GetHashCode (); + } + return hashCode; + } + } +} diff --git a/Xamarin.PropertyEditing/ViewModels/DateTimePropertyViewModel.cs b/Xamarin.PropertyEditing/ViewModels/DateTimePropertyViewModel.cs new file mode 100644 index 0000000..67bf892 --- /dev/null +++ b/Xamarin.PropertyEditing/ViewModels/DateTimePropertyViewModel.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using Xamarin.PropertyEditing.Drawing; + +namespace Xamarin.PropertyEditing.ViewModels +{ + internal class DateTimePropertyViewModel + : PropertyViewModel<CommonDateTime> + { + public int Year { + get { return Value.Year; } + set { + if (Value.Year == value) + return; + + Value = new CommonDateTime (value, Value.Month, Value.Day, Value.Hour, Value.Minute, Value.Second); + } + } + + public int Month { + get { return Value.Month; } + set { + if (Value.Month == value) + return; + + Value = new CommonDateTime (Value.Year, value, Value.Day, Value.Hour, Value.Minute, Value.Second); + } + } + + public int Day { + get { return Value.Day; } + set { + if (Value.Day == value) + return; + + Value = new CommonDateTime (Value.Year, Value.Month, value, Value.Hour, Value.Minute, Value.Second); + } + } + + public int Hour { + get { return Value.Hour; } + set { + if (Value.Hour == value) + return; + + Value = new CommonDateTime (Value.Year, Value.Month, Value.Day, value, Value.Minute, Value.Second); + } + } + + public int Minute { + get { return Value.Minute; } + set { + if (Value.Minute == value) + return; + + Value = new CommonDateTime (Value.Year, Value.Month, Value.Day, Value.Hour, value, Value.Second); + } + } + + public int Second { + get { return Value.Minute; } + set { + if (Value.Minute == value) + return; + + Value = new CommonDateTime (Value.Year, Value.Month, Value.Day, Value.Hour, Value.Minute, value); + } + } + + public DateTimePropertyViewModel (TargetPlatform platform, IPropertyInfo property, IEnumerable<IObjectEditor> editors, PropertyVariation variation = null) + : base (platform, property, editors, variation) + { + } + + protected override void OnValueChanged () + { + base.OnValueChanged (); + OnPropertyChanged (nameof (Year)); + OnPropertyChanged (nameof (Month)); + OnPropertyChanged (nameof (Day)); + OnPropertyChanged (nameof (Hour)); + OnPropertyChanged (nameof (Minute)); + OnPropertyChanged (nameof (Second)); + } + } +} diff --git a/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs b/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs index 0ee785b..6e36c87 100644 --- a/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs +++ b/Xamarin.PropertyEditing/ViewModels/PropertiesViewModel.cs @@ -615,6 +615,7 @@ namespace Xamarin.PropertyEditing.ViewModels { typeof(Resource), (tp,p,e,v) => new PropertyViewModel<Resource> (tp, p, e, v) }, { typeof(object), (tp,p,e,v) => new ObjectPropertyViewModel (tp, p, e, v) }, { typeof(CommonRatio), (tp, p, e, v) => new RatioViewModel (tp, p, e, v) }, + { typeof(CommonDateTime), (tp,p,e,v) => new DateTimePropertyViewModel (tp, p, e, v) }, }; } } diff --git a/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj b/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj index 39ba238..fec9db7 100644 --- a/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj +++ b/Xamarin.PropertyEditing/Xamarin.PropertyEditing.csproj @@ -171,6 +171,8 @@ <Compile Include="IReadOnlyOrderedDictionary.cs" /> <Compile Include="ViewModels\CreateVariationEventArgs.cs" /> <Compile Include="ViewModels\IPropertyValue.cs" /> + <Compile Include="Drawing\CommonDateTime.cs" /> + <Compile Include="ViewModels\DateTimePropertyViewModel.cs" /> </ItemGroup> <ItemGroup> <Compile Include="Themes\BaseThemeManager.cs" /> |