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

BrowserSettings: Add setting for search engine

This commit is contained in:
Sam Atkins 2021-11-25 12:26:54 +00:00 committed by Andreas Kling
parent e927048754
commit 11466c5316
4 changed files with 152 additions and 0 deletions

View file

@ -7,6 +7,7 @@
#include "BrowserSettingsWidget.h" #include "BrowserSettingsWidget.h"
#include <Applications/BrowserSettings/BrowserSettingsWidgetGML.h> #include <Applications/BrowserSettings/BrowserSettingsWidgetGML.h>
#include <LibConfig/Client.h> #include <LibConfig/Client.h>
#include <LibGUI/JsonArrayModel.h>
BrowserSettingsWidget::BrowserSettingsWidget() BrowserSettingsWidget::BrowserSettingsWidget()
{ {
@ -18,6 +19,66 @@ BrowserSettingsWidget::BrowserSettingsWidget()
m_show_bookmarks_bar_checkbox = find_descendant_of_type_named<GUI::CheckBox>("show_bookmarks_bar_checkbox"); m_show_bookmarks_bar_checkbox = find_descendant_of_type_named<GUI::CheckBox>("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", true), GUI::AllowCallback::No);
m_enable_search_engine_checkbox = find_descendant_of_type_named<GUI::CheckBox>("enable_search_engine_checkbox");
m_search_engine_combobox_group = find_descendant_of_type_named<GUI::Widget>("search_engine_combobox_group");
m_search_engine_combobox = find_descendant_of_type_named<GUI::ComboBox>("search_engine_combobox");
m_custom_search_engine_group = find_descendant_of_type_named<GUI::Widget>("custom_search_engine_group");
m_custom_search_engine_textbox = find_descendant_of_type_named<GUI::TextBox>("custom_search_engine_textbox");
m_enable_search_engine_checkbox->on_checked = [this](bool checked) {
m_search_engine_combobox_group->set_enabled(checked);
m_custom_search_engine_group->set_enabled(checked && m_is_custom_search_engine);
};
Vector<GUI::JsonArrayModel::FieldSpec> search_engine_fields;
search_engine_fields.empend("title", "Title", Gfx::TextAlignment::CenterLeft);
search_engine_fields.empend("url_format", "Url format", Gfx::TextAlignment::CenterLeft);
auto search_engines_model = GUI::JsonArrayModel::create(String::formatted("{}/SearchEngines.json", Core::StandardPaths::config_directory()), move(search_engine_fields));
search_engines_model->invalidate();
Vector<JsonValue> custom_search_engine;
custom_search_engine.append("Custom...");
custom_search_engine.append("");
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);
m_search_engine_combobox->on_change = [this](AK::String const&, GUI::ModelIndex const& cursor_index) {
auto url_format = m_search_engine_combobox->model()->index(cursor_index.row(), 1).data().to_string();
m_is_custom_search_engine = url_format.is_empty();
m_custom_search_engine_group->set_enabled(m_is_custom_search_engine);
};
auto current_search_engine_url = Config::read_string("Browser", "Preferences", "SearchEngine", {});
if (current_search_engine_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);
m_search_engine_combobox->set_selected_index(0);
} else {
m_enable_search_engine_checkbox->set_checked(true);
m_search_engine_combobox_group->set_enabled(true);
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) {
m_search_engine_combobox->set_selected_index(item_index);
found_url = true;
break;
}
}
if (!found_url) {
m_is_custom_search_engine = true;
m_custom_search_engine_textbox->set_text(current_search_engine_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);
} else {
m_custom_search_engine_group->set_enabled(false);
}
}
m_auto_close_download_windows_checkbox = find_descendant_of_type_named<GUI::CheckBox>("auto_close_download_windows_checkbox"); 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", "Preferences", "CloseDownloadWidgetOnFinish", false), GUI::AllowCallback::No); m_auto_close_download_windows_checkbox->set_checked(Config::read_bool("Browser", "Preferences", "CloseDownloadWidgetOnFinish", false), GUI::AllowCallback::No);
} }
@ -33,5 +94,15 @@ void BrowserSettingsWidget::apply_settings()
Config::write_bool("Browser", "Preferences", "ShowBookmarksBar", m_show_bookmarks_bar_checkbox->is_checked()); Config::write_bool("Browser", "Preferences", "ShowBookmarksBar", m_show_bookmarks_bar_checkbox->is_checked());
if (!m_enable_search_engine_checkbox->is_checked()) {
Config::write_string("Browser", "Preferences", "SearchEngine", {});
} else if (m_is_custom_search_engine) {
Config::write_string("Browser", "Preferences", "SearchEngine", m_custom_search_engine_textbox->text());
} else {
auto selected_index = m_search_engine_combobox->selected_index();
auto url = m_search_engine_combobox->model()->index(selected_index, 1).data().to_string();
Config::write_string("Browser", "Preferences", "SearchEngine", url);
}
Config::write_bool("Browser", "Preferences", "CloseDownloadWidgetOnFinish", m_auto_close_download_windows_checkbox->is_checked()); Config::write_bool("Browser", "Preferences", "CloseDownloadWidgetOnFinish", m_auto_close_download_windows_checkbox->is_checked());
} }

