/// <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 left aligned with Panel or has no horizontal alignment instructions /// </summary> private bool IsAlignLeftWithPanel(IFrameworkElement child) { return(RelativePanel.GetAlignLeftWithPanel(child) || !( RelativePanel.GetAlignRightWithPanel(child) || RelativePanel.GetAlignHorizontalCenterWithPanel(child) || RelativePanel.GetAlignLeftWith(child) != null || RelativePanel.GetAlignRightWith(child) != null || RelativePanel.GetRightOf(child) != null || RelativePanel.GetLeftOf(child) != null )); }
/// <summary> /// Checks if the given FrameworkElement has any dependencies that would provide a right boundary using the existing layout info /// </summary> private double ComputeChildRightBound( IFrameworkElement child, Size availableSize, Dictionary <IFrameworkElement, SiblingLayoutInfo> siblingLayoutInfos, double childLeft, Thickness graphPadding, bool useInferred = false ) { var leftOf = GetAvailableDependencies(GetSibling(child), DependencyType.LeftOf, useInferred, siblingLayoutInfos); if (leftOf.Length != 0) { ExecuteOnSiblingLayoutInfoIfAvailable(child, siblingLayoutInfos, sli => sli.IsRightBound = !useInferred); return(leftOf.Min(d => siblingLayoutInfos[d.Sibling.Element].Area.Left)); } var rightAlign = GetAvailableDependencies(GetSibling(child), DependencyType.AlignRightWith, useInferred, siblingLayoutInfos); if (rightAlign.Length != 0) { ExecuteOnSiblingLayoutInfoIfAvailable(child, siblingLayoutInfos, sli => sli.IsRightBound = !useInferred); return(rightAlign.Min(d => siblingLayoutInfos[d.Sibling.Element].Area.Right)); } if (RelativePanel.GetAlignRightWithPanel(child)) { ExecuteOnSiblingLayoutInfoIfAvailable(child, siblingLayoutInfos, sli => sli.IsRightBound = true); return(availableSize.Width - graphPadding.Right); } if (!useInferred) { return(ComputeChildRightBound(child, availableSize, siblingLayoutInfos, childLeft, graphPadding, true)); } // If there is no dependency, base yourself off of the available width, margins and Width/Min/Max properties var spacing = childLeft + child.Margin.Left + child.Margin.Right; return(double.IsNaN(child.Width) ? Math.Max( Math.Min( availableSize.Width - graphPadding.Right, child.MaxWidth + spacing ), child.MinWidth + spacing ) : child.Width + 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); } } }