summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/TimePickerRenderer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.MacOS/Renderers/TimePickerRenderer.cs')
-rw-r--r--Xamarin.Forms.Platform.MacOS/Renderers/TimePickerRenderer.cs104
1 files changed, 104 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.MacOS/Renderers/TimePickerRenderer.cs b/Xamarin.Forms.Platform.MacOS/Renderers/TimePickerRenderer.cs
new file mode 100644
index 0000000..fc645c4
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Renderers/TimePickerRenderer.cs
@@ -0,0 +1,104 @@
+´╗┐using System;
+using System.ComponentModel;
+using AppKit;
+using Foundation;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public class TimePickerRenderer : ViewRenderer<TimePicker, NSDatePicker>
+ {
+ NSColor _defaultTextColor;
+ NSColor _defaultBackgroundColor;
+ bool _disposed;
+
+ IElementController ElementController => Element;
+
+ protected override void OnElementChanged(ElementChangedEventArgs<TimePicker> e)
+ {
+ base.OnElementChanged(e);
+
+ if (e.NewElement != null)
+ {
+ if (Control == null)
+ {
+ SetNativeControl(new NSDatePicker
+ {
+ DatePickerMode = NSDatePickerMode.Single,
+ TimeZone = new NSTimeZone("UTC"),
+ DatePickerStyle = NSDatePickerStyle.TextFieldAndStepper,
+ DatePickerElements = NSDatePickerElementFlags.HourMinuteSecond
+ });
+
+ Control.ValidateProposedDateValue += HandleValueChanged;
+ _defaultTextColor = Control.TextColor;
+ _defaultBackgroundColor = Control.BackgroundColor;
+ }
+
+ UpdateTime();
+ UpdateTextColor();
+ }
+ }
+
+ protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
+ {
+ base.OnElementPropertyChanged(sender, e);
+
+ if (e.PropertyName == TimePicker.TimeProperty.PropertyName ||
+ e.PropertyName == TimePicker.FormatProperty.PropertyName)
+ UpdateTime();
+
+ if (e.PropertyName == TimePicker.TextColorProperty.PropertyName ||
+ e.PropertyName == VisualElement.IsEnabledProperty.PropertyName)
+ UpdateTextColor();
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && !_disposed)
+ {
+ if (Control != null)
+ Control.ValidateProposedDateValue -= HandleValueChanged;
+
+ _disposed = true;
+ }
+ base.Dispose(disposing);
+ }
+
+ protected override void SetBackgroundColor(Color color)
+ {
+ base.SetBackgroundColor(color);
+
+ if (Control == null)
+ return;
+ Control.BackgroundColor = color == Color.Default ? _defaultBackgroundColor : color.ToNSColor();
+ }
+
+ void HandleValueChanged(object sender, NSDatePickerValidatorEventArgs e)
+ {
+ ElementController?.SetValueFromRenderer(TimePicker.TimeProperty,
+ Control.DateValue.ToDateTime() - new DateTime(2001, 1, 1));
+ }
+
+ void UpdateTime()
+ {
+ if (Control == null || Element == null)
+ return;
+ var time = new DateTime(2001, 1, 1).Add(Element.Time);
+ var newDate = time.ToNSDate();
+ if (!Equals(Control.DateValue, newDate))
+ Control.DateValue = newDate;
+ }
+
+ 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