1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:27:35 +00:00

Playground: Add a menu action to save the file

There was already 'Save As' and now we also have 'Save'.
This commit is contained in:
Gunnar Beutner 2021-06-22 19:20:57 +02:00 committed by Andreas Kling
parent de84b3fa1c
commit c2ae25967a

View file

@ -159,18 +159,32 @@ int main(int argc, char** argv)
auto menubar = GUI::Menubar::construct();
auto& file_menu = menubar->add_menu("&File");
auto save_action = GUI::CommonActions::make_save_as_action([&](auto&) {
Optional<String> save_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "gml");
if (!save_path.has_value())
auto save_as_action = GUI::CommonActions::make_save_as_action([&](auto&) {
Optional<String> new_save_path = GUI::FilePicker::get_save_filepath(window, "Untitled", "gml");
if (!new_save_path.has_value())
return;
if (!editor.write_to_file(save_path.value())) {
if (!editor.write_to_file(new_save_path.value())) {
GUI::MessageBox::show(window, "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
return;
}
file_path = new_save_path.value();
update_title();
});
auto save_action = GUI::CommonActions::make_save_action([&](auto&) {
if (!file_path.is_empty()) {
if (!editor.write_to_file(file_path)) {
GUI::MessageBox::show(window, "Unable to save file.\n", "Error", GUI::MessageBox::Type::Error);
return;
}
update_title();
return;
}
save_as_action->activate();
});
file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
if (window->is_modified()) {
auto save_document_first_result = GUI::MessageBox::show(window, "Save changes to current document first?", "Warning", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
@ -202,6 +216,7 @@ int main(int argc, char** argv)
}));
file_menu.add_action(save_action);
file_menu.add_action(save_as_action);
file_menu.add_separator();
file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) {