InputBox.cpp 4.1 KB

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