Example #1
0
        /// <summary>
        /// Get the size of the child element
        /// </summary>
        /// <param name="availableSize"></param>
        /// <returns>Returns the size of the child</returns>
        private static Size GetChildSize(UIElement child, Size availableSize, int columns, int rows)
        {
            var heightSizing = EdgeLegacyPanel.GetHeightSizing(child);
            var widthSizing  = EdgeLegacyPanel.GetWidthSizing(child);
            var width        = (child.DesiredSize.Width == 0 || widthSizing == Sizing.FromParent || child.DesiredSize.Width > availableSize.Width / columns) ?
                               availableSize.Width / columns :
                               child.DesiredSize.Width;
            var height = (child.DesiredSize.Height == 0 || heightSizing == Sizing.FromParent || child.DesiredSize.Height > availableSize.Height / rows) ?
                         availableSize.Height / rows :
                         child.DesiredSize.Height;

            return(new Size(width, height));
        }
Example #2
0
        public static Size Measure2(IEnumerable <UIElement> children, Size availableSize, Func <UIElement, Sizing, bool> includeWidth, Func <UIElement, Sizing, bool> includeHeight)
        {
            List <UIElement> leftWidth = new List <UIElement>(), centerWidth = new List <UIElement>(), rightWidth = new List <UIElement>();
            List <UIElement> topHeight = new List <UIElement>(), middleHeight = new List <UIElement>(), bottomHeight = new List <UIElement>();

            List <UIElement> left2Width = new List <UIElement>(), center2Width = new List <UIElement>(), right2Width = new List <UIElement>();
            List <UIElement> top2Height = new List <UIElement>(), middle2Height = new List <UIElement>(), bottom2Height = new List <UIElement>();

            double leftCount = 0, centerCount = 0, rightCount = 0;
            double topCount = 0, middleCount = 0, bottomCount = 0;

            double center2Count = 0, middle2Count = 0;


            int bottomleftWidthCount = 0, bottomrightWidthCount = 0;
            int topleftWidthCount = 0, toprightWidthCount = 0;

            int bottomleftHeightCount = 0, bottomrightHeightCount = 0;
            int topleftHeightCount = 0, toprightHeightCount = 0;

            foreach (UIElement child in children)
            {
                var region       = EdgeLegacyPanel.GetCircleRegion(child);
                var heightSizing = EdgeLegacyPanel.GetHeightSizing(child);
                var widthSizing  = EdgeLegacyPanel.GetWidthSizing(child);

                //var aSize = asizes[region] = asizes.GetValueOrDefault(region, new A());

                child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));

                int widthCount  = includeWidth(child, widthSizing) ? 1 : 0;
                int heightCount = includeHeight(child, widthSizing) ? 1 : 0;


                switch (region)
                {
                case CircleRegion.TopLeft:

                    topleftWidthCount  += widthCount;
                    topleftHeightCount += heightCount;

                    break;

                case CircleRegion.BottomLeft:

                    bottomleftWidthCount  += widthCount;
                    bottomleftHeightCount += heightCount;

                    break;

                case CircleRegion.BottomRight:

                    bottomrightWidthCount  += widthCount;
                    bottomrightHeightCount += heightCount;

                    break;

                case CircleRegion.TopRight:

                    toprightWidthCount  += widthCount;
                    toprightHeightCount += heightCount;

                    break;

                case CircleRegion.Left:

                    leftCount += widthCount;

                    middle2Count = Math.Max(1, heightCount);
                    break;

                case CircleRegion.Right:

                    rightCount += widthCount;

                    middle2Count = Math.Max(1, heightCount);
                    break;


                case CircleRegion.Top:
                    center2Count = Math.Max(1, widthCount);

                    topCount += heightCount;
                    break;

                case CircleRegion.Bottom:
                    center2Count = Math.Max(1, widthCount);
                    bottomCount += heightCount;
                    break;


                case CircleRegion.Middle:
                    centerCount += widthCount;
                    middleCount += heightCount;
                    break;
                }
                ;
            }

            bottomCount = Math.Max(bottomCount, Math.Max(Sizer.GetCount(bottomleftHeightCount), Sizer.GetCount(bottomrightHeightCount)));
            topCount    = Math.Max(topCount, Math.Max(Sizer.GetCount(topleftHeightCount), Sizer.GetCount(toprightHeightCount)));
            leftCount   = Math.Max(leftCount, Math.Max(Sizer.GetCount(bottomleftWidthCount), Sizer.GetCount(bottomrightWidthCount)));
            rightCount  = Math.Max(rightCount, Math.Max(Sizer.GetCount(bottomleftHeightCount), Sizer.GetCount(bottomrightHeightCount)));

            //var remainingWidth = availableSize.Width - leftWidth.Sum() - centerWidth.Sum() - rightWidth.Sum();
            //var remainingHeight = availableSize.Height - topHeight.Sum() - middleHeight.Sum() - bottomHeight.Sum();



            var newMethod1 = NewMethod(availableSize.Height, GetHeight, new[] { bottomHeight, middleHeight, topHeight }).ToArray();

            var wrappedHeight = newMethod1
                                .SelectMany(a => a.Select((c, i) => (c, i)))
                                .GroupBy(a => a.i)
                                .Select(a => a.Max(c => c.c.Sum(GetHeight))).ToOptionalArray();

            var grouped = newMethod1
                          .SelectMany(a => a.Select((c, i) => (c, i)))
                          .GroupBy(a => a.i).ToArray();

            var wrappedWidth = grouped
                               .Select(a => a.Where(a => a.c.Any()).Sum(c => c.c.Max(GetWidth))).ToArray();

            var offsetCount = grouped.Select(a => a.Count(c => c.c.Any())).ToOptionalArray();



            var newMethod2 = NewMethod(availableSize.Width, GetWidth, new[] { leftWidth, centerWidth, rightWidth }).ToArray();

            var wrappedWidth2 = newMethod2
                                .SelectMany(a => a.Select((c, i) => (c, i)))
                                .GroupBy(a => a.i)
                                .Select(a => a.Max(c => c.c.Sum(GetWidth))).ToOptionalArray();

            var grouped2 = newMethod2
                           .SelectMany(a => a.Select((c, i) => (c, i)))
                           .GroupBy(a => a.i).ToArray();

            var wrappedHeight2 = grouped2
                                 .Select(a => a.Where(a => a.c.Any()).Sum(c => c.c.Max(GetHeight))).ToArray();

            var offsetCount2 = grouped2.Select(a => a.Count(c => c.c.Any())).ToOptionalArray();



            var newMethod3 = NewMethod(availableSize.Height, GetHeight, new[] {
                bottom2Height.OrderByDescending(GetHeight).Take(1).ToArray(),
                middle2Height.OrderByDescending(GetHeight).Take(1).ToArray(),
                top2Height.OrderByDescending(GetHeight).Take(1).ToArray()
            }).ToArray();

            var wrappedHeight3 = newMethod3
                                 .SelectMany(a => a.Select((c, i) => (c, i)))
                                 .GroupBy(a => a.i)
                                 .Select(a => a.Max(c => c.c.Sum(GetHeight))).ToOptionalArray();

            var grouped3 = newMethod3
                           .SelectMany(a => a.Select((c, i) => (c, i)))
                           .GroupBy(a => a.i).ToArray();


            var wrappedWidth3 = grouped3
                                .Select(a => a.Where(a => a.c.Any()).Sum(c => c.c.Max(GetWidth))).ToArray();

            var offsetCount3 = grouped3.Select(a => a.Count(c => c.c.Any())).ToOptionalArray();



            var newMethod4 = NewMethod(availableSize.Width, GetWidth, new[] {
                left2Width.OrderByDescending(GetWidth).Take(1).ToArray(),
                center2Width.OrderByDescending(GetWidth).Take(1).ToArray(),
                right2Width.OrderByDescending(GetWidth).Take(1).ToArray()
            }).ToArray();

            var wrappedWidth4 = newMethod4
                                .SelectMany(a => a.Select((c, i) => (c, i)))
                                .GroupBy(a => a.i)
                                .Select(a => a.Max(c => c.c.Sum(GetWidth))).ToOptionalArray();

            var grouped4 = newMethod4
                           .SelectMany(a => a.Select((c, i) => (c, i)))
                           .GroupBy(a => a.i).ToArray();

            var wrappedHeight4 = grouped4
                                 .Select(a => a.Where(a => a.c.Any()).Sum(c => c.c.Max(GetHeight))).ToArray();

            var offsetCount4 = grouped4.Select(a => a.Count(c => c.c.Any())).ToOptionalArray();


            //actualLeftOffset = Common.Max(offsetCount4[0], offsetCount2[0]).GetValueOrDefault(0);
            //actualCenterOffset = Common.Max(offsetCount4[1], offsetCount2[1]).GetValueOrDefault(0);
            //actualRightOffset = Common.Max(offsetCount4[2], offsetCount2[2]).GetValueOrDefault(0);

            //actualBottomOffset = Common.Max(offsetCount3[0], offsetCount[0]).GetValueOrDefault(0);
            //actualMiddleOffset = Common.Max(offsetCount3[1], offsetCount[1]).GetValueOrDefault(0);
            //actualTopOffset = Common.Max(offsetCount3[2], offsetCount[2]).GetValueOrDefault(0);

            //actualLeftWidth = Common.Max(wrappedWidth4[0], wrappedWidth2[0]).GetValueOrDefault(0);
            //actualCenterWidth = Common.Max(wrappedWidth4[1], wrappedWidth2[1]).GetValueOrDefault(0);
            //actualRightWidth = Common.Max(wrappedWidth4[2], wrappedWidth2[2]).GetValueOrDefault(0);

            //actualBottomHeight = Common.Max(wrappedHeight3[0], wrappedHeight[0]).GetValueOrDefault(0);
            //actualMiddleHeight = Common.Max(wrappedHeight3[1], wrappedHeight[1]).GetValueOrDefault(0);
            //actualTopHeight = Common.Max(wrappedHeight3[2], wrappedHeight[2]).GetValueOrDefault(0);

            var remainingWidth  = 0; //= availableSize.Width - actualLeftWidth - actualCenterWidth - actualRightWidth;
            var remainingHeight = 0; //= availableSize.Height - actualBottomHeight - actualMiddleHeight - actualTopHeight;


            //if (middleCount > 0)
            //{
            //var ax = remainingHeight * HeightRatio;
            //remainingHeight -= ax;
            //middleRequiredHeight = ax / middleCount;
            //}
            //else
            //    middleRequiredHeight= 0;

            //if (centerCount > 0)
            //{
            //var ax2 = remainingWidth * WidthRatio;
            //remainingWidth -= ax2;
            //centerRequiredWidth = ax2 / centerCount;
            //}
            //else
            //    centerRequiredWidth = 0;

            double individualWidth  = 0; //= remainingWidth / ((leftCount > 0 ? 1 : 0) + (rightCount > 0 ? 1 : 0) + (center2Count > 0 ? 1 : 0));
            double individualHeight = 0; //= remainingHeight / ((topCount > 0 ? 1 : 0) + (bottomCount > 0 ? 1 : 0) + (middle2Count > 0 ? 1 : 0));


            //if (leftCount > 0)
            //{
            //    remainingWidth -= individualWidth;
            //    leftRequiredWidth = individualWidth / leftCount;
            //    leftRegionWidth = individualWidth;
            //}
            //if (rightCount > 0)
            //{
            //    remainingWidth -= individualWidth;
            //    rightRequiredWidth = individualWidth / rightCount;
            //    rightRegionWidth = individualWidth;
            //}
            //if (center2Count > 0)
            //{
            //    remainingWidth -= individualWidth;
            //    centerRequiredWidth = individualWidth / center2Count;
            //    centerRegionWidth = individualWidth;
            //}
            //if (topCount > 0)
            //{
            //    remainingHeight -= individualHeight;
            //    topRequiredHeight = individualHeight / topCount;
            //    topRegionHeight = individualHeight;
            //}
            //if (bottomCount > 0)
            //{
            //    remainingHeight -= individualHeight;
            //    bottomRequiredHeight = individualHeight / bottomCount;
            //    bottomRegionHeight = individualHeight;
            //}
            //if (middle2Count > 0)
            //{
            //    remainingHeight -= individualHeight;
            //    middleRequiredHeight = individualHeight / middle2Count;
            //    middleRegionHeight = individualHeight;
            //}



            double finalWidth;

            if (individualWidth != double.PositiveInfinity)
            {
                finalWidth = availableSize.Width;
            }
            else
            {
                finalWidth = availableSize.Width - remainingWidth;
            }

            double finalHeight;

            if (individualHeight != double.PositiveInfinity)
            {
                finalHeight = availableSize.Height;
            }
            else
            {
                finalHeight = availableSize.Height - remainingHeight;
            }


            return(new Size(finalWidth, finalHeight));
        }