1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

LibGUI: Add optional unmodified timestamp to "unsaved changes" dialogs

This allows us to show "last saved N seconds ago" when asking the user
what to do. :^)
This commit is contained in:
Andreas Kling 2022-01-04 17:33:26 +01:00
parent c4cc796f86
commit e5e109a556
2 changed files with 15 additions and 7 deletions

View file

@ -27,15 +27,22 @@ int MessageBox::show_error(Window* parent_window, StringView text)
return show(parent_window, text, "Error", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK);
}
int MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path)
int MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Time> last_unmodified_timestamp)
{
String text;
StringBuilder builder;
builder.append("Save changes to ");
if (path.is_empty())
text = "Save changes to untitled document before closing?";
builder.append("untitled document");
else
text = String::formatted("Save changes to '{}' before closing?", LexicalPath::basename(path));
builder.appendff("\"{}\"", LexicalPath::basename(path));
builder.append(" before closing?");
auto box = MessageBox::construct(parent_window, text, "Unsaved changes", Type::Warning, InputType::YesNoCancel);
if (last_unmodified_timestamp.has_value()) {
auto age = (Time::now_monotonic() - *last_unmodified_timestamp).to_seconds();
builder.appendff("\nLast saved {} second{} ago.", age, age == 1 ? "" : "s");
}
auto box = MessageBox::construct(parent_window, builder.string_view(), "Unsaved changes", Type::Warning, InputType::YesNoCancel);
if (parent_window)
box->set_icon(parent_window->icon());