Window.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * Copyright (c) 2020, 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/Screen.h>
  14. #include <LibWeb/DOM/Event.h>
  15. #include <LibWeb/DOM/EventTarget.h>
  16. namespace Web::DOM {
  17. class Window final
  18. : public RefCounted<Window>
  19. , public EventTarget {
  20. public:
  21. static NonnullRefPtr<Window> create_with_document(Document&);
  22. ~Window();
  23. using RefCounted::ref;
  24. using RefCounted::unref;
  25. virtual void ref_event_target() override { RefCounted::ref(); }
  26. virtual void unref_event_target() override { RefCounted::unref(); }
  27. virtual bool dispatch_event(NonnullRefPtr<Event>) override;
  28. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  29. const Document& document() const { return m_document; }
  30. Document& document() { return m_document; }
  31. void alert(const String&);
  32. bool confirm(const String&);
  33. String prompt(const String&, const String&);
  34. i32 request_animation_frame(JS::FunctionObject&);
  35. void cancel_animation_frame(i32);
  36. i32 set_timeout(JS::FunctionObject&, i32);
  37. i32 set_interval(JS::FunctionObject&, i32);
  38. void clear_timeout(i32);
  39. void clear_interval(i32);
  40. int inner_width() const;
  41. int inner_height() const;
  42. void did_set_location_href(Badge<Bindings::LocationObject>, const URL& new_href);
  43. void did_call_location_reload(Badge<Bindings::LocationObject>);
  44. Bindings::WindowObject* wrapper() { return m_wrapper; }
  45. const Bindings::WindowObject* wrapper() const { return m_wrapper; }
  46. void set_wrapper(Badge<Bindings::WindowObject>, Bindings::WindowObject&);
  47. i32 allocate_timer_id(Badge<Timer>);
  48. void deallocate_timer_id(Badge<Timer>, i32);
  49. void timer_did_fire(Badge<Timer>, Timer&);
  50. HighResolutionTime::Performance& performance() { return *m_performance; }
  51. CSS::Screen& screen() { return *m_screen; }
  52. const Event* current_event() const { return m_current_event; }
  53. void set_current_event(Event* event) { m_current_event = event; }
  54. private:
  55. explicit Window(Document&);
  56. Document& m_document;
  57. WeakPtr<Bindings::WindowObject> m_wrapper;
  58. IDAllocator m_timer_id_allocator;
  59. HashMap<int, NonnullRefPtr<Timer>> m_timers;
  60. NonnullOwnPtr<HighResolutionTime::Performance> m_performance;
  61. NonnullRefPtr<CSS::Screen> m_screen;
  62. RefPtr<Event> m_current_event;
  63. };
  64. }