2020-06-13 22:22:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-13 22:22:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Function.h>
|
2020-06-14 19:26:25 +02:00
|
|
|
#include <LibCore/Timer.h>
|
2020-06-13 22:22:54 +02:00
|
|
|
#include <LibWeb/Loader/ImageResource.h>
|
|
|
|
|
|
|
|
namespace Web {
|
|
|
|
|
|
|
|
class ImageLoader : public ImageResourceClient {
|
|
|
|
public:
|
2021-04-14 10:39:12 -04:00
|
|
|
ImageLoader(DOM::Element& owner_element);
|
2020-06-13 22:22:54 +02:00
|
|
|
|
|
|
|
void load(const URL&);
|
|
|
|
|
2021-01-29 22:30:48 +01:00
|
|
|
const Gfx::Bitmap* bitmap(size_t index) const;
|
|
|
|
size_t current_frame_index() const { return m_current_frame_index; }
|
2020-06-13 22:22:54 +02:00
|
|
|
|
2020-06-22 21:41:10 +02:00
|
|
|
bool has_image() const;
|
|
|
|
|
2020-08-12 13:47:55 +02:00
|
|
|
bool has_loaded_or_failed() const { return m_loading_state != LoadingState::Loading; }
|
|
|
|
|
2020-06-14 19:32:23 +02:00
|
|
|
void set_visible_in_viewport(bool) const;
|
2020-06-13 22:22:54 +02:00
|
|
|
|
2020-06-22 21:41:10 +02:00
|
|
|
unsigned width() const;
|
|
|
|
unsigned height() const;
|
|
|
|
|
2020-06-13 22:22:54 +02:00
|
|
|
Function<void()> on_load;
|
|
|
|
Function<void()> on_fail;
|
2020-06-14 19:26:25 +02:00
|
|
|
Function<void()> on_animate;
|
2020-06-13 22:22:54 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// ^ImageResourceClient
|
|
|
|
virtual void resource_did_load() override;
|
|
|
|
virtual void resource_did_fail() override;
|
|
|
|
virtual bool is_visible_in_viewport() const override { return m_visible_in_viewport; }
|
|
|
|
|
2020-06-14 19:26:25 +02:00
|
|
|
void animate();
|
|
|
|
|
2020-08-12 13:47:55 +02:00
|
|
|
enum class LoadingState {
|
|
|
|
None,
|
|
|
|
Loading,
|
|
|
|
Loaded,
|
|
|
|
Failed,
|
|
|
|
};
|
|
|
|
|
2021-04-14 10:39:12 -04:00
|
|
|
DOM::Element& m_owner_element;
|
|
|
|
|
2020-06-14 19:32:23 +02:00
|
|
|
mutable bool m_visible_in_viewport { false };
|
2020-06-14 19:26:25 +02:00
|
|
|
|
|
|
|
size_t m_current_frame_index { 0 };
|
|
|
|
size_t m_loops_completed { 0 };
|
2020-08-12 13:47:55 +02:00
|
|
|
LoadingState m_loading_state { LoadingState::Loading };
|
2020-06-14 19:26:25 +02:00
|
|
|
NonnullRefPtr<Core::Timer> m_timer;
|
2020-06-13 22:22:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|