mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 22:17:43 +00:00
Ladybird: Add LocationEdit Highlighting
This commit is contained in:
parent
b97f9f5809
commit
3bd96f29d2
2 changed files with 76 additions and 6 deletions
|
@ -5,12 +5,78 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "LocationEdit.h"
|
#include "LocationEdit.h"
|
||||||
#include "Tab.h"
|
#include "Utilities.h"
|
||||||
|
#include <AK/URL.h>
|
||||||
|
#include <QCoreApplication>
|
||||||
|
#include <QPalette>
|
||||||
|
#include <QTextLayout>
|
||||||
|
|
||||||
LocationEdit::LocationEdit(Tab* tab)
|
LocationEdit::LocationEdit(QWidget* parent)
|
||||||
: QLineEdit(tab)
|
: QLineEdit(parent)
|
||||||
{
|
{
|
||||||
connect(this, &QLineEdit::returnPressed, this, [&] {
|
connect(this, &QLineEdit::returnPressed, this, [&] {
|
||||||
clearFocus();
|
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<QInputMethodEvent::Attribute> 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<int>(text().length()),
|
||||||
|
defaultFormat,
|
||||||
|
});
|
||||||
|
|
||||||
|
QTextCharFormat hostFormat;
|
||||||
|
hostFormat.setForeground(QPalette().color(QPalette::Text));
|
||||||
|
attributes.append({
|
||||||
|
QInputMethodEvent::TextFormat,
|
||||||
|
host_start,
|
||||||
|
static_cast<int>(host_length),
|
||||||
|
hostFormat,
|
||||||
|
});
|
||||||
|
} else if (url.scheme() == "file") {
|
||||||
|
QTextCharFormat schemeFormat;
|
||||||
|
schemeFormat.setForeground(QPalette().color(QPalette::PlaceholderText));
|
||||||
|
attributes.append({
|
||||||
|
QInputMethodEvent::TextFormat,
|
||||||
|
-cursorPosition(),
|
||||||
|
static_cast<int>(url.scheme().length() + 3),
|
||||||
|
schemeFormat,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QInputMethodEvent event(QString(), attributes);
|
||||||
|
QCoreApplication::sendEvent(this, &event);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,14 @@
|
||||||
|
|
||||||
#include <QLineEdit>
|
#include <QLineEdit>
|
||||||
|
|
||||||
class Tab;
|
|
||||||
|
|
||||||
class LocationEdit final : public QLineEdit {
|
class LocationEdit final : public QLineEdit {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit LocationEdit(Tab*);
|
explicit LocationEdit(QWidget*);
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual void focusInEvent(QFocusEvent* event) override;
|
||||||
|
virtual void focusOutEvent(QFocusEvent* event) override;
|
||||||
|
|
||||||
|
void highlight_location();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue