Skip to content

Commit

Permalink
UI/Qt: Don't show URL when a new tab is initially focused
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tcl3 committed Jun 17, 2024
1 parent 28927d6 commit 4388624
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
10 changes: 8 additions & 2 deletions Ladybird/Qt/BrowserWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion Ladybird/Qt/LocationEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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()));
}
}

}
9 changes: 9 additions & 0 deletions Ladybird/Qt/LocationEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<AutoComplete> m_autocomplete;

URL::URL m_url;
bool m_url_is_hidden { false };
};

}
6 changes: 3 additions & 3 deletions Ladybird/Qt/Tab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand All @@ -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);
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions Ladybird/Qt/Tab.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4388624

Please sign in to comment.