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());

View file

@ -1,11 +1,12 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Time.h>
#include <LibGUI/Dialog.h>
namespace GUI {
@ -32,7 +33,7 @@ public:
static int show(Window* parent_window, StringView text, StringView title, Type type = Type::None, InputType input_type = InputType::OK);
static int show_error(Window* parent_window, StringView text);
static int ask_about_unsaved_changes(Window* parent_window, StringView path);
static int ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Time> last_unmodified_timestamp = {});
private:
explicit MessageBox(Window* parent_window, StringView text, StringView title, Type type = Type::None, InputType input_type = InputType::OK);