Skip to content

Truly independent web browser

LibWebView: List per-site force-dark choices in settings

sideshowbarkerc6375a86e7c6
diff --git a/Base/res/ladybird/about-pages/settings.html b/Base/res/ladybird/about-pages/settings.html
index 87cf198d7d4..12ec8d2a175 100644
--- a/Base/res/ladybird/about-pages/settings.html
+++ b/Base/res/ladybird/about-pages/settings.html
@@ -297,7 +297,8 @@
flex-grow: 1;
}
- dialog .dialog-list-item {
+ dialog .dialog-list-item,
+ #force-dark-sites .dialog-list-item {
padding: 10px;
display: flex;
@@ -305,7 +306,8 @@
align-items: center;
}
- dialog .dialog-list-item:hover {
+ dialog .dialog-list-item:hover,
+ #force-dark-sites .dialog-list-item:hover {
background-color: var(--control);
}
@@ -314,10 +316,24 @@
opacity: 0.6;
}
- dialog .dialog-list-item-label {
+ dialog .dialog-list-item-label,
+ #force-dark-sites .dialog-list-item-label {
flex-grow: 1;
}
+ #force-dark-sites .dialog-list-item {
+ gap: 16px;
+ }
+
+ #force-dark-sites .dialog-list-item-label {
+ min-width: 0;
+ overflow-wrap: anywhere;
+ }
+
+ #force-dark-sites button {
+ flex-shrink: 0;
+ }
+
dialog .dialog-controls {
display: flex;
justify-content: flex-end;
@@ -798,6 +814,7 @@
</label>
<input id="enable-force-dark" type="checkbox" switch />
</div>
+ <div id="force-dark-sites" class="card-group" aria-label="Sites with a force-dark choice"></div>
</div>
</div>
diff --git a/Base/res/ladybird/about-pages/settings/content.js b/Base/res/ladybird/about-pages/settings/content.js
index 1b0bf72d609..8ea92d9212a 100644
--- a/Base/res/ladybird/about-pages/settings/content.js
+++ b/Base/res/ladybird/about-pages/settings/content.js
@@ -1,9 +1,14 @@
const defaultZoomLevel = document.querySelector("#default-zoom-level");
const enableForceDark = document.querySelector("#enable-force-dark");
+const forceDarkSites = document.querySelector("#force-dark-sites");
let CONTENT = {};
+let forceDarkSitesReadOnly = false;
const loadSettings = settings => {
+ if (Object.hasOwn(settings, "forceDarkSitesReadOnly")) {
+ forceDarkSitesReadOnly = settings.forceDarkSitesReadOnly;
+ }
if (!Object.hasOwn(settings, "content")) {
return;
}
@@ -18,6 +23,24 @@ const loadSettings = settings => {
}
enableForceDark.checked = !!CONTENT.enableForceDark;
+ forceDarkSites.replaceChildren();
+ const choices = Object.entries(CONTENT.forceDarkPerHost || {}).sort(([a], [b]) => a.localeCompare(b));
+ forceDarkSites.hidden = choices.length === 0;
+ for (const [host, enabled] of choices) {
+ const row = document.createElement("div");
+ row.className = "dialog-list-item";
+ row.dataset.host = host;
+ const label = document.createElement("span");
+ label.className = "dialog-list-item-label";
+ label.textContent = `${host}: ${enabled ? "On" : "Off"}`;
+ const reset = document.createElement("button");
+ reset.textContent = "Use default";
+ reset.disabled = forceDarkSitesReadOnly;
+ reset.setAttribute("aria-label", `Use default force-dark setting for ${host}`);
+ reset.addEventListener("click", () => ladybird.sendMessage("resetForceDarkForHost", host));
+ row.append(label, reset);
+ forceDarkSites.appendChild(row);
+ }
};
const ZOOM_LEVEL_FACTORS = [1 / 3.0, 0.5, 2 / 3.0, 0.75, 0.8, 0.9, 1.0, 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0];
diff --git a/Libraries/LibWebView/WebUI/SettingsUI.cpp b/Libraries/LibWebView/WebUI/SettingsUI.cpp
index 1ca4493a0f0..7ba7223281c 100644
--- a/Libraries/LibWebView/WebUI/SettingsUI.cpp
+++ b/Libraries/LibWebView/WebUI/SettingsUI.cpp
@@ -18,6 +18,7 @@
#include <LibWebView/CrashReportStore.h>
#include <LibWebView/PermissionManager.h>
#include <LibWebView/SearchEngine.h>
+#include <LibWebView/WebContentClient.h>
#include <LibWebView/WebUI/SettingsUI.h>
namespace WebView {
@@ -137,6 +138,9 @@ void SettingsUI::register_interfaces()
register_interface("setAppearance"sv, [this](auto const& data) {
set_appearance(data);
});
+ register_interface("resetForceDarkForHost"sv, [this](auto const& data) {
+ reset_force_dark_for_host(data);
+ });
register_interface("setContentSettings"sv, [this](auto const& data) {
set_content_settings(data);
});
@@ -229,6 +233,7 @@ void SettingsUI::load_features()
JsonObject SettingsUI::serialize_settings() const
{
auto settings = Application::settings().serialize_json();
+ settings.as_object().set("forceDarkSitesReadOnly"sv, !has_client() || client().is_private() == IsPrivate::Yes);
// The sites the user allowed or blocked, for each permission.
JsonObject site_permissions;
@@ -385,10 +390,19 @@ void SettingsUI::set_content_settings(JsonValue const& content_settings)
// Zoom-per-host is not controlled by the settings UI. Don't overwrite it.
parsed_content_settings.zoom_per_host = current_content_settings.zoom_per_host;
+ // Host choices change through their own controls, independently of edits to the default.
+ parsed_content_settings.force_dark_per_host = current_content_settings.force_dark_per_host;
Application::settings().set_content_settings(move(parsed_content_settings));
}
+void SettingsUI::reset_force_dark_for_host(JsonValue const& host)
+{
+ if (!host.is_string() || !has_client() || client().is_private() == IsPrivate::Yes)
+ return;
+ Application::settings().reset_force_dark_for_host(host.as_string());
+}
+
void SettingsUI::set_tab_settings(JsonValue const& tab_settings)
{
auto& settings = Application::settings();
diff --git a/Libraries/LibWebView/WebUI/SettingsUI.h b/Libraries/LibWebView/WebUI/SettingsUI.h
index acd7e6f438d..6752d3560e8 100644
--- a/Libraries/LibWebView/WebUI/SettingsUI.h
+++ b/Libraries/LibWebView/WebUI/SettingsUI.h
@@ -70,6 +70,7 @@ private:
void set_languages(JsonValue const&);
void set_appearance(JsonValue const&);
void set_content_settings(JsonValue const&);
+ void reset_force_dark_for_host(JsonValue const&);
void set_tab_settings(JsonValue const&);
void set_browsing_behavior(JsonValue const&);
void set_config_variable(JsonValue const&);
diff --git a/Tests/LibWebView/test-webdriver-force-dark.py b/Tests/LibWebView/test-webdriver-force-dark.py
index 49555c7a748..c66495a8f53 100755
--- a/Tests/LibWebView/test-webdriver-force-dark.py
+++ b/Tests/LibWebView/test-webdriver-force-dark.py
@@ -169,6 +169,60 @@ def run_test(default):
wait_for_page(target_url + "-same-host-next")
assert_force_dark("Reload", not default)
print(f"PASS: per-host force-dark across navigation (default={default})")
+
+ def evaluate(script):
+ status, payload, raw = command("POST", "execute/sync", {"script": script, "args": []})
+ assert status == 200, raw
+ return payload["value"]
+
+ def wait_for_condition(script):
+ deadline = time.monotonic() + 10
+ while time.monotonic() < deadline:
+ if evaluate(script):
+ return
+ time.sleep(0.01)
+ raise AssertionError(f"Condition did not become true: {script}")
+
+ status, payload, raw = command("GET", "window")
+ assert status == 200, raw
+ site_handle = payload["value"]
+ status, payload, raw = command("POST", "window/new", {"type": "tab"})
+ assert status == 200, raw
+ settings_handle = payload["value"]["handle"]
+
+ def switch_to(handle):
+ status, _, raw = command("POST", "window", {"handle": handle})
+ assert status == 200, raw
+
+ switch_to(settings_handle)
+ status, _, raw = command("POST", "url", {"url": "about:settings"})
+ assert status == 200, raw
+ wait_for_page("about:settings")
+ wait_for_condition("return document.querySelector('#force-dark-sites button') !== null")
+ assert evaluate(
+ "return document.querySelector('#force-dark-sites').textContent.includes('localhost: ' + "
+ + ("'Off'" if default else "'On'")
+ + ")"
+ )
+ evaluate("document.querySelector('#enable-force-dark').click()")
+ wait_for_condition(
+ "return document.querySelector('#enable-force-dark').checked === " + str(not default).lower()
+ )
+ switch_to(site_handle)
+ assert_force_dark("Explicit choice survives a global toggle", not default, check_initial=False)
+ switch_to(settings_handle)
+ evaluate("document.querySelector('#enable-force-dark').click()")
+ wait_for_condition(
+ "return document.querySelector('#enable-force-dark').checked === " + str(default).lower()
+ )
+ evaluate("document.querySelector('#force-dark-sites button').click()")
+ wait_for_condition("return document.querySelector('#force-dark-sites button') === null")
+ switch_to(site_handle)
+ wait_for_condition("return matchMedia('(prefers-color-scheme: dark)').matches === " + str(default).lower())
+ assert_force_dark("Reset updates an already-open page", default, check_initial=False)
+ saved = json.loads((config / "Settings.json").read_text())["content"]
+ assert "localhost" not in saved["forceDarkPerHost"]
+ print(f"PASS: settings toggle and per-host reset (default={default})")
finally:
try:
if session is not None: