ShutdownDialog.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ShutdownDialog.h"
  7. #include <AK/String.h>
  8. #include <AK/Vector.h>
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGUI/Button.h>
  11. #include <LibGUI/ImageWidget.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/RadioButton.h>
  14. #include <LibGUI/Widget.h>
  15. #include <LibGfx/Font/Font.h>
  16. #include <LibGfx/Font/FontDatabase.h>
  17. struct Option {
  18. String title;
  19. Vector<char const*> cmd;
  20. bool enabled;
  21. bool default_action;
  22. };
  23. static Vector<Option> const options = {
  24. { "Power off computer", { "/bin/shutdown", "--now", nullptr }, true, true },
  25. { "Reboot", { "/bin/reboot", nullptr }, true, false },
  26. { "Log out", { "/bin/logout", nullptr }, true, false },
  27. };
  28. Vector<char const*> ShutdownDialog::show()
  29. {
  30. auto dialog = ShutdownDialog::construct();
  31. auto rc = dialog->exec();
  32. if (rc == ExecResult::OK && dialog->m_selected_option != -1)
  33. return options[dialog->m_selected_option].cmd;
  34. return {};
  35. }
  36. ShutdownDialog::ShutdownDialog()
  37. : Dialog(nullptr)
  38. {
  39. auto& widget = set_main_widget<GUI::Widget>();
  40. widget.set_fill_with_background_color(true);
  41. widget.set_layout<GUI::VerticalBoxLayout>();
  42. widget.layout()->set_spacing(0);
  43. auto& banner_image = widget.add<GUI::ImageWidget>();
  44. banner_image.load_from_file("/res/graphics/brand-banner.png");
  45. auto& content_container = widget.add<GUI::Widget>();
  46. content_container.set_layout<GUI::HorizontalBoxLayout>();
  47. auto& left_container = content_container.add<GUI::Widget>();
  48. left_container.set_fixed_width(60);
  49. left_container.set_layout<GUI::VerticalBoxLayout>();
  50. left_container.layout()->set_margins({ 12, 0, 0 });
  51. auto& icon_wrapper = left_container.add<GUI::Widget>();
  52. icon_wrapper.set_fixed_size(32, 48);
  53. icon_wrapper.set_layout<GUI::VerticalBoxLayout>();
  54. auto& icon_image = icon_wrapper.add<GUI::ImageWidget>();
  55. icon_image.set_bitmap(Gfx::Bitmap::try_load_from_file("/res/icons/32x32/shutdown.png").release_value_but_fixme_should_propagate_errors());
  56. auto& right_container = content_container.add<GUI::Widget>();
  57. right_container.set_layout<GUI::VerticalBoxLayout>();
  58. right_container.layout()->set_margins({ 12, 12, 8, 0 });
  59. auto& label = right_container.add<GUI::Label>("What would you like to do?");
  60. label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  61. label.set_fixed_height(22);
  62. label.set_font(Gfx::FontDatabase::default_font().bold_variant());
  63. for (size_t i = 0; i < options.size(); i++) {
  64. auto action = options[i];
  65. auto& radio = right_container.add<GUI::RadioButton>();
  66. radio.set_enabled(action.enabled);
  67. radio.set_text(action.title);
  68. radio.on_checked = [this, i](auto) {
  69. m_selected_option = i;
  70. };
  71. if (action.default_action) {
  72. radio.set_checked(true);
  73. m_selected_option = i;
  74. }
  75. }
  76. right_container.layout()->add_spacer();
  77. auto& button_container = right_container.add<GUI::Widget>();
  78. button_container.set_fixed_height(23);
  79. button_container.set_layout<GUI::HorizontalBoxLayout>();
  80. button_container.layout()->set_spacing(5);
  81. button_container.layout()->add_spacer();
  82. auto& ok_button = button_container.add<GUI::Button>("OK");
  83. ok_button.set_fixed_size(80, 23);
  84. ok_button.on_click = [this](auto) {
  85. done(ExecResult::OK);
  86. };
  87. auto& cancel_button = button_container.add<GUI::Button>("Cancel");
  88. cancel_button.set_fixed_size(80, 23);
  89. cancel_button.on_click = [this](auto) {
  90. done(ExecResult::Cancel);
  91. };
  92. resize(413, 235);
  93. center_on_screen();
  94. set_resizable(false);
  95. set_title("Exit SerenityOS");
  96. set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/power.png").release_value_but_fixme_should_propagate_errors());
  97. // Request WindowServer to re-update us on the current theme as we might've not been alive for the last notification.
  98. refresh_system_theme();
  99. }