CheckBoxPaintable.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Event.h>
  7. #include <LibGfx/StylePainter.h>
  8. #include <LibWeb/HTML/BrowsingContext.h>
  9. #include <LibWeb/HTML/HTMLImageElement.h>
  10. #include <LibWeb/Layout/CheckBox.h>
  11. #include <LibWeb/Layout/Label.h>
  12. #include <LibWeb/Painting/CheckBoxPaintable.h>
  13. namespace Web::Painting {
  14. NonnullRefPtr<CheckBoxPaintable> CheckBoxPaintable::create(Layout::CheckBox const& layout_box)
  15. {
  16. return adopt_ref(*new CheckBoxPaintable(layout_box));
  17. }
  18. CheckBoxPaintable::CheckBoxPaintable(Layout::CheckBox const& layout_box)
  19. : LabelablePaintable(layout_box)
  20. {
  21. }
  22. Layout::CheckBox const& CheckBoxPaintable::layout_box() const
  23. {
  24. return static_cast<Layout::CheckBox const&>(layout_node());
  25. }
  26. Layout::CheckBox& CheckBoxPaintable::layout_box()
  27. {
  28. return static_cast<Layout::CheckBox&>(layout_node());
  29. }
  30. void CheckBoxPaintable::paint(PaintContext& context, PaintPhase phase) const
  31. {
  32. if (!is_visible())
  33. return;
  34. PaintableBox::paint(context, phase);
  35. if (phase == PaintPhase::Foreground)
  36. Gfx::StylePainter::paint_check_box(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), layout_box().dom_node().enabled(), layout_box().dom_node().checked(), m_being_pressed);
  37. }
  38. void CheckBoxPaintable::handle_mousedown(Badge<EventHandler>, Gfx::IntPoint const&, unsigned button, unsigned)
  39. {
  40. if (button != GUI::MouseButton::Primary || !layout_box().dom_node().enabled())
  41. return;
  42. m_being_pressed = true;
  43. set_needs_display();
  44. m_tracking_mouse = true;
  45. browsing_context().event_handler().set_mouse_event_tracking_layout_node(&layout_box());
  46. }
  47. void CheckBoxPaintable::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  48. {
  49. if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !layout_box().dom_node().enabled())
  50. return;
  51. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  52. NonnullRefPtr protect = *this;
  53. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  54. if (!is_inside_node_or_label)
  55. is_inside_node_or_label = Layout::Label::is_inside_associated_label(layout_box(), position);
  56. if (is_inside_node_or_label) {
  57. layout_box().dom_node().did_click_checkbox({});
  58. layout_box().dom_node().set_checked(!layout_box().dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User);
  59. }
  60. m_being_pressed = false;
  61. m_tracking_mouse = false;
  62. browsing_context().event_handler().set_mouse_event_tracking_layout_node(nullptr);
  63. }
  64. void CheckBoxPaintable::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  65. {
  66. if (!m_tracking_mouse || !layout_box().dom_node().enabled())
  67. return;
  68. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  69. if (!is_inside_node_or_label)
  70. is_inside_node_or_label = Layout::Label::is_inside_associated_label(layout_box(), position);
  71. if (m_being_pressed == is_inside_node_or_label)
  72. return;
  73. m_being_pressed = is_inside_node_or_label;
  74. set_needs_display();
  75. }
  76. void CheckBoxPaintable::handle_associated_label_mousedown(Badge<Layout::Label>)
  77. {
  78. if (!layout_box().dom_node().enabled())
  79. return;
  80. m_being_pressed = true;
  81. set_needs_display();
  82. }
  83. void CheckBoxPaintable::handle_associated_label_mouseup(Badge<Layout::Label>)
  84. {
  85. if (!layout_box().dom_node().enabled())
  86. return;
  87. // NOTE: Changing the checked state of the DOM node may run arbitrary JS, which could disappear this node.
  88. NonnullRefPtr protect = *this;
  89. layout_box().dom_node().did_click_checkbox({});
  90. layout_box().dom_node().set_checked(!layout_box().dom_node().checked(), HTML::HTMLInputElement::ChangeSource::User);
  91. m_being_pressed = false;
  92. }
  93. void CheckBoxPaintable::handle_associated_label_mousemove(Badge<Layout::Label>, bool is_inside_node_or_label)
  94. {
  95. if (m_being_pressed == is_inside_node_or_label || !layout_box().dom_node().enabled())
  96. return;
  97. m_being_pressed = is_inside_node_or_label;
  98. set_needs_display();
  99. }
  100. }