Serenity Operating System
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
27#include <AK/String.h>
28#include <AK/Vector.h>
29#include <LibGUI/BoxLayout.h>
30#include <LibGUI/Button.h>
31#include <LibGUI/Desktop.h>
32#include <LibGUI/Label.h>
33#include <LibGUI/RadioButton.h>
34#include <LibGUI/Widget.h>
35#include <LibGfx/Font.h>
36
37#include "PowerDialog.h"
38
39struct PowerOption {
40 String title;
41 Vector<char const*> cmd;
42 bool enabled;
43 bool default_action;
44};
45
46static const Vector<PowerOption> options = {
47 { "Shut down", { "/bin/shutdown", "--now", nullptr }, true, true },
48 { "Restart", { "/bin/reboot", nullptr }, true, false },
49 { "Log out", {}, false, false },
50 { "Sleep", {}, false, false },
51};
52
53Vector<char const*> PowerDialog::show()
54{
55 auto rc = PowerDialog::construct()->exec();
56 if (rc < 0)
57 return {};
58
59 return options[rc].cmd;
60}
61
62PowerDialog::PowerDialog()
63 : GUI::Dialog(nullptr)
64{
65 Gfx::Rect rect({ 0, 0, 180, 180 + ((static_cast<int>(options.size()) - 3) * 16) });
66 rect.center_within(GUI::Desktop::the().rect());
67 set_rect(rect);
68 set_resizable(false);
69 set_title("SerenityOS");
70 set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"));
71
72 auto main = GUI::Widget::construct();
73 set_main_widget(main);
74 main->set_layout(make<GUI::VerticalBoxLayout>());
75 main->layout()->set_margins({ 8, 8, 8, 8 });
76 main->layout()->set_spacing(8);
77 main->set_fill_with_background_color(true);
78
79 auto header = main->add<GUI::Label>();
80 header->set_text("What would you like to do?");
81 header->set_preferred_size(0, 16);
82 header->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
83 header->set_font(Gfx::Font::default_bold_font());
84
85 int selected = -1;
86 for (size_t i = 0; i < options.size(); i++) {
87 auto action = options[i];
88 auto radio = main->add<GUI::RadioButton>();
89 radio->set_enabled(action.enabled);
90 radio->set_text(action.title);
91
92 radio->on_checked = [&selected, i](auto) {
93 selected = i;
94 };
95
96 if (action.default_action) {
97 radio->set_checked(true);
98 selected = i;
99 }
100 }
101
102 auto button_box = main->add<GUI::Widget>();
103 button_box->set_layout(make<GUI::HorizontalBoxLayout>());
104 button_box->layout()->set_spacing(8);
105
106 auto ok_button = button_box->add<GUI::Button>();
107 ok_button->on_click = [this, &selected](auto&) {
108 done(selected);
109 };
110 ok_button->set_text("OK");
111
112 auto cancel_button = button_box->add<GUI::Button>();
113 cancel_button->on_click = [this](auto&) {
114 done(-1);
115 };
116 cancel_button->set_text("Cancel");
117}
118
119PowerDialog::~PowerDialog()
120{
121}