ImageStyleValue.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #pragma once
  10. #include <AK/URL.h>
  11. #include <LibJS/Heap/Cell.h>
  12. #include <LibJS/Heap/Handle.h>
  13. #include <LibWeb/CSS/Enums.h>
  14. #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
  15. #include <LibWeb/HTML/SharedImageRequest.h>
  16. namespace Web::CSS {
  17. class ImageStyleValue final
  18. : public AbstractImageStyleValue
  19. , public Weakable<ImageStyleValue> {
  20. public:
  21. static ValueComparingNonnullRefPtr<ImageStyleValue> create(URL const& url)
  22. {
  23. return adopt_ref(*new (nothrow) ImageStyleValue(url));
  24. }
  25. virtual ~ImageStyleValue() override = default;
  26. void visit_edges(JS::Cell::Visitor& visitor) const
  27. {
  28. // FIXME: visit_edges in non-GC allocated classes is confusing pattern.
  29. // Consider making StyleValue to be GC allocated instead.
  30. visitor.visit(m_image_request);
  31. }
  32. virtual String to_string() const override;
  33. virtual bool equals(StyleValue const& other) const override;
  34. virtual void load_any_resources(DOM::Document&) override;
  35. Optional<CSSPixels> natural_width() const override;
  36. Optional<CSSPixels> natural_height() const override;
  37. Optional<CSSPixelFraction> natural_aspect_ratio() const override;
  38. virtual bool is_paintable() const override;
  39. void paint(PaintContext& context, DevicePixelRect const& dest_rect, CSS::ImageRendering image_rendering, Vector<Gfx::Path> const& clip_paths = {}) const override;
  40. virtual Optional<Gfx::Color> color_if_single_pixel_bitmap() const override;
  41. Function<void()> on_animate;
  42. JS::GCPtr<HTML::DecodedImageData> image_data() const;
  43. private:
  44. ImageStyleValue(URL const&);
  45. JS::GCPtr<HTML::SharedImageRequest> m_image_request;
  46. void animate();
  47. Gfx::ImmutableBitmap const* bitmap(size_t frame_index, Gfx::IntSize = {}) const;
  48. URL m_url;
  49. WeakPtr<DOM::Document> m_document;
  50. size_t m_current_frame_index { 0 };
  51. size_t m_loops_completed { 0 };
  52. RefPtr<Platform::Timer> m_timer;
  53. };
  54. }