diff --git a/Userland/Applications/Browser/CMakeLists.txt b/Userland/Applications/Browser/CMakeLists.txt index 0cc7c4eb5fa..284787cac41 100644 --- a/Userland/Applications/Browser/CMakeLists.txt +++ b/Userland/Applications/Browser/CMakeLists.txt @@ -25,6 +25,7 @@ set(SOURCES StorageModel.cpp StorageWidget.cpp Tab.cpp + URLBox.cpp WindowActions.cpp main.cpp ) diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 7822198bcde..2b65b21a770 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -157,7 +158,7 @@ Tab::Tab(BrowserWindow& window) toolbar.add_action(window.reload_action()); - m_location_box = toolbar.add(); + m_location_box = toolbar.add(); m_location_box->set_placeholder("Search or enter address"sv); m_location_box->on_return_pressed = [this] { diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index bdb679367c2..2868bdfee6c 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -28,6 +28,7 @@ class InspectorWidget; class ConsoleWidget; class HistoryWidget; class StorageWidget; +class URLBox; class Tab final : public GUI::Widget { C_OBJECT(Tab); @@ -120,7 +121,7 @@ private: RefPtr m_web_content_view; - RefPtr m_location_box; + RefPtr m_location_box; RefPtr m_reset_zoom_button; RefPtr m_bookmark_button; RefPtr m_dom_inspector_widget; diff --git a/Userland/Applications/Browser/URLBox.cpp b/Userland/Applications/Browser/URLBox.cpp new file mode 100644 index 00000000000..a5e7862c4de --- /dev/null +++ b/Userland/Applications/Browser/URLBox.cpp @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2023, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include +#include +#include + +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() +{ + auto url = URL::create_with_url_or_path(text()); + Vector 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(); +} + +} diff --git a/Userland/Applications/Browser/URLBox.h b/Userland/Applications/Browser/URLBox.h new file mode 100644 index 00000000000..f51aa45b92e --- /dev/null +++ b/Userland/Applications/Browser/URLBox.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023, the SerenityOS developers. + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace Browser { + +class URLBox : public GUI::TextBox { + C_OBJECT(URLBox) + +public: + virtual ~URLBox() override = default; + + void set_focus_transition(bool focus_transition) { m_focus_transition = focus_transition; } + bool is_focus_transition() const { return m_focus_transition; } + +private: + URLBox(); + + void highlight_url(); + + virtual void mousedown_event(GUI::MouseEvent&) override; + virtual void focusout_event(GUI::FocusEvent&) override; + virtual void focusin_event(GUI::FocusEvent&) override; + + bool m_focus_transition { true }; +}; + +} diff --git a/Userland/Libraries/LibGUI/Forward.h b/Userland/Libraries/LibGUI/Forward.h index da238609f7e..a7d7f67a372 100644 --- a/Userland/Libraries/LibGUI/Forward.h +++ b/Userland/Libraries/LibGUI/Forward.h @@ -79,7 +79,6 @@ class Statusbar; class TabWidget; class TableView; class TextBox; -class UrlBox; class TextDocument; class TextDocumentUndoCommand; class TextEditor; diff --git a/Userland/Libraries/LibGUI/TextBox.cpp b/Userland/Libraries/LibGUI/TextBox.cpp index 48dd48e14c1..4504932fe45 100644 --- a/Userland/Libraries/LibGUI/TextBox.cpp +++ b/Userland/Libraries/LibGUI/TextBox.cpp @@ -6,16 +6,13 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include #include #include #include #include -#include 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 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(); -} - } diff --git a/Userland/Libraries/LibGUI/TextBox.h b/Userland/Libraries/LibGUI/TextBox.h index 6c9ddeade84..fdc79906d0a 100644 --- a/Userland/Libraries/LibGUI/TextBox.h +++ b/Userland/Libraries/LibGUI/TextBox.h @@ -61,24 +61,4 @@ private: bool m_show_reveal_button { false }; }; -class UrlBox : public TextBox { - C_OBJECT(UrlBox) -public: - virtual ~UrlBox() override = default; - - void set_focus_transition(bool focus_transition) { m_focus_transition = focus_transition; } - bool is_focus_transition() const { return m_focus_transition; } - -private: - UrlBox(); - - void highlight_url(); - - virtual void mousedown_event(GUI::MouseEvent&) override; - virtual void focusout_event(GUI::FocusEvent&) override; - virtual void focusin_event(GUI::FocusEvent&) override; - - bool m_focus_transition { true }; -}; - }