InputBox.cpp 6.9 KB

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