From d01ca4e3e2cbe6cb0d2878a6db96f74b6c086948 Mon Sep 17 00:00:00 2001 From: stijndr Date: Fri, 18 Feb 2022 00:18:34 +0100 Subject: [PATCH] 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. --- .../DisplaySettings/MonitorSettingsWidget.cpp | 20 +++++++++++++++---- Userland/Libraries/LibGUI/MessageBox.cpp | 6 ++++++ Userland/Libraries/LibGUI/MessageBox.h | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp b/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp index 6ce34f372c..335859121a 100644 --- a/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp @@ -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) { diff --git a/Userland/Libraries/LibGUI/MessageBox.cpp b/Userland/Libraries/LibGUI/MessageBox.cpp index 416006a6e2..b282430435 100644 --- a/Userland/Libraries/LibGUI/MessageBox.cpp +++ b/Userland/Libraries/LibGUI/MessageBox.cpp @@ -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) diff --git a/Userland/Libraries/LibGUI/MessageBox.h b/Userland/Libraries/LibGUI/MessageBox.h index 8a705f0a7d..98265ca8ed 100644 --- a/Userland/Libraries/LibGUI/MessageBox.h +++ b/Userland/Libraries/LibGUI/MessageBox.h @@ -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