From c9404c3a63850719d79837a5c888382760b5d620 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Fri, 14 Apr 2023 08:52:09 -0400 Subject: [PATCH] LibGUI+Userland: Convert MessageBox to fallible construction Adds fallible versions of MessageBox's helper factories, ports DeprecatedString, and rewrites build() to be both fallible and more responsive to font changes. MessageBox now auto shrinks and no longer has to re-build its layout when text changes. It is manually resized once at creation to position properly as a Dialog before being shown. --- .../DisplaySettings/MonitorSettingsWidget.cpp | 7 +- Userland/Libraries/LibGUI/MessageBox.cpp | 145 +++++++++--------- Userland/Libraries/LibGUI/MessageBox.h | 19 ++- .../LibWebView/OutOfProcessWebView.cpp | 4 +- 4 files changed, 92 insertions(+), 83 deletions(-) diff --git a/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp b/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp index 20d2a655fb..0dc5607a00 100644 --- a/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp @@ -265,8 +265,9 @@ void MonitorSettingsWidget::apply_settings() return; auto current_box_text = current_box_text_or_error.release_value(); - auto box = GUI::MessageBox::construct(window(), current_box_text, "Apply new screen layout"sv, - GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo); + auto box = GUI::MessageBox::create(window(), current_box_text, "Apply new screen layout"sv, + GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo) + .release_value_but_fixme_should_propagate_errors(); box->set_icon(window()->icon()); // If after 10 seconds the user doesn't close the message box, just close it. @@ -279,7 +280,7 @@ void MonitorSettingsWidget::apply_settings() return; } auto current_box_text = current_box_text_or_error.release_value(); - box->set_text(current_box_text.to_deprecated_string()); + box->set_text(current_box_text); if (seconds_until_revert <= 0) { box->close(); } diff --git a/Userland/Libraries/LibGUI/MessageBox.cpp b/Userland/Libraries/LibGUI/MessageBox.cpp index 0ed8291247..9047ba65b4 100644 --- a/Userland/Libraries/LibGUI/MessageBox.cpp +++ b/Userland/Libraries/LibGUI/MessageBox.cpp @@ -12,13 +12,29 @@ #include #include #include -#include namespace GUI { +ErrorOr> MessageBox::create(Window* parent_window, StringView text, StringView title, Type type, InputType input_type) +{ + auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MessageBox(parent_window, type, input_type))); + TRY(box->build()); + box->set_title(TRY(String::from_utf8(title)).to_deprecated_string()); + box->set_text(TRY(String::from_utf8(text))); + auto size = box->main_widget()->effective_min_size(); + box->resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value())); + + return box; +} + Dialog::ExecResult MessageBox::show(Window* parent_window, StringView text, StringView title, Type type, InputType input_type) { - auto box = MessageBox::construct(parent_window, text, title, type, input_type); + return MUST(try_show(parent_window, text, title, type, input_type)); +} + +ErrorOr MessageBox::try_show(Window* parent_window, StringView text, StringView title, Type type, InputType input_type) +{ + auto box = TRY(MessageBox::create(parent_window, text, title, type, input_type)); if (parent_window) box->set_icon(parent_window->icon()); return box->exec(); @@ -26,31 +42,41 @@ Dialog::ExecResult MessageBox::show(Window* parent_window, StringView text, Stri Dialog::ExecResult MessageBox::show_error(Window* parent_window, StringView text) { - return show(parent_window, text, "Error"sv, GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK); + return MUST(try_show_error(parent_window, text)); +} + +ErrorOr MessageBox::try_show_error(Window* parent_window, StringView text) +{ + return TRY(try_show(parent_window, text, "Error"sv, GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK)); } Dialog::ExecResult MessageBox::ask_about_unsaved_changes(Window* parent_window, StringView path, Optional