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.
This commit is contained in:
thankyouverycool 2023-06-08 07:46:11 -04:00 committed by Andreas Kling
parent 96e60c98cf
commit caa8f43dbe
12 changed files with 178 additions and 115 deletions

View file

@ -116,10 +116,13 @@ ErrorOr<void> 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())

View file

@ -122,13 +122,19 @@ private:
REGISTER_WIDGET(FontEditor, GlyphPreviewWidget);
NewFontDialog::NewFontDialog(GUI::Window* parent_window)
: GUI::WizardDialog(parent_window)
ErrorOr<NonnullRefPtr<NewFontDialog>> 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<void> 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<GUI::TextBox>("name_textbox");
m_family_textbox = m_font_properties_page->body_widget().find_descendant_of_type_named<GUI::TextBox>("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<GUI::ComboBox>("slope_combobox");
m_presentation_spinbox = m_font_properties_page->body_widget().find_descendant_of_type_named<GUI::SpinBox>("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<DeprecatedString>::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<String>::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<DeprecatedString>::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<String>::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<GUI::SpinBox>("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()

View file

@ -12,13 +12,17 @@
#include <LibGfx/Font/BitmapFont.h>
class NewFontDialog final : public GUI::WizardDialog {
C_OBJECT(NewFontDialog);
C_OBJECT_ABSTRACT(NewFontDialog);
public:
static ErrorOr<NonnullRefPtr<NewFontDialog>> create(GUI::Window* parent_window);
ErrorOr<NonnullRefPtr<Gfx::BitmapFont>> create_font();
private:
NewFontDialog(GUI::Window* parent_window);
explicit NewFontDialog(GUI::Window* parent_window);
virtual ErrorOr<void> build() override;
void save_metadata();
@ -51,6 +55,6 @@ private:
RefPtr<GUI::SpinBox> m_spacing_spinbox;
RefPtr<GUI::CheckBox> m_fixed_width_checkbox;
Vector<DeprecatedString> m_font_weight_list;
Vector<DeprecatedString> m_font_slope_list;
Vector<String> m_font_weight_list;
Vector<String> m_font_slope_list;
};

View file

@ -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<void> 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<void> 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; };

View file

@ -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<Vector<NonnullRefPtr<Sheet>>, 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<Vector<NonnullRefPtr<Sheet>>, 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; };

View file

@ -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<GUI::TextBox>("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<GUI::Progressbar>("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);

View file

@ -13,36 +13,44 @@
namespace GUI {
CoverWizardPage::CoverWizardPage()
: AbstractWizardPage()
ErrorOr<NonnullRefPtr<CoverWizardPage>> 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<void> CoverWizardPage::build(String title, String subtitle)
{
set_fill_with_background_color(true);
set_background_role(Gfx::ColorRole::Base);
set_layout<HorizontalBoxLayout>();
m_banner_image_widget = add<ImageWidget>();
TRY(try_set_layout<HorizontalBoxLayout>());
m_banner_image_widget = TRY(try_add<ImageWidget>());
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<Widget>();
m_content_widget->set_layout<VerticalBoxLayout>(20);
m_content_widget = TRY(try_add<Widget>());
TRY(m_content_widget->try_set_layout<VerticalBoxLayout>(20));
m_header_label = m_content_widget->add<Label>();
m_header_label = TRY(m_content_widget->try_add<Label>(move(title)));
m_header_label->set_font(Gfx::FontDatabase::the().get("Pebbleton", 14, 700, Gfx::FontWidth::Normal, 0));
m_header_label->set_text_alignment(Gfx::TextAlignment::TopLeft);
m_header_label->set_fixed_height(48);
m_body_label = m_content_widget->add<Label>();
m_body_label = TRY(m_content_widget->try_add<Label>(move(subtitle)));
m_body_label->set_text_alignment(Gfx::TextAlignment::TopLeft);
return {};
}
void CoverWizardPage::set_header_text(DeprecatedString const& text)
void CoverWizardPage::set_header_text(String text)
{
m_header_label->set_text(String::from_deprecated_string(text).release_value_but_fixme_should_propagate_errors());
m_header_label->set_text(move(text));
}
void CoverWizardPage::set_body_text(DeprecatedString const& text)
void CoverWizardPage::set_body_text(String text)
{
m_body_label->set_text(String::from_deprecated_string(text).release_value_but_fixme_should_propagate_errors());
m_body_label->set_text(move(text));
}
}

View file

