RadioButton.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  3. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/Event.h>
  8. #include <LibGfx/Painter.h>
  9. #include <LibGfx/StylePainter.h>
  10. #include <LibWeb/DOM/Document.h>
  11. #include <LibWeb/HTML/BrowsingContext.h>
  12. #include <LibWeb/Layout/Label.h>
  13. #include <LibWeb/Layout/RadioButton.h>
  14. #include <LibWeb/Painting/RadioButtonPaintable.h>
  15. namespace Web::Layout {
  16. RadioButton::RadioButton(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style)
  17. : LabelableNode(document, element, move(style))
  18. {
  19. set_intrinsic_width(12);
  20. set_intrinsic_height(12);
  21. }
  22. RadioButton::~RadioButton()
  23. {
  24. }
  25. void RadioButton::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  26. {
  27. if (button != GUI::MouseButton::Primary || !dom_node().enabled())
  28. return;
  29. m_being_pressed = true;
  30. set_needs_display();
  31. m_tracking_mouse = true;
  32. browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
  33. }
  34. void RadioButton::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  35. {
  36. if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !dom_node().enabled())
  37. return;
  38. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  39. NonnullRefPtr protect = *this;
  40. bool is_inside_node_or_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position);
  41. if (!is_inside_node_or_label)
  42. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  43. if (is_inside_node_or_label)
  44. set_checked_within_group();
  45. m_being_pressed = false;
  46. m_tracking_mouse = false;
  47. browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
  48. }
  49. void RadioButton::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  50. {
  51. if (!m_tracking_mouse || !dom_node().enabled())
  52. return;
  53. bool is_inside_node_or_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position);
  54. if (!is_inside_node_or_label)
  55. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  56. if (m_being_pressed == is_inside_node_or_label)
  57. return;
  58. m_being_pressed = is_inside_node_or_label;
  59. set_needs_display();
  60. }
  61. void RadioButton::handle_associated_label_mousedown(Badge<Label>)
  62. {
  63. m_being_pressed = true;
  64. set_needs_display();
  65. }
  66. void RadioButton::handle_associated_label_mouseup(Badge<Label>)
  67. {
  68. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  69. NonnullRefPtr protect = *this;
  70. set_checked_within_group();
  71. m_being_pressed = false;
  72. }
  73. void RadioButton::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  74. {
  75. if (m_being_pressed == is_inside_node_or_label)
  76. return;
  77. m_being_pressed = is_inside_node_or_label;
  78. set_needs_display();
  79. }
  80. void RadioButton::set_checked_within_group()
  81. {
  82. if (dom_node().checked())
  83. return;
  84. dom_node().set_checked(true, HTML::HTMLInputElement::ChangeSource::User);
  85. String name = dom_node().name();
  86. document().for_each_in_inclusive_subtree_of_type<HTML::HTMLInputElement>([&](auto& element) {
  87. if (element.checked() && (element.layout_node() != this) && (element.name() == name))
  88. element.set_checked(false, HTML::HTMLInputElement::ChangeSource::User);
  89. return IterationDecision::Continue;
  90. });
  91. }
  92. RefPtr<Painting::Paintable> RadioButton::create_paintable() const
  93. {
  94. return Painting::RadioButtonPaintable::create(*this);
  95. }
  96. }