From 3bd96f29d2d42a6cc99da530cbc900ac8176d4ce Mon Sep 17 00:00:00 2001 From: Cameron Youell Date: Mon, 23 Jan 2023 14:11:01 +1100 Subject: [PATCH] Ladybird: Add LocationEdit Highlighting --- Ladybird/LocationEdit.cpp | 72 +++++++++++++++++++++++++++++++++++++-- Ladybird/LocationEdit.h | 10 ++++-- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/Ladybird/LocationEdit.cpp b/Ladybird/LocationEdit.cpp index 474a3fd0634..61489c0b16b 100644 --- a/Ladybird/LocationEdit.cpp +++ b/Ladybird/LocationEdit.cpp @@ -5,12 +5,78 @@ */ #include "LocationEdit.h" -#include "Tab.h" +#include "Utilities.h" +#include +#include +#include +#include -LocationEdit::LocationEdit(Tab* tab) - : QLineEdit(tab) +LocationEdit::LocationEdit(QWidget* parent) + : QLineEdit(parent) { connect(this, &QLineEdit::returnPressed, this, [&] { clearFocus(); }); + + connect(this, &QLineEdit::textChanged, this, [&] { + highlight_location(); + }); +} + +void LocationEdit::focusInEvent(QFocusEvent* event) +{ + QLineEdit::focusInEvent(event); + highlight_location(); +} + +void LocationEdit::focusOutEvent(QFocusEvent* event) +{ + QLineEdit::focusOutEvent(event); + highlight_location(); +} + +void LocationEdit::highlight_location() +{ + auto url = AK::URL::create_with_url_or_path(ak_deprecated_string_from_qstring(text())); + + QList attributes; + if (url.is_valid() && !hasFocus()) { + if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "gemini") { + int host_start = (url.scheme().length() + 3) - cursorPosition(); + auto host_length = url.host().length(); + + // FIXME: Maybe add a generator to use https://publicsuffix.org/list/public_suffix_list.dat + // for now just highlight the whole host + + QTextCharFormat defaultFormat; + defaultFormat.setForeground(QPalette().color(QPalette::PlaceholderText)); + attributes.append({ + QInputMethodEvent::TextFormat, + -cursorPosition(), + static_cast(text().length()), + defaultFormat, + }); + + QTextCharFormat hostFormat; + hostFormat.setForeground(QPalette().color(QPalette::Text)); + attributes.append({ + QInputMethodEvent::TextFormat, + host_start, + static_cast(host_length), + hostFormat, + }); + } else if (url.scheme() == "file") { + QTextCharFormat schemeFormat; + schemeFormat.setForeground(QPalette().color(QPalette::PlaceholderText)); + attributes.append({ + QInputMethodEvent::TextFormat, + -cursorPosition(), + static_cast(url.scheme().length() + 3), + schemeFormat, + }); + } + } + + QInputMethodEvent event(QString(), attributes); + QCoreApplication::sendEvent(this, &event); } diff --git a/Ladybird/LocationEdit.h b/Ladybird/LocationEdit.h index d8ebda54900..20ed6771f19 100644 --- a/Ladybird/LocationEdit.h +++ b/Ladybird/LocationEdit.h @@ -10,10 +10,14 @@ #include -class Tab; - class LocationEdit final : public QLineEdit { Q_OBJECT public: - explicit LocationEdit(Tab*); + explicit LocationEdit(QWidget*); + +private: + virtual void focusInEvent(QFocusEvent* event) override; + virtual void focusOutEvent(QFocusEvent* event) override; + + void highlight_location(); };