@ -14,15 +14,20 @@
namespace GUI {
class CoverWizardPage : public AbstractWizardPage {
C_OBJECT(CoverWizardPage);
C_OBJECT_ABSTRACT(CoverWizardPage);
static ErrorOr<NonnullRefPtr<CoverWizardPage>> create(StringView title, StringView subtitle);
ImageWidget& banner_image_widget() { return *m_banner_image_widget; }
void set_header_text(DeprecatedString const& text);
void set_body_text(DeprecatedString const& text);
void set_header_text(String);
void set_body_text(String);
protected:
virtual ErrorOr<void> build(String title, String subtitle);
private:
explicit CoverWizardPage();
CoverWizardPage() = default;
RefPtr<ImageWidget> m_banner_image_widget;
RefPtr<Widget> m_content_widget;

View file

@ -16,39 +16,37 @@
namespace GUI {
WizardDialog::WizardDialog(Window* parent_window)
: Dialog(parent_window)
, m_page_stack()
ErrorOr<NonnullRefPtr<WizardDialog>> WizardDialog::create(Window* parent_window)
{
resize(500, 360);
set_title(DeprecatedString::formatted("Sample wizard"));
set_resizable(false);
auto dialog = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WizardDialog(parent_window)));
TRY(dialog->build());
return dialog;
}
if (parent_window)
set_icon(parent_window->icon());
auto main_widget = set_main_widget<Widget>().release_value_but_fixme_should_propagate_errors();
ErrorOr<void> WizardDialog::build()
{
auto main_widget = TRY(set_main_widget<Widget>());
main_widget->set_fill_with_background_color(true);
main_widget->set_layout<VerticalBoxLayout>(GUI::Margins {}, 0);
TRY(main_widget->try_set_layout<VerticalBoxLayout>(Margins {}, 0));
m_page_container_widget = main_widget->add<Widget>();
m_page_container_widget = TRY(main_widget->try_add<Widget>());
m_page_container_widget->set_fixed_size(500, 315);
m_page_container_widget->set_layout<VerticalBoxLayout>();
TRY(m_page_container_widget->try_set_layout<VerticalBoxLayout>());
auto& separator = main_widget->add<SeparatorWidget>(Gfx::Orientation::Horizontal);
separator.set_fixed_height(2);
auto separator = TRY(main_widget->try_add<SeparatorWidget>(Gfx::Orientation::Horizontal));
separator->set_fixed_height(2);
auto& nav_container_widget = main_widget->add<Widget>();
nav_container_widget.set_layout<HorizontalBoxLayout>(GUI::Margins { 0, 10 }, 0);
nav_container_widget.set_fixed_height(42);
nav_container_widget.add_spacer().release_value_but_fixme_should_propagate_errors();
auto nav_container_widget = TRY(main_widget->try_add<Widget>());
TRY(nav_container_widget->try_set_layout<HorizontalBoxLayout>(Margins { 0, 10 }, 0));
nav_container_widget->set_fixed_height(42);
TRY(nav_container_widget->add_spacer());
m_back_button = nav_container_widget.add<DialogButton>("< Back"_short_string);
m_back_button = TRY(nav_container_widget->try_add<DialogButton>("< Back"_short_string));
m_back_button->on_click = [&](auto) {
pop_page();
};
m_next_button = nav_container_widget.add<DialogButton>("Next >"_short_string);
m_next_button = TRY(nav_container_widget->try_add<DialogButton>("Next >"_short_string));
m_next_button->on_click = [&](auto) {
VERIFY(has_pages());
@ -62,15 +60,25 @@ WizardDialog::WizardDialog(Window* parent_window)
push_page(*next_page);
};
auto& button_spacer = nav_container_widget.add<Widget>();
button_spacer.set_fixed_width(10);
auto button_spacer = TRY(nav_container_widget->try_add<Widget>());
button_spacer->set_fixed_width(10);
m_cancel_button = nav_container_widget.add<DialogButton>("Cancel"_short_string);
m_cancel_button = TRY(nav_container_widget->try_add<DialogButton>("Cancel"_short_string));
m_cancel_button->on_click = [&](auto) {
handle_cancel();
};
update_navigation();
return {};
}
WizardDialog::WizardDialog(Window* parent_window)
: Dialog(parent_window)
, m_page_stack()
{
resize(500, 360);
set_resizable(false);
}
void WizardDialog::push_page(AbstractWizardPage& page)

View file

@ -13,16 +13,11 @@
namespace GUI {
class WizardDialog : public Dialog {
C_OBJECT(WizardDialog)
C_OBJECT_ABSTRACT(WizardDialog)
public:
virtual ~WizardDialog() override = default;
static void show(AbstractWizardPage& first_page, Window* parent_window = nullptr)
{
auto dialog = WizardDialog::construct(parent_window);
dialog->push_page(first_page);
dialog->exec();
}
static ErrorOr<NonnullRefPtr<WizardDialog>> create(Window* parent_window);
Function<void()> on_cancel;
@ -36,8 +31,9 @@ public:
inline bool has_pages() const { return !m_page_stack.is_empty(); }
protected:
WizardDialog(Window* parent_window);
explicit WizardDialog(Window* parent_window);
virtual ErrorOr<void> build();
virtual void handle_cancel();
private:

View file

@ -14,41 +14,50 @@
namespace GUI {
WizardPage::WizardPage(DeprecatedString const& title_text, DeprecatedString const& subtitle_text)
: AbstractWizardPage()
ErrorOr<NonnullRefPtr<WizardPage>> WizardPage::create(StringView title, StringView subtitle)
{
set_layout<VerticalBoxLayout>(GUI::Margins {}, 0);
auto page = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WizardPage()));
TRY(page->build(TRY(String::from_utf8(title)), TRY(String::from_utf8(subtitle))));
return page;
}
auto& header_widget = add<Widget>();
header_widget.set_fill_with_background_color(true);
header_widget.set_background_role(Gfx::ColorRole::Base);
header_widget.set_fixed_height(58);
ErrorOr<void> WizardPage::build(String title, String subtitle)
{
TRY(try_set_layout<VerticalBoxLayout>(Margins {}, 0));
header_widget.set_layout<VerticalBoxLayout>(GUI::Margins { 15, 30, 0 });
m_title_label = header_widget.add<Label>(String::from_deprecated_string(title_text).release_value_but_fixme_should_propagate_errors());
auto header_widget = TRY(try_add<Widget>());
header_widget->set_fill_with_background_color(true);
header_widget->set_background_role(Gfx::ColorRole::Base);
header_widget->set_fixed_height(58);
TRY(header_widget->try_set_layout<VerticalBoxLayout>(Margins { 15, 30, 0 }));
m_title_label = TRY(header_widget->try_add<Label>(move(title)));
m_title_label->set_font(Gfx::FontDatabase::default_font().bold_variant());
m_title_label->set_fixed_height(m_title_label->font().pixel_size_rounded_up() + 2);
m_title_label->set_text_alignment(Gfx::TextAlignment::TopLeft);
m_subtitle_label = header_widget.add<Label>(String::from_deprecated_string(subtitle_text).release_value_but_fixme_should_propagate_errors());
m_subtitle_label = TRY(header_widget->try_add<Label>(move(subtitle)));
m_subtitle_label->set_text_alignment(Gfx::TextAlignment::TopLeft);
m_subtitle_label->set_fixed_height(m_subtitle_label->font().pixel_size_rounded_up());
header_widget.add_spacer().release_value_but_fixme_should_propagate_errors();
TRY(header_widget->add_spacer());
auto& separator = add<SeparatorWidget>(Gfx::Orientation::Horizontal);
separator.set_fixed_height(2);
auto separator = TRY(try_add<SeparatorWidget>(Gfx::Orientation::Horizontal));
separator->set_fixed_height(2);
m_body_widget = add<Widget>();
m_body_widget->set_layout<VerticalBoxLayout>(20);
m_body_widget = TRY(try_add<Widget>());
TRY(m_body_widget->try_set_layout<VerticalBoxLayout>(20));
return {};
}
void WizardPage::set_page_title(DeprecatedString const& text)
void WizardPage::set_page_title(String text)
{
m_title_label->set_text(String::from_deprecated_string(text).release_value_but_fixme_should_propagate_errors());
m_title_label->set_text(move(text));
}
void WizardPage::set_page_subtitle(DeprecatedString const& text)
void WizardPage::set_page_subtitle(String text)
{
m_subtitle_label->set_text(String::from_deprecated_string(text).release_value_but_fixme_should_propagate_errors());
m_subtitle_label->set_text(move(text));
}
}

View file

@ -14,15 +14,20 @@
namespace GUI {
class WizardPage : public AbstractWizardPage {
C_OBJECT(WizardPage);
C_OBJECT_ABSTRACT(WizardPage);
static ErrorOr<NonnullRefPtr<WizardPage>> create(StringView title, StringView subtitle);
Widget& body_widget() { return *m_body_widget; };
void set_page_title(DeprecatedString const& text);
void set_page_subtitle(DeprecatedString const& text);
void set_page_title(String);
void set_page_subtitle(String);
protected:
virtual ErrorOr<void> build(String title, String subtitle);
private:
explicit WizardPage(DeprecatedString const& title_text, DeprecatedString const& subtitle_text);
WizardPage() = default;
RefPtr<Widget> m_body_widget;