Resize window when drag ends

Hi, when setting a window to be rezisable, is there any way to have resized() called only when the drag ends rather than constantly?

thanks

Maybe there is some easier and more elegant way but I wrote once a simple class doing something like this (I wanted the effect of delayed resizing):

class DelayedResizer: private juce::Timer
{
public:
    DelayedResizer() {};
    virtual ~DelayedResizer() {};

    void resizeAfterDelay (int delay = 50) noexcept
    {
        lastTime = juce::Time::getMillisecondCounter();

        if (! isTimerRunning())
             startTimer (delay);
    }
    
    std::function<void()> resized = nullptr;
    
private:
    void timerCallback() override
    {
        if (juce::Time::getMillisecondCounter() - lastTime > getTimerInterval() - 10)
        {
            stopTimer();
            
            if (resized != nullptr)
                resized();
        }
    }

    double lastTime = std::numeric_limits<double>::max();
    
    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DelayedResizer)
};

Then in your MainComponent or AudioEditor:

void MainComponent::resized()
{
     delayedResizer.resizeAfterDelay (100);
}

And earlier you should assign a function when actual resizing is done, for example:

delayedResizer.resized = [this] { button.setBounds (getLocalBounds()); };

So approx.100ms (in this example) after last resize event proper resizing will be triggered.

1 Like

Thanks for the suggestion.

FYI, you can simplify your code here - don’t bother checking if the timer is running as calling it multiple times will just reset the timer, so timerCallback will just get called after the movement has stopped so you won’t need to perform that calculation.

Yes, it will work without this check too but startTimer does few extra things so I guessed one boolean check was probably faster :slight_smile:

1 Like

cool. i’ve used that method for years and it causes no issues and it simplifies the code so that’s what I go for.

1 Like

Out of curiosity I’ve just measured the differences with Timer::getMiillisecondsHiRes(): checking boolean is just 0, startTimer() takes some time but it still far, far below 1ms, so yes, it’s negligible.

1 Like

I guess the problem with this method is that when increasing the size, there’s a blank area until the timer kicks in. I guess what I really want is to rubber band whilst resizing, until I let go of the mouse, and then to process the resize.

AudioProcessorEditor::resized() gets called when in standalone, but not when running inside a VST it would seem. Is there some other way inside a VST to get the size change?

Have you set setResizable (true, true) allowing the host to resize?

yep.