summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.UAP/StepperControl.cs
blob: 0de09318af76323d5149b63c9eba2630437492e1 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace Xamarin.Forms.Platform.UWP
{
	public sealed class StepperControl : Control
	{
		public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(StepperControl), new PropertyMetadata(default(double), OnValueChanged));

		public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(StepperControl), new PropertyMetadata(default(double), OnMaxMinChanged));

		public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(StepperControl), new PropertyMetadata(default(double), OnMaxMinChanged));

		public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(StepperControl),
			new PropertyMetadata(default(double), OnIncrementChanged));

		Windows.UI.Xaml.Controls.Button _plus;
		Windows.UI.Xaml.Controls.Button _minus;
		VisualStateCache _plusStateCache;
		VisualStateCache _minusStateCache;

		public StepperControl()
		{
			DefaultStyleKey = typeof(StepperControl);
		}

		public double Increment
		{
			get { return (double)GetValue(IncrementProperty); }
			set { SetValue(IncrementProperty, value); }
		}

		public double Maximum
		{
			get { return (double)GetValue(MaximumProperty); }
			set { SetValue(MaximumProperty, value); }
		}

		public double Minimum
		{
			get { return (double)GetValue(MinimumProperty); }
			set { SetValue(MinimumProperty, value); }
		}

		public double Value
		{
			get { return (double)GetValue(ValueProperty); }
			set { SetValue(ValueProperty, value); }
		}

		public event EventHandler ValueChanged;

		protected override void OnApplyTemplate()
		{
			base.OnApplyTemplate();

			_plus = GetTemplateChild("Plus") as Windows.UI.Xaml.Controls.Button;
			if (_plus != null)
				_plus.Click += OnPlusClicked;

			_minus = GetTemplateChild("Minus") as Windows.UI.Xaml.Controls.Button;
			if (_minus != null)
				_minus.Click += OnMinusClicked;

			UpdateEnabled(Value);
		}

		static void OnIncrementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var stepper = (StepperControl)d;
			stepper.UpdateEnabled(stepper.Value);
		}

		static void OnMaxMinChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var stepper = (StepperControl)d;
			stepper.UpdateEnabled(stepper.Value);
		}

		void OnMinusClicked(object sender, RoutedEventArgs e)
		{
			UpdateValue(-Increment);
		}

		void OnPlusClicked(object sender, RoutedEventArgs e)
		{
			UpdateValue(+Increment);
		}

		static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var stepper = (StepperControl)d;
			stepper.UpdateEnabled((double)e.NewValue);

			EventHandler changed = stepper.ValueChanged;
			if (changed != null)
				changed(d, EventArgs.Empty);
		}

		VisualStateCache PseudoDisable(Control control)
		{
			if (VisualTreeHelper.GetChildrenCount(control) == 0)
				control.ApplyTemplate();

			VisualStateManager.GoToState(control, "Disabled", true);

			var rootElement = (FrameworkElement)VisualTreeHelper.GetChild(control, 0);

			var cache = new VisualStateCache();
			IList<VisualStateGroup> groups = VisualStateManager.GetVisualStateGroups(rootElement);

			VisualStateGroup common = null;
			foreach (var group in groups)
			{
				if (group.Name == "CommonStates")
					common = group;
				else if (group.Name == "FocusStates")
					cache.FocusStates = group;
				else if (cache.FocusStates != null && common != null)
					break;
			}

			if (cache.FocusStates != null)
				groups.Remove(cache.FocusStates);

			if (common != null)
			{
				foreach (VisualState state in common.States)
				{
					if (state.Name == "Normal")
						cache.Normal = state;
					else if (state.Name == "Pressed")
						cache.Pressed = state;
					else if (state.Name == "PointerOver")
						cache.PointerOver = state;
				}

				if (cache.Normal != null)
					common.States.Remove(cache.Normal);
				if (cache.Pressed != null)
					common.States.Remove(cache.Pressed);
				if (cache.PointerOver != null)
					common.States.Remove(cache.PointerOver);
			}

			return cache;
		}

		/*
		The below serves as a way to disable the button visually, rather than using IsEnabled. It's a hack
		but should remain stable as long as the user doesn't change the WinRT Button template too much.

		The reason we're not using IsEnabled is that the buttons have a click radius that overlap about 40%
		of the next button. This doesn't cause a problem until one button becomes disabled, then if you think
		you're still hitting + (haven't noticed its disabled), you will actually hit -. This hack doesn't
		completely solve the problem, but it drops the overlap to something like 20%. I haven't found the root
		cause, so this will have to suffice for now.
		*/

		void PsuedoEnable(Control control, ref VisualStateCache cache)
		{
			if (cache == null || VisualTreeHelper.GetChildrenCount(control) == 0)
				return;

			var rootElement = (FrameworkElement)VisualTreeHelper.GetChild(control, 0);

			IList<VisualStateGroup> groups = VisualStateManager.GetVisualStateGroups(rootElement);

			if (cache.FocusStates != null)
				groups.Add(cache.FocusStates);

			var commonStates = groups.FirstOrDefault(g => g.Name == "CommonStates");
			if (commonStates == null)
				return;

			if (cache.Normal != null)
				commonStates.States.Insert(0, cache.Normal); // defensive
			if (cache.Pressed != null)
				commonStates.States.Add(cache.Pressed);
			if (cache.PointerOver != null)
				commonStates.States.Add(cache.PointerOver);

			VisualStateManager.GoToState(control, "Normal", true);

			cache = null;
		}

		void UpdateEnabled(double value)
		{
			double increment = Increment;
			if (_plus != null)
			{
				if (value + increment > Maximum)
					_plusStateCache = PseudoDisable(_plus);
				else
					PsuedoEnable(_plus, ref _plusStateCache);
			}

			if (_minus != null)
			{
				if (value - increment < Minimum)
					_minusStateCache = PseudoDisable(_minus);
				else
					PsuedoEnable(_minus, ref _minusStateCache);
			}
		}

		void UpdateValue(double delta)
		{
			double newValue = Value + delta;
			if (newValue > Maximum || newValue < Minimum)
				return;

			Value = newValue;
		}

		class VisualStateCache
		{
			public VisualStateGroup FocusStates;
			public VisualState Normal, PointerOver, Pressed;
		}
	}
}