From 4388624f971b4ab6bc73ddeb7e51db0beb77236c Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Mon, 10 Jun 2024 10:33:35 +0100 Subject: [PATCH] UI/Qt: Don't show URL when a new tab is initially focused The URL is now not shown when a new tab is initially activated until the location bar loses focus. This allows the user to see the location bar placeholder text when opening a new tab. It also makes it easier to paste URLs into the location bar after opening a new tab. --- Ladybird/Qt/BrowserWindow.cpp | 10 ++++++++-- Ladybird/Qt/LocationEdit.cpp | 17 ++++++++++++++++- Ladybird/Qt/LocationEdit.h | 9 +++++++++ Ladybird/Qt/Tab.cpp | 6 +++--- Ladybird/Qt/Tab.h | 3 +++ 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Ladybird/Qt/BrowserWindow.cpp b/Ladybird/Qt/BrowserWindow.cpp index 35882001daa2..db533a658a2e 100644 --- a/Ladybird/Qt/BrowserWindow.cpp +++ b/Ladybird/Qt/BrowserWindow.cpp @@ -599,8 +599,11 @@ Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, Tab& par } m_tabs_container->addTab(tab, "New Tab"); - if (activate_tab == Web::HTML::ActivateTab::Yes) + if (activate_tab == Web::HTML::ActivateTab::Yes) { m_tabs_container->setCurrentWidget(tab); + if (m_tabs_container->count() != 1) + tab->set_url_is_hidden(true); + } initialize_tab(tab); return *tab; } @@ -614,8 +617,11 @@ Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab) } m_tabs_container->addTab(tab, "New Tab"); - if (activate_tab == Web::HTML::ActivateTab::Yes) + if (activate_tab == Web::HTML::ActivateTab::Yes) { m_tabs_container->setCurrentWidget(tab); + if (m_tabs_container->count() != 1) + tab->set_url_is_hidden(true); + } initialize_tab(tab); return *tab; diff --git a/Ladybird/Qt/LocationEdit.cpp b/Ladybird/Qt/LocationEdit.cpp index 2fe4c09f112d..39664ecacaf9 100644 --- a/Ladybird/Qt/LocationEdit.cpp +++ b/Ladybird/Qt/LocationEdit.cpp @@ -37,7 +37,7 @@ LocationEdit::LocationEdit(QWidget* parent) auto query = ak_string_from_qstring(text()); if (auto url = WebView::sanitize_url(query, search_engine_url); url.has_value()) - setText(qstring_from_ak_string(url->serialize())); + set_url(url.release_value()); }); connect(this, &QLineEdit::textEdited, [this] { @@ -65,6 +65,11 @@ void LocationEdit::focusInEvent(QFocusEvent* event) void LocationEdit::focusOutEvent(QFocusEvent* event) { QLineEdit::focusOutEvent(event); + if (m_url_is_hidden) { + m_url_is_hidden = false; + if (text().isEmpty()) + setText(qstring_from_ak_string(m_url.serialize())); + } highlight_location(); } @@ -109,4 +114,14 @@ void LocationEdit::highlight_location() QCoreApplication::sendEvent(this, &event); } +void LocationEdit::set_url(URL::URL const& url) +{ + m_url = url; + if (m_url_is_hidden) { + clear(); + } else { + setText(qstring_from_ak_string(url.serialize())); + } +} + } diff --git a/Ladybird/Qt/LocationEdit.h b/Ladybird/Qt/LocationEdit.h index 668792a4f206..8dd00a9851b1 100644 --- a/Ladybird/Qt/LocationEdit.h +++ b/Ladybird/Qt/LocationEdit.h @@ -17,12 +17,21 @@ class LocationEdit final : public QLineEdit { public: explicit LocationEdit(QWidget*); + URL::URL url() const { return m_url; } + void set_url(URL::URL const&); + + bool url_is_hidden() const { return m_url_is_hidden; } + void set_url_is_hidden(bool url_is_hidden) { m_url_is_hidden = url_is_hidden; } + private: virtual void focusInEvent(QFocusEvent* event) override; virtual void focusOutEvent(QFocusEvent* event) override; void highlight_location(); AK::OwnPtr m_autocomplete; + + URL::URL m_url; + bool m_url_is_hidden { false }; }; } diff --git a/Ladybird/Qt/Tab.cpp b/Ladybird/Qt/Tab.cpp index d00db16a7109..af8a4255b849 100644 --- a/Ladybird/Qt/Tab.cpp +++ b/Ladybird/Qt/Tab.cpp @@ -145,7 +145,7 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St m_favicon = default_favicon(); emit favicon_changed(tab_index(), m_favicon); - m_location_edit->setText(url_serialized); + m_location_edit->set_url(url); m_location_edit->setCursorPosition(0); }; @@ -155,7 +155,7 @@ Tab::Tab(BrowserWindow* window, WebContentOptions const& web_content_options, St }; view().on_url_change = [this](auto const& url) { - m_location_edit->setText(qstring_from_ak_string(url.serialize())); + m_location_edit->set_url(url); }; QObject::connect(m_location_edit, &QLineEdit::returnPressed, this, &Tab::location_edit_return_pressed); @@ -863,7 +863,7 @@ void Tab::copy_link_url(URL::URL const& url) void Tab::location_edit_return_pressed() { - navigate(ak_url_from_qstring(m_location_edit->text())); + navigate(m_location_edit->url()); } void Tab::open_file() diff --git a/Ladybird/Qt/Tab.h b/Ladybird/Qt/Tab.h index 9d2ebe28778d..4563e63f2a33 100644 --- a/Ladybird/Qt/Tab.h +++ b/Ladybird/Qt/Tab.h @@ -71,6 +71,9 @@ class Tab final : public QWidget { void set_scripting(bool); void set_user_agent_string(ByteString const&); + bool url_is_hidden() const { return m_location_edit->url_is_hidden(); } + void set_url_is_hidden(bool url_is_hidden) { m_location_edit->set_url_is_hidden(url_is_hidden); } + public slots: void focus_location_editor(); void location_edit_return_pressed();