1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:27:45 +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 { struct Option {
DeprecatedString title; DeprecatedString title;
Vector<char const*> cmd; ShutdownDialog::Command command;
bool enabled; bool enabled;
bool default_action; bool default_action;
}; };
static Vector<Option> const options = { static Array const options = {
{ "Power off computer", { "/bin/shutdown", "--now", nullptr }, true, true }, Option { "Power off computer", { "/bin/shutdown"sv, { "--now" } }, true, true },
{ "Reboot", { "/bin/reboot", nullptr }, true, false }, Option { "Reboot", { "/bin/reboot"sv, {} }, true, false },
{ "Log out", { "/bin/logout", nullptr }, 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 dialog = ShutdownDialog::construct();
auto rc = dialog->exec(); auto rc = dialog->exec();
if (rc == ExecResult::OK && dialog->m_selected_option != -1) if (rc == ExecResult::OK && dialog->m_selected_option != -1)
return options[dialog->m_selected_option].cmd; return options[dialog->m_selected_option].command;
return {}; return {};
} }

View file

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

View file

@ -31,7 +31,6 @@
#include <WindowServer/Window.h> #include <WindowServer/Window.h>
#include <serenity.h> #include <serenity.h>
#include <signal.h> #include <signal.h>
#include <spawn.h>
#include <stdio.h> #include <stdio.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.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()); GUI::Process::spawn_or_show_error(&window, "/bin/Run"sv, ReadonlySpan<StringView> {}, Core::StandardPaths::home_directory());
})); }));
system_menu->add_separator(); 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&) { 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 (auto command = ShutdownDialog::show(); command.has_value())
GUI::Process::spawn_or_show_error(&window, command->executable, command->arguments);
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");
}
})); }));
return system_menu; return system_menu;