ShutdownDialog.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ShutdownDialog.h"
  27. #include <AK/String.h>
  28. #include <AK/Vector.h>
  29. #include <LibGUI/BoxLayout.h>
  30. #include <LibGUI/Button.h>
  31. #include <LibGUI/Label.h>
  32. #include <LibGUI/RadioButton.h>
  33. #include <LibGUI/Widget.h>
  34. #include <LibGfx/Font.h>
  35. #include <LibGfx/FontDatabase.h>
  36. struct Option {
  37. String title;
  38. Vector<char const*> cmd;
  39. bool enabled;
  40. bool default_action;
  41. };
  42. static const Vector<Option> options = {
  43. { "Shut down", { "/bin/shutdown", "--now", nullptr }, true, true },
  44. { "Restart", { "/bin/reboot", nullptr }, true, false },
  45. { "Log out", {}, false, false },
  46. { "Sleep", {}, false, false },
  47. };
  48. Vector<char const*> ShutdownDialog::show()
  49. {
  50. auto dialog = ShutdownDialog::construct();
  51. auto rc = dialog->exec();
  52. if (rc == ExecResult::ExecOK && dialog->m_selected_option != -1)
  53. return options[dialog->m_selected_option].cmd;
  54. return {};
  55. }
  56. ShutdownDialog::ShutdownDialog()
  57. : Dialog(nullptr)
  58. {
  59. resize(180, 180 + ((static_cast<int>(options.size()) - 3) * 16));
  60. center_on_screen();
  61. set_resizable(false);
  62. set_title("SerenityOS");
  63. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"));
  64. auto& main = set_main_widget<GUI::Widget>();
  65. main.set_layout<GUI::VerticalBoxLayout>();
  66. main.layout()->set_margins({ 8, 8, 8, 8 });
  67. main.layout()->set_spacing(8);
  68. main.set_fill_with_background_color(true);
  69. auto& header = main.add<GUI::Label>();
  70. header.set_text("What would you like to do?");
  71. header.set_fixed_height(16);
  72. header.set_font(Gfx::FontDatabase::default_bold_font());
  73. for (size_t i = 0; i < options.size(); i++) {
  74. auto action = options[i];
  75. auto& radio = main.add<GUI::RadioButton>();
  76. radio.set_enabled(action.enabled);
  77. radio.set_text(action.title);
  78. radio.on_checked = [this, i](auto) {
  79. m_selected_option = i;
  80. };
  81. if (action.default_action) {
  82. radio.set_checked(true);
  83. m_selected_option = i;
  84. }
  85. }
  86. auto& button_box = main.add<GUI::Widget>();
  87. button_box.set_layout<GUI::HorizontalBoxLayout>();
  88. button_box.layout()->set_spacing(8);
  89. auto& ok_button = button_box.add<GUI::Button>();
  90. ok_button.on_click = [this](auto) {
  91. done(ExecResult::ExecOK);
  92. };
  93. ok_button.set_text("OK");
  94. auto& cancel_button = button_box.add<GUI::Button>();
  95. cancel_button.on_click = [this](auto) {
  96. done(ExecResult::ExecCancel);
  97. };
  98. cancel_button.set_text("Cancel");
  99. }
  100. ShutdownDialog::~ShutdownDialog()
  101. {
  102. }