1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 20:07:35 +00:00

LibGUI+Browser: Move GUI::UrlBox to the Browser application

Browser is the only user of this component. Move it to allow making use
of LibWebView for URL highlighting.
This commit is contained in:
Timothy Flynn 2023-10-17 13:09:41 -04:00 committed by Andreas Kling
parent 0715ba889e
commit 55092dd164
8 changed files with 139 additions and 112 deletions

View file

@ -6,16 +6,13 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/URL.h>
#include <AK/Vector.h>
#include <LibGUI/Painter.h>
#include <LibGUI/TextBox.h>
#include <LibGfx/Palette.h>
#include <LibGfx/TextAttributes.h>
REGISTER_WIDGET(GUI, TextBox)
REGISTER_WIDGET(GUI, PasswordBox)
REGISTER_WIDGET(GUI, UrlBox)
namespace GUI {
@ -132,90 +129,4 @@ void PasswordBox::mousedown_event(GUI::MouseEvent& event)
}
}
UrlBox::UrlBox()
: TextBox()
{
set_auto_focusable(false);
on_change = [this] {
highlight_url();
};
}
void UrlBox::focusout_event(GUI::FocusEvent& event)
{
set_focus_transition(true);
highlight_url();
TextBox::focusout_event(event);
}
void UrlBox::focusin_event(GUI::FocusEvent& event)
{
highlight_url();
TextBox::focusin_event(event);
}
void UrlBox::mousedown_event(GUI::MouseEvent& event)
{
if (is_displayonly())
return;
if (event.button() != MouseButton::Primary)
return;
if (is_focus_transition()) {
TextBox::select_current_line();
set_focus_transition(false);
} else {
TextBox::mousedown_event(event);
}
}
void UrlBox::highlight_url()
{
auto url = AK::URL::create_with_url_or_path(text());
Vector<GUI::TextDocumentSpan> spans;
if (url.is_valid() && !is_focused()) {
if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "gemini") {
auto serialized_host = url.serialized_host().release_value_but_fixme_should_propagate_errors().to_deprecated_string();
auto host_start = url.scheme().bytes_as_string_view().length() + 3;
auto host_length = serialized_host.length();
// FIXME: Maybe add a generator to use https://publicsuffix.org/list/public_suffix_list.dat
// for now just highlight the whole host
Gfx::TextAttributes default_format;
default_format.color = palette().color(Gfx::ColorRole::PlaceholderText);
spans.append({
{ { 0, 0 }, { 0, host_start } },
default_format,
});
Gfx::TextAttributes host_format;
host_format.color = palette().color(Gfx::ColorRole::BaseText);
spans.append({
{ { 0, host_start }, { 0, host_start + host_length } },
host_format,
});
spans.append({
{ { 0, host_start + host_length }, { 0, text().length() } },
default_format,
});
} else if (url.scheme() == "file") {
Gfx::TextAttributes scheme_format;
scheme_format.color = palette().color(Gfx::ColorRole::PlaceholderText);
spans.append({
{ { 0, 0 }, { 0, url.scheme().bytes_as_string_view().length() + 3 } },
scheme_format,
});
}
}
document().set_spans(0, move(spans));
update();
}
}