iPlug2 Forum

Design of mLayoutFunc

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

if(resizing)
  SetTargetAndDrawRECTs
else
 AttachControl

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

    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:

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);
  }
}