[Tizen] Add Checkbox (#6526) fixes #1728
authorKangho Hur <kangho.hur@samsung.com>
Wed, 26 Jun 2019 15:52:22 +0000 (00:52 +0900)
committerRui Marinho <me@ruimarinho.net>
Wed, 26 Jun 2019 15:52:22 +0000 (16:52 +0100)
Xamarin.Forms.Material.Tizen/MaterialCheckBoxRenderer.cs [new file with mode: 0644]
Xamarin.Forms.Platform.Tizen/Extensions/ColorExtensions.cs
Xamarin.Forms.Platform.Tizen/Properties/AssemblyInfo.cs
Xamarin.Forms.Platform.Tizen/Renderers/CheckBoxRenderer.cs [new file with mode: 0644]

diff --git a/Xamarin.Forms.Material.Tizen/MaterialCheckBoxRenderer.cs b/Xamarin.Forms.Material.Tizen/MaterialCheckBoxRenderer.cs
new file mode 100644 (file)
index 0000000..cd7a13d
--- /dev/null
@@ -0,0 +1,21 @@
+using Xamarin.Forms;
+using Xamarin.Forms.Platform.Tizen;
+using Xamarin.Forms.Material.Tizen;
+using Tizen.NET.MaterialComponents;
+using TForms = Xamarin.Forms.Platform.Tizen.Forms;
+
+[assembly: ExportRenderer(typeof(CheckBox), typeof(MaterialCheckBoxRenderer), new[] { typeof(VisualMarker.MaterialVisual) })]
+namespace Xamarin.Forms.Material.Tizen
+{
+       public class MaterialCheckBoxRenderer : CheckBoxRenderer
+       {
+               protected override void OnElementChanged(ElementChangedEventArgs<CheckBox> e)
+               {
+                       if (Control == null)
+                       {
+                               SetNativeControl(new MCheckBox(TForms.NativeParent));
+                       }
+                       base.OnElementChanged(e);
+               }
+       }
+}
index ae34827..95e9a0d 100644 (file)
@@ -22,6 +22,16 @@ namespace Xamarin.Forms.Platform.Tizen
                        }
                }
 
+               public static Color WithAlpha(this Color color, double alpha)
+               {
+                       return new Color(color.R, color.G, color.B, (int)(255 * alpha));
+               }
+
+               public static Color WithPremultiplied(this Color color, double alpha)
+               {
+                       return new Color((int)(color.R * alpha), (int)(color.G * alpha), (int)(color.B * alpha), color.A);
+               }
+
                /// <summary>
                /// Returns a string representing the provided ElmSharp.Color instance in a hexagonal notation
                /// </summary>
index 13a0854..0cfab87 100644 (file)
@@ -26,6 +26,7 @@ using Xamarin.Forms.Platform.Tizen;
 [assembly: ExportRenderer(typeof(TimePicker), typeof(TimePickerRenderer))]
 [assembly: ExportRenderer(typeof(ProgressBar), typeof(ProgressBarRenderer))]
 [assembly: ExportRenderer(typeof(Switch), typeof(SwitchRenderer))]
+[assembly: ExportRenderer(typeof(CheckBox), typeof(CheckBoxRenderer))]
 [assembly: ExportRenderer(typeof(ListView), typeof(ListViewRenderer))]
 [assembly: ExportRenderer(typeof(BoxView), typeof(BoxViewRenderer))]
 [assembly: ExportRenderer(typeof(ActivityIndicator), typeof(ActivityIndicatorRenderer))]
diff --git a/Xamarin.Forms.Platform.Tizen/Renderers/CheckBoxRenderer.cs b/Xamarin.Forms.Platform.Tizen/Renderers/CheckBoxRenderer.cs
new file mode 100644 (file)
index 0000000..e13925d
--- /dev/null
@@ -0,0 +1,78 @@
+using System;
+using ElmSharp;
+using EColor = ElmSharp.Color;
+
+namespace Xamarin.Forms.Platform.Tizen
+{
+       public class CheckBoxRenderer : ViewRenderer<CheckBox, Check>
+       {
+               readonly string[] _onColorParts;
+               readonly string[] _onColorEdjeParts;
+
+               public CheckBoxRenderer()
+               {
+                       _onColorParts = Device.Idiom == TargetIdiom.Watch ? new string[] {"outer_bg_on", "outer_bg_on_pressed", "check_on", "check_on_pressed"} : new string[] {"bg_on", "stroke"};
+                       _onColorEdjeParts = new string[_onColorParts.Length];
+                       RegisterPropertyHandler(CheckBox.IsCheckedProperty, UpdateIsChecked);
+                       RegisterPropertyHandler(CheckBox.ColorProperty, UpdateOnColor);
+               }
+
+               protected override void OnElementChanged(ElementChangedEventArgs<CheckBox> e)
+               {
+                       if (Control == null)
+                       {
+                               SetNativeControl(new Check(Forms.NativeParent));
+                               Control.StateChanged += OnStateChanged;
+                               for (int i=0; i<_onColorParts.Length; i++)
+                               {
+                                       _onColorEdjeParts[i] = Control.ClassName.ToLower().Replace("elm_", "") + "/" + _onColorParts[i];
+                               }
+                       }
+                       base.OnElementChanged(e);
+               }
+
+               protected override void Dispose(bool disposing)
+               {
+                       if (disposing)
+                       {
+                               if (Control != null)
+                               {
+                                       Control.StateChanged -= OnStateChanged;
+                               }
+                       }
+                       base.Dispose(disposing);
+               }
+
+               void OnStateChanged(object sender, EventArgs e)
+               {
+                       Element.SetValueFromRenderer(CheckBox.IsCheckedProperty, Control.IsChecked);
+               }
+
+               void UpdateIsChecked()
+               {
+                       Control.IsChecked = Element.IsChecked;
+               }
+
+               void UpdateOnColor(bool initialize)
+               {
+                       if (initialize && Element.Color.IsDefault)
+                               return;
+
+                       if (Element.Color.IsDefault)
+                       {
+                               foreach(string s in _onColorEdjeParts)
+                               {
+                                       Control.EdjeObject.DeleteColorClass(s);
+                               }
+                       }
+                       else
+                       {
+                               EColor color = Element.Color.ToNative();
+                               foreach (string s in _onColorParts)
+                               {
+                                       Control.SetPartColor(s, color);
+                               }
+                       }
+               }
+       }
+}