InputBox.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, String& text_value, StringView prompt, StringView title, StringView placeholder, InputType input_type)
  16. : Dialog(parent_window)
  17. , m_text_value(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, String& 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::build(InputType input_type)
  35. {
  36. auto& widget = set_main_widget<Widget>();
  37. int text_width = widget.font().width(m_prompt);
  38. int title_width = widget.font().width(title()) + 24 /* icon, plus a little padding -- not perfect */;
  39. int max_width = max(text_width, title_width);
  40. widget.set_layout<VerticalBoxLayout>();
  41. widget.set_fill_with_background_color(true);
  42. widget.set_preferred_height(SpecialDimension::Fit);
  43. widget.layout()->set_margins(6);
  44. widget.layout()->set_spacing(6);
  45. auto& label_editor_container = widget.add<Widget>();
  46. label_editor_container.set_layout<HorizontalBoxLayout>();
  47. label_editor_container.set_preferred_height(SpecialDimension::Fit);
  48. auto& label = label_editor_container.add<Label>(m_prompt);
  49. label.set_preferred_width(text_width);
  50. switch (input_type) {
  51. case InputType::Text:
  52. m_text_editor = label_editor_container.add<TextBox>();
  53. break;
  54. case InputType::Password:
  55. m_text_editor = label_editor_container.add<PasswordBox>();
  56. break;
  57. }
  58. m_text_editor->set_text(m_text_value);
  59. if (!m_placeholder.is_null())
  60. m_text_editor->set_placeholder(m_placeholder);
  61. auto& button_container_outer = widget.add<Widget>();
  62. button_container_outer.set_preferred_height(SpecialDimension::Fit);
  63. button_container_outer.set_layout<VerticalBoxLayout>();
  64. auto& button_container_inner = button_container_outer.add<Widget>();
  65. button_container_inner.set_layout<HorizontalBoxLayout>();
  66. button_container_inner.set_preferred_height(SpecialDimension::Fit);
  67. button_container_inner.layout()->set_spacing(6);
  68. button_container_inner.layout()->add_spacer();
  69. m_ok_button = button_container_inner.add<DialogButton>();
  70. m_ok_button->set_text("OK");
  71. m_ok_button->on_click = [this](auto) {
  72. dbgln("GUI::InputBox: OK button clicked");
  73. m_text_value = m_text_editor->text();
  74. done(ExecResult::OK);
  75. };
  76. m_ok_button->set_default(true);
  77. m_cancel_button = button_container_inner.add<DialogButton>();
  78. m_cancel_button->set_text("Cancel");
  79. m_cancel_button->on_click = [this](auto) {
  80. dbgln("GUI::InputBox: Cancel button clicked");
  81. done(ExecResult::Cancel);
  82. };
  83. m_text_editor->on_escape_pressed = [this] {
  84. m_cancel_button->click();
  85. };
  86. m_text_editor->set_focus(true);
  87. set_rect(x(), y(), max_width + 140, widget.effective_preferred_size().height().as_int());
  88. }
  89. }