LibGUI: Simplify GCommonActions a bit
Use the same callback signature as GAction so we can just forward it to GAction instead of chaining callbacks.
This commit is contained in:
parent
b7bedab28a
commit
e83390387c
Notes:
sideshowbarker
2024-07-19 12:06:36 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/e83390387c5
14 changed files with 45 additions and 65 deletions
Applications
FileManager
IRCClient
PaintBrush
Piano
QuickShow
SystemMonitor
Terminal
TextEditor
DevTools/VisualBuilder
Games
Libraries/LibGUI
|
@ -131,7 +131,7 @@ int main(int argc, char** argv)
|
|||
|
||||
view_as_icons_action->set_checked(true);
|
||||
|
||||
auto copy_action = GAction::create("Copy", { Mod_Ctrl, Key_C }, GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [&](const GAction&) {
|
||||
auto copy_action = GCommonActions::make_copy_action([&](const GAction&) {
|
||||
if (!selected_file_paths.has_value()) {
|
||||
return;
|
||||
}
|
||||
|
@ -143,7 +143,7 @@ int main(int argc, char** argv)
|
|||
});
|
||||
copy_action->set_enabled(false);
|
||||
|
||||
auto paste_action = GAction::create("Paste", { Mod_Ctrl, Key_V }, GraphicsBitmap::load_from_file("/res/icons/paste16.png"), [&](const GAction&) {
|
||||
auto paste_action = GCommonActions::make_paste_action([&](const GAction&) {
|
||||
auto data_and_type = GClipboard::the().data_and_type();
|
||||
if (data_and_type.type != "file-list") {
|
||||
dbg() << "Cannot paste clipboard type " << data_and_type.type;
|
||||
|
@ -177,7 +177,7 @@ int main(int argc, char** argv)
|
|||
auto properties_action
|
||||
= GAction::create("Properties...", { Mod_Alt, Key_Return }, GraphicsBitmap::load_from_file("/res/icons/16x16/properties.png"), [](auto&) {});
|
||||
|
||||
auto delete_action = GAction::create("Delete", { Mod_None, Key_Delete }, GraphicsBitmap::load_from_file("/res/icons/16x16/delete.png"), [](const GAction&) {
|
||||
auto delete_action = GCommonActions::make_delete_action([](const GAction&) {
|
||||
dbgprintf("'Delete' action activated!\n");
|
||||
});
|
||||
delete_action->set_enabled(false);
|
||||
|
@ -204,7 +204,7 @@ int main(int argc, char** argv)
|
|||
app_menu->add_action(paste_action);
|
||||
app_menu->add_action(delete_action);
|
||||
app_menu->add_separator();
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
GApplication::the().quit(0);
|
||||
}));
|
||||
menubar->add_menu(move(app_menu));
|
||||
|
|
|
@ -124,7 +124,7 @@ void IRCAppWindow::setup_menus()
|
|||
{
|
||||
auto menubar = make<GMenuBar>();
|
||||
auto app_menu = make<GMenu>("IRC Client");
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
dbgprintf("Terminal: Quit menu activated!\n");
|
||||
GApplication::the().quit(0);
|
||||
return;
|
||||
|
|
|
@ -37,7 +37,7 @@ int main(int argc, char** argv)
|
|||
|
||||
auto menubar = make<GMenuBar>();
|
||||
auto app_menu = make<GMenu>("PaintBrush");
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
GApplication::the().quit(0);
|
||||
return;
|
||||
}));
|
||||
|
|
|
@ -46,7 +46,7 @@ int main(int argc, char** argv)
|
|||
auto menubar = make<GMenuBar>();
|
||||
|
||||
auto app_menu = make<GMenu>("Piano");
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
GApplication::the().quit(0);
|
||||
return;
|
||||
}));
|
||||
|
|
|
@ -16,7 +16,7 @@ int main(int argc, char** argv)
|
|||
auto menubar = make<GMenuBar>();
|
||||
|
||||
auto app_menu = make<GMenu>("QuickShow");
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
GApplication::the().quit(0);
|
||||
return;
|
||||
}));
|
||||
|
|
|
@ -139,7 +139,7 @@ int main(int argc, char** argv)
|
|||
|
||||
auto menubar = make<GMenuBar>();
|
||||
auto app_menu = make<GMenu>("System Monitor");
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
GApplication::the().quit(0);
|
||||
return;
|
||||
}));
|
||||
|
|
|
@ -187,7 +187,7 @@ int main(int argc, char** argv)
|
|||
settings_window->move_to_front();
|
||||
}));
|
||||
app_menu->add_separator();
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
dbgprintf("Terminal: Quit menu activated!\n");
|
||||
GApplication::the().quit(0);
|
||||
}));
|
||||
|
|
|
@ -118,14 +118,14 @@ TextEditorWidget::TextEditorWidget()
|
|||
|
||||
m_new_action = GAction::create("New", { Mod_Ctrl, Key_N }, GraphicsBitmap::load_from_file("/res/icons/16x16/new.png"), [this](const GAction&) {
|
||||
if (m_document_dirty) {
|
||||
GMessageBox save_document_first_box("Save Document First?", "Warning", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel, window());
|
||||
GMessageBox save_document_first_box("Save Document First?", "Warning", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel, window());
|
||||
auto save_document_first_result = save_document_first_box.exec();
|
||||
|
||||
if (save_document_first_result != GDialog::ExecResult::ExecOK)
|
||||
return;
|
||||
return;
|
||||
m_save_action->activate();
|
||||
}
|
||||
|
||||
|
||||
m_document_dirty = false;
|
||||
m_editor->set_text(StringView());
|
||||
set_path(FileSystemPath());
|
||||
|
@ -184,7 +184,7 @@ TextEditorWidget::TextEditorWidget()
|
|||
app_menu->add_action(*m_save_action);
|
||||
app_menu->add_action(*m_save_as_action);
|
||||
app_menu->add_separator();
|
||||
app_menu->add_action(GCommonActions::make_quit_action([this] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([this](auto&) {
|
||||
if (!request_close())
|
||||
return;
|
||||
GApplication::the().quit(0);
|
||||
|
|
|
@ -30,7 +30,7 @@ int main(int argc, char** argv)
|
|||
|
||||
auto menubar = make<GMenuBar>();
|
||||
auto app_menu = make<GMenu>("Visual Builder");
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
GApplication::the().quit(0);
|
||||
return;
|
||||
}));
|
||||
|
|
|
@ -67,7 +67,7 @@ int main(int argc, char** argv)
|
|||
app_menu->add_action(*chord_toggler_action);
|
||||
app_menu->add_separator();
|
||||
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
GApplication::the().quit(0);
|
||||
return;
|
||||
}));
|
||||
|
|
|
@ -28,7 +28,7 @@ int main(int argc, char** argv)
|
|||
app_menu->add_action(GAction::create("New game", { Mod_None, Key_F2 }, [&](const GAction&) {
|
||||
game->reset();
|
||||
}));
|
||||
app_menu->add_action(GCommonActions::make_quit_action([] {
|
||||
app_menu->add_action(GCommonActions::make_quit_action([](auto&) {
|
||||
GApplication::the().quit(0);
|
||||
}));
|
||||
|
||||
|
|
|
@ -4,38 +4,33 @@
|
|||
#include <LibGUI/GButton.h>
|
||||
#include <LibGUI/GMenuItem.h>
|
||||
|
||||
NonnullRefPtr<GAction> GCommonActions::make_cut_action(Function<void()> callback, GWidget* widget)
|
||||
namespace GCommonActions {
|
||||
|
||||
NonnullRefPtr<GAction> make_delete_action(Function<void(GAction&)> callback, GWidget* widget)
|
||||
{
|
||||
return GAction::create(
|
||||
"Cut", { Mod_Ctrl, Key_X }, GraphicsBitmap::load_from_file("/res/icons/cut16.png"), [callback = move(callback)](const GAction&) {
|
||||
callback();
|
||||
},
|
||||
widget);
|
||||
return GAction::create("Delete", { Mod_None, Key_Delete }, GraphicsBitmap::load_from_file("/res/icons/16x16/delete.png"), move(callback), widget);
|
||||
}
|
||||
|
||||
NonnullRefPtr<GAction> GCommonActions::make_copy_action(Function<void()> callback, GWidget* widget)
|
||||
NonnullRefPtr<GAction> make_cut_action(Function<void(GAction&)> callback, GWidget* widget)
|
||||
{
|
||||
return GAction::create(
|
||||
"Copy", { Mod_Ctrl, Key_C }, GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), [callback = move(callback)](const GAction&) {
|
||||
callback();
|
||||
},
|
||||
widget);
|
||||
return GAction::create("Cut", { Mod_Ctrl, Key_X }, GraphicsBitmap::load_from_file("/res/icons/cut16.png"), move(callback), widget);
|
||||
}
|
||||
|
||||
NonnullRefPtr<GAction> GCommonActions::make_paste_action(Function<void()> callback, GWidget* widget)
|
||||
NonnullRefPtr<GAction> make_copy_action(Function<void(GAction&)> callback, GWidget* widget)
|
||||
{
|
||||
return GAction::create(
|
||||
"Paste", { Mod_Ctrl, Key_V }, GraphicsBitmap::load_from_file("/res/icons/paste16.png"), [callback = move(callback)](const GAction&) {
|
||||
callback();
|
||||
},
|
||||
widget);
|
||||
return GAction::create("Copy", { Mod_Ctrl, Key_C }, GraphicsBitmap::load_from_file("/res/icons/16x16/edit-copy.png"), move(callback), widget);
|
||||
}
|
||||
|
||||
NonnullRefPtr<GAction> GCommonActions::make_quit_action(Function<void()> callback)
|
||||
NonnullRefPtr<GAction> make_paste_action(Function<void(GAction&)> callback, GWidget* widget)
|
||||
{
|
||||
return GAction::create("Quit", { Mod_Alt, Key_F4 }, [callback = move(callback)](const GAction&) {
|
||||
callback();
|
||||
});
|
||||
return GAction::create("Paste", { Mod_Ctrl, Key_V }, GraphicsBitmap::load_from_file("/res/icons/paste16.png"), move(callback), widget);
|
||||
}
|
||||
|
||||
NonnullRefPtr<GAction> make_quit_action(Function<void(GAction&)> callback)
|
||||
{
|
||||
return GAction::create("Quit", { Mod_Alt, Key_F4 }, move(callback));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
GAction::GAction(const StringView& text, Function<void(GAction&)> on_activation_callback, GWidget* widget)
|
||||
|
|
|
@ -19,10 +19,11 @@ class GMenuItem;
|
|||
class GWidget;
|
||||
|
||||
namespace GCommonActions {
|
||||
NonnullRefPtr<GAction> make_cut_action(Function<void()>, GWidget* widget);
|
||||
NonnullRefPtr<GAction> make_copy_action(Function<void()>, GWidget* widget);
|
||||
NonnullRefPtr<GAction> make_paste_action(Function<void()>, GWidget* widget);
|
||||
NonnullRefPtr<GAction> make_quit_action(Function<void()>);
|
||||
NonnullRefPtr<GAction> make_cut_action(Function<void(GAction&)>, GWidget* widget = nullptr);
|
||||
NonnullRefPtr<GAction> make_copy_action(Function<void(GAction&)>, GWidget* widget = nullptr);
|
||||
NonnullRefPtr<GAction> make_paste_action(Function<void(GAction&)>, GWidget* widget = nullptr);
|
||||
NonnullRefPtr<GAction> make_delete_action(Function<void(GAction&)>, GWidget* widget = nullptr);
|
||||
NonnullRefPtr<GAction> make_quit_action(Function<void(GAction&)>);
|
||||
};
|
||||
|
||||
class GAction : public RefCounted<GAction>
|
||||
|
|
|
@ -47,26 +47,10 @@ void GTextEditor::create_actions()
|
|||
},
|
||||
this);
|
||||
|
||||
m_cut_action = GCommonActions::make_cut_action([&] {
|
||||
cut();
|
||||
},
|
||||
this);
|
||||
|
||||
m_copy_action = GCommonActions::make_copy_action([&] {
|
||||
copy();
|
||||
},
|
||||
this);
|
||||
|
||||
m_paste_action = GCommonActions::make_paste_action([&] {
|
||||
paste();
|
||||
},
|
||||
this);
|
||||
|
||||
m_delete_action = GAction::create(
|
||||
"Delete", { 0, Key_Delete }, GraphicsBitmap::load_from_file("/res/icons/16x16/delete.png"), [&](const GAction&) {
|
||||
do_delete();
|
||||
},
|
||||
this);
|
||||
m_cut_action = GCommonActions::make_cut_action([&](auto&) { cut(); }, this);
|
||||
m_copy_action = GCommonActions::make_copy_action([&](auto&) { copy(); }, this);
|
||||
m_paste_action = GCommonActions::make_paste_action([&](auto&) { paste(); }, this);
|
||||
m_delete_action = GCommonActions::make_delete_action([&](auto&) { do_delete(); }, this);
|
||||
}
|
||||
|
||||
void GTextEditor::set_text(const StringView& text)
|
||||
|
@ -554,13 +538,13 @@ void GTextEditor::keydown_event(GKeyEvent& event)
|
|||
} else if (m_cursor.line() != line_count() - 1) {
|
||||
new_line = m_cursor.line() + 1;
|
||||
new_column = 0;
|
||||
}
|
||||
}
|
||||
toggle_selection_if_needed_for_event(event);
|
||||
set_cursor(new_line, new_column);
|
||||
if (m_selection.start().is_valid()) {
|
||||
m_selection.set_end(m_cursor);
|
||||
did_update_selection();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
|
||||
|
|
Loading…
Add table
Reference in a new issue