Playground: Prompt to save changes after the user picked a file to open

There's no point in saving the file - and potentially having to ask the
user for a file name - if the user abandons the 'Open' action by
clicking 'Cancel' in the file picker. This now also matches TextEditor's
behavior.
This commit is contained in:
Gunnar Beutner 2021-06-22 19:24:49 +02:00 committed by Andreas Kling
parent c2ae25967a
commit 92fdc5bd69
Notes: sideshowbarker 2024-07-18 11:38:45 +09:00

View file

@ -186,6 +186,11 @@ int main(int argc, char** argv)
});
file_menu.add_action(GUI::CommonActions::make_open_action([&](auto&) {
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window);
if (!open_path.has_value())
return;
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);
if (save_document_first_result == GUI::Dialog::ExecResult::ExecYes)
@ -194,11 +199,6 @@ int main(int argc, char** argv)
return;
}
Optional<String> open_path = GUI::FilePicker::get_open_filepath(window);
if (!open_path.has_value())
return;
auto file = Core::File::construct(open_path.value());
if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) {
GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: {}", open_path.value(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);