summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.Android/Renderers/DescendantFocusToggler.cs
blob: 1f54b7c50651961c7b3bccafb1932b34c737fc6a (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
using System;
using Android.Views;

namespace Xamarin.Forms.Platform.Android
{
	internal class DescendantFocusToggler : IDescendantFocusToggler
	{
		public bool RequestFocus(global::Android.Views.View control, Func<bool> baseRequestFocus)
		{
			IViewParent ancestor = control.Parent;
			var previousFocusability = DescendantFocusability.BlockDescendants;
			ConditionalFocusLayout cfl = null;

			// Work our way up through the tree until we find a ConditionalFocusLayout
			while (ancestor is ViewGroup)
			{
				cfl = ancestor as ConditionalFocusLayout;

				if (cfl != null)
				{
					previousFocusability = cfl.DescendantFocusability;
					// Toggle DescendantFocusability to allow this control to get focus
					cfl.DescendantFocusability = DescendantFocusability.AfterDescendants;
					break;
				}

				ancestor = ancestor.Parent;
			}

			// Call the original RequestFocus implementation for the View
			bool result = baseRequestFocus();

			if (cfl != null)
			{
				// Toggle descendantfocusability back to whatever it was
				cfl.DescendantFocusability = previousFocusability;
			}

			return result;
		}
	}
}