1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 08:37:45 +00:00

ThemeEditor: Make GUI initialization fallible

To do this, all GUI logic is moved into try_create() factory functions.
This commit is contained in:
Sam Atkins 2022-12-30 12:13:23 +00:00 committed by Tim Flynn
parent fa98034ff7
commit 59d9d1d07a
4 changed files with 78 additions and 47 deletions

View file

@ -206,21 +206,30 @@ static const PropertyTab color_scheme_tab {
} }
}; };
MainWidget::MainWidget() ErrorOr<NonnullRefPtr<MainWidget>> MainWidget::try_create()
: m_current_palette(GUI::Application::the()->palette())
{ {
load_from_gml(theme_editor_gml); auto alignment_model = TRY(AlignmentModel::try_create());
m_alignment_model = MUST(AlignmentModel::try_create()); auto main_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MainWidget(move(alignment_model))));
m_preview_widget = find_descendant_of_type_named<ThemeEditor::PreviewWidget>("preview_widget"); TRY(main_widget->try_load_from_gml(theme_editor_gml));
m_property_tabs = find_descendant_of_type_named<GUI::TabWidget>("property_tabs"); main_widget->m_preview_widget = main_widget->find_descendant_of_type_named<ThemeEditor::PreviewWidget>("preview_widget");
add_property_tab(window_tab); main_widget->m_property_tabs = main_widget->find_descendant_of_type_named<GUI::TabWidget>("property_tabs");
add_property_tab(widgets_tab);
add_property_tab(syntax_highlighting_tab);
add_property_tab(color_scheme_tab);
build_override_controls(); TRY(main_widget->add_property_tab(window_tab));
TRY(main_widget->add_property_tab(widgets_tab));
TRY(main_widget->add_property_tab(syntax_highlighting_tab));
TRY(main_widget->add_property_tab(color_scheme_tab));
main_widget->build_override_controls();
return main_widget;
}
MainWidget::MainWidget(NonnullRefPtr<AlignmentModel> alignment_model)
: m_current_palette(GUI::Application::the()->palette())
, m_alignment_model(move(alignment_model))
{
} }
ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window) ErrorOr<void> MainWidget::initialize_menubar(GUI::Window& window)
@ -418,31 +427,31 @@ void MainWidget::build_override_controls()
}; };
} }
void MainWidget::add_property_tab(PropertyTab const& property_tab) ErrorOr<void> MainWidget::add_property_tab(PropertyTab const& property_tab)
{ {
auto& scrollable_container = m_property_tabs->add_tab<GUI::ScrollableContainerWidget>(property_tab.title); auto scrollable_container = TRY(m_property_tabs->try_add_tab<GUI::ScrollableContainerWidget>(property_tab.title));
scrollable_container.set_should_hide_unnecessary_scrollbars(true); scrollable_container->set_should_hide_unnecessary_scrollbars(true);
auto properties_list = GUI::Widget::construct(); auto properties_list = TRY(GUI::Widget::try_create());
scrollable_container.set_widget(properties_list); scrollable_container->set_widget(properties_list);
properties_list->set_layout<GUI::VerticalBoxLayout>(); (void)TRY(properties_list->try_set_layout<GUI::VerticalBoxLayout>());
properties_list->layout()->set_spacing(12); properties_list->layout()->set_spacing(12);
properties_list->layout()->set_margins({ 8 }); properties_list->layout()->set_margins({ 8 });
for (auto const& group : property_tab.property_groups) { for (auto const& group : property_tab.property_groups) {
NonnullRefPtr<GUI::GroupBox> group_box = properties_list->add<GUI::GroupBox>(group.title); NonnullRefPtr<GUI::GroupBox> group_box = TRY(properties_list->try_add<GUI::GroupBox>(group.title));
group_box->set_layout<GUI::VerticalBoxLayout>(); (void)TRY(group_box->try_set_layout<GUI::VerticalBoxLayout>());
group_box->layout()->set_spacing(12); group_box->layout()->set_spacing(12);
// 1px less on the left makes the text line up with the group title. // 1px less on the left makes the text line up with the group title.
group_box->layout()->set_margins({ 8, 8, 8, 7 }); group_box->layout()->set_margins({ 8, 8, 8, 7 });
group_box->set_preferred_height(GUI::SpecialDimension::Fit); group_box->set_preferred_height(GUI::SpecialDimension::Fit);
for (auto const& property : group.properties) { for (auto const& property : group.properties) {
NonnullRefPtr<GUI::Widget> row_widget = group_box->add<GUI::Widget>(); NonnullRefPtr<GUI::Widget> row_widget = TRY(group_box->try_add<GUI::Widget>());
row_widget->set_fixed_height(22); row_widget->set_fixed_height(22);
property.role.visit( TRY(property.role.visit(
[&](Gfx::AlignmentRole role) { [&](Gfx::AlignmentRole role) -> ErrorOr<void> {
row_widget->load_from_gml(alignment_property_gml); TRY(row_widget->try_load_from_gml(alignment_property_gml));
auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name"); auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
name_label.set_text(to_string(role)); name_label.set_text(to_string(role));
@ -456,9 +465,10 @@ void MainWidget::add_property_tab(PropertyTab const& property_tab)
VERIFY(m_alignment_inputs[to_underlying(role)].is_null()); VERIFY(m_alignment_inputs[to_underlying(role)].is_null());
m_alignment_inputs[to_underlying(role)] = alignment_picker; m_alignment_inputs[to_underlying(role)] = alignment_picker;
return {};
}, },
[&](Gfx::ColorRole role) { [&](Gfx::ColorRole role) -> ErrorOr<void> {
row_widget->load_from_gml(color_property_gml); TRY(row_widget->try_load_from_gml(color_property_gml));
auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name"); auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
name_label.set_text(to_string(role)); name_label.set_text(to_string(role));
@ -471,9 +481,10 @@ void MainWidget::add_property_tab(PropertyTab const& property_tab)
VERIFY(m_color_inputs[to_underlying(role)].is_null()); VERIFY(m_color_inputs[to_underlying(role)].is_null());
m_color_inputs[to_underlying(role)] = color_input; m_color_inputs[to_underlying(role)] = color_input;
return {};
}, },
[&](Gfx::FlagRole role) { [&](Gfx::FlagRole role) -> ErrorOr<void> {
row_widget->load_from_gml(flag_property_gml); TRY(row_widget->try_load_from_gml(flag_property_gml));
auto& checkbox = *row_widget->find_descendant_of_type_named<GUI::CheckBox>("checkbox"); auto& checkbox = *row_widget->find_descendant_of_type_named<GUI::CheckBox>("checkbox");
checkbox.set_text(to_string(role)); checkbox.set_text(to_string(role));
@ -484,9 +495,10 @@ void MainWidget::add_property_tab(PropertyTab const& property_tab)
VERIFY(m_flag_inputs[to_underlying(role)].is_null()); VERIFY(m_flag_inputs[to_underlying(role)].is_null());
m_flag_inputs[to_underlying(role)] = checkbox; m_flag_inputs[to_underlying(role)] = checkbox;
return {};
}, },
[&](Gfx::MetricRole role) { [&](Gfx::MetricRole role) -> ErrorOr<void> {
row_widget->load_from_gml(metric_property_gml); TRY(row_widget->try_load_from_gml(metric_property_gml));
auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name"); auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
name_label.set_text(to_string(role)); name_label.set_text(to_string(role));
@ -499,9 +511,10 @@ void MainWidget::add_property_tab(PropertyTab const& property_tab)
VERIFY(m_metric_inputs[to_underlying(role)].is_null()); VERIFY(m_metric_inputs[to_underlying(role)].is_null());
m_metric_inputs[to_underlying(role)] = spin_box; m_metric_inputs[to_underlying(role)] = spin_box;
return {};
}, },
[&](Gfx::PathRole role) { [&](Gfx::PathRole role) -> ErrorOr<void> {
row_widget->load_from_gml(path_property_gml); TRY(row_widget->try_load_from_gml(path_property_gml));
auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name"); auto& name_label = *row_widget->find_descendant_of_type_named<GUI::Label>("name");
name_label.set_text(to_string(role)); name_label.set_text(to_string(role));
@ -520,9 +533,12 @@ void MainWidget::add_property_tab(PropertyTab const& property_tab)
VERIFY(m_path_inputs[to_underlying(role)].is_null()); VERIFY(m_path_inputs[to_underlying(role)].is_null());
m_path_inputs[to_underlying(role)] = path_input; m_path_inputs[to_underlying(role)] = path_input;
}); return {};
}));
} }
} }
return {};
} }
void MainWidget::set_alignment(Gfx::AlignmentRole role, Gfx::TextAlignment value) void MainWidget::set_alignment(Gfx::AlignmentRole role, Gfx::TextAlignment value)

