Skip to content

[Proposal]: Should the Element.Measure method modify the DesiredSize returned by the subclass? #174

Description

@chenjing1294

Problem statement

Should the Element.Measure method modify the DesiredSize returned by the subclass? As far as I know, in WPF and AvaloniaUI, DesiredSize is not modified; it's limited during the Arrange phase.

So, is it possible to modify it like this?

// src/MewUI/Elements/Element.cs
public void Measure(Size availableSize)
{
  if (!IsMeasureDirty && _hasMeasureConstraint && _lastMeasureConstraint == availableSize)
  {
      return;
  }

  Size measured;
  using (PerformanceProfiler.Instance.SampleElement(GetType(), ProfilerSampleCategory.Measure))
  {
      measured = MeasureCore(availableSize);
  }

  /*var clamped = new Size(
      double.IsPositiveInfinity(availableSize.Width)
          ? measured.Width
          : Math.Min(measured.Width, availableSize.Width),
      double.IsPositiveInfinity(availableSize.Height)
          ? measured.Height
          : Math.Min(measured.Height, availableSize.Height));

  DesiredSize = ApplyLayoutRounding(clamped);*/

  DesiredSize = ApplyLayoutRounding(measured);
  IsMeasureDirty = false;
  _lastMeasureConstraint = availableSize;
  _hasMeasureConstraint = true;
}
// src/MewUI/Elements/FrameworkElement.cs
protected override Rect GetArrangedBounds(Rect finalRect)
{
  var innerSlot = finalRect.Deflate(Margin);

  double availableWidth = Math.Max(0, innerSlot.Width);
  double availableHeight = Math.Max(0, innerSlot.Height);

  /*double arrangeWidth = !double.IsNaN(Width) ? Width : DesiredSize.Width - Margin.Left - Margin.Right;
    double arrangeHeight = !double.IsNaN(Height) ? Height : DesiredSize.Height - Margin.Top - Margin.Bottom;*/
  double arrangeWidth;
  double arrangeHeight;
  // If we have explicit size, use it; otherwise use desired (excluding margin)
  if (!double.IsNaN(Width))
  {
      arrangeWidth = Width;
  }
  else
  {
      arrangeWidth = DesiredSize.Width - Margin.HorizontalThickness;
      arrangeWidth = Math.Min(availableWidth, arrangeWidth);
  }
  
  if (!double.IsNaN(Height))
  {
      arrangeHeight = Height;
  }
  else
  {
      arrangeHeight = DesiredSize.Height - Margin.VerticalThickness;
      arrangeHeight = Math.Min(availableHeight, arrangeHeight);
  }

Why the current approach is insufficient

see above

Type of change

Behavior change

Expected impact

see above

Alternatives or tradeoffs

see above

Additional context

No response

Before submitting

  • I searched for existing issues and discussions first.
  • I described why this belongs in MewUI core rather than only in app-level code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions