InputBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. m_text_editor = label_editor_container.add<TextBox>();
  62. break;
  63. case InputType::Password:
  64. m_text_editor = label_editor_container.add<PasswordBox>();
  65. break;
  66. }
  67. m_text_editor->set_text(m_text_value);
  68. if (!m_placeholder.is_null())
  69. m_text_editor->set_placeholder(m_placeholder);
  70. auto& button_container_outer = widget->add<Widget>();
  71. button_container_outer.set_preferred_height(SpecialDimension::Fit);
  72. button_container_outer.set_layout<VerticalBoxLayout>();
  73. auto& button_container_inner = button_container_outer.add<Widget>();
  74. button_container_inner.set_layout<HorizontalBoxLayout>();
  75. button_container_inner.set_preferred_height(SpecialDimension::Fit);
  76. button_container_inner.layout()->set_spacing(6);
  77. button_container_inner.layout()->add_spacer();
  78. m_ok_button = button_container_inner.add<DialogButton>();
  79. m_ok_button->set_text("OK");
  80. m_ok_button->on_click = [this](auto) {
  81. dbgln("GUI::InputBox: OK button clicked");
  82. done(ExecResult::OK);
  83. };
  84. m_ok_button->set_default(true);
  85. m_cancel_button = button_container_inner.add<DialogButton>();
  86. m_cancel_button->set_text("Cancel");
  87. m_cancel_button->on_click = [this](auto) {
  88. dbgln("GUI::InputBox: Cancel button clicked");
  89. done(ExecResult::Cancel);
  90. };
  91. m_text_editor->on_escape_pressed = [this] {
  92. m_cancel_button->click();
  93. };
  94. m_text_editor->set_focus(true);
  95. set_rect(x(), y(), max_width + 140, widget->effective_preferred_size().height().as_int());
  96. }
  97. }