How to bypass (stop) paint or update when app is hidden

I started a new project from Projucer, an “Animated Application”.

I am just playing around with a super simple code snippet’

{
    // Make sure you set the size of the component after
    // you add any child components.
    setSize (1024, 768);
    setFramesPerSecond (60); // This sets the frequency of the update calls.
}

MainComponent::~MainComponent()
{
}

//==============================================================================
void MainComponent::update()
{
    // This function is called at the frequency specified by the setFramesPerSecond() call
    // in the constructor. You can use it to update counters, animate values, etc.
	x++;
	if (x > 500) x = 0;
}

//==============================================================================
void MainComponent::paint (Graphics& g)
{
    // Blank window every frame rate (Our component is opaque, so we must completely fill the background with a solid colour)	
	g.fillAll(Colours::darkgrey);

	g.setColour(Colours::orange);
	g.fillRect(x, x, 40, 40);
}

Question is how do I prevent the code in MainComponent::paint from being called on every update, when the document component (well app in this case) is hidden by other apps, or minimized.

I figure some kind of if test around the code with this something like this;
if (&MainComponent::isOnDesktop)
or
if (&MainComponent::isShowing)

But that does not make any difference, most likely because that is not the right way to do it.

Pausing animation when app is hidden is necessary when for example the app is a game, and therefore do not want the animation to continue.

1 Like

What yo are probably looking for is Process::isForegroundProcess(). See this post for a bit more information

These two might also be interesting for you:

Component::focusLost() or Component::focusGained()

Toby

1 Like

Ahh thanks! I made the mistake of checking in the paint instead of the update. So this worked;

void MainComponent::update()
{
    // This function is called at the frequency specified by the setFramesPerSecond() call
    // in the constructor. You can use it to update counters, animate values, etc.
	if (Process::isForegroundProcess()) {
		x++;
		if (x > 500) x = 0;
	}
}