Truly independent web browser
Qt: Add force-dark to the site settings popover
sideshowbarker933d66348283
diff --git a/Tests/UI/Qt/CMakeLists.txt b/Tests/UI/Qt/CMakeLists.txtindex 6a507cb0523..ba675a8d62d 100644--- a/Tests/UI/Qt/CMakeLists.txt+++ b/Tests/UI/Qt/CMakeLists.txt@@ -56,6 +56,7 @@ set(TEST_SOURCESTestScreenshotSelectionOverlay.cppTestScreenshotViewer.cppTestSelectDropdown.cpp+ TestSiteSettingsPopover.cppTestWebCompatReportReview.cppTestWebContentViewInputMethod.cpp"${LADYBIRD_SOURCE_DIR}/Libraries/LibWebView/ProcessMonitor.cpp"@@ -70,6 +71,7 @@ set(TEST_SOURCES"${LADYBIRD_SOURCE_DIR}/UI/Qt/ScreenshotSelectionOverlay.cpp""${LADYBIRD_SOURCE_DIR}/UI/Qt/ScreenshotViewer.cpp""${LADYBIRD_SOURCE_DIR}/UI/Qt/SelectDropdown.cpp"+ "${LADYBIRD_SOURCE_DIR}/UI/Qt/SiteSettingsPopover.cpp""${LADYBIRD_SOURCE_DIR}/UI/Qt/StringUtils.cpp"# The chrome's styles take their colors from the theme, which follows the system's appearance."${LADYBIRD_SOURCE_DIR}/UI/Qt/SystemAppearance.cpp"diff --git a/Tests/UI/Qt/TestSiteSettingsPopover.cpp b/Tests/UI/Qt/TestSiteSettingsPopover.cppnew file mode 100644index 00000000000..e3aecd433ce--- /dev/null+++ b/Tests/UI/Qt/TestSiteSettingsPopover.cpp@@ -0,0 +1,159 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/LexicalPath.h>+#include <AK/ScopeGuard.h>+#include <LibCore/EventLoop.h>+#include <LibCore/StandardPaths.h>+#include <LibCore/System.h>+#include <LibFileSystem/FileSystem.h>+#include <LibTest/TestCase.h>+#include <LibWebView/Settings.h>+#include <UI/Qt/MacWindow.h>+#include <UI/Qt/SiteSettingsPopover.h>++#include <QCheckBox>+#include <QMenu>+#include <QPushButton>+#include <QTimer>++#if defined(AK_OS_MACOS)+// The offscreen platform has no AppKit window to animate.+void Ladybird::disable_appkit_window_animations(QWidget&)+{+}+#endif++static ByteString settings_path()+{+ return LexicalPath::join(Core::StandardPaths::tempfile_directory(), ByteString::formatted("test-site-popover-{}.json", Core::System::getpid())).string();+}++static void remove_settings()+{+ if (FileSystem::exists(settings_path()))+ MUST(FileSystem::remove(settings_path(), FileSystem::RecursionMode::Disallowed));+}++static Ladybird::SiteSettingsPopover::Site site_for(String force_dark_host = "example.com"_string, bool is_private = false)+{+ Ladybird::SiteSettingsPopover::Site site {};+ site.host = "example.com"_string;+ site.force_dark_host = move(force_dark_host);+ site.is_private = is_private;+ return site;+}++static QCheckBox& force_dark_switch(Ladybird::SiteSettingsPopover& popover)+{+ for (auto* widget : popover.findChildren<QCheckBox*>()) {+ if (widget->accessibleName() == "Force dark mode on this site")+ return *widget;+ }+ VERIFY_NOT_REACHED();+}++static QPushButton& reset_button(Ladybird::SiteSettingsPopover& popover)+{+ for (auto* widget : popover.findChildren<QPushButton*>()) {+ if (widget->text() == "Reset permissions")+ return *widget;+ }+ VERIFY_NOT_REACHED();+}++TEST_CASE(force_dark_switch_saves_explicit_choices_and_observes_changes)+{+ remove_settings();+ ScopeGuard cleanup { remove_settings };+ auto settings = WebView::Settings::create(settings_path());+ Ladybird::SiteSettingsPopover popover(nullptr, settings);+ popover.set_site(site_for());+ auto& toggle = force_dark_switch(popover);+ EXPECT(toggle.isEnabled());+ EXPECT(!toggle.isChecked());+ EXPECT(!reset_button(popover).isEnabled());+ toggle.click();+ EXPECT(settings.force_dark_for_host("example.com"sv));+ toggle.click();+ EXPECT_EQ(settings.content_settings().force_dark_per_host.get("example.com"sv), false);+ EXPECT(reset_button(popover).isEnabled());+ settings.set_force_dark_for_host("example.com"_string, true);+ EXPECT(toggle.isChecked());+ reset_button(popover).click();+ EXPECT(!toggle.isChecked());+ EXPECT(!settings.content_settings().force_dark_per_host.contains("example.com"sv));+ EXPECT(!reset_button(popover).isEnabled());+}++TEST_CASE(private_site_controls_apply_saved_choices_without_changing_them)+{+ remove_settings();+ ScopeGuard cleanup { remove_settings };+ auto settings = WebView::Settings::create(settings_path());+ settings.set_force_dark_for_host("example.com"_string, true);+ settings.add_autoplay_site_filter("example.com"_string);+ Ladybird::SiteSettingsPopover popover(nullptr, settings);+ popover.set_site(site_for("example.com"_string, true));+ auto& toggle = force_dark_switch(popover);+ EXPECT(!toggle.isEnabled());+ EXPECT(toggle.isChecked());+ toggle.click();+ reset_button(popover).click();+ EXPECT_EQ(settings.content_settings().force_dark_per_host.get("example.com"sv), true);+}++TEST_CASE(hostless_site_uses_only_the_default)+{+ remove_settings();+ ScopeGuard cleanup { remove_settings };+ auto settings = WebView::Settings::create(settings_path());+ settings.set_force_dark_for_host("example.com"_string, true);+ Ladybird::SiteSettingsPopover popover(nullptr, settings);+ popover.set_site(site_for());+ EXPECT(force_dark_switch(popover).isChecked());+ popover.set_site(site_for({}));+ auto& toggle = force_dark_switch(popover);+ EXPECT(!toggle.isEnabled());+ EXPECT(!toggle.isChecked());+ auto content = settings.content_settings();+ content.enable_force_dark = true;+ settings.set_content_settings(move(content));+ EXPECT(toggle.isChecked());+ EXPECT(!toggle.isEnabled());+ EXPECT_EQ(settings.content_settings().force_dark_per_host.size(), 1u);+}++TEST_CASE(canceling_autoplay_does_not_reset_force_dark)+{+ remove_settings();+ ScopeGuard cleanup { remove_settings };+ auto settings = WebView::Settings::create(settings_path());+ settings.set_force_dark_for_host("example.com"_string, true);+ Core::EventLoop event_loop;+ Ladybird::SiteSettingsPopover popover(nullptr, settings);+ popover.set_site(site_for());+ popover.show();+ QPushButton* autoplay = nullptr;+ for (auto* button : popover.findChildren<QPushButton*>()) {+ if (button->objectName() == "LadybirdPopoverValueButton")+ autoplay = button;+ }+ VERIFY(autoplay);+ bool closed_menu = false;+ QTimer::singleShot(0, &popover, [&] {+ auto* menu = popover.findChild<QMenu*>();+ EXPECT(menu);+ if (!menu)+ return;+ closed_menu = true;+ menu->close();+ });+ autoplay->click();+ EXPECT(closed_menu);+ EXPECT_EQ(settings.content_settings().force_dark_per_host.get("example.com"sv), true);+ EXPECT(force_dark_switch(popover).isChecked());+}diff --git a/UI/Qt/Icon.cpp b/UI/Qt/Icon.cppindex d93bac3d543..0225f0c976c 100644--- a/UI/Qt/Icon.cpp+++ b/UI/Qt/Icon.cpp@@ -364,6 +364,8 @@ static StringView chrome_icon_path(ChromeIcon icon)return CHEVRON_DOWN_ICON_PATH;case ChromeIcon::ContentBlocking:return CONTENT_BLOCKING_ICON_PATH;+ case ChromeIcon::Moon:+ return "M13.5 9.5A5.8 5.8 0 0 1 6.5 2.5A5.8 5.8 0 1 0 13.5 9.5Z"sv;case ChromeIcon::SiteSettings:return SITE_SETTINGS_ICON_PATH;case ChromeIcon::Microphone:diff --git a/UI/Qt/Icon.h b/UI/Qt/Icon.hindex e6b2ba66889..705efc3de14 100644--- a/UI/Qt/Icon.h+++ b/UI/Qt/Icon.h@@ -43,6 +43,7 @@ enum class ChromeIcon {ChevronUp,ChevronDown,ContentBlocking,+ Moon,SiteSettings,Microphone,Location,diff --git a/UI/Qt/SiteSettingsPopover.cpp b/UI/Qt/SiteSettingsPopover.cppindex 0b22f0a262f..38a8719034e 100644--- a/UI/Qt/SiteSettingsPopover.cpp+++ b/UI/Qt/SiteSettingsPopover.cpp@@ -4,7 +4,6 @@* SPDX-License-Identifier: BSD-2-Clause*/-#include <LibWebView/Application.h>#include <UI/Qt/ChromeStyle.h>#include <UI/Qt/Icon.h>#include <UI/Qt/SiteSettingsPopover.h>@@ -126,8 +125,10 @@ static QPushButton* create_value_button(QWidget& parent)return button;}-SiteSettingsPopover::SiteSettingsPopover(QWidget* parent)+SiteSettingsPopover::SiteSettingsPopover(QWidget* parent, WebView::Settings& settings): Popover(parent)+ , WebView::SettingsObserver(settings)+ , m_settings(settings){setAccessibleName("Site settings");card().setObjectName("LadybirdSiteSettingsPopover");@@ -186,7 +187,7 @@ SiteSettingsPopover::SiteSettingsPopover(QWidget* parent)QObject::connect(m_autoplay_button, &QPushButton::clicked, this, [this] {// A site is either allowed to autoplay, or follows the default, which the menu names so that the choice it shows// on the button is always one of those in the menu.- auto const& autoplay = WebView::Application::settings().autoplay_settings();+ auto const& autoplay = m_settings.autoplay_settings();auto is_allowed_on_site = autoplay.site_filters.contains(m_host);QMenu menu(this);auto* allow = menu.addAction("Allow");@@ -197,9 +198,9 @@ SiteSettingsPopover::SiteSettingsPopover(QWidget* parent)use_default->setChecked(!is_allowed_on_site);auto* chosen = menu.exec(m_autoplay_button->mapToGlobal(QPoint(0, m_autoplay_button->height())));if (chosen == allow)- WebView::Application::settings().add_autoplay_site_filter(m_host);+ m_settings.add_autoplay_site_filter(m_host);else if (chosen == use_default)- WebView::Application::settings().remove_autoplay_site_filter(m_host);+ m_settings.remove_autoplay_site_filter(m_host);update_autoplay();});autoplay_row->addWidget(m_autoplay_button);@@ -207,6 +208,24 @@ SiteSettingsPopover::SiteSettingsPopover(QWidget* parent)add_separator(*m_site_page, *layout, 6, 6);+ // Whether pages on the site are darkened, overriding the default for every site.+ auto* force_dark_row = new QHBoxLayout;+ force_dark_row->setContentsMargins(10, 0, 10, 0);+ force_dark_row->setSpacing(10);+ m_force_dark_icon = create_icon(*m_site_page);+ force_dark_row->addWidget(m_force_dark_icon);+ auto* force_dark_label = new QLabel("Force dark mode", m_site_page);+ force_dark_label->setFixedHeight(ROW_HEIGHT);+ force_dark_row->addWidget(force_dark_label, 1);+ m_force_dark_switch = new PopoverSwitch(m_site_page);+ m_force_dark_switch->setAccessibleName("Force dark mode on this site");+ QObject::connect(m_force_dark_switch, &QCheckBox::toggled, this, [this](bool enabled) {+ if (!m_is_private && !m_force_dark_host.is_empty())+ m_settings.set_force_dark_for_host(m_force_dark_host, enabled);+ });+ force_dark_row->addWidget(m_force_dark_switch);+ layout->addLayout(force_dark_row);+// Whether content blocking is on for the site, and what it blocked on the page.auto* blocking_row = new QHBoxLayout;blocking_row->setContentsMargins(10, 0, 10, 0);@@ -256,7 +275,9 @@ SiteSettingsPopover::SiteSettingsPopover(QWidget* parent)// What can be set for the site is reset to the defaults.m_reset_permissions_link = create_link(*m_site_page, "Reset permissions");QObject::connect(m_reset_permissions_link, &QPushButton::clicked, this, [this] {- WebView::Application::settings().remove_autoplay_site_filter(m_host);+ m_settings.remove_autoplay_site_filter(m_host);+ if (!m_is_private)+ m_settings.reset_force_dark_for_host(m_force_dark_host);update_autoplay();if (on_reset_permissions)on_reset_permissions();@@ -335,6 +356,9 @@ SiteSettingsPopover::SiteSettingsPopover(QWidget* parent)void SiteSettingsPopover::set_site(Site site){m_host = AK::move(site.host);+ m_force_dark_host = AK::move(site.force_dark_host);+ m_is_private = site.is_private;+ content_settings_changed();m_host_label->setText(qstring_from_ak_string(m_host));// A secure connection is the norm, so only an insecure one is mentioned.m_security_label->setText("Connection is not secure");@@ -475,22 +499,31 @@ void SiteSettingsPopover::fit_to_page()adjustSize();}+void SiteSettingsPopover::content_settings_changed()+{+ QSignalBlocker blocker(m_force_dark_switch);+ m_force_dark_switch->setChecked(m_settings.force_dark_for_host(m_force_dark_host));+ m_force_dark_switch->setEnabled(!m_is_private && !m_force_dark_host.is_empty());+ update_reset_link();+}+void SiteSettingsPopover::update_autoplay(){- auto const& autoplay = WebView::Application::settings().autoplay_settings();+ auto const& autoplay = m_settings.autoplay_settings();auto name = autoplay.site_filters.contains(m_host) ? QStringLiteral("Allow") : autoplay_policy_name(autoplay.policy);m_autoplay_button->setText(name);m_autoplay_button->setIcon(create_chrome_icon(ChromeIcon::ChevronDown, Theme::panel().muted));update_reset_link();}-// There is something to reset once the site has a decision of its own, for a permission or for autoplay.+// There is something to reset once the site has a decision of its own.void SiteSettingsPopover::update_reset_link(){if (!m_reset_permissions_link)return;- auto const& autoplay = WebView::Application::settings().autoplay_settings();- m_reset_permissions_link->setEnabled(m_has_decided_permissions || autoplay.site_filters.contains(m_host));+ auto const& autoplay = m_settings.autoplay_settings();+ auto has_force_dark_choice = !m_is_private && m_settings.content_settings().force_dark_per_host.contains(m_force_dark_host);+ m_reset_permissions_link->setEnabled(m_has_decided_permissions || autoplay.site_filters.contains(m_host) || has_force_dark_choice);}void SiteSettingsPopover::update_chrome_style(QPalette const& palette)@@ -509,6 +542,7 @@ void SiteSettingsPopover::update_icons()permission.button->setIcon(create_chrome_icon(ChromeIcon::ChevronDown, panel.muted));}m_autoplay_icon->setPixmap(create_chrome_icon(ChromeIcon::Volume, panel.body).pixmap(QSize(16, 16), devicePixelRatioF()));+ m_force_dark_icon->setPixmap(create_chrome_icon(ChromeIcon::Moon, panel.body).pixmap(QSize(16, 16), devicePixelRatioF()));m_content_blocking_icon->setPixmap(create_chrome_icon(ChromeIcon::ContentBlocking, panel.body).pixmap(QSize(16, 16), devicePixelRatioF()));m_blocked_row_chevron->setPixmap(create_chrome_icon(ChromeIcon::Forward, panel.muted).pixmap(QSize(12, 12), devicePixelRatioF()));m_back_button->setIcon(create_chrome_icon(ChromeIcon::Back, panel.body));diff --git a/UI/Qt/SiteSettingsPopover.h b/UI/Qt/SiteSettingsPopover.hindex 7a5335b1b07..205a221d028 100644--- a/UI/Qt/SiteSettingsPopover.h+++ b/UI/Qt/SiteSettingsPopover.h@@ -25,14 +25,17 @@ class QVBoxLayout;namespace Ladybird {// What Ladybird knows about the site a page is from, and the settings that apply to it.-class SiteSettingsPopover final : public Popover {+class SiteSettingsPopover final : public Popover+ , public WebView::SettingsObserver {public:AK_ALLOC_WITH_KMALLOC;- explicit SiteSettingsPopover(QWidget* parent);+ explicit SiteSettingsPopover(QWidget* parent, WebView::Settings&);struct Site {String host;+ String force_dark_host;+ bool is_private { false };bool is_secure { false };QIcon favicon;WebView::ContentBlockingSnapshot content_blocking;@@ -52,6 +55,7 @@ public:Function<void()> on_open_filter_lists;private:+ virtual void content_settings_changed() override;void update_autoplay();void update_reset_link();void update_icons();@@ -64,7 +68,12 @@ private:QPushButton* button { nullptr };};+ WebView::Settings& m_settings;String m_host;+ String m_force_dark_host;+ bool m_is_private { false };+ QLabel* m_force_dark_icon { nullptr };+ PopoverSwitch* m_force_dark_switch { nullptr };QWidget* m_site_page { nullptr };QWidget* m_blocked_page { nullptr };QLabel* m_favicon { nullptr };diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex 19a771908ac..c6d451f9495 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -1471,7 +1471,7 @@ void Tab::show_site_settings_popover()return;if (!m_site_settings_popover) {- m_site_settings_popover = new SiteSettingsPopover(this);+ m_site_settings_popover = new SiteSettingsPopover(this, WebView::Application::settings());m_location_edit->follow_site_settings_popover(*m_site_settings_popover);m_site_settings_popover->on_content_blocking_toggle = [this](bool enabled) {auto snapshot = view().content_blocking_snapshot();@@ -1506,6 +1506,8 @@ void Tab::show_site_settings_popover()m_site_settings_popover->update_chrome_style(palette());m_site_settings_popover->set_site({.host = url.host().has_value() ? MUST(String::from_utf8(url.serialized_host())) : origin.host().serialize(),+ .force_dark_host = view().force_dark_host(),+ .is_private = view().is_private() == WebView::IsPrivate::Yes,.is_secure = (url.host().has_value() ? url.scheme() : origin.scheme().value_or({})) == "https"sv,.favicon = m_favicon,.content_blocking = view().content_blocking_snapshot(),