/// <summary> /// Prioritizes elements for layouting. First treat the entrypoints, starting from top-left then around. /// Then select the ones with dependencies that are already laid out. /// </summary> /// <param name="cluster"></param> /// <returns></returns> private IFrameworkElement[] OrderClusterForLayouting(List <Sibling> cluster) { // Give weight to panel alignments. Weight 3 for top or left so that a left-aligned only // element (3) has precedence over a bottom-right aligned element (1+1) var orderedByAlignment = new List <Sibling>(cluster .OrderByDescending(s => (IsAlignLeftWithPanel(s.Element) ? 3 : 0) + (IsAlignTopWithPanel(s.Element) ? 3 : 0) + (RelativePanel.GetAlignRightWithPanel(s.Element) ? 1 : 0) + (RelativePanel.GetAlignBottomWithPanel(s.Element) ? 1 : 0) ) ); var ordered = new List <Sibling>(); while (orderedByAlignment.Count != 0) { // Take the first item in the previous list that has no direct dependency that have not already been selected var next = orderedByAlignment.First(s => s.Dependencies .Where(sd => !sd.IsInferred) .Select(sd => sd.Sibling) .Except(ordered) .Empty() ); orderedByAlignment.Remove(next); ordered.Add(next); } return(ordered .SelectToArray(s => s.Element)); }
/// <summary> /// Returns true if the child is top aligned with Panel or has no vertical alignment instructions /// </summary> private bool IsAlignTopWithPanel(IFrameworkElement child) { return(RelativePanel.GetAlignTopWithPanel(child) || !( RelativePanel.GetAlignBottomWithPanel(child) || RelativePanel.GetAlignVerticalCenterWithPanel(child) || RelativePanel.GetAlignTopWith(child) != null || RelativePanel.GetAlignBottomWith(child) != null || RelativePanel.GetAbove(child) != null || RelativePanel.GetBelow(child) != null )); }
/// <summary> /// Checks if the given FrameworkElement has any dependencies that would provide a bottom boundary using the existing layout info /// </summary> private double ComputeChildBottomBound( IFrameworkElement child, Size availableSize, Dictionary <IFrameworkElement, SiblingLayoutInfo> siblingLayoutInfos, double childTop, Thickness graphPadding, bool useInferred = false ) { var above = GetAvailableDependencies(GetSibling(child), DependencyType.Above, useInferred, siblingLayoutInfos); if (above.Length != 0) { ExecuteOnSiblingLayoutInfoIfAvailable(child, siblingLayoutInfos, sli => sli.IsBottomBound = !useInferred); return(above.Min(d => siblingLayoutInfos[d.Sibling.Element].Area.Top)); } var bottomAlign = GetAvailableDependencies(GetSibling(child), DependencyType.AlignBottomWith, useInferred, siblingLayoutInfos); if (bottomAlign.Length != 0) { ExecuteOnSiblingLayoutInfoIfAvailable(child, siblingLayoutInfos, sli => sli.IsBottomBound = !useInferred); return(bottomAlign.Min(d => siblingLayoutInfos[d.Sibling.Element].Area.Bottom)); } if (RelativePanel.GetAlignBottomWithPanel(child)) { ExecuteOnSiblingLayoutInfoIfAvailable(child, siblingLayoutInfos, sli => sli.IsBottomBound = true); return(availableSize.Height - graphPadding.Bottom); } if (!useInferred) { ComputeChildBottomBound(child, availableSize, siblingLayoutInfos, childTop, graphPadding, true); } // If there is no dependency, base yourself off of the available height, margins and Height/Min/Max properties var spacing = childTop + child.Margin.Top + child.Margin.Bottom; return(double.IsNaN(child.Height) ? Math.Max( Math.Min( availableSize.Height - graphPadding.Bottom, child.MaxHeight + spacing ), child.MinHeight + spacing ) : child.Height + spacing); }
/// <summary> /// Makes sure dependencies in the cluster are reversed if they are based on unknown boundaries /// </summary> private void CleanupDependencies(List <Sibling> cluster, bool isHorizontallyInfinite, bool isVerticallyInfinite) { if (!isHorizontallyInfinite && !isVerticallyInfinite) { return; } foreach (var sibling in cluster) { if (isHorizontallyInfinite && RelativePanel.GetAlignRightWithPanel(sibling.Element)) { ReverseDependencies(sibling, true); } if (isVerticallyInfinite && RelativePanel.GetAlignBottomWithPanel(sibling.Element)) { ReverseDependencies(sibling, false); } } }