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 = set_main_widget<GUI::Widget>();
73 main.set_layout<GUI::VerticalBoxLayout>();
74 main.layout()->set_margins({ 8, 8, 8, 8 });
75 main.layout()->set_spacing(8);
76 main.set_fill_with_background_color(true);
77
78 auto& header = main.add<GUI::Label>();
79 header.set_text("What would you like to do?");
80 header.set_preferred_size(0, 16);
81 header.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
82 header.set_font(Gfx::Font::default_bold_font());
83
84 for (size_t i = 0; i < options.size(); i++) {
85 auto action = options[i];
86 auto& radio = main.add<GUI::RadioButton>();
87 radio.set_enabled(action.enabled);
88 radio.set_text(action.title);
89
90 radio.on_checked = [this, i](auto) {
91 m_selected_option = i;
92 };
93
94 if (action.default_action) {
95 radio.set_checked(true);
96 m_selected_option = i;
97 }
98 }
99
100 auto& button_box = main.add<GUI::Widget>();
101 button_box.set_layout<GUI::HorizontalBoxLayout>();
102 button_box.layout()->set_spacing(8);
103
104 auto& ok_button = button_box.add<GUI::Button>();
105 ok_button.on_click = [this] {
106 done(m_selected_option);
107 };
108 ok_button.set_text("OK");
109
110 auto& cancel_button = button_box.add<GUI::Button>();
111 cancel_button.on_click = [this] {
112 done(-1);
113 };
114 cancel_button.set_text("Cancel");
115}
116
117PowerDialog::~PowerDialog()
118{
119}