1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 06:14:58 +00:00

Taskbar: Use Process::spawn_or_show_error() for shutdown actions

This commit is contained in:
MacDue 2023-03-13 20:35:34 +00:00 committed by Linus Groh
parent e46b9c189e
commit 43529ea25e
3 changed files with 17 additions and 23 deletions

View file

@ -18,24 +18,23 @@
struct Option {
DeprecatedString title;
Vector<char const*> cmd;
ShutdownDialog::Command command;
bool enabled;
bool default_action;
};
static Vector<Option> const options = {
{ "Power off computer", { "/bin/shutdown", "--now", nullptr }, true, true },
{ "Reboot", { "/bin/reboot", nullptr }, true, false },
{ "Log out", { "/bin/logout", nullptr }, true, false },
static Array const options = {
Option { "Power off computer", { "/bin/shutdown"sv, { "--now" } }, true, true },
Option { "Reboot", { "/bin/reboot"sv, {} }, true, false },
Option { "Log out", { "/bin/logout"sv, {} }, true, false },
};
Vector<char const*> ShutdownDialog::show()
Optional<ShutdownDialog::Command const&> ShutdownDialog::show()
{
auto dialog = ShutdownDialog::construct();
auto rc = dialog->exec();
if (rc == ExecResult::OK && dialog->m_selected_option != -1)
return options[dialog->m_selected_option].cmd;
return options[dialog->m_selected_option].command;
return {};
}

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/Optional.h>
#include <AK/Vector.h>
#include <LibGUI/Dialog.h>
@ -13,7 +14,12 @@ class ShutdownDialog : public GUI::Dialog {
C_OBJECT(ShutdownDialog);
public:
static Vector<char const*> show();
struct Command {
StringView executable;
Vector<char const*, 2> arguments;
};
static Optional<Command const&> show();
private:
ShutdownDialog();

View file

@ -31,7 +31,6 @@
#include <WindowServer/Window.h>
#include <serenity.h>
#include <signal.h>
#include <spawn.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
@ -254,19 +253,9 @@ ErrorOr<NonnullRefPtr<GUI::Menu>> build_system_menu(GUI::Window& window)
GUI::Process::spawn_or_show_error(&window, "/bin/Run"sv, ReadonlySpan<StringView> {}, Core::StandardPaths::home_directory());
}));
system_menu->add_separator();
system_menu->add_action(GUI::Action::create("E&xit...", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"sv)), [](auto&) {
auto command = ShutdownDialog::show();
if (command.size() == 0)
return;
pid_t child_pid;
if ((errno = posix_spawn(&child_pid, command[0], nullptr, nullptr, const_cast<char**>(command.data()), environ))) {
perror("posix_spawn");
} else {
if (disown(child_pid) < 0)
perror("disown");
}
system_menu->add_action(GUI::Action::create("E&xit...", TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"sv)), [&](auto&) {
if (auto command = ShutdownDialog::show(); command.has_value())
GUI::Process::spawn_or_show_error(&window, command->executable, command->arguments);
}));
return system_menu;