ButtonPaintable.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 text_rect = context.enclosing_device_rect(absolute_rect());
  38. if (being_pressed()) {
  39. auto offset = context.rounded_device_pixels(1);
  40. text_rect.translate_by(offset, offset);
  41. }
  42. context.painter().draw_text(
  43. text_rect.to_type<int>(),
  44. static_cast<HTML::HTMLInputElement const&>(dom_node).value(),
  45. FontCache::the().scaled_font(layout_box().font(), context.device_pixels_per_css_pixel()),
  46. Gfx::TextAlignment::Center,
  47. computed_values().color());
  48. }
  49. }
  50. }