From a0c773da9ee77c7603e39c40524046b1b6c3c60d Mon Sep 17 00:00:00 2001 From: EvilHowl Date: Tue, 2 Feb 2021 11:34:45 +0100 Subject: [PATCH] DisplaySettings: Show a confirmation message when applying changes The changes will be reverted after 10 seconds of no user input. --- .../DisplaySettings/DisplaySettings.cpp | 31 +++++++++++++++++++ .../DisplaySettings/DisplaySettings.h | 1 + 2 files changed, 32 insertions(+) diff --git a/Userland/Applications/DisplaySettings/DisplaySettings.cpp b/Userland/Applications/DisplaySettings/DisplaySettings.cpp index 8ad1c01f91..3bc9799122 100644 --- a/Userland/Applications/DisplaySettings/DisplaySettings.cpp +++ b/Userland/Applications/DisplaySettings/DisplaySettings.cpp @@ -272,10 +272,41 @@ void DisplaySettingsWidget::load_current_settings() void DisplaySettingsWidget::send_settings_to_window_server() { + // Store the current screen resolution and scale factor in case the user wants to revert to it. + auto ws_config(Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini")); + Gfx::IntSize current_resolution; + current_resolution.set_width(ws_config->read_num_entry("Screen", "Width", 1024)); + current_resolution.set_height(ws_config->read_num_entry("Screen", "Height", 768)); + int current_scale_factor = ws_config->read_num_entry("Screen", "ScaleFactor", 1); + if (current_scale_factor != 1 && current_scale_factor != 2) { + dbgln("unexpected ScaleFactor {}, setting to 1", current_scale_factor); + current_scale_factor = 1; + } + auto result = GUI::WindowServerConnection::the().send_sync(m_monitor_widget->desktop_resolution(), m_monitor_widget->desktop_scale_factor()); if (!result->success()) { GUI::MessageBox::show(nullptr, String::formatted("Reverting to resolution {}x{} @ {}x", result->resolution().width(), result->resolution().height(), result->scale_factor()), "Unable to set resolution", GUI::MessageBox::Type::Error); + } else { + auto box = GUI::MessageBox::construct(window(), String::formatted("Do you want to keep the new settings? They will be reverted after 10 seconds."), + String::formatted("New screen resolution: {}x{} @ {}x", m_monitor_widget->desktop_resolution().width(), m_monitor_widget->desktop_resolution().height(), + m_monitor_widget->desktop_scale_factor()), + 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(); + }); + + // 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) { + result = GUI::WindowServerConnection::the().send_sync(current_resolution, current_scale_factor); + if (!result->success()) { + GUI::MessageBox::show(nullptr, String::formatted("Reverting to resolution {}x{} @ {}x", result->resolution().width(), result->resolution().height(), result->scale_factor()), + "Unable to set resolution", GUI::MessageBox::Type::Error); + } + } } if (!m_monitor_widget->wallpaper().is_empty()) { diff --git a/Userland/Applications/DisplaySettings/DisplaySettings.h b/Userland/Applications/DisplaySettings/DisplaySettings.h index ebda18addd..c232a798e5 100644 --- a/Userland/Applications/DisplaySettings/DisplaySettings.h +++ b/Userland/Applications/DisplaySettings/DisplaySettings.h @@ -27,6 +27,7 @@ #pragma once #include "MonitorWidget.h" +#include #include #include #include