From caa8f43dbe6a3a5d6320ca928d48ce6d65528773 Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Thu, 8 Jun 2023 07:46:11 -0400 Subject: [PATCH] Applications+Demos+LibGUI: Migrate to fallible WizardDialogs and Pages And port page text to String. Also removes WizardDialog::show() helper as all current implementations prefer to derive their own Dialog. --- .../Applications/FontEditor/MainWidget.cpp | 9 ++- .../Applications/FontEditor/NewFontDialog.cpp | 39 +++++++++---- .../Applications/FontEditor/NewFontDialog.h | 12 ++-- .../Applications/Spreadsheet/ExportDialog.cpp | 15 ++--- .../Applications/Spreadsheet/ImportDialog.cpp | 16 +++--- .../Demos/WidgetGallery/DemoWizardDialog.cpp | 29 ++++++---- .../LibGUI/Wizards/CoverWizardPage.cpp | 32 +++++++---- .../LibGUI/Wizards/CoverWizardPage.h | 13 +++-- .../Libraries/LibGUI/Wizards/WizardDialog.cpp | 56 +++++++++++-------- .../Libraries/LibGUI/Wizards/WizardDialog.h | 12 ++-- .../Libraries/LibGUI/Wizards/WizardPage.cpp | 47 +++++++++------- .../Libraries/LibGUI/Wizards/WizardPage.h | 13 +++-- 12 files changed, 178 insertions(+), 115 deletions(-) diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp index 1c0944bee5..317a766f60 100644 --- a/Userland/Applications/FontEditor/MainWidget.cpp +++ b/Userland/Applications/FontEditor/MainWidget.cpp @@ -116,10 +116,13 @@ ErrorOr MainWidget::create_actions() m_new_action = GUI::Action::create("&New Font...", { Mod_Ctrl, Key_N }, g_resources.new_font, [this](auto&) { if (!request_close()) return; - auto new_font_wizard = NewFontDialog::construct(window()); - if (new_font_wizard->exec() != GUI::Dialog::ExecResult::OK) + auto maybe_wizard = NewFontDialog::create(window()); + if (maybe_wizard.is_error()) + return show_error(maybe_wizard.release_error(), "Creating font wizard failed"sv); + auto wizard = maybe_wizard.release_value(); + if (wizard->exec() != GUI::Dialog::ExecResult::OK) return; - auto maybe_font = new_font_wizard->create_font(); + auto maybe_font = wizard->create_font(); if (maybe_font.is_error()) return show_error(maybe_font.release_error(), "Creating new font failed"sv); if (auto result = initialize({}, move(maybe_font.value())); result.is_error()) diff --git a/Userland/Applications/FontEditor/NewFontDialog.cpp b/Userland/Applications/FontEditor/NewFontDialog.cpp index 5a39d30a6f..9de99494be 100644 --- a/Userland/Applications/FontEditor/NewFontDialog.cpp +++ b/Userland/Applications/FontEditor/NewFontDialog.cpp @@ -122,13 +122,19 @@ private: REGISTER_WIDGET(FontEditor, GlyphPreviewWidget); -NewFontDialog::NewFontDialog(GUI::Window* parent_window) - : GUI::WizardDialog(parent_window) +ErrorOr> NewFontDialog::create(GUI::Window* parent_window) { - set_title("New Font"); + auto dialog = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) NewFontDialog(parent_window))); + TRY(dialog->build()); + return dialog; +} - m_font_properties_page = GUI::WizardPage::construct("Typeface properties", "Edit details about this font."); - m_font_properties_page->body_widget().load_from_gml(new_font_dialog_page_1_gml).release_value_but_fixme_should_propagate_errors(); +ErrorOr NewFontDialog::build() +{ + TRY(GUI::WizardDialog::build()); + + m_font_properties_page = TRY(GUI::WizardPage::create("Typeface properties"sv, "Edit details about this font."sv)); + TRY(m_font_properties_page->body_widget().load_from_gml(new_font_dialog_page_1_gml)); m_name_textbox = m_font_properties_page->body_widget().find_descendant_of_type_named("name_textbox"); m_family_textbox = m_font_properties_page->body_widget().find_descendant_of_type_named("family_textbox"); @@ -136,14 +142,16 @@ NewFontDialog::NewFontDialog(GUI::Window* parent_window) m_slope_combobox = m_font_properties_page->body_widget().find_descendant_of_type_named("slope_combobox"); m_presentation_spinbox = m_font_properties_page->body_widget().find_descendant_of_type_named("presentation_spinbox"); + TRY(m_font_weight_list.try_ensure_capacity(Gfx::font_weight_names.size())); for (auto& it : Gfx::font_weight_names) - m_font_weight_list.append(it.name); - m_weight_combobox->set_model(*GUI::ItemListModel::create(m_font_weight_list)); + m_font_weight_list.unchecked_append(TRY(String::from_utf8(it.name))); + m_weight_combobox->set_model(TRY(GUI::ItemListModel::try_create(m_font_weight_list))); m_weight_combobox->set_selected_index(3); + TRY(m_font_slope_list.try_ensure_capacity(Gfx::font_slope_names.size())); for (auto& it : Gfx::font_slope_names) - m_font_slope_list.append(it.name); - m_slope_combobox->set_model(*GUI::ItemListModel::create(m_font_slope_list)); + m_font_slope_list.unchecked_append(TRY(String::from_utf8(it.name))); + m_slope_combobox->set_model(TRY(GUI::ItemListModel::try_create(m_font_slope_list))); m_slope_combobox->set_selected_index(0); m_presentation_spinbox->set_value(12); @@ -155,8 +163,8 @@ NewFontDialog::NewFontDialog(GUI::Window* parent_window) return m_glyph_properties_page; }; - m_glyph_properties_page = GUI::WizardPage::construct("Glyph properties", "Edit details about this font."); - m_glyph_properties_page->body_widget().load_from_gml(new_font_dialog_page_2_gml).release_value_but_fixme_should_propagate_errors(); + m_glyph_properties_page = TRY(GUI::WizardPage::create("Glyph properties"sv, "Edit details about this font."sv)); + TRY(m_glyph_properties_page->body_widget().load_from_gml(new_font_dialog_page_2_gml)); m_glyph_properties_page->set_is_final_page(true); m_glyph_height_spinbox = m_glyph_properties_page->body_widget().find_descendant_of_type_named("height_spinbox"); @@ -197,6 +205,15 @@ NewFontDialog::NewFontDialog(GUI::Window* parent_window) }; push_page(*m_font_properties_page); + + return {}; +} + +NewFontDialog::NewFontDialog(GUI::Window* parent_window) + : GUI::WizardDialog(parent_window) +{ + set_title("New Font"); + set_icon(parent_window->icon()); } void NewFontDialog::save_metadata() diff --git a/Userland/Applications/FontEditor/NewFontDialog.h b/Userland/Applications/FontEditor/NewFontDialog.h index 91796df434..14635d28c4 100644 --- a/Userland/Applications/FontEditor/NewFontDialog.h +++ b/Userland/Applications/FontEditor/NewFontDialog.h @@ -12,13 +12,17 @@ #include class NewFontDialog final : public GUI::WizardDialog { - C_OBJECT(NewFontDialog); + C_OBJECT_ABSTRACT(NewFontDialog); public: + static ErrorOr> create(GUI::Window* parent_window); + ErrorOr> create_font(); private: - NewFontDialog(GUI::Window* parent_window); + explicit NewFontDialog(GUI::Window* parent_window); + + virtual ErrorOr build() override; void save_metadata(); @@ -51,6 +55,6 @@ private: RefPtr m_spacing_spinbox; RefPtr m_fixed_width_checkbox; - Vector m_font_weight_list; - Vector m_font_slope_list; + Vector m_font_weight_list; + Vector m_font_slope_list; }; diff --git a/Userland/Applications/Spreadsheet/ExportDialog.cpp b/Userland/Applications/Spreadsheet/ExportDialog.cpp index 41441f3e11..cc84d1618e 100644 --- a/Userland/Applications/Spreadsheet/ExportDialog.cpp +++ b/Userland/Applications/Spreadsheet/ExportDialog.cpp @@ -34,9 +34,10 @@ CSVExportDialogPage::CSVExportDialogPage(Sheet const& sheet) { m_headers.extend(m_data.take_first()); - m_page = GUI::WizardPage::construct( - "CSV Export Options", - "Please select the options for the csv file you wish to export to"); + m_page = GUI::WizardPage::create( + "CSV Export Options"sv, + "Please select the options for the csv file you wish to export to"sv) + .release_value_but_fixme_should_propagate_errors(); m_page->body_widget().load_from_gml(csv_export_gml).release_value_but_fixme_should_propagate_errors(); m_page->set_is_final_page(true); @@ -180,7 +181,7 @@ void CSVExportDialogPage::update_preview() ErrorOr ExportDialog::make_and_run_for(StringView mime, Core::File& file, DeprecatedString filename, Workbook& workbook) { - auto wizard = GUI::WizardDialog::construct(GUI::Application::the()->active_window()); + auto wizard = TRY(GUI::WizardDialog::create(GUI::Application::the()->active_window())); wizard->set_title("File Export Wizard"); wizard->set_icon(GUI::Icon::default_icon("app-spreadsheet"sv).bitmap_for_size(16)); @@ -213,9 +214,9 @@ ErrorOr ExportDialog::make_and_run_for(StringView mime, Core::File& file, } else if (mime == "application/x-sheets+json") { return export_worksheet(); } else { - auto page = GUI::WizardPage::construct( - "Export File Format", - DeprecatedString::formatted("Select the format you wish to export to '{}' as", LexicalPath::basename(filename))); + auto page = TRY(GUI::WizardPage::create( + "Export File Format"sv, + TRY(String::formatted("Select the format you wish to export to '{}' as", LexicalPath::basename(filename))))); page->on_next_page = [] { return nullptr; }; diff --git a/Userland/Applications/Spreadsheet/ImportDialog.cpp b/Userland/Applications/Spreadsheet/ImportDialog.cpp index 8cb8a6bd39..242621b0ed 100644 --- a/Userland/Applications/Spreadsheet/ImportDialog.cpp +++ b/Userland/Applications/Spreadsheet/ImportDialog.cpp @@ -27,9 +27,10 @@ namespace Spreadsheet { CSVImportDialogPage::CSVImportDialogPage(StringView csv) : m_csv(csv) { - m_page = GUI::WizardPage::construct( - "CSV Import Options", - "Please select the options for the csv file you wish to import"); + m_page = GUI::WizardPage::create( + "CSV Import Options"sv, + "Please select the options for the csv file you wish to import"sv) + .release_value_but_fixme_should_propagate_errors(); m_page->body_widget().load_from_gml(csv_import_gml).release_value_but_fixme_should_propagate_errors(); m_page->set_is_final_page(true); @@ -178,7 +179,7 @@ void CSVImportDialogPage::update_preview() ErrorOr>, DeprecatedString> ImportDialog::make_and_run_for(GUI::Window& parent, StringView mime, String const& filename, Core::File& file, Workbook& workbook) { - auto wizard = GUI::WizardDialog::construct(&parent); + auto wizard = GUI::WizardDialog::create(&parent).release_value_but_fixme_should_propagate_errors(); wizard->set_title("File Import Wizard"); wizard->set_icon(GUI::Icon::default_icon("app-spreadsheet"sv).bitmap_for_size(16)); @@ -244,9 +245,10 @@ ErrorOr>, DeprecatedString> ImportDialog::make_and_r } else if (mime == "application/x-sheets+json") { return import_worksheet(); } else { - auto page = GUI::WizardPage::construct( - "Import File Format", - DeprecatedString::formatted("Select the format you wish to import '{}' as", LexicalPath::basename(filename.to_deprecated_string()))); + auto page = GUI::WizardPage::create( + "Import File Format"sv, + DeprecatedString::formatted("Select the format you wish to import '{}' as", LexicalPath::basename(filename.to_deprecated_string()))) + .release_value_but_fixme_should_propagate_errors(); page->on_next_page = [] { return nullptr; }; diff --git a/Userland/Demos/WidgetGallery/DemoWizardDialog.cpp b/Userland/Demos/WidgetGallery/DemoWizardDialog.cpp index 0cff4d365e..8a671f40b2 100644 --- a/Userland/Demos/WidgetGallery/DemoWizardDialog.cpp +++ b/Userland/Demos/WidgetGallery/DemoWizardDialog.cpp @@ -15,18 +15,22 @@ DemoWizardDialog::DemoWizardDialog(GUI::Window* parent_window) : GUI::WizardDialog(parent_window) { + set_title("Demo Wizard"); + build().release_value_but_fixme_should_propagate_errors(); + // Create the front cover - m_front_page = GUI::CoverWizardPage::try_create().release_value_but_fixme_should_propagate_errors(); - m_front_page->set_header_text("Welcome to the SerenityOS demo wizard!"); - m_front_page->set_body_text("This wizard demonstrates the amazing wizardry\ncapabilities of LibGUI :^)"); + m_front_page = GUI::CoverWizardPage::create( + "Welcome to the SerenityOS demo wizard!"sv, + "This wizard demonstrates the amazing wizardry\ncapabilities of LibGUI :^)"sv) + .release_value_but_fixme_should_propagate_errors(); m_front_page->on_next_page = [&]() { return m_page_1; }; // Create Page 1 - m_page_1 = GUI::WizardPage::try_create( - "Installation location", - "Choose where Demo Application is installed on your computer.") + m_page_1 = GUI::WizardPage::create( + "Installation location"sv, + "Choose where Demo Application is installed on your computer."sv) .release_value_but_fixme_should_propagate_errors(); m_page_1->body_widget().load_from_gml(demo_wizard_page_1_gml).release_value_but_fixme_should_propagate_errors(); m_page_1_location_text_box = m_page_1->body_widget().find_descendant_of_type_named("page_1_location_text_box"); @@ -35,9 +39,9 @@ DemoWizardDialog::DemoWizardDialog(GUI::Window* parent_window) }; // Create Page 2 with a progress bar :^) - m_page_2 = GUI::WizardPage::try_create( - "Installation in progress...", - "Please wait. Do not turn off your computer.") + m_page_2 = GUI::WizardPage::create( + "Installation in progress..."sv, + "Please wait. Do not turn off your computer."sv) .release_value_but_fixme_should_propagate_errors(); m_page_2->body_widget().load_from_gml(demo_wizard_page_2_gml).release_value_but_fixme_should_propagate_errors(); m_page_2_progressbar = m_page_2->body_widget().find_descendant_of_type_named("page_2_progressbar"); @@ -66,9 +70,10 @@ DemoWizardDialog::DemoWizardDialog(GUI::Window* parent_window) // Don't set a on_next_page handler for page 2 as we automatically navigate to the final page on progress completion // Create the back cover - m_back_page = GUI::CoverWizardPage::try_create().release_value_but_fixme_should_propagate_errors(); - m_back_page->set_header_text("Wizard complete."); - m_back_page->set_body_text("That concludes the SerenityOS demo wizard :^)"); + m_back_page = GUI::CoverWizardPage::create( + "Wizard complete."sv, + "That concludes the SerenityOS demo wizard :^)"sv) + .release_value_but_fixme_should_propagate_errors(); m_back_page->set_is_final_page(true); push_page(*m_front_page); diff --git a/Userland/Libraries/LibGUI/Wizards/CoverWizardPage.cpp b/Userland/Libraries/LibGUI/Wizards/CoverWizardPage.cpp index 5183b4c290..af02150861 100644 --- a/Userland/Libraries/LibGUI/Wizards/CoverWizardPage.cpp +++ b/Userland/Libraries/LibGUI/Wizards/CoverWizardPage.cpp @@ -13,36 +13,44 @@ namespace GUI { -CoverWizardPage::CoverWizardPage() - : AbstractWizardPage() +ErrorOr> CoverWizardPage::create(StringView title, StringView subtitle) +{ + auto page = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) CoverWizardPage())); + TRY(page->build(TRY(String::from_utf8(title)), TRY(String::from_utf8(subtitle)))); + return page; +} + +ErrorOr CoverWizardPage::build(String title, String subtitle) { set_fill_with_background_color(true); set_background_role(Gfx::ColorRole::Base); - set_layout(); - m_banner_image_widget = add(); + TRY(try_set_layout()); + m_banner_image_widget = TRY(try_add()); m_banner_image_widget->set_fixed_size(160, 315); m_banner_image_widget->load_from_file("/res/graphics/wizard-banner-simple.png"sv); - m_content_widget = add(); - m_content_widget->set_layout(20); + m_content_widget = TRY(try_add()); + TRY(m_content_widget->try_set_layout(20)); - m_header_label = m_content_widget->add