Skip to content

Commit

Permalink
UI/Qt: Don't show about:newtab or about:blank in location bar
Browse files Browse the repository at this point in the history
When visiting `about:newtab` or `about:blank` page, the location bar is
now left blank. 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 12, 2024
1 parent 28927d6 commit 9d41329
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
12 changes: 11 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 @@ -109,4 +109,14 @@ void LocationEdit::highlight_location()
QCoreApplication::sendEvent(this, &event);
}

void LocationEdit::set_url(URL::URL const& url)
{
m_url = url;
if (WebView::should_show_url_in_location_bar(url)) {
setText(qstring_from_ak_string(url.serialize()));
} else {
clear();
}
}

}
5 changes: 5 additions & 0 deletions Ladybird/Qt/LocationEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ class LocationEdit final : public QLineEdit {
public:
explicit LocationEdit(QWidget*);

URL::URL url() const { return m_url; }
void set_url(URL::URL const&);

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;
};

}
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
10 changes: 10 additions & 0 deletions Userland/Libraries/LibWebView/URL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,14 @@ String url_text_to_copy(URL::URL const& url)
return url_text;
}

bool should_show_url_in_location_bar(URL::URL const& url)
{
static Array urls_to_hide {
URL::URL { "about:newtab"sv },
URL::URL { "about:blank"sv },
};

return !urls_to_hide.contains_slow(url);
}

}
2 changes: 2 additions & 0 deletions Userland/Libraries/LibWebView/URL.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ enum class URLType {
URLType url_type(URL::URL const&);
String url_text_to_copy(URL::URL const&);

bool should_show_url_in_location_bar(URL::URL const&);

}

0 comments on commit 9d41329

Please sign in to comment.