InputBox.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include <LibGfx/Font/Font.h>
  14. namespace GUI {
  15. InputBox::InputBox(Window* parent_window, DeprecatedString text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder)
  16. : Dialog(parent_window)
  17. , m_text_value(move(text_value))
  18. , m_prompt(prompt)
  19. , m_input_type(input_type)
  20. , m_placeholder(placeholder)
  21. {
  22. set_title(title);
  23. build();
  24. }
  25. Dialog::ExecResult InputBox::show(Window* parent_window, DeprecatedString& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder)
  26. {
  27. auto box = InputBox::construct(parent_window, text_value, prompt, title, input_type, placeholder);
  28. box->set_resizable(false);
  29. if (parent_window)
  30. box->set_icon(parent_window->icon());
  31. auto result = box->exec();
  32. text_value = box->text_value();
  33. return result;
  34. }
  35. void InputBox::set_text_value(DeprecatedString text_value)
  36. {
  37. m_text_editor->set_text(move(text_value));
  38. }
  39. void InputBox::on_done(ExecResult result)
  40. {
  41. if (result == ExecResult::OK) {
  42. m_text_value = m_text_editor->text();
  43. switch (m_input_type) {
  44. case InputType::Text:
  45. case InputType::Password:
  46. break;
  47. case InputType::NonemptyText:
  48. VERIFY(!m_text_value.is_empty());
  49. break;
  50. }
  51. }
  52. }
  53. void InputBox::build()
  54. {
  55. auto widget = set_main_widget<Widget>().release_value_but_fixme_should_propagate_errors();
  56. int text_width = widget->font().width(m_prompt);
  57. int title_width = widget->font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
  58. int max_width = max(text_width, title_width);
  59. widget->set_layout<VerticalBoxLayout>(6, 6);
  60. widget->set_fill_with_background_color(true);
  61. widget->set_preferred_height(SpecialDimension::Fit);
  62. auto& label_editor_container = widget->add<Widget>();
  63. label_editor_container.set_layout<HorizontalBoxLayout>();
  64. label_editor_container.set_preferred_height(SpecialDimension::Fit);
  65. auto& label = label_editor_container.add<Label>(m_prompt);
  66. label.set_preferred_width(text_width);
  67. switch (m_input_type) {
  68. case InputType::Text:
  69. case InputType::NonemptyText:
  70. m_text_editor = label_editor_container.add<TextBox>();
  71. break;
  72. case InputType::Password:
  73. m_text_editor = label_editor_container.add<PasswordBox>();
  74. break;
  75. }
  76. m_text_editor->set_text(m_text_value);
  77. if (!m_placeholder.is_null())
  78. m_text_editor->set_placeholder(m_placeholder);
  79. auto& button_container_outer = widget->add<Widget>();
  80. button_container_outer.set_preferred_height(SpecialDimension::Fit);
  81. button_container_outer.set_layout<VerticalBoxLayout>();
  82. auto& button_container_inner = button_container_outer.add<Widget>();
  83. button_container_inner.set_layout<HorizontalBoxLayout>(GUI::Margins {}, 6);
  84. button_container_inner.set_preferred_height(SpecialDimension::Fit);
  85. button_container_inner.add_spacer().release_value_but_fixme_should_propagate_errors();
  86. m_ok_button = button_container_inner.add<DialogButton>();
  87. m_ok_button->set_text(String::from_utf8_short_string("OK"sv));
  88. m_ok_button->on_click = [this](auto) {
  89. dbgln("GUI::InputBox: OK button clicked");
  90. done(ExecResult::OK);
  91. };
  92. m_ok_button->set_default(true);
  93. m_cancel_button = button_container_inner.add<DialogButton>();
  94. m_cancel_button->set_text(String::from_utf8_short_string("Cancel"sv));
  95. m_cancel_button->on_click = [this](auto) {
  96. dbgln("GUI::InputBox: Cancel button clicked");
  97. done(ExecResult::Cancel);
  98. };
  99. m_text_editor->on_escape_pressed = [this] {
  100. m_cancel_button->click();
  101. };
  102. m_text_editor->set_focus(true);
  103. if (m_input_type == InputType::NonemptyText) {
  104. m_text_editor->on_change = [this] {
  105. m_ok_button->set_enabled(!m_text_editor->text().is_empty());
  106. };
  107. m_text_editor->on_change();
  108. }
  109. set_rect(x(), y(), max_width + 140, widget->effective_preferred_size().height().as_int());
  110. }
  111. }