mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-10-26 06:49:12 +03:00
cb657a038f
The placeholder text is there to prompt the user as to what could be added in the address bar. The current text tells the user that they can "Search or enter web address" even when the search setting is disabled. When attempting to "Search" the user is instead sent to page ":", with an error in the console: WebContent(575249): (FIXME) Don't know how to navigate to : This patch fixes this by checking whether the search feature is enabled and setting the placeholder appropriately. This provides a slightly better user experience. Closes #132
116 lines
3.4 KiB
C++
116 lines
3.4 KiB
C++
/*
|
|
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include "LocationEdit.h"
|
|
#include "Settings.h"
|
|
#include "StringUtils.h"
|
|
#include <LibURL/URL.h>
|
|
#include <LibWebView/URL.h>
|
|
#include <QApplication>
|
|
#include <QPalette>
|
|
#include <QTextLayout>
|
|
#include <QTimer>
|
|
|
|
namespace Ladybird {
|
|
|
|
LocationEdit::LocationEdit(QWidget* parent)
|
|
: QLineEdit(parent)
|
|
{
|
|
if (Settings::the()->enable_search())
|
|
setPlaceholderText("Search or enter web address");
|
|
else
|
|
setPlaceholderText("Enter web address");
|
|
m_autocomplete = make<AutoComplete>(this);
|
|
this->setCompleter(m_autocomplete);
|
|
|
|
connect(m_autocomplete, &AutoComplete::activated, [&](QModelIndex const&) {
|
|
emit returnPressed();
|
|
});
|
|
|
|
connect(this, &QLineEdit::returnPressed, [&] {
|
|
clearFocus();
|
|
|
|
Optional<StringView> search_engine_url;
|
|
if (Settings::the()->enable_search())
|
|
search_engine_url = Settings::the()->search_engine().query_url;
|
|
|
|
auto query = ak_string_from_qstring(text());
|
|
|
|
if (auto url = WebView::sanitize_url(query, search_engine_url); url.has_value())
|
|
setText(qstring_from_ak_string(url->serialize()));
|
|
});
|
|
|
|
connect(this, &QLineEdit::textEdited, [this] {
|
|
if (!Settings::the()->enable_autocomplete()) {
|
|
m_autocomplete->clear_suggestions();
|
|
return;
|
|
}
|
|
|
|
auto cursor_position = cursorPosition();
|
|
|
|
m_autocomplete->get_search_suggestions(ak_string_from_qstring(text()));
|
|
setCursorPosition(cursor_position);
|
|
});
|
|
|
|
connect(this, &QLineEdit::textChanged, this, &LocationEdit::highlight_location);
|
|
}
|
|
|
|
void LocationEdit::focusInEvent(QFocusEvent* event)
|
|
{
|
|
QLineEdit::focusInEvent(event);
|
|
highlight_location();
|
|
QTimer::singleShot(0, this, &QLineEdit::selectAll);
|
|
}
|
|
|
|
void LocationEdit::focusOutEvent(QFocusEvent* event)
|
|
{
|
|
QLineEdit::focusOutEvent(event);
|
|
highlight_location();
|
|
}
|
|
|
|
void LocationEdit::highlight_location()
|
|
{
|
|
auto url = ak_string_from_qstring(text());
|
|
QList<QInputMethodEvent::Attribute> attributes;
|
|
|
|
if (auto url_parts = WebView::break_url_into_parts(url); url_parts.has_value()) {
|
|
auto darkened_text_color = QPalette().color(QPalette::Text);
|
|
darkened_text_color.setAlpha(127);
|
|
|
|
QTextCharFormat dark_attributes;
|
|
dark_attributes.setForeground(darkened_text_color);
|
|
|
|
QTextCharFormat highlight_attributes;
|
|
highlight_attributes.setForeground(QPalette().color(QPalette::Text));
|
|
|
|
attributes.append({
|
|
QInputMethodEvent::TextFormat,
|
|
-cursorPosition(),
|
|
static_cast<int>(url_parts->scheme_and_subdomain.length()),
|
|
dark_attributes,
|
|
});
|
|
|
|
attributes.append({
|
|
QInputMethodEvent::TextFormat,
|
|
static_cast<int>(url_parts->scheme_and_subdomain.length() - cursorPosition()),
|
|
static_cast<int>(url_parts->effective_tld_plus_one.length()),
|
|
highlight_attributes,
|
|
});
|
|
|
|
attributes.append({
|
|
QInputMethodEvent::TextFormat,
|
|
static_cast<int>(url_parts->scheme_and_subdomain.length() + url_parts->effective_tld_plus_one.length() - cursorPosition()),
|
|
static_cast<int>(url_parts->remainder.length()),
|
|
dark_attributes,
|
|
});
|
|
}
|
|
|
|
QInputMethodEvent event(QString(), attributes);
|
|
QCoreApplication::sendEvent(this, &event);
|
|
}
|
|
|
|
}
|