BrowserSettings: Convert BrowserSettingsWidget to a failable factory

This commit is contained in:
Cameron Youell 2023-04-17 15:59:38 +10:00 committed by Andreas Kling
parent 2df0eeaa20
commit e9ad7b8eed
Notes: sideshowbarker 2024-07-17 09:49:48 +09:00
3 changed files with 19 additions and 6 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -57,10 +58,18 @@ private:
Vector<ColorScheme> m_color_schemes;
};
BrowserSettingsWidget::BrowserSettingsWidget()
ErrorOr<NonnullRefPtr<BrowserSettingsWidget>> BrowserSettingsWidget::create()
{
load_from_gml(browser_settings_widget_gml).release_value_but_fixme_should_propagate_errors();
auto widget = TRY(try_make_ref_counted<BrowserSettingsWidget>());
TRY(widget->load_from_gml(browser_settings_widget_gml));
TRY(widget->setup());
return widget;
}
ErrorOr<void> BrowserSettingsWidget::setup()
{
m_homepage_url_textbox = find_descendant_of_type_named<GUI::TextBox>("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<JsonValue> 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<GUI::CheckBox>("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)

View file

@ -13,15 +13,16 @@
#include <LibGUI/TextBox.h>
class BrowserSettingsWidget final : public GUI::SettingsWindow::Tab {
C_OBJECT(BrowserSettingsWidget)
C_OBJECT_ABSTRACT(BrowserSettingsWidget)
public:
static ErrorOr<NonnullRefPtr<BrowserSettingsWidget>> create();
virtual ~BrowserSettingsWidget() override = default;
virtual void apply_settings() override;
virtual void reset_default_values() override;
private:
BrowserSettingsWidget();
ErrorOr<void> setup();
RefPtr<GUI::TextBox> m_homepage_url_textbox;
RefPtr<GUI::TextBox> m_new_tab_url_textbox;

View file

@ -36,7 +36,8 @@ ErrorOr<int> 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<BrowserSettingsWidget>("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);