InputBox.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/InputBox.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGUI/TextBox.h>
  13. namespace GUI {
  14. ErrorOr<NonnullRefPtr<InputBox>> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type)
  15. {
  16. 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)));
  17. TRY(box->build());
  18. return box;
  19. }
  20. InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type)
  21. : Dialog(parent_window)
  22. , m_text_value(move(text_value))
  23. , m_prompt(move(prompt))
  24. , m_input_type(input_type)
  25. {
  26. set_title(move(title).to_deprecated_string());
  27. set_resizable(false);
  28. set_auto_shrink(true);
  29. }
  30. Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder)
  31. {
  32. return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder));
  33. }
  34. ErrorOr<Dialog::ExecResult> InputBox::try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder)
  35. {
  36. auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type));
  37. if (parent_window)
  38. box->set_icon(parent_window->icon());
  39. box->set_placeholder(placeholder);
  40. auto result = box->exec();
  41. text_value = box->text_value();
  42. return result;
  43. }
  44. void InputBox::set_placeholder(StringView view)
  45. {
  46. m_text_editor->set_placeholder(view);
  47. }
  48. void InputBox::set_text_value(String value)
  49. {
  50. if (m_text_value == value)
  51. return;
  52. m_text_value = move(value);
  53. m_text_editor->set_text(m_text_value);
  54. }
  55. void InputBox::on_done(ExecResult result)
  56. {
  57. if (result != ExecResult::OK)
  58. return;
  59. if (auto value = String::from_deprecated_string(m_text_editor->text()); !value.is_error())
  60. m_text_value = value.release_value();
  61. switch (m_input_type) {
  62. case InputType::Text:
  63. case InputType::Password:
  64. break;
  65. case InputType::NonemptyText:
  66. VERIFY(!m_text_value.is_empty());
  67. break;
  68. }
  69. }
  70. ErrorOr<void> InputBox::build()
  71. {
  72. auto main_widget = TRY(set_main_widget<Widget>());
  73. TRY(main_widget->try_set_layout<VerticalBoxLayout>(4, 6));
  74. main_widget->set_fill_with_background_color(true);
  75. auto input_container = TRY(main_widget->try_add<Widget>());
  76. input_container->set_layout<HorizontalBoxLayout>();
  77. m_prompt_label = TRY(input_container->try_add<Label>());
  78. m_prompt_label->set_autosize(true);
  79. m_prompt_label->set_text(move(m_prompt).to_deprecated_string());
  80. TRY(input_container->add_spacer());
  81. switch (m_input_type) {
  82. case InputType::Text:
  83. case InputType::NonemptyText:
  84. m_text_editor = TRY(input_container->try_add<TextBox>());
  85. break;
  86. case InputType::Password:
  87. m_text_editor = TRY(input_container->try_add<PasswordBox>());
  88. break;
  89. }
  90. auto button_container = TRY(main_widget->try_add<Widget>());
  91. TRY(button_container->try_set_layout<HorizontalBoxLayout>(0, 6));
  92. TRY(button_container->add_spacer());
  93. m_ok_button = TRY(button_container->try_add<DialogButton>("OK"_short_string));
  94. m_ok_button->on_click = [this](auto) { done(ExecResult::OK); };
  95. m_ok_button->set_default(true);
  96. m_cancel_button = TRY(button_container->try_add<DialogButton>("Cancel"_short_string));
  97. m_cancel_button->on_click = [this](auto) { done(ExecResult::Cancel); };
  98. auto resize_editor = [this, button_container] {
  99. auto width = button_container->effective_min_size().width().as_int();
  100. m_text_editor->set_min_width(width);
  101. };
  102. resize_editor();
  103. on_font_change = [resize_editor] { resize_editor(); };
  104. m_text_editor->set_text(m_text_value);
  105. if (m_input_type == InputType::NonemptyText) {
  106. m_text_editor->on_change = [this] {
  107. m_ok_button->set_enabled(!m_text_editor->text().is_empty());
  108. };
  109. m_text_editor->on_change();
  110. }
  111. auto size = main_widget->effective_min_size();
  112. resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value()));
  113. return {};
  114. }
  115. }