summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/SwitchRenderer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.MacOS/Renderers/SwitchRenderer.cs')
-rw-r--r--Xamarin.Forms.Platform.MacOS/Renderers/SwitchRenderer.cs61
1 files changed, 61 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.MacOS/Renderers/SwitchRenderer.cs b/Xamarin.Forms.Platform.MacOS/Renderers/SwitchRenderer.cs
new file mode 100644
index 0000000..c93007e
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Renderers/SwitchRenderer.cs
@@ -0,0 +1,61 @@
+´╗┐using System;
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public class SwitchRenderer : ViewRenderer<Switch, NSButton>
+ {
+ bool _disposed;
+
+ IElementController ElementController => Element;
+
+ protected override void OnElementChanged(ElementChangedEventArgs<Switch> e)
+ {
+ if (e.OldElement != null)
+ e.OldElement.Toggled -= OnElementToggled;
+
+ if (e.NewElement != null)
+ {
+ if (Control == null)
+ {
+ SetNativeControl(new NSButton { AllowsMixedState = false, Title = string.Empty });
+
+ Control.SetButtonType(NSButtonType.Switch);
+ Control.Activated += OnControlActivated;
+ }
+
+ UpdateState();
+ e.NewElement.Toggled += OnElementToggled;
+ }
+
+ base.OnElementChanged(e);
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && !_disposed)
+ {
+ _disposed = true;
+ if (Control != null)
+ Control.Activated -= OnControlActivated;
+ }
+
+ base.Dispose(disposing);
+ }
+
+ void OnControlActivated(object sender, EventArgs e)
+ {
+ ElementController?.SetValueFromRenderer(Switch.IsToggledProperty, Control.State == NSCellStateValue.On);
+ }
+
+ void OnElementToggled(object sender, EventArgs e)
+ {
+ UpdateState();
+ }
+
+ void UpdateState()
+ {
+ Control.State = Element.IsToggled ? NSCellStateValue.On : NSCellStateValue.Off;
+ }
+ }
+} \ No newline at end of file