ButtonBox.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Event.h>
  7. #include <LibGfx/Font.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/ButtonBox.h>
  13. #include <LibWeb/Layout/Label.h>
  14. #include <LibWeb/Painting/ButtonPaintable.h>
  15. namespace Web::Layout {
  16. ButtonBox::ButtonBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style)
  17. : LabelableNode(document, element, move(style))
  18. {
  19. }
  20. ButtonBox::~ButtonBox()
  21. {
  22. }
  23. void ButtonBox::prepare_for_replaced_layout()
  24. {
  25. set_intrinsic_width(font().width(dom_node().value()));
  26. set_intrinsic_height(font().glyph_height());
  27. }
  28. void ButtonBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  29. {
  30. if (button != GUI::MouseButton::Primary || !dom_node().enabled())
  31. return;
  32. m_being_pressed = true;
  33. set_needs_display();
  34. m_tracking_mouse = true;
  35. browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
  36. }
  37. void ButtonBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  38. {
  39. if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !dom_node().enabled())
  40. return;
  41. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  42. NonnullRefPtr protected_this = *this;
  43. NonnullRefPtr protected_browsing_context = browsing_context();
  44. bool is_inside_node_or_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position);
  45. if (!is_inside_node_or_label)
  46. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  47. if (is_inside_node_or_label)
  48. dom_node().did_click_button({});
  49. m_being_pressed = false;
  50. m_tracking_mouse = false;
  51. protected_browsing_context->event_handler().set_mouse_event_tracking_layout_node(nullptr);
  52. }
  53. void ButtonBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  54. {
  55. if (!m_tracking_mouse || !dom_node().enabled())
  56. return;
  57. bool is_inside_node_or_label = enclosing_int_rect(paint_box()->absolute_rect()).contains(position);
  58. if (!is_inside_node_or_label)
  59. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  60. if (m_being_pressed == is_inside_node_or_label)
  61. return;
  62. m_being_pressed = is_inside_node_or_label;
  63. set_needs_display();
  64. }
  65. void ButtonBox::handle_associated_label_mousedown(Badge<Label>)
  66. {
  67. m_being_pressed = true;
  68. set_needs_display();
  69. }
  70. void ButtonBox::handle_associated_label_mouseup(Badge<Label>)
  71. {
  72. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  73. NonnullRefPtr protected_this = *this;
  74. NonnullRefPtr protected_browsing_context = browsing_context();
  75. dom_node().did_click_button({});
  76. m_being_pressed = false;
  77. }
  78. void ButtonBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  79. {
  80. if (m_being_pressed == is_inside_node_or_label)
  81. return;
  82. m_being_pressed = is_inside_node_or_label;
  83. set_needs_display();
  84. }
  85. RefPtr<Painting::Paintable> ButtonBox::create_paintable() const
  86. {
  87. return Painting::ButtonPaintable::create(*this);
  88. }
  89. }