TextPaintable.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/BrowsingContext.h>
  7. #include <LibWeb/Layout/Label.h>
  8. #include <LibWeb/Page/EventHandler.h>
  9. #include <LibWeb/Painting/TextPaintable.h>
  10. namespace Web::Painting {
  11. NonnullRefPtr<TextPaintable> TextPaintable::create(Layout::TextNode const& layout_node)
  12. {
  13. return adopt_ref(*new TextPaintable(layout_node));
  14. }
  15. TextPaintable::TextPaintable(Layout::TextNode const& layout_node)
  16. : Paintable(layout_node)
  17. {
  18. }
  19. bool TextPaintable::wants_mouse_events() const
  20. {
  21. return layout_node().first_ancestor_of_type<Layout::Label>();
  22. }
  23. void TextPaintable::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  24. {
  25. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  26. if (!label)
  27. return;
  28. const_cast<Layout::Label*>(label)->handle_mousedown_on_label({}, position, button);
  29. const_cast<HTML::BrowsingContext&>(browsing_context()).event_handler().set_mouse_event_tracking_layout_node(&const_cast<Layout::TextNode&>(layout_node()));
  30. }
  31. void TextPaintable::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  32. {
  33. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  34. if (!label)
  35. return;
  36. // NOTE: Changing the state of the DOM node may run arbitrary JS, which could disappear this node.
  37. NonnullRefPtr protect = *this;
  38. const_cast<Layout::Label*>(label)->handle_mouseup_on_label({}, position, button);
  39. const_cast<HTML::BrowsingContext&>(browsing_context()).event_handler().set_mouse_event_tracking_layout_node(nullptr);
  40. }
  41. void TextPaintable::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  42. {
  43. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  44. if (!label)
  45. return;
  46. const_cast<Layout::Label*>(label)->handle_mousemove_on_label({}, position, button);
  47. }
  48. }