InputBox.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, networkException <networkexception@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/Button.h>
  10. #include <LibGUI/ImageWidget.h>
  11. #include <LibGUI/InputBox.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/SpinBox.h>
  14. #include <LibGUI/TextBox.h>
  15. namespace GUI {
  16. ErrorOr<NonnullRefPtr<InputBox>> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
  17. {
  18. VERIFY(input_type != InputType::Numeric);
  19. auto title_string = TRY(String::from_utf8(title));
  20. auto prompt_string = TRY(String::from_utf8(prompt));
  21. auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, text_value, title_string, prompt_string, input_type, move(icon))));
  22. TRY(box->build());
  23. return box;
  24. }
  25. ErrorOr<NonnullRefPtr<InputBox>> InputBox::create_numeric(Window* parent_window, int value, StringView title, StringView prompt, RefPtr<Gfx::Bitmap const> icon)
  26. {
  27. auto title_string = TRY(String::from_utf8(title));
  28. auto prompt_string = TRY(String::from_utf8(prompt));
  29. auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, value, title_string, prompt_string, move(icon))));
  30. TRY(box->build());
  31. return box;
  32. }
  33. InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
  34. : Dialog(parent_window)
  35. , m_text_value(move(text_value))
  36. , m_prompt(move(prompt))
  37. , m_input_type(input_type)
  38. , m_icon(move(icon))
  39. {
  40. set_title(move(title).to_deprecated_string());
  41. set_resizable(false);
  42. set_auto_shrink(true);
  43. }
  44. InputBox::InputBox(Window* parent_window, int value, String title, String prompt, RefPtr<Gfx::Bitmap const> icon)
  45. : Dialog(parent_window)
  46. , m_numeric_value(value)
  47. , m_prompt(move(prompt))
  48. , m_input_type(InputType::Numeric)
  49. , m_icon(move(icon))
  50. {
  51. set_title(move(title).to_deprecated_string());
  52. set_resizable(false);
  53. set_auto_shrink(true);
  54. }
  55. Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
  56. {
  57. return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder, move(icon)));
  58. }
  59. ErrorOr<Dialog::ExecResult> InputBox::try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
  60. {
  61. VERIFY(input_type != InputType::Numeric);
  62. auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type, move(icon)));
  63. if (parent_window)
  64. box->set_icon(parent_window->icon());
  65. box->set_placeholder(placeholder);
  66. auto result = box->exec();
  67. text_value = box->text_value();
  68. return result;
  69. }
  70. ErrorOr<Dialog::ExecResult> InputBox::show_numeric(Window* parent_window, int& value, int min, int max, StringView title, StringView prompt, RefPtr<Gfx::Bitmap const> icon)
  71. {
  72. auto box = TRY(InputBox::create_numeric(parent_window, value, title, prompt, move(icon)));
  73. if (parent_window)
  74. box->set_icon(parent_window->icon());
  75. box->set_range(min, max);
  76. auto result = box->exec();
  77. value = box->numeric_value();
  78. return result;
  79. }
  80. void InputBox::set_placeholder(StringView view)
  81. {
  82. m_text_editor->set_placeholder(view);
  83. }
  84. void InputBox::set_range(int min, int max)
  85. {
  86. m_spinbox->set_range(min, max);
  87. }
  88. void InputBox::set_text_value(String value)
  89. {
  90. if (m_text_value == value)
  91. return;
  92. m_text_value = move(value);
  93. m_text_editor->set_text(m_text_value);
  94. }
  95. void InputBox::set_numeric_value(int value)
  96. {
  97. if (m_numeric_value == value)
  98. return;
  99. m_numeric_value = value;
  100. m_spinbox->set_value(value);
  101. }
  102. void InputBox::on_done(ExecResult result)
  103. {
  104. if (result != ExecResult::OK)
  105. return;
  106. if (m_text_editor) {
  107. auto value = String::from_deprecated_string(m_text_editor->text());
  108. if (!value.is_error())
  109. m_text_value = value.release_value();
  110. } else if (m_spinbox)
  111. m_numeric_value = m_spinbox->value();
  112. if (m_input_type == InputType::NonemptyText)
  113. VERIFY(!m_text_value.is_empty());
  114. }
  115. ErrorOr<void> InputBox::build()
  116. {
  117. auto main_widget = set_main_widget<Widget>();
  118. main_widget->set_layout<VerticalBoxLayout>(6, 6);
  119. main_widget->set_fill_with_background_color(true);
  120. if (!m_prompt.is_empty()) {
  121. auto& prompt_container = main_widget->add<Widget>();
  122. prompt_container.set_layout<HorizontalBoxLayout>(0, 8);
  123. if (m_icon) {
  124. auto& image_widget = prompt_container.add<ImageWidget>();
  125. image_widget.set_bitmap(m_icon);
  126. }
  127. m_prompt_label = prompt_container.add<Label>();
  128. m_prompt_label->set_autosize(true);
  129. m_prompt_label->set_text_wrapping(Gfx::TextWrapping::DontWrap);
  130. m_prompt_label->set_text(m_prompt);
  131. }
  132. switch (m_input_type) {
  133. case InputType::Text:
  134. case InputType::NonemptyText:
  135. m_text_editor = main_widget->add<TextBox>();
  136. break;
  137. case InputType::Password:
  138. m_text_editor = main_widget->add<PasswordBox>();
  139. break;
  140. case InputType::Numeric:
  141. m_spinbox = main_widget->add<SpinBox>();
  142. break;
  143. }
  144. auto& button_container = main_widget->add<Widget>();
  145. button_container.set_layout<HorizontalBoxLayout>(0, 6);
  146. button_container.add_spacer();
  147. m_ok_button = button_container.add<DialogButton>("OK"_string);
  148. m_ok_button->on_click = [this](auto) {
  149. if (m_spinbox)
  150. m_spinbox->set_value_from_current_text();
  151. done(ExecResult::OK);
  152. };
  153. m_ok_button->set_default(true);
  154. m_cancel_button = button_container.add<DialogButton>("Cancel"_string);
  155. m_cancel_button->on_click = [this](auto) { done(ExecResult::Cancel); };
  156. auto guarantee_width = [this, &button_container] {
  157. if (m_prompt.is_empty())
  158. return;
  159. auto width = button_container.calculated_min_size().value().width().as_int();
  160. auto constexpr golden_ratio = 1.618;
  161. button_container.set_min_width(width * golden_ratio);
  162. };
  163. guarantee_width();
  164. on_font_change = [guarantee_width] { guarantee_width(); };
  165. if (m_text_editor) {
  166. m_text_editor->set_text(m_text_value);
  167. if (m_input_type == InputType::NonemptyText) {
  168. m_text_editor->on_change = [this] {
  169. m_ok_button->set_enabled(!m_text_editor->text().is_empty());
  170. };
  171. m_text_editor->on_change();
  172. }
  173. }
  174. if (m_spinbox)
  175. m_spinbox->set_value(m_numeric_value);
  176. auto size = main_widget->effective_min_size();
  177. resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value()));
  178. return {};
  179. }
  180. }