TextBox.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/Painter.h>
  8. #include <LibGUI/TextBox.h>
  9. #include <LibGfx/Palette.h>
  10. REGISTER_WIDGET(GUI, TextBox)
  11. REGISTER_WIDGET(GUI, PasswordBox)
  12. REGISTER_WIDGET(GUI, UrlBox)
  13. namespace GUI {
  14. TextBox::TextBox()
  15. : TextEditor(TextEditor::SingleLine)
  16. {
  17. set_min_size({ 40, 22 });
  18. set_preferred_size({ SpecialDimension::OpportunisticGrow, 22 });
  19. }
  20. void TextBox::keydown_event(GUI::KeyEvent& event)
  21. {
  22. TextEditor::keydown_event(event);
  23. if (event.key() == Key_Up) {
  24. if (on_up_pressed)
  25. on_up_pressed();
  26. if (has_no_history() || !can_go_backwards_in_history())
  27. return;
  28. if (m_history_index >= static_cast<int>(m_history.size()))
  29. m_saved_input = text();
  30. m_history_index--;
  31. set_text(m_history[m_history_index]);
  32. } else if (event.key() == Key_Down) {
  33. if (on_down_pressed)
  34. on_down_pressed();
  35. if (has_no_history())
  36. return;
  37. if (can_go_forwards_in_history()) {
  38. m_history_index++;
  39. set_text(m_history[m_history_index]);
  40. } else if (m_history_index < static_cast<int>(m_history.size())) {
  41. m_history_index++;
  42. set_text(m_saved_input);
  43. }
  44. }
  45. }
  46. void TextBox::add_current_text_to_history()
  47. {
  48. if (!m_history_enabled)
  49. return;
  50. auto input = text();
  51. if (m_history.is_empty() || m_history.last() != input)
  52. add_input_to_history(input);
  53. m_history_index = static_cast<int>(m_history.size());
  54. m_saved_input = {};
  55. }
  56. void TextBox::add_input_to_history(String input)
  57. {
  58. m_history.append(move(input));
  59. m_history_index++;
  60. }
  61. constexpr u32 password_box_substitution_code_point = '*';
  62. PasswordBox::PasswordBox()
  63. : TextBox()
  64. {
  65. set_substitution_code_point(password_box_substitution_code_point);
  66. set_text_is_secret(true);
  67. REGISTER_BOOL_PROPERTY("show_reveal_button", is_showing_reveal_button, set_show_reveal_button);
  68. }
  69. Gfx::IntRect PasswordBox::reveal_password_button_rect() const
  70. {
  71. constexpr i32 button_box_margin = 3;
  72. auto button_box_size = height() - button_box_margin - button_box_margin;
  73. return { width() - button_box_size - button_box_margin, button_box_margin, button_box_size, button_box_size };
  74. }
  75. void PasswordBox::paint_event(PaintEvent& event)
  76. {
  77. TextBox::paint_event(event);
  78. if (is_showing_reveal_button()) {
  79. auto button_rect = reveal_password_button_rect();
  80. Painter painter(*this);
  81. painter.add_clip_rect(event.rect());
  82. auto icon_color = palette().button_text();
  83. if (substitution_code_point().has_value())
  84. icon_color = palette().disabled_text_front();
  85. i32 dot_indicator_padding = height() / 5;
  86. Gfx::IntRect dot_indicator_rect = { button_rect.x() + dot_indicator_padding, button_rect.y() + dot_indicator_padding, button_rect.width() - dot_indicator_padding * 2, button_rect.height() - dot_indicator_padding * 2 };
  87. painter.fill_ellipse(dot_indicator_rect, icon_color);
  88. Gfx::IntPoint arc_start_point { dot_indicator_rect.x() - dot_indicator_padding / 2, dot_indicator_rect.y() + dot_indicator_padding / 2 };
  89. Gfx::IntPoint arc_end_point = { dot_indicator_rect.top_right().x() + dot_indicator_padding / 2, dot_indicator_rect.top_right().y() + dot_indicator_padding / 2 };
  90. Gfx::IntPoint arc_center_point = { dot_indicator_rect.center().x(), dot_indicator_rect.top() - dot_indicator_padding };
  91. painter.draw_quadratic_bezier_curve(arc_center_point, arc_start_point, arc_end_point, icon_color, 1);
  92. }
  93. }
  94. void PasswordBox::mousedown_event(GUI::MouseEvent& event)
  95. {
  96. if (is_showing_reveal_button() && reveal_password_button_rect().contains(event.position())) {
  97. Optional<u32> next_substitution_code_point;
  98. if (!substitution_code_point().has_value())
  99. next_substitution_code_point = password_box_substitution_code_point;
  100. set_substitution_code_point(next_substitution_code_point);
  101. } else {
  102. TextBox::mousedown_event(event);
  103. }
  104. }
  105. UrlBox::UrlBox()
  106. : TextBox()
  107. {
  108. set_auto_focusable(false);
  109. }
  110. void UrlBox::focusout_event(GUI::FocusEvent& event)
  111. {
  112. set_focus_transition(true);
  113. TextBox::focusout_event(event);
  114. }
  115. void UrlBox::mousedown_event(GUI::MouseEvent& event)
  116. {
  117. if (is_displayonly())
  118. return;
  119. if (event.button() != MouseButton::Primary)
  120. return;
  121. if (is_focus_transition()) {
  122. TextBox::select_current_line();
  123. set_focus_transition(false);
  124. } else {
  125. TextBox::mousedown_event(event);
  126. }
  127. }
  128. }