diff options
author | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 13:02:25 -0700 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-03-22 16:13:41 -0700 |
commit | 17fdde66d94155fc62a034fa6658995bef6fd6e5 (patch) | |
tree | b5e5073a2a7b15cdbe826faa5c763e270a505729 /Xamarin.Forms.Platform.Android/RendererPool.cs | |
download | xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.gz xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.tar.bz2 xamarin-forms-17fdde66d94155fc62a034fa6658995bef6fd6e5.zip |
Initial import
Diffstat (limited to 'Xamarin.Forms.Platform.Android/RendererPool.cs')
-rw-r--r-- | Xamarin.Forms.Platform.Android/RendererPool.cs | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.Android/RendererPool.cs b/Xamarin.Forms.Platform.Android/RendererPool.cs new file mode 100644 index 00000000..95a28778 --- /dev/null +++ b/Xamarin.Forms.Platform.Android/RendererPool.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; + +namespace Xamarin.Forms.Platform.Android +{ + public sealed class RendererPool + { + readonly Dictionary<Type, Stack<IVisualElementRenderer>> _freeRenderers = new Dictionary<Type, Stack<IVisualElementRenderer>>(); + + readonly VisualElement _oldElement; + + readonly IVisualElementRenderer _parent; + + public RendererPool(IVisualElementRenderer renderer, VisualElement oldElement) + { + if (renderer == null) + throw new ArgumentNullException("renderer"); + + if (oldElement == null) + throw new ArgumentNullException("oldElement"); + + _oldElement = oldElement; + _parent = renderer; + } + + public void ClearChildrenRenderers() + { + if (_parent.Element.LogicalChildren.Count == 0) + return; + ClearChildrenRenderers(_oldElement); + } + + public IVisualElementRenderer GetFreeRenderer(VisualElement view) + { + if (view == null) + throw new ArgumentNullException("view"); + + Type rendererType = Registrar.Registered.GetHandlerType(view.GetType()) ?? typeof(ViewRenderer); + + Stack<IVisualElementRenderer> renderers; + if (!_freeRenderers.TryGetValue(rendererType, out renderers) || renderers.Count == 0) + return null; + + IVisualElementRenderer renderer = renderers.Pop(); + renderer.SetElement(view); + return renderer; + } + + void ClearChildrenRenderers(VisualElement view) + { + if (view == null) + return; + + foreach (Element logicalChild in view.LogicalChildren) + { + var child = logicalChild as VisualElement; + if (child != null) + { + IVisualElementRenderer renderer = Platform.GetRenderer(child); + if (renderer == null) + continue; + + if (renderer.ViewGroup.Parent != _parent.ViewGroup) + continue; + + renderer.ViewGroup.RemoveFromParent(); + + Platform.SetRenderer(child, null); + PushRenderer(renderer); + } + } + + if (_parent.ViewGroup.ChildCount != 0) + _parent.ViewGroup.RemoveAllViews(); + } + + void PushRenderer(IVisualElementRenderer renderer) + { + Type rendererType = renderer.GetType(); + + Stack<IVisualElementRenderer> renderers; + if (!_freeRenderers.TryGetValue(rendererType, out renderers)) + _freeRenderers[rendererType] = renderers = new Stack<IVisualElementRenderer>(); + + renderers.Push(renderer); + } + } +}
\ No newline at end of file |