PasswordInputDialog.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Button.h>
  7. #include <LibGUI/Label.h>
  8. #include <LibGUI/PasswordInputDialog.h>
  9. #include <LibGUI/PasswordInputDialogGML.h>
  10. #include <LibGUI/TextBox.h>
  11. namespace GUI {
  12. PasswordInputDialog::PasswordInputDialog(Window* parent_window, String title, String server, String username)
  13. : Dialog(parent_window)
  14. {
  15. if (parent_window)
  16. set_icon(parent_window->icon());
  17. set_resizable(false);
  18. resize(340, 122);
  19. set_title(move(title));
  20. auto& widget = set_main_widget<Widget>();
  21. widget.load_from_gml(password_input_dialog_gml);
  22. auto& key_icon_label = *widget.find_descendant_of_type_named<GUI::Label>("key_icon_label");
  23. key_icon_label.set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/32x32/key.png").release_value_but_fixme_should_propagate_errors());
  24. auto& server_label = *widget.find_descendant_of_type_named<GUI::Label>("server_label");
  25. server_label.set_text(move(server));
  26. auto& username_label = *widget.find_descendant_of_type_named<GUI::Label>("username_label");
  27. username_label.set_text(move(username));
  28. auto& password_box = *widget.find_descendant_of_type_named<GUI::PasswordBox>("password_box");
  29. auto& ok_button = *widget.find_descendant_of_type_named<GUI::Button>("ok_button");
  30. ok_button.on_click = [&](auto) {
  31. dbgln("GUI::PasswordInputDialog: OK button clicked");
  32. m_password = password_box.text();
  33. done(ExecOK);
  34. };
  35. auto& cancel_button = *widget.find_descendant_of_type_named<GUI::Button>("cancel_button");
  36. cancel_button.on_click = [this](auto) {
  37. dbgln("GUI::PasswordInputDialog: Cancel button clicked");
  38. done(ExecCancel);
  39. };
  40. password_box.on_return_pressed = [&] {
  41. ok_button.click();
  42. };
  43. password_box.on_escape_pressed = [&] {
  44. cancel_button.click();
  45. };
  46. password_box.set_focus(true);
  47. }
  48. PasswordInputDialog::~PasswordInputDialog()
  49. {
  50. }
  51. int PasswordInputDialog::show(Window* parent_window, String& text_value, String title, String server, String username)
  52. {
  53. auto box = PasswordInputDialog::construct(parent_window, move(title), move(server), move(username));
  54. auto result = box->exec();
  55. text_value = box->m_password;
  56. return result;
  57. }
  58. }