summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Tizen/Renderers/StepperRenderer.cs
blob: e5e4c58d49af0ae9fd1be00e3ba189870f724688 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using ElmSharp;

namespace Xamarin.Forms.Platform.Tizen
{
	public class StepperRenderer : ViewRenderer<Stepper, Spinner>
	{

		public StepperRenderer()
		{
			RegisterPropertyHandler(Stepper.ValueProperty, UpdateValue);
			RegisterPropertyHandler(Stepper.MinimumProperty, UpdateMinimum);
			RegisterPropertyHandler(Stepper.MaximumProperty, UpdateMaximum);
			RegisterPropertyHandler(Stepper.IncrementProperty, UpdateStep);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<Stepper> e)
		{
			if (Control == null)
			{
				var stepper = new Spinner(Forms.Context.MainWindow)
				{
					IsEditable = false,
				};

				SetNativeControl(stepper);
			}

			if (e.OldElement != null)
			{
				Control.ValueChanged -= StepperValueChangedHandler;
			}

			if (e.NewElement != null)
			{
				Control.ValueChanged += StepperValueChangedHandler;
			}

			base.OnElementChanged(e);
		}

		void StepperValueChangedHandler(object sender, EventArgs e)
		{
			double newValue = Control.Value;
			((IElementController)Element).SetValueFromRenderer(Stepper.ValueProperty, newValue);

			// Determines how many decimal places are there in current Stepper's value.
			// The 15 pound characters below correspond to the maximum precision of Double type.
			var decimalValue = Decimal.Parse(newValue.ToString("0.###############"));

			// GetBits() method returns an array of four 32-bit integer values.
			// The third (0-indexing) element of an array contains the following information:
			//     bits 00-15: unused, required to be 0
			//     bits 16-23: an exponent between 0 and 28 indicating the power of 10 to divide the integer number passed as a parameter.
			//                 Conversely this is the number of decimal digits in the number as well.
			//     bits 24-30: unused, required to be 0
			//     bit     31: indicates the sign. 0 means positive number, 1 is for negative numbers.
			//
			// The precision information needs to be extracted from bits 16-23 of third element of an array
			// returned by GetBits() call. Right-shifting by 16 bits followed by zeroing anything else results
			// in a nice conversion of this data to integer variable.
			var precision = (Decimal.GetBits(decimalValue)[3] >> 16) & 0x000000FF;

			// Sets Stepper's inner label decimal format to use exactly as many decimal places as needed:
			Control.LabelFormat = string.Format("%.{0}f", precision);
		}

		protected void UpdateValue()
		{
			Control.Value = Element.Value;
		}

		protected void UpdateMinimum()
		{
			Control.Minimum = Element.Minimum;
		}

		protected void UpdateMaximum()
		{
			Control.Maximum = Element.Maximum;
		}

		void UpdateStep()
		{
			Control.Step = Element.Increment;
		}
	}
}