ladybird/Userland/Services/WindowServer/Animation.cpp
Tom 9f59d7d9a0 WindowServer: Fix animation crash
If an Animation's on_stop handler cleared itself inside the on_stop
event handler, it would remove itself from the animation map that was
still being iterated, leading to a crash.

To solve this, we'll iterate over the animations using the
remove_all_matching function, which enables us to delete it by simply
returning true when the animation finished. In the event that the
Animation is kept alive elsewhere and the on_stop event clears its own
reference, we need to temporarily bump the reference count. Another
advantage is that we only need to bump the reference count when an
animation is finished, whereas before this we bumped it
unconditionally.
2022-03-18 20:00:30 +01:00

59 lines
1.1 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Animation.h"
#include "Compositor.h"
#include <AK/Badge.h>
namespace WindowServer {
Animation::Animation()
{
Compositor::the().register_animation({}, *this);
}
Animation::~Animation()
{
if (!m_was_removed)
Compositor::the().unregister_animation({}, *this);
}
void Animation::set_duration(int duration_in_ms)
{
m_duration = duration_in_ms;
}
void Animation::start()
{
m_running = true;
m_timer.start();
Compositor::the().animation_started({});
}
void Animation::stop()
{
m_running = false;
if (on_stop)
on_stop();
}
void Animation::was_removed(Badge<Compositor>)
{
m_was_removed = true;
}
bool Animation::update(Badge<Compositor>, Gfx::Painter& painter, Screen& screen, Gfx::DisjointRectSet& flush_rects)
{
int elapsed_ms = m_timer.elapsed();
float progress = min((float)elapsed_ms / (float)m_duration, 1.0f);
if (on_update)
on_update(progress, painter, screen, flush_rects);
return progress < 1.0f;
}
}