Window.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/IDAllocator.h>
  9. #include <AK/RefCounted.h>
  10. #include <AK/RefPtr.h>
  11. #include <LibWeb/Bindings/WindowObject.h>
  12. #include <LibWeb/Bindings/Wrappable.h>
  13. #include <LibWeb/CSS/MediaQueryList.h>
  14. #include <LibWeb/CSS/Screen.h>
  15. #include <LibWeb/DOM/Event.h>
  16. #include <LibWeb/DOM/EventTarget.h>
  17. #include <LibWeb/HTML/GlobalEventHandlers.h>
  18. namespace Web::DOM {
  19. class RequestAnimationFrameCallback;
  20. class Window final
  21. : public RefCounted<Window>
  22. , public EventTarget
  23. , public HTML::GlobalEventHandlers {
  24. public:
  25. static NonnullRefPtr<Window> create_with_document(Document&);
  26. ~Window();
  27. using RefCounted::ref;
  28. using RefCounted::unref;
  29. virtual void ref_event_target() override { RefCounted::ref(); }
  30. virtual void unref_event_target() override { RefCounted::unref(); }
  31. virtual bool dispatch_event(NonnullRefPtr<Event>) override;
  32. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  33. Page* page();
  34. Page const* page() const;
  35. Document const& associated_document() const { return *m_associated_document; }
  36. Document& associated_document() { return *m_associated_document; }
  37. void alert(String const&);
  38. bool confirm(String const&);
  39. String prompt(String const&, String const&);
  40. i32 request_animation_frame(JS::FunctionObject&);
  41. void cancel_animation_frame(i32);
  42. i32 set_timeout(JS::FunctionObject&, i32);
  43. i32 set_interval(JS::FunctionObject&, i32);
  44. void clear_timeout(i32);
  45. void clear_interval(i32);
  46. void queue_microtask(JS::FunctionObject&);
  47. int inner_width() const;
  48. int inner_height() const;
  49. void did_set_location_href(Badge<Bindings::LocationObject>, AK::URL const& new_href);
  50. void did_call_location_reload(Badge<Bindings::LocationObject>);
  51. Bindings::WindowObject* wrapper() { return m_wrapper; }
  52. Bindings::WindowObject const* wrapper() const { return m_wrapper; }
  53. void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
  54. i32 allocate_timer_id(Badge<Timer>);
  55. void deallocate_timer_id(Badge<Timer>, i32);
  56. void timer_did_fire(Badge<Timer>, Timer&);
  57. HighResolutionTime::Performance& performance() { return *m_performance; }
  58. CSS::Screen& screen() { return *m_screen; }
  59. Event const* current_event() const { return m_current_event; }
  60. void set_current_event(Event* event) { m_current_event = event; }
  61. NonnullRefPtr<CSS::CSSStyleDeclaration> get_computed_style(DOM::Element&) const;
  62. NonnullRefPtr<CSS::MediaQueryList> match_media(String);
  63. float scroll_x() const;
  64. float scroll_y() const;
  65. void fire_a_page_transition_event(FlyString event_name, bool persisted);
  66. float device_pixel_ratio() const;
  67. int screen_x() const;
  68. int screen_y() const;
  69. private:
  70. explicit Window(Document&);
  71. // ^HTML::GlobalEventHandlers
  72. virtual DOM::EventTarget& global_event_handlers_to_event_target() override { return *this; }
  73. // https://html.spec.whatwg.org/multipage/window-object.html#concept-document-window
  74. WeakPtr<Document> m_associated_document;
  75. WeakPtr<Bindings::WindowObject> m_wrapper;
  76. IDAllocator m_timer_id_allocator;
  77. HashMap<int, NonnullRefPtr<Timer>> m_timers;
  78. NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
  79. NonnullRefPtr<CSS::Screen> m_screen;
  80. RefPtr<Event> m_current_event;
  81. HashMap<i32, NonnullRefPtr<RequestAnimationFrameCallback>> m_request_animation_frame_callbacks;
  82. };
  83. }