TextBox.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/URL.h>
  9. #include <AK/Vector.h>
  10. #include <LibGUI/Painter.h>
  11. #include <LibGUI/TextBox.h>
  12. #include <LibGfx/Palette.h>
  13. #include <LibGfx/TextAttributes.h>
  14. REGISTER_WIDGET(GUI, TextBox)
  15. REGISTER_WIDGET(GUI, PasswordBox)
  16. REGISTER_WIDGET(GUI, UrlBox)
  17. namespace GUI {
  18. TextBox::TextBox()
  19. : TextEditor(TextEditor::SingleLine)
  20. {
  21. set_min_size({ SpecialDimension::Shrink });
  22. set_preferred_size({ SpecialDimension::OpportunisticGrow, SpecialDimension::Shrink });
  23. }
  24. void TextBox::keydown_event(GUI::KeyEvent& event)
  25. {
  26. TextEditor::keydown_event(event);
  27. if (event.key() == Key_Up) {
  28. if (on_up_pressed)
  29. on_up_pressed();
  30. if (has_no_history() || !can_go_backwards_in_history())
  31. return;
  32. if (m_history_index >= static_cast<int>(m_history.size()))
  33. m_saved_input = text();
  34. m_history_index--;
  35. set_text(m_history[m_history_index]);
  36. } else if (event.key() == Key_Down) {
  37. if (on_down_pressed)
  38. on_down_pressed();
  39. if (has_no_history())
  40. return;
  41. if (can_go_forwards_in_history()) {
  42. m_history_index++;
  43. set_text(m_history[m_history_index]);
  44. } else if (m_history_index < static_cast<int>(m_history.size())) {
  45. m_history_index++;
  46. set_text(m_saved_input);
  47. }
  48. }
  49. }
  50. void TextBox::add_current_text_to_history()
  51. {
  52. if (!m_history_enabled)
  53. return;
  54. auto input = text();
  55. if (m_history.is_empty() || m_history.last() != input)
  56. add_input_to_history(input);
  57. m_history_index = static_cast<int>(m_history.size());
  58. m_saved_input = {};
  59. }
  60. void TextBox::add_input_to_history(DeprecatedString input)
  61. {
  62. m_history.append(move(input));
  63. m_history_index++;
  64. }
  65. constexpr u32 password_box_substitution_code_point = '*';
  66. PasswordBox::PasswordBox()
  67. : TextBox()
  68. {
  69. set_substitution_code_point(password_box_substitution_code_point);
  70. set_text_is_secret(true);
  71. REGISTER_BOOL_PROPERTY("show_reveal_button", is_showing_reveal_button, set_show_reveal_button);
  72. }
  73. Gfx::IntRect PasswordBox::reveal_password_button_rect() const
  74. {
  75. constexpr i32 button_box_margin = 3;
  76. auto button_box_size = height() - button_box_margin - button_box_margin;
  77. return { width() - button_box_size - button_box_margin, button_box_margin, button_box_size, button_box_size };
  78. }
  79. void PasswordBox::paint_event(PaintEvent& event)
  80. {
  81. TextBox::paint_event(event);
  82. if (is_showing_reveal_button()) {
  83. auto button_rect = reveal_password_button_rect();
  84. Painter painter(*this);
  85. painter.add_clip_rect(event.rect());
  86. auto icon_color = palette().button_text();
  87. if (substitution_code_point().has_value())
  88. icon_color = palette().disabled_text_front();
  89. i32 dot_indicator_padding = height() / 5;
  90. 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 };
  91. painter.fill_ellipse(dot_indicator_rect, icon_color);
  92. Gfx::IntPoint arc_start_point { dot_indicator_rect.x() - dot_indicator_padding / 2, dot_indicator_rect.y() + dot_indicator_padding / 2 };
  93. Gfx::IntPoint arc_end_point = { dot_indicator_rect.right() - 1 + dot_indicator_padding / 2, dot_indicator_rect.top() + dot_indicator_padding / 2 };
  94. Gfx::IntPoint arc_center_point = { dot_indicator_rect.center().x(), dot_indicator_rect.top() - dot_indicator_padding };
  95. painter.draw_quadratic_bezier_curve(arc_center_point, arc_start_point, arc_end_point, icon_color, 1);
  96. }
  97. }
  98. void PasswordBox::mousedown_event(GUI::MouseEvent& event)
  99. {
  100. if (is_showing_reveal_button() && reveal_password_button_rect().contains(event.position())) {
  101. Optional<u32> next_substitution_code_point;
  102. if (!substitution_code_point().has_value())
  103. next_substitution_code_point = password_box_substitution_code_point;
  104. set_substitution_code_point(next_substitution_code_point);
  105. } else {
  106. TextBox::mousedown_event(event);
  107. }
  108. }
  109. UrlBox::UrlBox()
  110. : TextBox()
  111. {
  112. set_auto_focusable(false);
  113. on_change = [this] {
  114. highlight_url();
  115. };
  116. }
  117. void UrlBox::focusout_event(GUI::FocusEvent& event)
  118. {
  119. set_focus_transition(true);
  120. highlight_url();
  121. TextBox::focusout_event(event);
  122. }
  123. void UrlBox::focusin_event(GUI::FocusEvent& event)
  124. {
  125. highlight_url();
  126. TextBox::focusin_event(event);
  127. }
  128. void UrlBox::mousedown_event(GUI::MouseEvent& event)
  129. {
  130. if (is_displayonly())
  131. return;
  132. if (event.button() != MouseButton::Primary)
  133. return;
  134. if (is_focus_transition()) {
  135. TextBox::select_current_line();
  136. set_focus_transition(false);
  137. } else {
  138. TextBox::mousedown_event(event);
  139. }
  140. }
  141. void UrlBox::highlight_url()
  142. {
  143. auto url = AK::URL::create_with_url_or_path(text());
  144. Vector<GUI::TextDocumentSpan> spans;
  145. if (url.is_valid() && !is_focused()) {
  146. if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "gemini") {
  147. auto host_start = url.scheme().length() + 3;
  148. auto host_length = url.host().length();
  149. // FIXME: Maybe add a generator to use https://publicsuffix.org/list/public_suffix_list.dat
  150. // for now just highlight the whole host
  151. Gfx::TextAttributes default_format;
  152. default_format.color = palette().color(Gfx::ColorRole::PlaceholderText);
  153. spans.append({
  154. { { 0, 0 }, { 0, host_start } },
  155. default_format,
  156. });
  157. Gfx::TextAttributes host_format;
  158. host_format.color = palette().color(Gfx::ColorRole::BaseText);
  159. spans.append({
  160. { { 0, host_start }, { 0, host_start + host_length } },
  161. host_format,
  162. });
  163. spans.append({
  164. { { 0, host_start + host_length }, { 0, text().length() } },
  165. default_format,
  166. });
  167. } else if (url.scheme() == "file") {
  168. Gfx::TextAttributes scheme_format;
  169. scheme_format.color = palette().color(Gfx::ColorRole::PlaceholderText);
  170. spans.append({
  171. { { 0, 0 }, { 0, url.scheme().length() + 3 } },
  172. scheme_format,
  173. });
  174. }
  175. }
  176. document().set_spans(0, move(spans));
  177. update();
  178. }
  179. }