1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

WindowServer: Add a more generic mechanism for animations

This patch adds the WindowServer::Animation class, which represents
a simple animation driven by the compositor.

An animation has a length (in milliseconds) and two hooks:

- on_update: called whenever the animation should render something.
- on_stop: called when the animation is finished and/or stopped.

This patch also ports the window minimization animation to this new
mechanism. :^)
This commit is contained in:
Andreas Kling 2021-06-27 16:47:52 +02:00
parent 1f33c517df
commit 75f870a93f
7 changed files with 171 additions and 70 deletions

View file

@ -0,0 +1,53 @@
/*
* 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()
{
Compositor::the().unregister_animation({}, *this);
}
void Animation::set_length(int length_in_ms)
{
m_length = length_in_ms;
}
void Animation::start()
{
m_running = true;
m_timer.start();
}
void Animation::stop()
{
m_running = false;
if (on_stop)
on_stop();
}
void 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_length, 1.0f);
if (on_update)
on_update(progress, painter, screen, flush_rects);
if (progress >= 1.0f)
stop();
}
}