PasswordInputDialog.cpp 2.3 KB

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