xamarin / flex

Flex is a flexible box layout system written in C, designed to be easy to consume from other languages
MIT License
193 stars 26 forks source link

We need more information on the SelfSizingDelegate #10

Closed rmarinho closed 6 years ago

rmarinho commented 6 years ago

What i m missing is a MeasureMode,

MeasureMode
{
   Undefined,
   Exactly,
   AtMost
}

Undefined is - ok i don’t have any constrain use the max value you want,: MeasureMode AtMost - says take this constrain (parent size), measure yourself on it and try to be the min size you want , and MeasureMode.Exact is ok please use the size i gave to you .. for example in Stretch mode ..

void HandleSelfSizingDelegate(Xamarin.Flex.Item item, ref float width, ref float height) 
{
    var widthMode = FlexMeasureMode.Undefined;
    var heightMode = FlexMeasureMode.Undefined;

    var availableWidth = item.Parent.Width;
    var availableHeight = item.Parent.Height;
    var size = _measure?.Invoke(item as IFlexNode, availableWidth, widthMode, availableHeight, heightMode);
    width = (float)size.Value.Width;
    height = (float)size.Value.Height;
}

Here's my code on the Xamarin.Forms side of things

Size MeasureView(IFlexNode node, float width, FlexMeasureMode widthMode, float height, FlexMeasureMode heightMode)
{
    var constrainedWidth = (widthMode == FlexMeasureMode.Undefined) ? float.MaxValue : width;
    var constrainedHeight = (heightMode == FlexMeasureMode.Undefined) ? float.MaxValue : height;

    View view = node.Data as View;

    var sizeRequest = view.Measure(constrainedWidth, constrainedHeight);

    var sizeThatFitsWidth = (float)sizeRequest.Request.Width;
    var sizeThatFitsHeight = (float)sizeRequest.Request.Height;

    var finalWidth = SanitizeMeasurement(constrainedWidth, sizeThatFitsWidth, widthMode);
    var finalHeight = SanitizeMeasurement(constrainedHeight, sizeThatFitsHeight, heightMode);

    return new Size(finalWidth, finalHeight);
}

static float SanitizeMeasurement(float constrainedSize, float measuredSize, FlexMeasureMode measureMode)
{
    float result;
    if (measureMode == FlexMeasureMode.Exactly)
    {
        result = constrainedSize;
    }
        else if (measureMode == FlexMeasureMode.AtMost)
    {
        result = Math.Min(constrainedSize, measuredSize);
    }
    else
    {
        result = measuredSize;
    }

    return result;
}
lrz commented 6 years ago

Closing per prior discussion, we will instead introduce per-case fixes.