PowerDialog.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <AK/String.h>
  27. #include <AK/Vector.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/Desktop.h>
  31. #include <LibGUI/Label.h>
  32. #include <LibGUI/RadioButton.h>
  33. #include <LibGUI/Widget.h>
  34. #include <LibGfx/Font.h>
  35. #include "PowerDialog.h"
  36. struct PowerOption {
  37. String title;
  38. Vector<char const*> cmd;
  39. bool enabled;
  40. bool default_action;
  41. };
  42. static const Vector<PowerOption> 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*> PowerDialog::show()
  49. {
  50. auto rc = PowerDialog::construct()->exec();
  51. if (rc < 0)
  52. return {};
  53. return options[rc].cmd;
  54. }
  55. PowerDialog::PowerDialog()
  56. : GUI::Dialog(nullptr)
  57. {
  58. Gfx::Rect rect({ 0, 0, 180, 180 + ((static_cast<int>(options.size()) - 3) * 16) });
  59. rect.center_within(GUI::Desktop::the().rect());
  60. set_rect(rect);
  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_preferred_size(0, 16);
  72. header->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  73. header->set_font(Gfx::Font::default_bold_font());
  74. for (size_t i = 0; i < options.size(); i++) {
  75. auto action = options[i];
  76. auto radio = main.add<GUI::RadioButton>();
  77. radio->set_enabled(action.enabled);
  78. radio->set_text(action.title);
  79. radio->on_checked = [this, i](auto) {
  80. m_selected_option = i;
  81. };
  82. if (action.default_action) {
  83. radio->set_checked(true);
  84. m_selected_option = i;
  85. }
  86. }
  87. auto button_box = main.add<GUI::Widget>();
  88. button_box->set_layout<GUI::HorizontalBoxLayout>();
  89. button_box->layout()->set_spacing(8);
  90. auto ok_button = button_box->add<GUI::Button>();
  91. ok_button->on_click = [this] {
  92. done(m_selected_option);
  93. };
  94. ok_button->set_text("OK");
  95. auto cancel_button = button_box->add<GUI::Button>();
  96. cancel_button->on_click = [this] {
  97. done(-1);
  98. };
  99. cancel_button->set_text("Cancel");
  100. }
  101. PowerDialog::~PowerDialog()
  102. {
  103. }