InputBox.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
  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. set_rect(x(), y(), max_width + 140, 66);
  41. widget.set_layout<VerticalBoxLayout>();
  42. widget.set_fill_with_background_color(true);
  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. auto& label = label_editor_container.add<Label>(m_prompt);
  48. label.set_fixed_size(text_width, 16);
  49. switch (input_type) {
  50. case InputType::Text:
  51. m_text_editor = label_editor_container.add<TextBox>();
  52. break;
  53. case InputType::Password:
  54. m_text_editor = label_editor_container.add<PasswordBox>();
  55. break;
  56. }
  57. m_text_editor->set_text(m_text_value);
  58. if (!m_placeholder.is_null())
  59. m_text_editor->set_placeholder(m_placeholder);
  60. auto& button_container_outer = widget.add<Widget>();
  61. button_container_outer.set_fixed_height(22);
  62. button_container_outer.set_layout<VerticalBoxLayout>();
  63. auto& button_container_inner = button_container_outer.add<Widget>();
  64. button_container_inner.set_layout<HorizontalBoxLayout>();
  65. button_container_inner.layout()->set_spacing(6);
  66. button_container_inner.layout()->set_margins({ 4, 0, 4, 4 });
  67. button_container_inner.layout()->add_spacer();
  68. m_ok_button = button_container_inner.add<Button>();
  69. m_ok_button->set_text("OK");
  70. m_ok_button->on_click = [this](auto) {
  71. dbgln("GUI::InputBox: OK button clicked");
  72. m_text_value = m_text_editor->text();
  73. done(ExecResult::OK);
  74. };
  75. m_ok_button->set_default(true);
  76. m_cancel_button = button_container_inner.add<Button>();
  77. m_cancel_button->set_text("Cancel");
  78. m_cancel_button->on_click = [this](auto) {
  79. dbgln("GUI::InputBox: Cancel button clicked");
  80. done(ExecResult::Cancel);
  81. };
  82. m_text_editor->on_escape_pressed = [this] {
  83. m_cancel_button->click();
  84. };
  85. m_text_editor->set_focus(true);
  86. }
  87. }