public static RelativeLength Sum(this IEnumerable <RelativeLength> lengths) { RelativeLength sum = RelativeLength.Zero; foreach (var length in lengths) { sum += length; } return(sum); }
private float calculateLength(RelativeLength length, RelativeLength totalLength, float absoluteClientLength) { var independentLength = length[UnitType.Pixel] + unitToPixel(length[UnitType.Unit]) + (absoluteClientLength * length[UnitType.Percentage] / 100); var totalIndependentLength = totalLength[UnitType.Pixel] + unitToPixel(totalLength[UnitType.Unit]) + (absoluteClientLength * totalLength[UnitType.Percentage] / 100); if (length[UnitType.Ratio] != 0) { // Get the dynamic length var rest = absoluteClientLength - totalIndependentLength; var totalDynamicLength = totalLength[UnitType.Ratio]; var factor = rest / totalLength[UnitType.Ratio]; return(independentLength + (factor * length[UnitType.Ratio])); } else if (length == RelativeLength.Zero) { return(0); } else if (length == RelativeLength.Infinity) { return(absoluteClientLength - totalIndependentLength); } else if (length == RelativeLength.NaN) { throw new NotImplementedException(); } else { return(independentLength); } }
private float calculateLength(RelativeLength length, float absoluteClientLength) { return(length[UnitType.Pixel] + unitToPixel(length[UnitType.Unit]) + (absoluteClientLength * length[UnitType.Percentage] / 100)); }
private LayoutBoxInformation calculateRelativeLayoutBoxes(ILayoutBox layoutComponent, AbsoluteSize clientSize) { RelativeLength width = layoutComponent.Width; RelativeLength height = layoutComponent.Height; var component = (IComponent)layoutComponent; var siblings = component.Parent.Children.Where(x => x != component); var totalSiblingsWidth = siblings.Where(x => x is ILayoutBox).Select(x => ((ILayoutBox)x).Width).Aggregate((total, sibling) => { return(total + sibling); }); var totalSiblingsHeight = siblings.Where(x => x is ILayoutBox).Select(x => ((ILayoutBox)x).Height).Aggregate((total, sibling) => { return(total + sibling); }); var totalWidth = totalSiblingsWidth + width; var totalHeight = totalSiblingsHeight + height; // Does it have related length (Like 1x or 5x)? var hasRelatedWidth = width[UnitType.Ratio] != 0; var hasRelatedHeight = height[UnitType.Ratio] != 0; // Assume siblings related lengths to '0' if this component is 'Fill' if (width == RelativeLength.Infinity) { totalSiblingsWidth -= totalSiblingsWidth[UnitType.Ratio]; } if (height == RelativeLength.Infinity) { totalSiblingsHeight -= totalSiblingsHeight[UnitType.Ratio]; } // Assume the length as 'Fill' if this component have related length but siblings don't. if (hasRelatedWidth && totalSiblingsWidth[UnitType.Ratio] == 0) { width = RelativeLength.Infinity; } if (hasRelatedHeight && totalSiblingsHeight[UnitType.Ratio] == 0) { height = RelativeLength.Infinity; } // Assume the length as 'content length' if this component is 'Shrink' and at least one of the siblings has related or fill length. if (width == RelativeLength.NaN && (totalSiblingsWidth[UnitType.Ratio] != 0 || totalSiblingsWidth == RelativeLength.Infinity)) { width = calculateContentWidth(layoutComponent); } if (height == RelativeLength.NaN && (totalSiblingsHeight[UnitType.Ratio] != 0 || totalSiblingsHeight == RelativeLength.Infinity)) { height = calculateContentHeight(layoutComponent); } // Calculate size var absoluteWidth = calculateLength(width, totalWidth, clientSize.Width); var absoluteHeight = calculateLength(height, totalHeight, clientSize.Height); // Calculate margins (based on parent client area) var marginTop = calculateLength(layoutComponent.Margin.Top, clientSize.Height); var marginRight = calculateLength(layoutComponent.Margin.Right, clientSize.Width); var marginBottom = calculateLength(layoutComponent.Margin.Bottom, clientSize.Height); var marginLeft = calculateLength(layoutComponent.Margin.Left, clientSize.Width); // Calculate paddings (based on self client area) var paddingTop = calculateLength(layoutComponent.Padding.Top, absoluteHeight); var paddingRight = calculateLength(layoutComponent.Padding.Right, absoluteWidth); var paddingBottom = calculateLength(layoutComponent.Padding.Bottom, absoluteHeight); var paddingLeft = calculateLength(layoutComponent.Padding.Left, absoluteWidth); return(new LayoutBoxInformation() { AbsoluteMarginBox = new AbsoluteRectangle( 0, 0, absoluteWidth + marginLeft + marginRight, absoluteHeight + marginTop + marginBottom ), AbsoluteBox = new AbsoluteRectangle( marginLeft, marginTop, absoluteWidth, absoluteHeight ), AbsolutePaddingBox = new AbsoluteRectangle( marginLeft + paddingLeft, marginTop + paddingTop, absoluteWidth - paddingLeft - paddingRight, absoluteHeight - paddingTop - paddingBottom ), LayoutDirection = layoutComponent.LayoutDirection }); }