Serenity Operating System
1/*
2 * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include "Animation.h"
8#include "Compositor.h"
9#include <AK/Badge.h>
10
11namespace WindowServer {
12
13Animation::Animation()
14{
15 Compositor::the().register_animation({}, *this);
16}
17
18Animation::~Animation()
19{
20 if (!m_was_removed)
21 Compositor::the().unregister_animation({}, *this);
22}
23
24void Animation::set_duration(int duration_in_ms)
25{
26 m_duration = duration_in_ms;
27}
28
29void Animation::start()
30{
31 m_running = true;
32 m_timer.start();
33 Compositor::the().animation_started({});
34}
35
36void Animation::stop()
37{
38 m_running = false;
39 if (on_stop)
40 on_stop();
41}
42
43void Animation::was_removed(Badge<Compositor>)
44{
45 m_was_removed = true;
46}
47
48bool Animation::update(Badge<Compositor>, Gfx::Painter& painter, Screen& screen, Gfx::DisjointIntRectSet& flush_rects)
49{
50 i64 const elapsed_ms = m_timer.elapsed();
51 float progress = min((float)elapsed_ms / (float)m_duration, 1.0f);
52
53 if (on_update)
54 on_update(progress, painter, screen, flush_rects);
55
56 return progress < 1.0f;
57}
58
59}