ButtonBox.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/Layout/ButtonBox.h>
  12. #include <LibWeb/Layout/Label.h>
  13. #include <LibWeb/Page/BrowsingContext.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_has_intrinsic_width(true);
  26. set_intrinsic_height(20);
  27. set_has_intrinsic_height(true);
  28. }
  29. void ButtonBox::paint(PaintContext& context, PaintPhase phase)
  30. {
  31. if (!is_visible())
  32. return;
  33. LabelableNode::paint(context, phase);
  34. if (phase == PaintPhase::Foreground) {
  35. bool hovered = document().hovered_node() == &dom_node();
  36. if (!hovered)
  37. hovered = Label::is_associated_label_hovered(*this);
  38. 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());
  39. auto text_rect = enclosing_int_rect(absolute_rect());
  40. if (m_being_pressed)
  41. text_rect.translate_by(1, 1);
  42. context.painter().draw_text(text_rect, dom_node().value(), font(), Gfx::TextAlignment::Center, context.palette().button_text());
  43. }
  44. }
  45. void ButtonBox::handle_mousedown(Badge<EventHandler>, const Gfx::IntPoint&, unsigned button, unsigned)
  46. {
  47. if (button != GUI::MouseButton::Left || !dom_node().enabled())
  48. return;
  49. m_being_pressed = true;
  50. set_needs_display();
  51. m_tracking_mouse = true;
  52. browsing_context().event_handler().set_mouse_event_tracking_layout_node(this);
  53. }
  54. void ButtonBox::handle_mouseup(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned button, unsigned)
  55. {
  56. if (!m_tracking_mouse || button != GUI::MouseButton::Left || !dom_node().enabled())
  57. return;
  58. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  59. NonnullRefPtr protected_this = *this;
  60. NonnullRefPtr protected_frame = browsing_context();
  61. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  62. if (!is_inside_node_or_label)
  63. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  64. if (is_inside_node_or_label)
  65. dom_node().did_click_button({});
  66. m_being_pressed = false;
  67. m_tracking_mouse = false;
  68. protected_frame->event_handler().set_mouse_event_tracking_layout_node(nullptr);
  69. }
  70. void ButtonBox::handle_mousemove(Badge<EventHandler>, const Gfx::IntPoint& position, unsigned, unsigned)
  71. {
  72. if (!m_tracking_mouse || !dom_node().enabled())
  73. return;
  74. bool is_inside_node_or_label = enclosing_int_rect(absolute_rect()).contains(position);
  75. if (!is_inside_node_or_label)
  76. is_inside_node_or_label = Label::is_inside_associated_label(*this, position);
  77. if (m_being_pressed == is_inside_node_or_label)
  78. return;
  79. m_being_pressed = is_inside_node_or_label;
  80. set_needs_display();
  81. }
  82. void ButtonBox::handle_associated_label_mousedown(Badge<Label>)
  83. {
  84. m_being_pressed = true;
  85. set_needs_display();
  86. }
  87. void ButtonBox::handle_associated_label_mouseup(Badge<Label>)
  88. {
  89. // NOTE: Handling the click may run arbitrary JS, which could disappear this node.
  90. NonnullRefPtr protected_this = *this;
  91. NonnullRefPtr protected_frame = browsing_context();
  92. dom_node().did_click_button({});
  93. m_being_pressed = false;
  94. }
  95. void ButtonBox::handle_associated_label_mousemove(Badge<Label>, bool is_inside_node_or_label)
  96. {
  97. if (m_being_pressed == is_inside_node_or_label)
  98. return;
  99. m_being_pressed = is_inside_node_or_label;
  100. set_needs_display();
  101. }
  102. }