From 84d66ac538d13e8bdb4010a63298ab8c228873a5 Mon Sep 17 00:00:00 2001 From: Alec Murphy Date: Thu, 4 Jul 2024 20:18:27 -0400 Subject: [PATCH] LibGUI: Add alt shortcut for make_quit_action This patch adds the common Ctrl-W shortcut to make_quit_action as an alternate shortcut by default. The shortcut can be disabled in programs using this key binding for a different purpose, e.g. closing tabs, or instances where the "close window" concept would not be applicable, such as CatDog. To the best of my knowledge, I have disabled the shortcut for any existing programs in the system where the aforementioned examples or any additional conflicts would apply. --- Userland/Applications/Browser/BrowserWindow.cpp | 8 +++++--- Userland/Applications/KeyboardMapper/main.cpp | 3 ++- Userland/Applications/PixelPaint/MainWidget.cpp | 10 ++++++---- Userland/Applications/Terminal/main.cpp | 12 +++++++----- Userland/Demos/CatDog/main.cpp | 2 +- Userland/Libraries/LibGUI/Action.h | 7 ++++++- Userland/Libraries/LibGUI/CommonActions.cpp | 7 +++++-- 7 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index 3a2936d7cd7b27..e3be87a7f55fef 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -160,9 +160,11 @@ void BrowserWindow::build_menus(StringView const man_file) file_menu->add_action(close_tab_action); file_menu->add_separator(); - file_menu->add_action(GUI::CommonActions::make_quit_action([](auto&) { - GUI::Application::the()->quit(); - })); + file_menu->add_action(GUI::CommonActions::make_quit_action( + [](auto&) { + GUI::Application::the()->quit(); + }, + GUI::CommonActions::QuitAltShortcut::None)); auto view_menu = add_menu("&View"_string); view_menu->add_action(WindowActions::the().show_bookmarks_bar_action()); diff --git a/Userland/Applications/KeyboardMapper/main.cpp b/Userland/Applications/KeyboardMapper/main.cpp index 48bb39b351356a..b2a5cb33a1e18c 100644 --- a/Userland/Applications/KeyboardMapper/main.cpp +++ b/Userland/Applications/KeyboardMapper/main.cpp @@ -75,7 +75,8 @@ ErrorOr serenity_main(Main::Arguments arguments) auto quit_action = GUI::CommonActions::make_quit_action( [&](auto&) { app->quit(); - }); + }, + GUI::CommonActions::QuitAltShortcut::None); auto auto_modifier_action = GUI::Action::create("Auto-Modifier", [&](auto& act) { keyboard_mapper_widget->set_automatic_modifier(act.is_checked()); diff --git a/Userland/Applications/PixelPaint/MainWidget.cpp b/Userland/Applications/PixelPaint/MainWidget.cpp index ef4f298e95c4a4..fb574164a137f0 100644 --- a/Userland/Applications/PixelPaint/MainWidget.cpp +++ b/Userland/Applications/PixelPaint/MainWidget.cpp @@ -286,10 +286,12 @@ ErrorOr MainWidget::initialize_menubar(GUI::Window& window) file_menu->add_action(*m_close_image_action); - file_menu->add_action(GUI::CommonActions::make_quit_action([this](auto&) { - if (request_close()) - GUI::Application::the()->quit(); - })); + file_menu->add_action(GUI::CommonActions::make_quit_action( + [this](auto&) { + if (request_close()) + GUI::Application::the()->quit(); + }, + GUI::CommonActions::QuitAltShortcut::None)); m_edit_menu = window.add_menu("&Edit"_string); diff --git a/Userland/Applications/Terminal/main.cpp b/Userland/Applications/Terminal/main.cpp index 672e2d2f4f1d06..549990fa5924d6 100644 --- a/Userland/Applications/Terminal/main.cpp +++ b/Userland/Applications/Terminal/main.cpp @@ -384,11 +384,13 @@ ErrorOr serenity_main(Main::Arguments arguments) return GUI::MessageBox::ExecResult::OK; }; - file_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) { - dbgln("Terminal: Quit menu activated!"); - if (check_terminal_quit() == GUI::MessageBox::ExecResult::OK) - GUI::Application::the()->quit(); - })); + file_menu->add_action(GUI::CommonActions::make_quit_action( + [&](auto&) { + dbgln("Terminal: Quit menu activated!"); + if (check_terminal_quit() == GUI::MessageBox::ExecResult::OK) + GUI::Application::the()->quit(); + }, + GUI::CommonActions::QuitAltShortcut::None)); auto edit_menu = window->add_menu("&Edit"_string); edit_menu->add_action(terminal->copy_action()); diff --git a/Userland/Demos/CatDog/main.cpp b/Userland/Demos/CatDog/main.cpp index 8f32622d213080..7581571b020d80 100644 --- a/Userland/Demos/CatDog/main.cpp +++ b/Userland/Demos/CatDog/main.cpp @@ -56,7 +56,7 @@ ErrorOr serenity_main(Main::Arguments arguments) action.set_icon(catdog_widget->is_sleeping() ? catdog_icon_wake : catdog_icon_sleep); })); context_menu->add_separator(); - context_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); })); + context_menu->add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }, GUI::CommonActions::QuitAltShortcut::None)); window->show(); window->set_always_on_top(); diff --git a/Userland/Libraries/LibGUI/Action.h b/Userland/Libraries/LibGUI/Action.h index 207dda10e5e8f6..297c67dc2c9ca4 100644 --- a/Userland/Libraries/LibGUI/Action.h +++ b/Userland/Libraries/LibGUI/Action.h @@ -24,6 +24,11 @@ namespace GUI { namespace CommonActions { +enum class QuitAltShortcut { + None, + CtrlW +}; + NonnullRefPtr make_about_action(String const& app_name, Icon const& app_icon, Window* parent = nullptr); NonnullRefPtr make_open_action(Function, Core::EventReceiver* parent = nullptr); NonnullRefPtr make_save_action(Function, Core::EventReceiver* parent = nullptr); @@ -38,7 +43,7 @@ NonnullRefPtr make_move_to_front_action(Function, Core::E NonnullRefPtr make_move_to_back_action(Function, Core::EventReceiver* parent = nullptr); NonnullRefPtr make_insert_emoji_action(Function, Core::EventReceiver* parent = nullptr); NonnullRefPtr make_fullscreen_action(Function, Core::EventReceiver* parent = nullptr); -NonnullRefPtr make_quit_action(Function); +NonnullRefPtr make_quit_action(Function, QuitAltShortcut = QuitAltShortcut::CtrlW); NonnullRefPtr make_help_action(Function, Core::EventReceiver* parent = nullptr); NonnullRefPtr make_go_back_action(Function, Core::EventReceiver* parent = nullptr); NonnullRefPtr make_go_forward_action(Function, Core::EventReceiver* parent = nullptr); diff --git a/Userland/Libraries/LibGUI/CommonActions.cpp b/Userland/Libraries/LibGUI/CommonActions.cpp index bf907a670d9938..b3ba5cf1e7f07a 100644 --- a/Userland/Libraries/LibGUI/CommonActions.cpp +++ b/Userland/Libraries/LibGUI/CommonActions.cpp @@ -117,9 +117,12 @@ NonnullRefPtr make_fullscreen_action(Function callback, C return action; } -NonnullRefPtr make_quit_action(Function callback) +NonnullRefPtr make_quit_action(Function callback, QuitAltShortcut quit_alt_shortcut) { - auto action = Action::create("&Quit", { Mod_Alt, Key_F4 }, move(callback)); + Shortcut alternate_shortcut = { Mod_Ctrl, Key_W }; + if (quit_alt_shortcut == QuitAltShortcut::None) + alternate_shortcut = {}; + auto action = Action::create("&Quit", { Mod_Alt, Key_F4 }, alternate_shortcut, move(callback)); action->set_status_tip("Quit the application"_string); return action; }