From e84b3e71106a843c93255b5ae8f59d6838ace488 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 25 Nov 2021 13:58:05 +0000 Subject: [PATCH] BrowserSettings: Implement restoring default values --- .../BrowserSettings/BrowserSettingsWidget.cpp | 68 +++++++++++++------ .../BrowserSettings/BrowserSettingsWidget.h | 3 + .../Applications/BrowserSettings/main.cpp | 2 +- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp index 9e20bb12948..8339473ac2b 100644 --- a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp +++ b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.cpp @@ -10,6 +10,12 @@ #include #include +static String default_homepage_url = "file:///res/html/misc/welcome.html"; +static String default_search_engine = ""; +static String default_color_scheme = "auto"; +static bool default_show_bookmarks_bar = true; +static bool default_auto_close_download_windows = false; + struct ColorScheme { String title; String setting_value; @@ -53,23 +59,16 @@ BrowserSettingsWidget::BrowserSettingsWidget() load_from_gml(browser_settings_widget_gml); m_homepage_url_textbox = find_descendant_of_type_named("homepage_url_textbox"); - m_homepage_url_textbox->set_text(Config::read_string("Browser", "Preferences", "Home", "about:blank")); + m_homepage_url_textbox->set_text(Config::read_string("Browser", "Preferences", "Home", default_homepage_url)); m_color_scheme_combobox = find_descendant_of_type_named("color_scheme_combobox"); m_color_scheme_combobox->set_only_allow_values_from_model(true); m_color_scheme_combobox->set_model(adopt_ref(*new ColorSchemeModel())); m_color_scheme_combobox->set_selected_index(0); - auto selected_color_scheme = Config::read_string("Browser", "Preferences", "ColorScheme", "auto"); - for (int item_index = 0; item_index < m_color_scheme_combobox->model()->row_count(); ++item_index) { - auto scheme = m_color_scheme_combobox->model()->index(item_index, 1).data().to_string(); - if (scheme == selected_color_scheme) { - m_color_scheme_combobox->set_selected_index(item_index); - break; - } - } + set_color_scheme(Config::read_string("Browser", "Preferences", "ColorScheme", default_color_scheme)); m_show_bookmarks_bar_checkbox = find_descendant_of_type_named("show_bookmarks_bar_checkbox"); - m_show_bookmarks_bar_checkbox->set_checked(Config::read_bool("Browser", "Preferences", "ShowBookmarksBar", true), GUI::AllowCallback::No); + m_show_bookmarks_bar_checkbox->set_checked(Config::read_bool("Browser", "Preferences", "ShowBookmarksBar", default_show_bookmarks_bar), GUI::AllowCallback::No); m_enable_search_engine_checkbox = find_descendant_of_type_named("enable_search_engine_checkbox"); m_search_engine_combobox_group = find_descendant_of_type_named("search_engine_combobox_group"); @@ -99,9 +98,34 @@ BrowserSettingsWidget::BrowserSettingsWidget() m_is_custom_search_engine = url_format.is_empty(); m_custom_search_engine_group->set_enabled(m_is_custom_search_engine); }; + set_search_engine_url(Config::read_string("Browser", "Preferences", "SearchEngine", default_search_engine)); - auto current_search_engine_url = Config::read_string("Browser", "Preferences", "SearchEngine", {}); - if (current_search_engine_url.is_empty()) { + 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", "Preferences", "CloseDownloadWidgetOnFinish", default_auto_close_download_windows), GUI::AllowCallback::No); +} + +BrowserSettingsWidget::~BrowserSettingsWidget() +{ +} + +void BrowserSettingsWidget::set_color_scheme(StringView color_scheme) +{ + bool found_color_scheme = false; + for (int item_index = 0; item_index < m_color_scheme_combobox->model()->row_count(); ++item_index) { + auto scheme = m_color_scheme_combobox->model()->index(item_index, 1).data().to_string(); + if (scheme == color_scheme) { + m_color_scheme_combobox->set_selected_index(item_index); + found_color_scheme = true; + break; + } + } + if (!found_color_scheme) + m_color_scheme_combobox->set_selected_index(0); +} + +void BrowserSettingsWidget::set_search_engine_url(StringView url) +{ + if (url.is_empty()) { m_enable_search_engine_checkbox->set_checked(false); m_search_engine_combobox_group->set_enabled(false); m_custom_search_engine_group->set_enabled(false); @@ -113,7 +137,7 @@ BrowserSettingsWidget::BrowserSettingsWidget() bool found_url = false; for (int item_index = 0; item_index < m_search_engine_combobox->model()->row_count(); ++item_index) { auto url_format = m_search_engine_combobox->model()->index(item_index, 1).data().to_string(); - if (url_format == current_search_engine_url) { + if (url_format == url) { m_search_engine_combobox->set_selected_index(item_index); found_url = true; break; @@ -122,7 +146,7 @@ BrowserSettingsWidget::BrowserSettingsWidget() if (!found_url) { m_is_custom_search_engine = true; - m_custom_search_engine_textbox->set_text(current_search_engine_url); + m_custom_search_engine_textbox->set_text(url); // We assume that "Custom" is the last item m_search_engine_combobox->set_selected_index(m_search_engine_combobox->model()->row_count() - 1); m_custom_search_engine_group->set_enabled(true); @@ -130,13 +154,6 @@ BrowserSettingsWidget::BrowserSettingsWidget() m_custom_search_engine_group->set_enabled(false); } } - - 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", "Preferences", "CloseDownloadWidgetOnFinish", false), GUI::AllowCallback::No); -} - -BrowserSettingsWidget::~BrowserSettingsWidget() -{ } void BrowserSettingsWidget::apply_settings() @@ -162,3 +179,12 @@ void BrowserSettingsWidget::apply_settings() Config::write_bool("Browser", "Preferences", "CloseDownloadWidgetOnFinish", m_auto_close_download_windows_checkbox->is_checked()); } + +void BrowserSettingsWidget::reset_default_values() +{ + m_homepage_url_textbox->set_text(default_homepage_url); + m_show_bookmarks_bar_checkbox->set_checked(default_show_bookmarks_bar); + set_color_scheme(default_color_scheme); + m_auto_close_download_windows_checkbox->set_checked(default_auto_close_download_windows); + set_search_engine_url(default_search_engine); +} diff --git a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h index 6fe9796f042..692f865d531 100644 --- a/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h +++ b/Userland/Applications/BrowserSettings/BrowserSettingsWidget.h @@ -17,15 +17,18 @@ public: virtual ~BrowserSettingsWidget() override; virtual void apply_settings() override; + virtual void reset_default_values() override; private: BrowserSettingsWidget(); RefPtr m_homepage_url_textbox; + void set_color_scheme(StringView); RefPtr m_color_scheme_combobox; RefPtr m_show_bookmarks_bar_checkbox; RefPtr m_auto_close_download_windows_checkbox; + void set_search_engine_url(StringView); bool m_is_custom_search_engine { false }; RefPtr m_enable_search_engine_checkbox; RefPtr m_search_engine_combobox_group; diff --git a/Userland/Applications/BrowserSettings/main.cpp b/Userland/Applications/BrowserSettings/main.cpp index fd06a6fe340..55c12a8068e 100644 --- a/Userland/Applications/BrowserSettings/main.cpp +++ b/Userland/Applications/BrowserSettings/main.cpp @@ -24,7 +24,7 @@ ErrorOr serenity_main(Main::Arguments arguments) auto app_icon = GUI::Icon::default_icon("app-browser"); - auto window = TRY(GUI::SettingsWindow::try_create("Browser Settings")); + auto window = TRY(GUI::SettingsWindow::try_create("Browser Settings", GUI::SettingsWindow::ShowDefaultsButton::Yes)); window->set_icon(app_icon.bitmap_for_size(16)); window->add_tab("Browser");