Label.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Utf8View.h>
  8. #include <LibGUI/Label.h>
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/Bitmap.h>
  11. #include <LibGfx/Palette.h>
  12. #include <LibGfx/TextWrapping.h>
  13. REGISTER_WIDGET(GUI, Label)
  14. namespace GUI {
  15. Label::Label(String text)
  16. : m_text(move(text))
  17. {
  18. REGISTER_TEXT_ALIGNMENT_PROPERTY("text_alignment", text_alignment, set_text_alignment);
  19. REGISTER_TEXT_WRAPPING_PROPERTY("text_wrapping", text_wrapping, set_text_wrapping);
  20. set_preferred_size({ SpecialDimension::OpportunisticGrow });
  21. set_min_size({ SpecialDimension::Shrink });
  22. set_frame_style(Gfx::FrameStyle::NoFrame);
  23. set_foreground_role(Gfx::ColorRole::WindowText);
  24. REGISTER_STRING_PROPERTY("text", text, set_text);
  25. REGISTER_BOOL_PROPERTY("autosize", is_autosize, set_autosize);
  26. }
  27. void Label::set_autosize(bool autosize, size_t padding)
  28. {
  29. if (m_autosize == autosize && m_autosize_padding == padding)
  30. return;
  31. m_autosize = autosize;
  32. m_autosize_padding = padding;
  33. if (m_autosize)
  34. size_to_fit();
  35. }
  36. void Label::set_text(String text)
  37. {
  38. if (text == m_text)
  39. return;
  40. m_text = move(text);
  41. if (m_autosize)
  42. size_to_fit();
  43. update();
  44. did_change_text();
  45. }
  46. Gfx::IntRect Label::text_rect() const
  47. {
  48. return frame_inner_rect().shrunken(frame_thickness() > 0 ? font().glyph_width('x') : 0, 0);
  49. }
  50. void Label::paint_event(PaintEvent& event)
  51. {
  52. Frame::paint_event(event);
  53. Painter painter(*this);
  54. painter.add_clip_rect(event.rect());
  55. if (text().is_empty())
  56. return;
  57. auto text_rect = this->text_rect();
  58. if (is_enabled()) {
  59. painter.draw_text(text_rect, text(), text_alignment(), palette().color(foreground_role()), Gfx::TextElision::Right, text_wrapping());
  60. } else {
  61. painter.draw_text(text_rect.translated(1, 1), text(), font(), text_alignment(), palette().disabled_text_back(), Gfx::TextElision::Right, text_wrapping());
  62. painter.draw_text(text_rect, text(), font(), text_alignment(), palette().disabled_text_front(), Gfx::TextElision::Right, text_wrapping());
  63. }
  64. }
  65. void Label::did_change_font()
  66. {
  67. if (m_autosize)
  68. size_to_fit();
  69. }
  70. void Label::size_to_fit()
  71. {
  72. set_fixed_width(text_calculated_preferred_width());
  73. set_fixed_height(text_calculated_preferred_height());
  74. }
  75. int Label::text_calculated_preferred_width() const
  76. {
  77. return font().width_rounded_up(m_text) + m_autosize_padding * 2;
  78. }
  79. int Label::text_calculated_preferred_height() const
  80. {
  81. return static_cast<int>(ceilf(font().preferred_line_height()) * (m_text.count("\n"sv) + 1));
  82. }
  83. Optional<UISize> Label::calculated_preferred_size() const
  84. {
  85. return GUI::UISize(text_calculated_preferred_width(), text_calculated_preferred_height());
  86. }
  87. Optional<UISize> Label::calculated_min_size() const
  88. {
  89. int frame = frame_thickness() * 2;
  90. int width = font().width_rounded_up("..."sv) + frame;
  91. int height = font().pixel_size_rounded_up() + frame;
  92. height = max(height, 22);
  93. return UISize(width, height);
  94. }
  95. }