PasswordInputDialog.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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/ImageWidget.h>
  9. #include <LibGUI/Label.h>
  10. #include <LibGUI/PasswordInputDialog.h>
  11. #include <LibGUI/PasswordInputDialogWidget.h>
  12. #include <LibGUI/TextBox.h>
  13. namespace GUI {
  14. PasswordInputDialog::PasswordInputDialog(Window* parent_window, ByteString title, ByteString server, ByteString username)
  15. : Dialog(parent_window)
  16. {
  17. if (parent_window)
  18. set_icon(parent_window->icon());
  19. set_resizable(false);
  20. resize(340, 122);
  21. set_title(move(title));
  22. auto widget = PasswordInputDialogWidget::try_create().release_value_but_fixme_should_propagate_errors();
  23. set_main_widget(widget);
  24. auto& key_icon = *widget->find_descendant_of_type_named<GUI::ImageWidget>("key_icon");
  25. key_icon.set_bitmap(Gfx::Bitmap::load_from_file("/res/icons/32x32/key.png"sv).release_value_but_fixme_should_propagate_errors());
  26. auto& server_label = *widget->find_descendant_of_type_named<GUI::Label>("server_label");
  27. server_label.set_text(String::from_byte_string(server).release_value_but_fixme_should_propagate_errors());
  28. auto& username_label = *widget->find_descendant_of_type_named<GUI::Label>("username_label");
  29. username_label.set_text(String::from_byte_string(username).release_value_but_fixme_should_propagate_errors());
  30. auto& password_box = *widget->find_descendant_of_type_named<GUI::PasswordBox>("password_box");
  31. auto& ok_button = *widget->find_descendant_of_type_named<GUI::Button>("ok_button");
  32. ok_button.on_click = [&](auto) {
  33. dbgln("GUI::PasswordInputDialog: OK button clicked");
  34. m_password = password_box.text();
  35. done(ExecResult::OK);
  36. };
  37. ok_button.set_default(true);
  38. auto& cancel_button = *widget->find_descendant_of_type_named<GUI::Button>("cancel_button");
  39. cancel_button.on_click = [this](auto) {
  40. dbgln("GUI::PasswordInputDialog: Cancel button clicked");
  41. done(ExecResult::Cancel);
  42. };
  43. password_box.on_escape_pressed = [&] {
  44. cancel_button.click();
  45. };
  46. password_box.set_focus(true);
  47. }
  48. Dialog::ExecResult PasswordInputDialog::show(Window* parent_window, ByteString& text_value, ByteString title, ByteString server, ByteString username)
  49. {
  50. auto box = PasswordInputDialog::construct(parent_window, move(title), move(server), move(username));
  51. auto result = box->exec();
  52. text_value = box->m_password;
  53. return result;
  54. }
  55. }