Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#pragma once
8
9#include <AK/Function.h>
10#include <LibWeb/Loader/ImageResource.h>
11#include <LibWeb/Platform/Timer.h>
12
13namespace Web {
14
15class ImageLoader : public ImageResourceClient {
16public:
17 ImageLoader(DOM::Element& owner_element);
18
19 void adopt_object_resource(Badge<HTML::HTMLObjectElement>, Resource&);
20
21 void load(const AK::URL&);
22
23 Gfx::Bitmap const* bitmap(size_t index) const;
24 size_t current_frame_index() const { return m_current_frame_index; }
25
26 bool has_image() const;
27
28 bool has_loaded_or_failed() const { return m_loading_state != LoadingState::Loading; }
29
30 void set_visible_in_viewport(bool) const;
31
32 unsigned width() const;
33 unsigned height() const;
34
35 Function<void()> on_load;
36 Function<void()> on_fail;
37 Function<void()> on_animate;
38
39private:
40 void load_without_resetting_redirect_counter(AK::URL const&);
41
42 // ^ImageResourceClient
43 virtual void resource_did_load() override;
44 virtual void resource_did_fail() override;
45 virtual bool is_visible_in_viewport() const override { return m_visible_in_viewport; }
46
47 void animate();
48
49 enum class LoadingState {
50 None,
51 Loading,
52 Loaded,
53 Failed,
54 };
55
56 DOM::Element& m_owner_element;
57
58 mutable bool m_visible_in_viewport { false };
59
60 size_t m_current_frame_index { 0 };
61 size_t m_loops_completed { 0 };
62 LoadingState m_loading_state { LoadingState::Loading };
63 NonnullRefPtr<Platform::Timer> m_timer;
64 size_t m_redirects_count { 0 };
65};
66
67}