ButtonPaintable.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <LibWeb/HTML/BrowsingContext.h>
  8. #include <LibWeb/HTML/HTMLImageElement.h>
  9. #include <LibWeb/Layout/ButtonBox.h>
  10. #include <LibWeb/Layout/Label.h>
  11. #include <LibWeb/Painting/ButtonPaintable.h>
  12. namespace Web::Painting {
  13. NonnullRefPtr<ButtonPaintable> ButtonPaintable::create(Layout::ButtonBox const& layout_box)
  14. {
  15. return adopt_ref(*new ButtonPaintable(layout_box));
  16. }
  17. ButtonPaintable::ButtonPaintable(Layout::ButtonBox const& layout_box)
  18. : LabelablePaintable(layout_box)
  19. {
  20. }
  21. Layout::ButtonBox const& ButtonPaintable::layout_box() const
  22. {
  23. return static_cast<Layout::ButtonBox const&>(layout_node());
  24. }
  25. Layout::ButtonBox& ButtonPaintable::layout_box()
  26. {
  27. return static_cast<Layout::ButtonBox&>(layout_node());
  28. }
  29. void ButtonPaintable::paint(PaintContext& context, PaintPhase phase) const
  30. {
  31. if (!is_visible())
  32. return;
  33. PaintableBox::paint(context, phase);
  34. if (phase == PaintPhase::Foreground) {
  35. auto text_rect = enclosing_int_rect(absolute_rect());
  36. if (m_being_pressed)
  37. text_rect.translate_by(1, 1);
  38. context.painter().draw_text(text_rect, layout_box().dom_node().value(), layout_box().font(), Gfx::TextAlignment::Center, computed_values().color());
  39. }
  40. }
  41. void ButtonPaintable::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  42. {
  43. if (button != GUI::MouseButton::Primary || !layout_box().dom_node().enabled())
  44. return;
  45. m_being_pressed = true;
  46. set_needs_display();
  47. m_tracking_mouse = true;
  48. browsing_context().event_handler().set_mouse_event_tracking_layout_node(&layout_box());
  49. }
  50. void ButtonPaintable::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  51. {
  52. if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !layout_box().dom_node().enabled())
  53. return;
  54. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  55. NonnullRefPtr protected_this = *this;
  56. NonnullRefPtr protected_browsing_context = browsing_context();
  57. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  58. if (!is_inside_node_or_label)
  59. is_inside_node_or_label = Layout::Label::is_inside_associated_label(layout_box(), position);
  60. if (is_inside_node_or_label)
  61. const_cast<Layout::ButtonBox&>(layout_box()).dom_node().did_click_button({});
  62. m_being_pressed = false;
  63. m_tracking_mouse = false;
  64. protected_browsing_context->event_handler().set_mouse_event_tracking_layout_node(nullptr);
  65. }
  66. void ButtonPaintable::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  67. {
  68. if (!m_tracking_mouse || !layout_box().dom_node().enabled())
  69. return;
  70. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  71. if (!is_inside_node_or_label)
  72. is_inside_node_or_label = Layout::Label::is_inside_associated_label(layout_box(), position);
  73. if (m_being_pressed == is_inside_node_or_label)
  74. return;
  75. m_being_pressed = is_inside_node_or_label;
  76. set_needs_display();
  77. }
  78. void ButtonPaintable::handle_associated_label_mousedown(Badge<Layout::Label>)
  79. {
  80. m_being_pressed = true;
  81. set_needs_display();
  82. }
  83. void ButtonPaintable::handle_associated_label_mouseup(Badge<Layout::Label>)
  84. {
  85. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  86. NonnullRefPtr protected_this = *this;
  87. NonnullRefPtr protected_browsing_context = browsing_context();
  88. layout_box().dom_node().did_click_button({});
  89. m_being_pressed = false;
  90. }
  91. void ButtonPaintable::handle_associated_label_mousemove(Badge<Layout::Label>, bool is_inside_node_or_label)
  92. {
  93. if (m_being_pressed == is_inside_node_or_label)
  94. return;
  95. m_being_pressed = is_inside_node_or_label;
  96. set_needs_display();
  97. }
  98. }