Exemplo n.º 1
0
        protected virtual void ArrangeCore(Rect finalRect)
        {
            if (IsVisible)
            {
                var useLayoutRounding = UseLayoutRounding;
                var scale             = LayoutHelper.GetLayoutScale(this);

                var margin  = Margin;
                var originX = finalRect.X + margin.Left;
                var originY = finalRect.Y + margin.Top;

                // Margin has to be treated separately because the layout rounding function is not linear
                // f(a + b) != f(a) + f(b)
                // If the margin isn't pre-rounded some sizes will be offset by 1 pixel in certain scales.
                if (useLayoutRounding)
                {
                    margin = LayoutHelper.RoundLayoutThickness(margin, scale, scale);
                }

                var availableSizeMinusMargins = new Size(
                    Math.Max(0, finalRect.Width - margin.Left - margin.Right),
                    Math.Max(0, finalRect.Height - margin.Top - margin.Bottom));
                var horizontalAlignment = HorizontalAlignment;
                var verticalAlignment   = VerticalAlignment;
                var size = availableSizeMinusMargins;

                if (horizontalAlignment != HorizontalAlignment.Stretch)
                {
                    size = size.WithWidth(Math.Min(size.Width, DesiredSize.Width - margin.Left - margin.Right));
                }

                if (verticalAlignment != VerticalAlignment.Stretch)
                {
                    size = size.WithHeight(Math.Min(size.Height, DesiredSize.Height - margin.Top - margin.Bottom));
                }

                size = LayoutHelper.ApplyLayoutConstraints(this, size);

                if (useLayoutRounding)
                {
                    size = LayoutHelper.RoundLayoutSize(size, scale, scale);
                    availableSizeMinusMargins = LayoutHelper.RoundLayoutSize(availableSizeMinusMargins, scale, scale);
                }

                size = ArrangeOverride(size).Constrain(size);

                switch (horizontalAlignment)
                {
                case HorizontalAlignment.Center:
                case HorizontalAlignment.Stretch:
                    originX += (availableSizeMinusMargins.Width - size.Width) / 2;
                    break;

                case HorizontalAlignment.Right:
                    originX += availableSizeMinusMargins.Width - size.Width;
                    break;
                }

                switch (verticalAlignment)
                {
                case VerticalAlignment.Center:
                case VerticalAlignment.Stretch:
                    originY += (availableSizeMinusMargins.Height - size.Height) / 2;
                    break;

                case VerticalAlignment.Bottom:
                    originY += availableSizeMinusMargins.Height - size.Height;
                    break;
                }

                if (useLayoutRounding)
                {
                    originX = LayoutHelper.RoundLayoutValue(originX, scale);
                    originY = LayoutHelper.RoundLayoutValue(originY, scale);
                }

                Bounds = new Rect(originX, originY, size.Width, size.Height);
            }
        }
Exemplo n.º 2
0
        protected virtual void ArrangeCore(Rect finalRect)
        {
            if (IsVisible)
            {
                var margin  = Margin;
                var originX = finalRect.X + margin.Left;
                var originY = finalRect.Y + margin.Top;
                var availableSizeMinusMargins = new Size(
                    Math.Max(0, finalRect.Width - margin.Left - margin.Right),
                    Math.Max(0, finalRect.Height - margin.Top - margin.Bottom));
                var horizontalAlignment = HorizontalAlignment;
                var verticalAlignment   = VerticalAlignment;
                var size              = availableSizeMinusMargins;
                var scale             = LayoutHelper.GetLayoutScale(this);
                var useLayoutRounding = UseLayoutRounding;

                if (horizontalAlignment != HorizontalAlignment.Stretch)
                {
                    size = size.WithWidth(Math.Min(size.Width, DesiredSize.Width - margin.Left - margin.Right));
                }

                if (verticalAlignment != VerticalAlignment.Stretch)
                {
                    size = size.WithHeight(Math.Min(size.Height, DesiredSize.Height - margin.Top - margin.Bottom));
                }

                size = LayoutHelper.ApplyLayoutConstraints(this, size);

                if (useLayoutRounding)
                {
                    size = LayoutHelper.RoundLayoutSize(size, scale, scale);
                    availableSizeMinusMargins = LayoutHelper.RoundLayoutSize(availableSizeMinusMargins, scale, scale);
                }

                size = ArrangeOverride(size).Constrain(size);

                switch (horizontalAlignment)
                {
                case HorizontalAlignment.Center:
                case HorizontalAlignment.Stretch:
                    originX += (availableSizeMinusMargins.Width - size.Width) / 2;
                    break;

                case HorizontalAlignment.Right:
                    originX += availableSizeMinusMargins.Width - size.Width;
                    break;
                }

                switch (verticalAlignment)
                {
                case VerticalAlignment.Center:
                case VerticalAlignment.Stretch:
                    originY += (availableSizeMinusMargins.Height - size.Height) / 2;
                    break;

                case VerticalAlignment.Bottom:
                    originY += availableSizeMinusMargins.Height - size.Height;
                    break;
                }

                if (useLayoutRounding)
                {
                    originX = LayoutHelper.RoundLayoutValue(originX, scale);
                    originY = LayoutHelper.RoundLayoutValue(originY, scale);
                }

                Bounds = new Rect(originX, originY, size.Width, size.Height);
            }
        }