View file

@ -69,6 +69,78 @@
} }
} }
@GUI::GroupBox {
title: "Search Engine"
fixed_height: 140
layout: @GUI::VerticalBoxLayout {
margins: [16, 8, 8]
spacing: 2
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
spacing: 16
}
@GUI::Label {
fixed_width: 32
fixed_height: 32
name: "search_engine_image_label"
}
@GUI::CheckBox {
text: "Search using '?' in the URL box"
name: "enable_search_engine_checkbox"
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
spacing: 16
}
name: "search_engine_combobox_group"
@GUI::Widget {
fixed_width: 32
}
@GUI::Label {
text: "Search engine:"
text_alignment: "CenterLeft"
fixed_width: 110
}
@GUI::ComboBox {
name: "search_engine_combobox"
}
}
@GUI::Widget {
layout: @GUI::HorizontalBoxLayout {
spacing: 16
}
name: "custom_search_engine_group"
@GUI::Widget {
fixed_width: 32
}
@GUI::Label {
text: "Enter URL template:"
text_alignment: "CenterLeft"
fixed_width: 110
}
@GUI::TextBox {
name: "custom_search_engine_textbox"
placeholder: "https://host/search?q={}"
}
}
}
@GUI::GroupBox { @GUI::GroupBox {
title: "Downloads" title: "Downloads"
fixed_height: 70 fixed_height: 70

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <LibGUI/CheckBox.h> #include <LibGUI/CheckBox.h>
#include <LibGUI/ComboBox.h>
#include <LibGUI/SettingsWindow.h> #include <LibGUI/SettingsWindow.h>
#include <LibGUI/TextBox.h> #include <LibGUI/TextBox.h>
@ -23,4 +24,11 @@ private:
RefPtr<GUI::TextBox> m_homepage_url_textbox; RefPtr<GUI::TextBox> m_homepage_url_textbox;
RefPtr<GUI::CheckBox> m_show_bookmarks_bar_checkbox; RefPtr<GUI::CheckBox> m_show_bookmarks_bar_checkbox;
RefPtr<GUI::CheckBox> m_auto_close_download_windows_checkbox; RefPtr<GUI::CheckBox> m_auto_close_download_windows_checkbox;
bool m_is_custom_search_engine { false };
RefPtr<GUI::CheckBox> m_enable_search_engine_checkbox;
RefPtr<GUI::Widget> m_search_engine_combobox_group;
RefPtr<GUI::ComboBox> m_search_engine_combobox;
RefPtr<GUI::Widget> m_custom_search_engine_group;
RefPtr<GUI::TextBox> m_custom_search_engine_textbox;
}; };

View file

@ -19,6 +19,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Config::pledge_domains("Browser"); Config::pledge_domains("Browser");
TRY(Core::System::unveil("/res", "r")); TRY(Core::System::unveil("/res", "r"));
TRY(Core::System::unveil("/home", "r"));
TRY(Core::System::unveil(nullptr, nullptr)); TRY(Core::System::unveil(nullptr, nullptr));
auto app_icon = GUI::Icon::default_icon("app-browser"); auto app_icon = GUI::Icon::default_icon("app-browser");