Browser: Add more intuitive search

This commit is contained in:
Cameron Youell 2023-06-07 11:02:02 +10:00 committed by Sam Atkins
parent d68433653a
commit d972338208
Notes: sideshowbarker 2024-07-18 03:35:30 +09:00
4 changed files with 16 additions and 14 deletions

View file

@ -41,5 +41,5 @@ set(GENERATED_SOURCES
)
serenity_app(Browser ICON app-browser)
target_link_libraries(Browser PRIVATE LibCore LibFileSystem LibWebView LibWeb LibProtocol LibGUI LibDesktop LibConfig LibGfx LibIPC LibJS LibLocale LibMain LibSyntax LibSQL)
target_link_libraries(Browser PRIVATE LibCore LibFileSystem LibWebView LibWeb LibProtocol LibPublicSuffix LibGUI LibDesktop LibConfig LibGfx LibIPC LibJS LibLocale LibMain LibSyntax LibSQL)
link_with_locale_data(Browser)

View file

@ -35,6 +35,7 @@
#include <LibGUI/Toolbar.h>
#include <LibGUI/ToolbarContainer.h>
#include <LibGUI/Window.h>
#include <LibPublicSuffix/URL.h>
#include <LibWeb/HTML/BrowsingContext.h>
#include <LibWeb/HTML/SyntaxHighlighter/SyntaxHighlighter.h>
#include <LibWeb/Layout/BlockContainer.h>
@ -51,9 +52,6 @@ Tab::~Tab()
URL url_from_user_input(DeprecatedString const& input)
{
if (input.starts_with('?') && !g_search_engine.is_empty())
return URL(g_search_engine.replace("{}"sv, URL::percent_encode(input.substring_view(1)), ReplaceMode::FirstOnly));
URL url_with_http_schema = URL(DeprecatedString::formatted("https://{}", input));
if (url_with_http_schema.is_valid() && url_with_http_schema.port().has_value())
return url_with_http_schema;
@ -169,7 +167,7 @@ Tab::Tab(BrowserWindow& window)
toolbar.add_action(window.reload_action());
m_location_box = toolbar.add<GUI::UrlBox>();
m_location_box->set_placeholder("Address"sv);
m_location_box->set_placeholder("Search or enter address"sv);
m_location_box->on_return_pressed = [this] {
auto url = url_from_location_bar();
@ -647,11 +645,6 @@ void Tab::update_reset_zoom_button()
Optional<URL> Tab::url_from_location_bar(MayAppendTLD may_append_tld)
{
if (m_location_box->text().starts_with('?') && g_search_engine.is_empty()) {
GUI::MessageBox::show(&this->window(), "Select a search engine in the Settings menu before searching."sv, "No search engine selected"sv, GUI::MessageBox::Type::Information);
return {};
}
DeprecatedString text = m_location_box->text();
StringBuilder builder;
@ -664,8 +657,17 @@ Optional<URL> Tab::url_from_location_bar(MayAppendTLD may_append_tld)
}
auto final_text = builder.to_deprecated_string();
auto url = url_from_user_input(final_text);
return url;
auto error_or_absolute_url = PublicSuffix::absolute_url(final_text);
if (error_or_absolute_url.is_error()) {
if (g_search_engine.is_empty()) {
GUI::MessageBox::show(&this->window(), "Select a search engine in the Settings menu before searching."sv, "No search engine selected"sv, GUI::MessageBox::Type::Information);
return {};
}
return URL(g_search_engine.replace("{}"sv, URL::percent_encode(final_text), ReplaceMode::FirstOnly));
}
return URL(error_or_absolute_url.release_value());
}
void Tab::load(const URL& url, LoadType load_type)

View file

@ -129,7 +129,7 @@
}
@GUI::CheckBox {
text: "Search using '?' in the URL box"
text: "Search in the URL box"
name: "enable_search_engine_checkbox"
}
}

View file

@ -12,7 +12,7 @@ namespace Browser {
static constexpr StringView default_homepage_url = "file:///res/html/misc/welcome.html"sv;
static constexpr StringView default_new_tab_url = "file:///res/html/misc/new-tab.html"sv;
static constexpr StringView default_search_engine = ""sv;
static constexpr StringView default_search_engine = "https://www.google.com/search?q={}"sv;
static constexpr StringView default_color_scheme = "auto"sv;
static constexpr bool default_enable_content_filters = true;
static constexpr bool default_show_bookmarks_bar = true;