# Design of mLayoutFunc

**URL:** <https://iplug2.discourse.group/t/design-of-mlayoutfunc/434>\
**Category:** Uncategorized\
**Created:** [15 July 2021 13:03 UTC](https://iplug2.discourse.group/t/design-of-mlayoutfunc/434 "2021-07-15T13:03:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alechk4](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alechk4](https://iplug2.discourse.group/u/Alechk4)\
**Post date:** [15 July 2021 13:03 UTC](https://iplug2.discourse.group/t/design-of-mlayoutfunc/434/1 "2021-07-15T13:03:00Z")

</div>

What is the rationale behind is pGraphics-\>SetLayoutOnResize(true) calling the same closure (mLayoutFunc), instead of having two different functions - one for layout and one for resizing?  
My implementation is clearly split in two parts

```auto
if(resizing)
  SetTargetAndDrawRECTs
else
 AttachControl

```

---

<div class="post-metadata">

**Author:** ![Alechk4](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Alechk4](https://iplug2.discourse.group/u/Alechk4)\
**Post date:** [15 July 2021 14:42 UTC](https://iplug2.discourse.group/t/design-of-mlayoutfunc/434/2 "2021-07-15T14:42:44Z")

</div>

Also, isn’t there any better way than doing this (taken from IPlugResponsiveUI)?

```auto
    auto GetBounds = [pGraphics](int ctrlIdx, const IRECT& b) {
      IRECT main = b.GetPadded(-40.f);
      IRECT keys = main.FracRectVertical(0.25, false);
      IRECT scope = main.FracRectVertical(0.75, true).GetPadded(-10.f);
      IRECT gain = scope.ReduceFromRight(100.f);
      switch (ctrlIdx) {
        case 1: return keys;
        case 2: return gain;
        case 3: return scope;
        case 0: return b;
        default: return pGraphics->GetControl(ctrlIdx)->GetRECT();
      }
    };

```

The main problem is that it relies on the ordinal Idx of the controls, which shouldn’t be used in a high-level function.  
As a solution, for instance, each IControl could have a member closure that is called only if mLayoutOnResize is true:

```auto
void IGraphics::Resize(int w, int h, float scale, bool needsPlatformResize)
{
 [...]
    
  PlatformResize(GetDelegate()->EditorResizeFromUI(windowWidth, windowHeight, needsPlatformResize));
  ForAllControls(&IControl::OnResize);
  SetAllControlsDirty();
  DrawResize();
  
  if(mLayoutOnResize){
    ForAllControls(&IControl::LayoutFunc); //THIS
    GetDelegate()->LayoutUI(this);
  }
}

```
