Userland: Dynamically update the MonitorSettingsWidget countdown timer

This causes the number of seconds in "Do you want to keep the new
settings? They will be reverted after 10 seconds." to be dynamically
updated.
This commit is contained in:
stijndr 2022-02-18 00:18:34 +01:00 committed by Andreas Kling
parent 413bc9976c
commit d01ca4e3e2
Notes: sideshowbarker 2024-07-18 01:43:16 +09:00
3 changed files with 24 additions and 4 deletions

View file

@ -220,14 +220,26 @@ void MonitorSettingsWidget::apply_settings()
if (result.success()) {
load_current_settings(); // Refresh
auto box = GUI::MessageBox::construct(window(), String::formatted("Do you want to keep the new settings? They will be reverted after 10 seconds."),
"Apply new screen layout", GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
auto seconds_until_revert = 10;
auto box_text = [&seconds_until_revert] {
return String::formatted("Do you want to keep the new settings? They will be reverted after {} {}.",
seconds_until_revert, seconds_until_revert == 1 ? "second" : "seconds");
};
auto box = GUI::MessageBox::construct(window(), box_text(), "Apply new screen layout",
GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo);
box->set_icon(window()->icon());
// If after 10 seconds the user doesn't close the message box, just close it.
auto timer = Core::Timer::construct(10000, [&] {
box->close();
auto revert_timer = Core::Timer::create_repeating(1000, [&] {
seconds_until_revert -= 1;
box->set_text(box_text());
if (seconds_until_revert <= 0) {
box->close();
}
});
revert_timer->start();
// If the user selects "No", closes the window or the window gets closed by the 10 seconds timer, revert the changes.
if (box->exec() == GUI::MessageBox::ExecYes) {

View file

@ -55,6 +55,12 @@ int MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path
return box->exec();
}
void MessageBox::set_text(String text)
{
m_text = move(text);
build();
}
MessageBox::MessageBox(Window* parent_window, StringView text, StringView title, Type type, InputType input_type)
: Dialog(parent_window)
, m_text(text)

View file

@ -35,6 +35,8 @@ public:
static int show_error(Window* parent_window, StringView text);
static int ask_about_unsaved_changes(Window* parent_window, StringView path, Optional<Time> last_unmodified_timestamp = {});
void set_text(String text);
private:
explicit MessageBox(Window* parent_window, StringView text, StringView title, Type type = Type::None, InputType input_type = InputType::OK);