ButtonBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. namespace Web::Layout {
  15. ButtonBox::ButtonBox(DOM::Document& document, HTML::HTMLInputElement& element, NonnullRefPtr<CSS::StyleProperties> style)
  16. : LabelableNode(document, element, move(style))
  17. {
  18. }
  19. ButtonBox::~ButtonBox()
  20. {
  21. }
  22. void ButtonBox::prepare_for_replaced_layout()
  23. {
  24. set_intrinsic_width(font().width(dom_node().value()) + 20);
  25. set_intrinsic_height(20);
  26. }
  27. void ButtonBox::paint(PaintContext& context, PaintPhase phase)
  28. {
  29. if (!is_visible())
  30. return;
  31. LabelableNode::paint(context, phase);
  32. if (phase == PaintPhase::Foreground) {
  33. bool hovered = document().hovered_node() == &dom_node();
  34. if (!hovered)
  35. hovered = Label::is_associated_label_hovered(*this);
  36. Gfx::StylePainter::paint_button(context.painter(), enclosing_int_rect(absolute_rect()), context.palette(), Gfx::ButtonStyle::Normal, m_being_pressed, hovered, dom_node().checked(), dom_node().enabled());
  37. auto text_rect = enclosing_int_rect(absolute_rect());
  38. if (m_being_pressed)
  39. text_rect.translate_by(1, 1);
  40. context.painter().draw_text(text_rect, dom_node().value(), font(), Gfx::TextAlignment::Center, context.palette().button_text());
  41. }
  42. }
  43. void ButtonBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  44. {
  45. if (button != GUI::MouseButton::Primary || !dom_node().enabled())
  46. return;
  47. m_being_pressed = true;
  48. set_needs_display();
  49. m_tracking_mouse = true;
  50. browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
  51. }
  52. void ButtonBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  53. {
  54. if (!m_tracking_mouse || button != GUI::MouseButton::Primary || !dom_node().enabled())
  55. return;
  56. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  57. NonnullRefPtr protected_this = *this;
  58. NonnullRefPtr protected_frame = browsing_context();
  59. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  60. if (!is_inside_node_or_label)
  61. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  62. if (is_inside_node_or_label)
  63. dom_node().did_click_button({});
  64. m_being_pressed = false;
  65. m_tracking_mouse = false;
  66. protected_frame->event_handler().set_mouse_event_tracking_layout_node(nullptr);
  67. }
  68. void ButtonBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  69. {
  70. if (!m_tracking_mouse || !dom_node().enabled())
  71. return;
  72. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  73. if (!is_inside_node_or_label)
  74. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  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 ButtonBox::handle_associated_label_mousedown(Badge<Label>)
  81. {
  82. m_being_pressed = true;
  83. set_needs_display();
  84. }
  85. void ButtonBox::handle_associated_label_mouseup(Badge<Label>)
  86. {
  87. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  88. NonnullRefPtr protected_this = *this;
  89. NonnullRefPtr protected_frame = browsing_context();
  90. dom_node().did_click_button({});
  91. m_being_pressed = false;
  92. }
  93. void ButtonBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  94. {
  95. if (m_being_pressed == is_inside_node_or_label)
  96. return;
  97. m_being_pressed = is_inside_node_or_label;
  98. set_needs_display();
  99. }
  100. }