View file

@ -77,9 +77,10 @@ struct PropertyTab {
}; };
class MainWidget final : public GUI::Widget { class MainWidget final : public GUI::Widget {
C_OBJECT(MainWidget); C_OBJECT_ABSTRACT(MainWidget);
public: public:
static ErrorOr<NonnullRefPtr<MainWidget>> try_create();
virtual ~MainWidget() override = default; virtual ~MainWidget() override = default;
ErrorOr<void> initialize_menubar(GUI::Window&); ErrorOr<void> initialize_menubar(GUI::Window&);
@ -88,7 +89,7 @@ public:
ErrorOr<void> load_from_file(Core::File&); ErrorOr<void> load_from_file(Core::File&);
private: private:
MainWidget(); explicit MainWidget(NonnullRefPtr<AlignmentModel>);
void save_to_file(Core::File&); void save_to_file(Core::File&);
ErrorOr<Core::AnonymousBuffer> encode(); ErrorOr<Core::AnonymousBuffer> encode();
@ -96,7 +97,7 @@ private:
void build_override_controls(); void build_override_controls();
void add_property_tab(PropertyTab const&); ErrorOr<void> add_property_tab(PropertyTab const&);
void set_alignment(Gfx::AlignmentRole, Gfx::TextAlignment); void set_alignment(Gfx::AlignmentRole, Gfx::TextAlignment);
void set_color(Gfx::ColorRole, Gfx::Color); void set_color(Gfx::ColorRole, Gfx::Color);
void set_flag(Gfx::FlagRole, bool); void set_flag(Gfx::FlagRole, bool);

View file

@ -23,9 +23,22 @@ REGISTER_WIDGET(ThemeEditor, PreviewWidget);
namespace ThemeEditor { namespace ThemeEditor {
class MiniWidgetGallery final : public GUI::Widget { class MiniWidgetGallery final : public GUI::Widget {
C_OBJECT(MiniWidgetGallery); C_OBJECT_ABSTRACT(MiniWidgetGallery);
public: public:
static ErrorOr<NonnullRefPtr<MiniWidgetGallery>> try_create()
{
auto gallery = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) MiniWidgetGallery()));
TRY(gallery->try_load_from_gml(window_preview_gml));
gallery->for_each_child_widget([](auto& child) {
child.set_focus_policy(GUI::FocusPolicy::NoFocus);
return IterationDecision::Continue;
});
return gallery;
}
void set_preview_palette(Gfx::Palette const& palette) void set_preview_palette(Gfx::Palette const& palette)
{ {
set_palette(palette); set_palette(palette);
@ -42,19 +55,19 @@ public:
private: private:
MiniWidgetGallery() MiniWidgetGallery()
{ {
load_from_gml(window_preview_gml);
for_each_child_widget([](auto& child) {
child.set_focus_policy(GUI::FocusPolicy::NoFocus);
return IterationDecision::Continue;
});
} }
}; };
PreviewWidget::PreviewWidget(Gfx::Palette const& initial_preview_palette) ErrorOr<NonnullRefPtr<PreviewWidget>> PreviewWidget::try_create()
: GUI::AbstractThemePreview(initial_preview_palette) {
auto preview_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) PreviewWidget()));
preview_widget->m_gallery = TRY(preview_widget->try_add<MiniWidgetGallery>());
return preview_widget;
}
PreviewWidget::PreviewWidget()
: GUI::AbstractThemePreview(GUI::Application::the()->palette())
{ {
m_gallery = add<MiniWidgetGallery>();
set_greedy_for_hits(true); set_greedy_for_hits(true);
} }

View file

@ -24,15 +24,16 @@ class MiniWidgetGallery;
class PreviewWidget final class PreviewWidget final
: public GUI::AbstractThemePreview : public GUI::AbstractThemePreview
, public GUI::ColorFilterer { , public GUI::ColorFilterer {
C_OBJECT(PreviewWidget); C_OBJECT_ABSTRACT(PreviewWidget);
public: public:
static ErrorOr<NonnullRefPtr<PreviewWidget>> try_create();
virtual ~PreviewWidget() override = default; virtual ~PreviewWidget() override = default;
virtual void set_color_filter(OwnPtr<Gfx::ColorBlindnessFilter>) override; virtual void set_color_filter(OwnPtr<Gfx::ColorBlindnessFilter>) override;
private: private:
explicit PreviewWidget(Gfx::Palette const& = GUI::Application::the()->palette()); PreviewWidget();
virtual void paint_preview(GUI::PaintEvent&) override; virtual void paint_preview(GUI::PaintEvent&) override;
virtual void second_paint_event(GUI::PaintEvent&) override; virtual void second_paint_event(GUI::PaintEvent&) override;