ButtonPaintable.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. JS::NonnullGCPtr<ButtonPaintable> ButtonPaintable::create(Layout::ButtonBox const& layout_box)
  14. {
  15. return layout_box.heap().allocate_without_realm<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. auto const& dom_node = layout_box().dom_node();
  35. if (is<HTML::HTMLInputElement>(dom_node) && phase == PaintPhase::Foreground) {
  36. auto button_rect = context.enclosing_device_rect(absolute_rect());
  37. auto text_rect = button_rect;
  38. // Apply CSS text-indent property to text rect
  39. // FIXME: The second parameter to to_px() needs to be the block container’s own inline-axis inner size:
  40. // https://drafts.csswg.org/css-text-3/#propdef-text-indent
  41. auto text_indent = computed_values().text_indent().to_px(layout_box(), CSSPixels());
  42. text_rect.translate_by(context.rounded_device_pixels(text_indent), 0);
  43. // Apply button pressed state offset
  44. if (being_pressed()) {
  45. auto offset = context.rounded_device_pixels(1);
  46. text_rect.translate_by(offset, offset);
  47. }
  48. // Paint button text clipped to button rect
  49. auto& painter = context.recording_painter();
  50. painter.save();
  51. auto clip_rect = absolute_rect();
  52. clip_rect.translate_by(enclosing_scroll_frame_offset().value_or({}));
  53. painter.add_clip_rect(context.enclosing_device_rect(clip_rect).to_type<int>());
  54. painter.draw_text(
  55. text_rect.to_type<int>(),
  56. static_cast<HTML::HTMLInputElement const&>(dom_node).value(),
  57. layout_box().scaled_font(context.device_pixels_per_css_pixel()),
  58. Gfx::TextAlignment::Center,
  59. computed_values().color());
  60. painter.restore();
  61. }
  62. }
  63. }