2023-01-18 14:12:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "LocationEdit.h"
|
2023-08-05 16:42:26 +00:00
|
|
|
#include "StringUtils.h"
|
2023-01-23 03:11:01 +00:00
|
|
|
#include <AK/URL.h>
|
|
|
|
#include <QCoreApplication>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QTextLayout>
|
2023-05-20 15:13:45 +00:00
|
|
|
#include <QTimer>
|
2023-01-18 14:12:13 +00:00
|
|
|
|
2023-08-02 17:52:59 +00:00
|
|
|
namespace Ladybird {
|
|
|
|
|
2023-01-23 03:11:01 +00:00
|
|
|
LocationEdit::LocationEdit(QWidget* parent)
|
|
|
|
: QLineEdit(parent)
|
2023-01-18 14:12:13 +00:00
|
|
|
{
|
2023-05-20 15:46:19 +00:00
|
|
|
setPlaceholderText("Enter web address");
|
2023-01-18 14:12:13 +00:00
|
|
|
connect(this, &QLineEdit::returnPressed, this, [&] {
|
|
|
|
clearFocus();
|
|
|
|
});
|
2023-01-23 03:11:01 +00:00
|
|
|
|
|
|
|
connect(this, &QLineEdit::textChanged, this, [&] {
|
|
|
|
highlight_location();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void LocationEdit::focusInEvent(QFocusEvent* event)
|
|
|
|
{
|
|
|
|
QLineEdit::focusInEvent(event);
|
|
|
|
highlight_location();
|
2023-05-20 15:13:45 +00:00
|
|
|
QTimer::singleShot(0, this, &QLineEdit::selectAll);
|
2023-01-23 03:11:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
|
|
|
|
2023-03-14 13:34:26 +00:00
|
|
|
auto darkened_text_color = QPalette().color(QPalette::Text);
|
|
|
|
darkened_text_color.setAlpha(127);
|
|
|
|
|
2023-01-23 03:11:01 +00:00
|
|
|
QList<QInputMethodEvent::Attribute> attributes;
|
|
|
|
if (url.is_valid() && !hasFocus()) {
|
|
|
|
if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "gemini") {
|
2023-08-12 04:52:41 +00:00
|
|
|
int host_start = (url.scheme().bytes_as_string_view().length() + 3) - cursorPosition();
|
2023-07-27 09:40:41 +00:00
|
|
|
auto host_length = url.serialized_host().release_value_but_fixme_should_propagate_errors().bytes().size();
|
2023-01-23 03:11:01 +00:00
|
|
|
|
|
|
|
// FIXME: Maybe add a generator to use https://publicsuffix.org/list/public_suffix_list.dat
|
|
|
|
// for now just highlight the whole host
|
|
|
|
|
|
|
|
QTextCharFormat defaultFormat;
|
2023-03-14 13:34:26 +00:00
|
|
|
defaultFormat.setForeground(darkened_text_color);
|
2023-01-23 03:11:01 +00:00
|
|
|
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;
|
2023-03-14 13:34:26 +00:00
|
|
|
schemeFormat.setForeground(darkened_text_color);
|
2023-01-23 03:11:01 +00:00
|
|
|
attributes.append({
|
|
|
|
QInputMethodEvent::TextFormat,
|
|
|
|
-cursorPosition(),
|
2023-08-12 04:52:41 +00:00
|
|
|
static_cast<int>(url.scheme().bytes_as_string_view().length() + 3),
|
2023-01-23 03:11:01 +00:00
|
|
|
schemeFormat,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QInputMethodEvent event(QString(), attributes);
|
|
|
|
QCoreApplication::sendEvent(this, &event);
|
2023-01-18 14:12:13 +00:00
|
|
|
}
|
2023-08-02 17:52:59 +00:00
|
|
|
|
|
|
|
}
|