Truly independent web browser
UI/Qt: Open site settings from pages that are not from a website
Andreas Klingdeb2c0bace0a
diff --git a/Documentation/UI/QuietDesign.md b/Documentation/UI/QuietDesign.mdindex 834277caa64..df3bf4e6f51 100644--- a/Documentation/UI/QuietDesign.md+++ b/Documentation/UI/QuietDesign.md@@ -511,6 +511,9 @@ settings.lists" sit at the bottom of the drill-in.- "Cookies and site data" drill-in.- Footer: "Reset permissions", "All site settings".+- A page that is not from a website (Ladybird's own pages, files, a new tab) shows the same+ icon, and the panel shows only the page's icon and title, one line saying it has no site+ settings, and "All site settings".### Downloadsdiff --git a/UI/Qt/LocationEdit.cpp b/UI/Qt/LocationEdit.cppindex 14a3171b783..d6967e1b997 100644--- a/UI/Qt/LocationEdit.cpp+++ b/UI/Qt/LocationEdit.cpp@@ -1279,13 +1279,7 @@ void LocationEdit::update_location_icon()m_shows_editing_icon = false;m_leading_icon_button->setAttribute(Qt::WA_TransparentForMouseEvents, false);- // NB: A page without a site of its own, such as about:blank, has no site settings, so the icon is only shown there,- // and a click on it lands in the text.- enum class Clickable : u8 {- No,- Yes,- };- auto show_site_settings_button = [&](Clickable clickable) {+ auto show_site_settings_button = [&] {update_indicator_style(false);m_leading_icon_button->setToolButtonStyle(Qt::ToolButtonIconOnly);m_leading_icon_button->setText({});@@ -1293,8 +1287,7 @@ void LocationEdit::update_location_icon()m_shows_site_settings_button = true;m_leading_icon_button->setIconSize({ 16, 16 });m_leading_icon_button->setFixedSize(LOCATION_SITE_SETTINGS_BUTTON_WIDTH, LOCATION_PILL_HEIGHT);- m_leading_icon_button->setToolTip(clickable == Clickable::Yes ? "Site settings" : QString {});- m_leading_icon_button->setAttribute(Qt::WA_TransparentForMouseEvents, clickable == Clickable::No);+ m_leading_icon_button->setToolTip("Site settings");show_indicator(LOCATION_TEXT_MARGIN_FOR_TEXT_X);};@@ -1332,20 +1325,17 @@ void LocationEdit::update_location_icon()return;}- auto is_showing_current_url_for_display = !hasFocus()+ auto is_showing_insecure_url_for_display = !hasFocus()&& m_url.has_value()- && text() == display_url();-- if (is_showing_current_url_for_display) {- auto scheme = m_url->scheme();- if (scheme == "http"sv)- show_not_secure_indicator();- else- show_site_settings_button(scheme == "https"sv ? Clickable::Yes : Clickable::No);- return;- }-- show_site_settings_button(Clickable::No);+ && text() == display_url()+ && m_url->scheme() == "http"sv;++ // NB: Pages without a site of their own, such as about:blank, show the icon too, so there is always one in its+ // place.+ if (is_showing_insecure_url_for_display)+ show_not_secure_indicator();+ else+ show_site_settings_button();}void LocationEdit::update_zoom_indicator()diff --git a/UI/Qt/SiteSettingsPopover.cpp b/UI/Qt/SiteSettingsPopover.cppindex 2886db0fb9e..1d83427e864 100644--- a/UI/Qt/SiteSettingsPopover.cpp+++ b/UI/Qt/SiteSettingsPopover.cpp@@ -24,6 +24,9 @@ namespace Ladybird {static constexpr int POPOVER_WIDTH = 344;static constexpr int ROW_HEIGHT = 36;static constexpr int BLOCKED_RESOURCE_ROW_HEIGHT = 26;+// NB: The heading of a page without a site leaves its text the card's width less the card's border, the page's and the+// heading's side margins, and the favicon and the space after it.+static constexpr int NO_SITE_TITLE_WIDTH = POPOVER_WIDTH - 2 * 1 - 2 * 6 - 2 * 10 - 28 - 10;static void add_separator(QWidget& parent, QVBoxLayout& layout, int top_margin, int bottom_margin){@@ -45,6 +48,13 @@ static QLabel* create_icon(QWidget& parent)return label;}+// A page's favicon for the heading of the popover, or a globe for a page without one.+static QPixmap heading_favicon_pixmap(QIcon const& favicon, qreal device_pixel_ratio)+{+ auto const& icon = favicon.isNull() ? create_chrome_icon(ChromeIcon::Globe, Theme::panel().muted) : favicon;+ return icon.pixmap(QSize(16, 16), device_pixel_ratio);+}+static QPushButton* create_link(QWidget& parent, QString const& text){auto* link = new QPushButton(text, &parent);@@ -355,6 +365,50 @@ SiteSettingsPopover::SiteSettingsPopover(QWidget* parent, WebView::Settings& setpages->addWidget(m_blocked_page);m_blocked_page->hide();+ // A page that is not from a website.+ m_no_site_page = new QWidget(&card());+ auto* no_site_layout = new QVBoxLayout(m_no_site_page);+ no_site_layout->setContentsMargins(6, 14, 6, 8);+ no_site_layout->setSpacing(0);++ auto* no_site_heading = new QHBoxLayout;+ no_site_heading->setContentsMargins(10, 0, 10, 0);+ no_site_heading->setSpacing(10);+ m_no_site_favicon = new QLabel(m_no_site_page);+ m_no_site_favicon->setFixedSize(28, 28);+ m_no_site_favicon->setAlignment(Qt::AlignCenter);+ m_no_site_favicon->setObjectName("LadybirdSiteSettingsFavicon");+ no_site_heading->addWidget(m_no_site_favicon, 0, Qt::AlignTop);+ auto* no_site_text = new QVBoxLayout;+ no_site_text->setSpacing(2);+ m_no_site_title = new QLabel(m_no_site_page);+ m_no_site_title->setObjectName("LadybirdSiteSettingsHost");+ m_no_site_title->setTextFormat(Qt::PlainText);+ no_site_text->addWidget(m_no_site_title);+ m_no_site_explanation = new QLabel(m_no_site_page);+ m_no_site_explanation->setObjectName("LadybirdSiteSettingsDetail");+ m_no_site_explanation->setTextFormat(Qt::PlainText);+ m_no_site_explanation->setWordWrap(true);+ no_site_text->addWidget(m_no_site_explanation);+ no_site_heading->addLayout(no_site_text, 1);+ no_site_layout->addLayout(no_site_heading);++ add_separator(*m_no_site_page, *no_site_layout, 12, 2);++ auto* no_site_links = new QHBoxLayout;+ no_site_links->setContentsMargins(4, 0, 4, 0);+ no_site_links->addStretch(1);+ auto* no_site_settings_link = create_link(*m_no_site_page, "All site settings");+ QObject::connect(no_site_settings_link, &QPushButton::clicked, this, [this] {+ close();+ if (on_open_all_site_settings)+ on_open_all_site_settings();+ });+ no_site_links->addWidget(no_site_settings_link);+ no_site_layout->addLayout(no_site_links);+ pages->addWidget(m_no_site_page);+ m_no_site_page->hide();+update_icons();}@@ -369,14 +423,31 @@ void SiteSettingsPopover::set_site(Site site)m_security_label->setText("Connection is not secure");m_security_label->setVisible(site.origin.scheme() != "https"sv);- m_favicon->setPixmap(site.favicon.isNull() ? create_chrome_icon(ChromeIcon::Globe, Theme::panel().muted).pixmap(QSize(16, 16), devicePixelRatioF()) : site.favicon.pixmap(QSize(16, 16), devicePixelRatioF()));+ m_favicon->setPixmap(heading_favicon_pixmap(site.favicon, devicePixelRatioF()));set_permissions(site.permissions);update_autoplay();+ m_no_site_page->hide();show_blocked_resources(false);set_content_blocking(site.content_blocking);}+void SiteSettingsPopover::set_page_without_site(QString const& title, QString const& explanation, QIcon const& favicon)+{+ // NB: A long title, such as the path of a file or one a sandboxed page chose, would widen the text past the card,+ // so it is cut short. The style sheet sets the title's font once it is polished.+ m_no_site_title->ensurePolished();+ m_no_site_title->setText(m_no_site_title->fontMetrics().elidedText(title, Qt::ElideRight, NO_SITE_TITLE_WIDTH));+ m_no_site_explanation->setText(explanation);+ m_no_site_favicon->setPixmap(heading_favicon_pixmap(favicon, devicePixelRatioF()));++ // NB: The page shown before is hidden first, so that the popover never has to fit two pages at once.+ m_site_page->hide();+ m_blocked_page->hide();+ m_no_site_page->show();+ fit_to_page();+}+// A permission the user never decided and the page never asked for has no row.void SiteSettingsPopover::set_permissions(Vector<WebView::SitePermissionRow> const& permissions){@@ -495,7 +566,7 @@ void SiteSettingsPopover::fit_to_page(){// NB: The page's and the card's layouts take in a row that shows or hides only once they are activated, so they are// activated before the popover's own, which would otherwise keep the card at its old height.- for (auto* page : { m_site_page, m_blocked_page }) {+ for (auto* page : { m_site_page, m_blocked_page, m_no_site_page }) {if (!page->isHidden())page->layout()->activate();}diff --git a/UI/Qt/SiteSettingsPopover.h b/UI/Qt/SiteSettingsPopover.hindex bbb70c78624..4b511f1cbfe 100644--- a/UI/Qt/SiteSettingsPopover.h+++ b/UI/Qt/SiteSettingsPopover.h@@ -42,6 +42,9 @@ public:Vector<WebView::SitePermissionRow> permissions;};void set_site(Site);+ // A page that is not from a website, such as about:blank or a file, has no settings, so the popover says so+ // instead.+ void set_page_without_site(QString const& title, QString const& explanation, QIcon const& favicon);void set_content_blocking(WebView::ContentBlockingSnapshot const&);void set_permissions(Vector<WebView::SitePermissionRow> const&);void update_chrome_style(QPalette const&);@@ -78,6 +81,10 @@ private:PopoverSwitch* m_force_dark_switch { nullptr };QWidget* m_site_page { nullptr };QWidget* m_blocked_page { nullptr };+ QWidget* m_no_site_page { nullptr };+ QLabel* m_no_site_favicon { nullptr };+ QLabel* m_no_site_title { nullptr };+ QLabel* m_no_site_explanation { nullptr };QLabel* m_favicon { nullptr };QLabel* m_host_label { nullptr };QLabel* m_security_label { nullptr };diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex 6b9322706a4..254b4919c9f 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -247,7 +247,8 @@ void Tab::initialize(Optional<u64> hub_view_id)m_content_blocking_update_timer->setSingleShot(true);m_content_blocking_update_timer->setInterval(100);QObject::connect(m_content_blocking_update_timer, &QTimer::timeout, this, [this] {- if (m_site_settings_popover && m_site_settings_popover->isVisible())+ // NB: A page that is not from a website has no content blocking settings to show.+ if (m_site_settings_popover && m_site_settings_popover->isVisible() && m_site_settings_popover_origin.has_value())m_site_settings_popover->set_content_blocking(view().content_blocking_snapshot());});view().on_content_blocking_change = [this](auto const&) {@@ -1513,11 +1514,23 @@ void Tab::close_popovers()}}+// Settings are kept for websites, so a page that is not from one says why it has none.+static QString explanation_for_page_without_site(URL::URL const& url)+{+ if (url.scheme() == "about"sv)+ return QStringLiteral("Ladybird pages have no site settings.");+ if (url.scheme() == "file"sv)+ return QStringLiteral("Files on this computer have no site settings.");+ // NB: A page from a website can still have no origin of its own to keep settings for, for example when its+ // Content-Security-Policy sandboxes it.+ if (url.scheme().is_one_of("http"sv, "https"sv))+ return QStringLiteral("This page has no site settings.");+ return QStringLiteral("Pages that are not from a website have no site settings.");+}+void Tab::show_site_settings_popover(){auto origin = view().permission_origin();- if (origin.is_opaque())- return;if (!m_site_settings_popover) {m_site_settings_popover = new SiteSettingsPopover(this, Application::settings());@@ -1551,24 +1564,29 @@ void Tab::show_site_settings_popover()};}- m_site_settings_popover_origin = origin;m_site_settings_popover->update_chrome_style(palette());- m_site_settings_popover->set_site({- .origin = AK::move(origin),- .host_for_settings = view().current_host_for_settings(),- .is_private = view().is_private() == WebView::IsPrivate::Yes,- .favicon = m_favicon,- .content_blocking = view().content_blocking_snapshot(),- .permissions = view().site_permission_rows(),- });+ if (origin.is_opaque()) {+ // NB: The popover no longer shows the site it was last opened for.+ m_site_settings_popover_origin = {};+ m_site_settings_popover->set_page_without_site(title(), explanation_for_page_without_site(url()), m_favicon);+ } else {+ m_site_settings_popover_origin = origin;+ m_site_settings_popover->set_site({+ .origin = AK::move(origin),+ .host_for_settings = view().current_host_for_settings(),+ .is_private = view().is_private() == WebView::IsPrivate::Yes,+ .favicon = m_favicon,+ .content_blocking = view().content_blocking_snapshot(),+ .permissions = view().site_permission_rows(),+ });+ }move_popover_below(*m_site_settings_popover, site_settings_anchor(), PopoverAlignment::Leading);m_site_settings_popover->show();move_popover_below(*m_site_settings_popover, site_settings_anchor(), PopoverAlignment::Leading);m_site_settings_popover->raise();}-// What the popovers about a site hang from: the site settings button, or the field for a page with no host, which has no-// such button.+// What the popovers about a site hang from: the site settings button, or the field before the button is first shown.QWidget& Tab::site_settings_anchor() const{auto& button = m_location_edit->site_settings_button();@@ -1579,7 +1597,7 @@ QWidget& Tab::site_settings_anchor() const// instead, since its rows would be the new site's.void Tab::refresh_site_settings_popover(){- if (!m_site_settings_popover)+ if (!m_site_settings_popover || !m_site_settings_popover_origin.has_value())return;if (!view().permission_origin().is_same_origin(*m_site_settings_popover_origin)) {m_site_settings_popover->close();