Ejemplo n.º 1
0
        private static void ExpandFromStart(FlexElementCollection elements, double target)
        {
            var useLayoutRounding = elements.UseLayoutRounding;

            if (useLayoutRounding)
            {
                target = target.LayoutRound(elements.Orientation, RoundingMode.MidPointFromZero);
            }

            if (elements.TryExpandToMaximum(target))
            {
                return;
            }

            var current = elements.Actual;

            if (current.IsGreaterThanOrClose(elements.ActualMaximum))
            {
                return;
            }

            for (var index = 0; index < elements.Count; index++)
            {
                var item         = elements[index];
                var compensation = Math.Max(-item.ActualLength, Math.Min(target - current, item.ActualMaxLength - item.ActualLength));
                item.ActualLength += compensation;

                elements[index] = item;

                current += compensation;

                if (current.IsCloseTo(target, XamlConstants.LayoutComparisonPrecision))
                {
                    break;
                }
            }
        }
Ejemplo n.º 2
0
        private void ExpandAllImpl(FlexElementCollection elements, double target)
        {
            var useLayoutRounding = elements.UseLayoutRounding;
            var originalTarget    = target;
            var orientation       = elements.Orientation;

            if (useLayoutRounding)
            {
                target = target.LayoutRound(orientation, RoundingMode.MidPointFromZero);
            }

            if (elements.TryExpandToMaximum(target))
            {
                return;
            }

            target -= Sort(elements, FlexDistributeDirection.Expand);

            try
            {
                var deadlock = true;
                do
                {
                    var tmpTarget = target;

                    for (var iItem = _currentCount - 1; iItem >= 0; iItem--)
                    {
                        var item = _sortedElements[iItem];
                        if (item.ActualLength * (iItem + 1) <= tmpTarget)
                        {
                            deadlock = false;

                            var success = true;
                            var avg     = tmpTarget / (iItem + 1);

                            if (useLayoutRounding)
                            {
                                avg = avg.LayoutRound(orientation, RoundingMode.MidPointFromZero);
                            }

                            for (var jItem = iItem; jItem >= 0; jItem--)
                            {
                                var titem = _sortedElements[jItem];

                                if (avg < titem.MaxLength)
                                {
                                    titem.ActualLength = avg;
                                }
                                else
                                {
                                    titem.ActualLength = titem.MaxLength;
                                    success            = false;
                                }

                                _sortedElements[jItem] = titem;

                                if (success)
                                {
                                    continue;
                                }

                                target -= titem.ActualLength;

                                RemoveAt(elements, jItem);
                            }

                            if (success)
                            {
                                return;
                            }

                            break;
                        }

                        tmpTarget -= item.ActualLength;
                    }
                } while (_currentCount > 0 && deadlock == false);
            }
            finally
            {
                RestoreOrder(elements);
                DistributeRoundingError(elements, elements.Actual - originalTarget);
            }
        }