summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/DatePickerRenderer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.MacOS/Renderers/DatePickerRenderer.cs')
-rw-r--r--Xamarin.Forms.Platform.MacOS/Renderers/DatePickerRenderer.cs138
1 files changed, 138 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.MacOS/Renderers/DatePickerRenderer.cs b/Xamarin.Forms.Platform.MacOS/Renderers/DatePickerRenderer.cs
new file mode 100644
index 0000000..f31fa36
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Renderers/DatePickerRenderer.cs
@@ -0,0 +1,138 @@
+´╗┐using System;
+using System.ComponentModel;
+using AppKit;
+using Foundation;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public class DatePickerRenderer : ViewRenderer<DatePicker, NSDatePicker>
+ {
+ NSDatePicker _picker;
+ NSColor _defaultTextColor;
+ NSColor _defaultBackgroundColor;
+ bool _disposed;
+
+ IElementController ElementController => Element;
+
+ protected override void OnElementChanged(ElementChangedEventArgs<DatePicker> e)
+ {
+ base.OnElementChanged(e);
+
+ if (e.OldElement == null)
+ {
+ if (Control == null)
+ {
+ _picker = new NSDatePicker
+ {
+ DatePickerMode = NSDatePickerMode.Single,
+ TimeZone = new NSTimeZone("UTC"),
+ DatePickerStyle = NSDatePickerStyle.TextFieldAndStepper,
+ DatePickerElements = NSDatePickerElementFlags.YearMonthDateDay
+ };
+ _picker.ValidateProposedDateValue += HandleValueChanged;
+ _defaultTextColor = _picker.TextColor;
+ _defaultBackgroundColor = _picker.BackgroundColor;
+
+ SetNativeControl(_picker);
+ }
+ }
+
+ UpdateDateFromModel();
+ UpdateMaximumDate();
+ UpdateMinimumDate();
+ UpdateTextColor();
+ }
+
+ protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
+ {
+ base.OnElementPropertyChanged(sender, e);
+
+ if (e.PropertyName == DatePicker.DateProperty.PropertyName ||
+ e.PropertyName == DatePicker.FormatProperty.PropertyName)
+ UpdateDateFromModel();
+ else if (e.PropertyName == DatePicker.MinimumDateProperty.PropertyName)
+ UpdateMinimumDate();
+ else if (e.PropertyName == DatePicker.MaximumDateProperty.PropertyName)
+ UpdateMaximumDate();
+ else if (e.PropertyName == DatePicker.TextColorProperty.PropertyName ||
+ e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
+ UpdateTextColor();
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && !_disposed)
+ {
+ if (_picker != null)
+ _picker.ValidateProposedDateValue -= HandleValueChanged;
+
+ _disposed = true;
+ }
+ base.Dispose(disposing);
+ }
+
+ protected override void SetBackgroundColor(Color color)
+ {
+ base.SetBackgroundColor(color);
+
+ if (Control == null)
+ return;
+
+ if (color == Color.Default)
+ Control.BackgroundColor = _defaultBackgroundColor;
+ else
+ Control.BackgroundColor = color.ToNSColor();
+ }
+
+ void HandleValueChanged(object sender, NSDatePickerValidatorEventArgs e)
+ {
+ if (Control == null || Element == null)
+ return;
+ ElementController?.SetValueFromRenderer(DatePicker.DateProperty, _picker.DateValue.ToDateTime().Date);
+ }
+
+ void OnEnded(object sender, EventArgs eventArgs)
+ {
+ ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, false);
+ }
+
+ void OnStarted(object sender, EventArgs eventArgs)
+ {
+ ElementController?.SetValueFromRenderer(VisualElement.IsFocusedPropertyKey, true);
+ }
+
+ void UpdateDateFromModel()
+ {
+ if (Control == null || Element == null)
+ return;
+ if (_picker.DateValue.ToDateTime().Date != Element.Date.Date)
+ _picker.DateValue = Element.Date.ToNSDate();
+ }
+
+ void UpdateMaximumDate()
+ {
+ if (Control == null || Element == null)
+ return;
+ _picker.MaxDate = Element.MaximumDate.ToNSDate();
+ }
+
+ void UpdateMinimumDate()
+ {
+ if (Control == null || Element == null)
+ return;
+ _picker.MinDate = Element.MinimumDate.ToNSDate();
+ }
+
+ void UpdateTextColor()
+ {
+ if (Control == null || Element == null)
+ return;
+ var textColor = Element.TextColor;
+
+ if (textColor.IsDefault || !Element.IsEnabled)
+ Control.TextColor = _defaultTextColor;
+ else
+ Control.TextColor = textColor.ToNSColor();
+ }
+ }
+} \ No newline at end of file