summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.WinRT/FormsComboBox.cs
blob: f693c942f90fde6d1db4f434a287b697c8eba155 (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
using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Animation;

#if WINDOWS_UWP

namespace Xamarin.Forms.Platform.UWP
#else

namespace Xamarin.Forms.Platform.WinRT
#endif
{
	public class FormsComboBox : ComboBox
	{
		internal bool IsClosingAnimated { get; private set; }

		internal bool IsFullScreen => Device.Idiom == TargetIdiom.Phone && Items != null && Items.Count > 5;

		internal bool IsOpeningAnimated { get; private set; }

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

			if (Device.Idiom == TargetIdiom.Phone)
			{
				// If we're running on the phone, we have to give the PickerRenderer hooks
				// into the opening and closing animations so it can handle them smoothly
				// and measure at the appropriate times

				var openedState = GetTemplateChild("Opened") as VisualState;
				if (openedState != null)
				{
					openedState.Storyboard.Completed += (sender, o) => OnOpenAnimationCompleted();
					IsOpeningAnimated = true;
				}

				var closedState = GetTemplateChild("Closed") as VisualState;

				// On the phone, this is a dummy animation we've added to the closed state in the VSM
				// Since it finishes immediately, we can use its Completed event to signal that the 
				// closing animation has started 
				var closedSignalAnimation = closedState?.Storyboard.Children[0] as DoubleAnimation;

				if (closedSignalAnimation != null)
				{
					closedSignalAnimation.Completed += (sender, o) => OnClosedAnimationStarted();
					IsClosingAnimated = true;
				}
			}
		}

		protected virtual void OnClosedAnimationStarted()
		{
			ClosedAnimationStarted?.Invoke(this, EventArgs.Empty);
		}

		protected virtual void OnOpenAnimationCompleted()
		{
			OpenAnimationCompleted?.Invoke(this, EventArgs.Empty);
		}

		internal event EventHandler<EventArgs> ClosedAnimationStarted;

		internal event EventHandler<EventArgs> OpenAnimationCompleted;
	}
}