ButtonPaintable.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/FontCache.h>
  8. #include <LibWeb/HTML/BrowsingContext.h>
  9. #include <LibWeb/HTML/HTMLImageElement.h>
  10. #include <LibWeb/Layout/ButtonBox.h>
  11. #include <LibWeb/Layout/Label.h>
  12. #include <LibWeb/Painting/ButtonPaintable.h>
  13. namespace Web::Painting {
  14. JS::NonnullGCPtr<ButtonPaintable> ButtonPaintable::create(Layout::ButtonBox const& layout_box)
  15. {
  16. return layout_box.heap().allocate_without_realm<ButtonPaintable>(layout_box);
  17. }
  18. ButtonPaintable::ButtonPaintable(Layout::ButtonBox const& layout_box)
  19. : LabelablePaintable(layout_box)
  20. {
  21. }
  22. Layout::ButtonBox const& ButtonPaintable::layout_box() const
  23. {
  24. return static_cast<Layout::ButtonBox const&>(layout_node());
  25. }
  26. Layout::ButtonBox& ButtonPaintable::layout_box()
  27. {
  28. return static_cast<Layout::ButtonBox&>(layout_node());
  29. }
  30. void ButtonPaintable::paint(PaintContext& context, PaintPhase phase) const
  31. {
  32. if (!is_visible())
  33. return;
  34. PaintableBox::paint(context, phase);
  35. auto const& dom_node = layout_box().dom_node();
  36. if (is<HTML::HTMLInputElement>(dom_node) && phase == PaintPhase::Foreground) {
  37. auto button_rect = context.enclosing_device_rect(absolute_rect());
  38. auto text_rect = button_rect;
  39. // Apply CSS text-indent property to text rect
  40. // FIXME: The second parameter to to_px() needs to be the block container’s own inline-axis inner size:
  41. // https://drafts.csswg.org/css-text-3/#propdef-text-indent
  42. auto text_indent = computed_values().text_indent().to_px(layout_box(), CSSPixels());
  43. text_rect.translate_by(context.rounded_device_pixels(text_indent), 0);
  44. // Apply button pressed state offset
  45. if (being_pressed()) {
  46. auto offset = context.rounded_device_pixels(1);
  47. text_rect.translate_by(offset, offset);
  48. }
  49. // Paint button text clipped to button rect
  50. auto& painter = context.painter();
  51. painter.save();
  52. painter.add_clip_rect(button_rect.to_type<int>());
  53. painter.draw_text(
  54. text_rect.to_type<int>(),
  55. static_cast<HTML::HTMLInputElement const&>(dom_node).value(),
  56. document().style_computer().font_cache().scaled_font(layout_box().font(), context.device_pixels_per_css_pixel()),
  57. Gfx::TextAlignment::Center,
  58. computed_values().color());
  59. painter.restore();
  60. }
  61. }
  62. }