forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLBox.cpp
89 lines (70 loc) · 2.16 KB
/
URLBox.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
* Copyright (c) 2023, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/URL.h>
#include <Applications/Browser/URLBox.h>
#include <LibGfx/Palette.h>
#include <LibGfx/TextAttributes.h>
#include <LibWebView/URL.h>
namespace Browser {
URLBox::URLBox()
{
set_auto_focusable(false);
on_change = [this] {
highlight_url();
};
}
void URLBox::focusout_event(GUI::FocusEvent& event)
{
set_focus_transition(true);
highlight_url();
GUI::TextBox::focusout_event(event);
}
void URLBox::focusin_event(GUI::FocusEvent& event)
{
highlight_url();
GUI::TextBox::focusin_event(event);
}
void URLBox::mousedown_event(GUI::MouseEvent& event)
{
if (is_displayonly())
return;
if (event.button() != GUI::MouseButton::Primary)
return;
if (is_focus_transition()) {
GUI::TextBox::select_current_line();
set_focus_transition(false);
} else {
GUI::TextBox::mousedown_event(event);
}
}
void URLBox::highlight_url()
{
Vector<GUI::TextDocumentSpan> spans;
if (auto url_parts = WebView::break_url_into_parts(text()); url_parts.has_value()) {
Gfx::TextAttributes dark_attributes;
dark_attributes.color = palette().color(Gfx::ColorRole::PlaceholderText);
Gfx::TextAttributes highlight_attributes;
highlight_attributes.color = palette().color(Gfx::ColorRole::BaseText);
spans.append({
{ { 0, 0 }, { 0, url_parts->scheme_and_subdomain.length() } },
dark_attributes,
});
spans.append({
{ { 0, url_parts->scheme_and_subdomain.length() }, { 0, url_parts->scheme_and_subdomain.length() + url_parts->effective_tld_plus_one.length() } },
highlight_attributes,
});
spans.append({
{
{ 0, url_parts->scheme_and_subdomain.length() + url_parts->effective_tld_plus_one.length() },
{ 0, url_parts->scheme_and_subdomain.length() + url_parts->effective_tld_plus_one.length() + url_parts->remainder.length() },
},
dark_attributes,
});
}
document().set_spans(0, move(spans));
update();
}
}