TextPaintable.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/Layout/LabelableNode.h>
  9. #include <LibWeb/Page/EventHandler.h>
  10. #include <LibWeb/Painting/TextPaintable.h>
  11. namespace Web::Painting {
  12. JS::NonnullGCPtr<TextPaintable> TextPaintable::create(Layout::TextNode const& layout_node)
  13. {
  14. return layout_node.heap().allocate_without_realm<TextPaintable>(layout_node);
  15. }
  16. TextPaintable::TextPaintable(Layout::TextNode const& layout_node)
  17. : Paintable(layout_node)
  18. {
  19. }
  20. bool TextPaintable::wants_mouse_events() const
  21. {
  22. return layout_node().first_ancestor_of_type<Layout::Label>();
  23. }
  24. DOM::Node* TextPaintable::mouse_event_target() const
  25. {
  26. if (auto* label = layout_node().first_ancestor_of_type<Layout::Label>()) {
  27. if (auto* control = const_cast<Layout::Label*>(label)->labeled_control())
  28. return &control->dom_node();
  29. }
  30. return nullptr;
  31. }
  32. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousedown(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  33. {
  34. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  35. if (!label)
  36. return DispatchEventOfSameName::No;
  37. const_cast<Layout::Label*>(label)->handle_mousedown_on_label({}, position, button);
  38. const_cast<HTML::BrowsingContext&>(browsing_context()).event_handler().set_mouse_event_tracking_paintable(this);
  39. return DispatchEventOfSameName::Yes;
  40. }
  41. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mouseup(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  42. {
  43. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  44. if (!label)
  45. return DispatchEventOfSameName::No;
  46. const_cast<Layout::Label*>(label)->handle_mouseup_on_label({}, position, button);
  47. const_cast<HTML::BrowsingContext&>(browsing_context()).event_handler().set_mouse_event_tracking_paintable(nullptr);
  48. return DispatchEventOfSameName::Yes;
  49. }
  50. TextPaintable::DispatchEventOfSameName TextPaintable::handle_mousemove(Badge<EventHandler>, CSSPixelPoint position, unsigned button, unsigned)
  51. {
  52. auto* label = layout_node().first_ancestor_of_type<Layout::Label>();
  53. if (!label)
  54. return DispatchEventOfSameName::No;
  55. const_cast<Layout::Label*>(label)->handle_mousemove_on_label({}, position, button);
  56. return DispatchEventOfSameName::Yes;
  57. }
  58. }