From e9ad7b8eed79f4aabb29e68d4ea469b7431cf1fb Mon Sep 17 00:00:00 2001 From: Cameron Youell Date: Mon, 17 Apr 2023 15:59:38 +1000 Subject: [PATCH] BrowserSettings: Convert `BrowserSettingsWidget` to a failable factory --- .../BrowserSettings/BrowserSettingsWidget.cpp | 17 ++++++++++++++--- .../BrowserSettings/BrowserSettingsWidget.h | 5 +++-- Userland/Applications/BrowserSettings/main.cpp | 3 ++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp index 432b53bb887..c058a079848 100644 --- a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp +++ b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2021-2022, Sam Atkins + * Copyright (c) 2023, Cameron Youell * * SPDX-License-Identifier: BSD-2-Clause */ @@ -57,10 +58,18 @@ private: Vector m_color_schemes; }; -BrowserSettingsWidget::BrowserSettingsWidget() +ErrorOr> BrowserSettingsWidget::create() { - load_from_gml(browser_settings_widget_gml).release_value_but_fixme_should_propagate_errors(); + auto widget = TRY(try_make_ref_counted()); + TRY(widget->load_from_gml(browser_settings_widget_gml)); + TRY(widget->setup()); + + return widget; +} + +ErrorOr BrowserSettingsWidget::setup() +{ m_homepage_url_textbox = find_descendant_of_type_named("homepage_url_textbox"); m_homepage_url_textbox->set_text(Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, default_homepage_url), GUI::AllowCallback::No); m_homepage_url_textbox->on_change = [&]() { set_modified(true); }; @@ -101,7 +110,7 @@ BrowserSettingsWidget::BrowserSettingsWidget() Vector custom_search_engine; custom_search_engine.append("Custom..."); custom_search_engine.append(""); - search_engines_model->add(move(custom_search_engine)); + TRY(search_engines_model->add(move(custom_search_engine))); m_search_engine_combobox->set_model(move(search_engines_model)); m_search_engine_combobox->set_only_allow_values_from_model(true); @@ -116,6 +125,8 @@ BrowserSettingsWidget::BrowserSettingsWidget() m_auto_close_download_windows_checkbox = find_descendant_of_type_named("auto_close_download_windows_checkbox"); m_auto_close_download_windows_checkbox->set_checked(Config::read_bool("Browser"sv, "Preferences"sv, "CloseDownloadWidgetOnFinish"sv, default_auto_close_download_windows), GUI::AllowCallback::No); m_auto_close_download_windows_checkbox->on_checked = [&](auto) { set_modified(true); }; + + return {}; } void BrowserSettingsWidget::set_color_scheme(StringView color_scheme) diff --git a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h index 2743fe2336c..702b2bf1dfb 100644 --- a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h +++ b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h @@ -13,15 +13,16 @@ #include class BrowserSettingsWidget final : public GUI::SettingsWindow::Tab { - C_OBJECT(BrowserSettingsWidget) + C_OBJECT_ABSTRACT(BrowserSettingsWidget) public: + static ErrorOr> create(); virtual ~BrowserSettingsWidget() override = default; virtual void apply_settings() override; virtual void reset_default_values() override; private: - BrowserSettingsWidget(); + ErrorOr setup(); RefPtr m_homepage_url_textbox; RefPtr m_new_tab_url_textbox; diff --git a/Userland/Applications/BrowserSettings/main.cpp b/Userland/Applications/BrowserSettings/main.cpp index d40b6c34082..9fa6b1d7d35 100644 --- a/Userland/Applications/BrowserSettings/main.cpp +++ b/Userland/Applications/BrowserSettings/main.cpp @@ -36,7 +36,8 @@ ErrorOr serenity_main(Main::Arguments arguments) auto window = TRY(GUI::SettingsWindow::create("Browser Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes)); window->set_icon(app_icon.bitmap_for_size(16)); - (void)TRY(window->add_tab("Browser"_short_string, "browser"sv)); + + (void)TRY(window->add_tab(TRY(BrowserSettingsWidget::create()), "Browser"_short_string, "browser"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)); window->set_active_tab(selected_tab);