1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:17:35 +00:00

BrowserSettings: Propagate errors from creation of content filter widget

This commit is contained in:
Timothy Flynn 2023-04-21 08:19:10 -04:00 committed by Andreas Kling
parent 8352417278
commit 8da0a34515
3 changed files with 42 additions and 29 deletions

View file

@ -117,42 +117,52 @@ void DomainListModel::reset_default_values()
did_update(UpdateFlag::InvalidateAllIndices); did_update(UpdateFlag::InvalidateAllIndices);
} }
ContentFilterSettingsWidget::ContentFilterSettingsWidget() ErrorOr<NonnullRefPtr<ContentFilterSettingsWidget>> ContentFilterSettingsWidget::create()
{ {
load_from_gml(content_filter_settings_widget_gml).release_value_but_fixme_should_propagate_errors(); auto domain_list_model = TRY(try_make_ref_counted<DomainListModel>());
m_enable_content_filtering_checkbox = find_descendant_of_type_named<GUI::CheckBox>("enable_content_filtering_checkbox"); TRY(domain_list_model->load());
m_domain_list_view = find_descendant_of_type_named<GUI::ListView>("domain_list_view");
m_add_new_domain_button = find_descendant_of_type_named<GUI::Button>("add_new_domain_button");
m_enable_content_filtering_checkbox->set_checked(Config::read_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, s_default_enable_content_filtering), GUI::AllowCallback::No); auto widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ContentFilterSettingsWidget(move(domain_list_model))));
m_enable_content_filtering_checkbox->on_checked = [&](auto) { set_modified(true); }; TRY(widget->load_from_gml(content_filter_settings_widget_gml));
m_add_new_domain_button->on_click = [&](unsigned) { widget->m_enable_content_filtering_checkbox = widget->find_descendant_of_type_named<GUI::CheckBox>("enable_content_filtering_checkbox");
widget->m_enable_content_filtering_checkbox->set_checked(Config::read_bool("Browser"sv, "Preferences"sv, "EnableContentFilters"sv, s_default_enable_content_filtering), GUI::AllowCallback::No);
widget->m_enable_content_filtering_checkbox->on_checked = [widget](auto) {
widget->set_modified(true);
};
widget->m_domain_list_view = widget->find_descendant_of_type_named<GUI::ListView>("domain_list_view");
widget->m_domain_list_view->set_model(widget->m_domain_list_model);
widget->m_domain_list_view->on_context_menu_request = [widget](GUI::ModelIndex const& index, GUI::ContextMenuEvent const& event) {
widget->m_domain_list_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
widget->m_entry_context_menu->popup(event.screen_position());
};
widget->m_add_new_domain_button = widget->find_descendant_of_type_named<GUI::Button>("add_new_domain_button");
widget->m_add_new_domain_button->on_click = [widget](unsigned) {
String text; String text;
if (GUI::InputBox::show(window(), text, "Domain:"sv, "Add domain to Content Filter"sv, GUI::InputType::NonemptyText) == GUI::Dialog::ExecResult::OK) { if (GUI::InputBox::show(widget->window(), text, "Domain:"sv, "Add domain to Content Filter"sv, GUI::InputType::NonemptyText) == GUI::Dialog::ExecResult::OK) {
m_domain_list_model->add_domain(move(text)); widget->m_domain_list_model->add_domain(move(text));
set_modified(true); widget->set_modified(true);
} }
}; };
m_domain_list_model = make_ref_counted<DomainListModel>(); auto delete_action = GUI::CommonActions::make_delete_action([widget](GUI::Action const&) {
m_domain_list_model->load().release_value_but_fixme_should_propagate_errors(); if (!widget->m_domain_list_view->selection().is_empty()) {
m_domain_list_view->set_model(m_domain_list_model); widget->m_domain_list_model->delete_domain(widget->m_domain_list_view->selection().first().row());
widget->set_modified(true);
auto delete_action = GUI::CommonActions::make_delete_action([&](GUI::Action const&) {
if (!m_domain_list_view->selection().is_empty()) {
m_domain_list_model->delete_domain(m_domain_list_view->selection().first().row());
set_modified(true);
} }
}); });
m_entry_context_menu = GUI::Menu::construct(); widget->m_entry_context_menu = GUI::Menu::construct();
m_entry_context_menu->add_action(delete_action); widget->m_entry_context_menu->add_action(delete_action);
m_domain_list_view->on_context_menu_request = [&](GUI::ModelIndex const& index, GUI::ContextMenuEvent const& event) { return widget;
m_domain_list_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set); }
m_entry_context_menu->popup(event.screen_position());
}; ContentFilterSettingsWidget::ContentFilterSettingsWidget(NonnullRefPtr<DomainListModel> domain_list_model)
: m_domain_list_model(move(domain_list_model))
{
} }
void ContentFilterSettingsWidget::apply_settings() void ContentFilterSettingsWidget::apply_settings()

View file

@ -31,17 +31,20 @@ protected:
}; };
class ContentFilterSettingsWidget : public GUI::SettingsWindow::Tab { class ContentFilterSettingsWidget : public GUI::SettingsWindow::Tab {
C_OBJECT(ContentFilterSettingsWidget) C_OBJECT_ABSTRACT(ContentFilterSettingsWidget)
public: public:
static ErrorOr<NonnullRefPtr<ContentFilterSettingsWidget>> create();
virtual void apply_settings() override; virtual void apply_settings() override;
virtual void reset_default_values() override; virtual void reset_default_values() override;
private: private:
ContentFilterSettingsWidget(); explicit ContentFilterSettingsWidget(NonnullRefPtr<DomainListModel>);
RefPtr<GUI::Menu> m_entry_context_menu; RefPtr<GUI::Menu> m_entry_context_menu;
RefPtr<GUI::CheckBox> m_enable_content_filtering_checkbox; RefPtr<GUI::CheckBox> m_enable_content_filtering_checkbox;
RefPtr<GUI::Button> m_add_new_domain_button; RefPtr<GUI::Button> m_add_new_domain_button;
RefPtr<GUI::ListView> m_domain_list_view; RefPtr<GUI::ListView> m_domain_list_view;
RefPtr<DomainListModel> m_domain_list_model; NonnullRefPtr<DomainListModel> m_domain_list_model;
}; };

View file

@ -37,7 +37,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto window = TRY(GUI::SettingsWindow::create("Browser Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes)); auto window = TRY(GUI::SettingsWindow::create("Browser Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes));
window->set_icon(app_icon.bitmap_for_size(16)); window->set_icon(app_icon.bitmap_for_size(16));
(void)TRY(window->add_tab<BrowserSettingsWidget>("Browser"_short_string, "browser"sv)); (void)TRY(window->add_tab<BrowserSettingsWidget>("Browser"_short_string, "browser"sv));
(void)TRY(window->add_tab<ContentFilterSettingsWidget>(TRY("Content Filtering"_string), "content-filtering"sv)); (void)TRY(window->add_tab(TRY(ContentFilterSettingsWidget::create()), TRY("Content Filtering"_string), "content-filtering"sv));
(void)TRY(window->add_tab(TRY(AutoplaySettingsWidget::create()), TRY("Autoplay"_string), "autoplay"sv)); (void)TRY(window->add_tab(TRY(AutoplaySettingsWidget::create()), TRY("Autoplay"_string), "autoplay"sv));
window->set_active_tab(selected_tab); window->set_active_tab(selected_tab);