Skip to content

Truly independent web browser

LibWebView+Hub+UI/Qt: Let pages follow the system’s contrast setting

sideshowbarker910649b2f6cc
diff --git a/Libraries/LibWeb/HTML/Window.cpp b/Libraries/LibWeb/HTML/Window.cpp
index d6adf5a5851..0eef7734140 100644
--- a/Libraries/LibWeb/HTML/Window.cpp
+++ b/Libraries/LibWeb/HTML/Window.cpp
@@ -689,7 +689,8 @@ CSS::Parser::ValueParserFFI::FfiMediaFeatureValue Window::query_media_feature(CS
return ident_media_feature_value(CSS::Keyword::NoPreference);
case CSS::PreferredContrast::Auto:
default:
- // FIXME: Fallback to system settings
+ // The browser resolves Auto from the platform's settings before a page sees it, so Auto only remains where
+ // nothing reads them.
return ident_media_feature_value(CSS::Keyword::NoPreference);
}
case CSS::MediaFeatureID::PrefersReducedData:
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 89f66ef6381..57a810a1e67 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -2924,6 +2924,17 @@ void Application::set_preferred_contrast(Web::CSS::PreferredContrast contrast)
});
}
+void Application::platform_contrast_preference_changed()
+{
+ // Only Auto follows the platform.
+ if (m_contrast != Web::CSS::PreferredContrast::Auto)
+ return;
+ ViewImplementation::for_each_view([&](ViewImplementation& view) {
+ view.set_preferred_contrast(m_contrast);
+ return IterationDecision::Continue;
+ });
+}
+
void Application::set_preferred_motion(Web::CSS::PreferredMotion motion)
{
m_motion = motion;
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.h
index be3f191a60b..c98c84f6550 100644
--- a/Libraries/LibWebView/Application.h
+++ b/Libraries/LibWebView/Application.h
@@ -157,6 +157,10 @@ public:
virtual bool platform_prefers_reduced_motion() const { return false; }
// Tells every page of a change to what platform_prefers_reduced_motion() returns.
void platform_motion_preference_changed();
+ // Returns true if the platform's settings ask for more contrast, which pages see while the contrast preference is Auto.
+ virtual bool platform_prefers_more_contrast() const { return false; }
+ // Tells every page of a change to what platform_prefers_more_contrast() returns.
+ void platform_contrast_preference_changed();
// The languages the user prefers, as the platform lists them (BCP 47 tags, most preferred first).
virtual Vector<String> platform_preferred_languages() const { return {}; }
// The GPU the platform presents on, which the compositor paints on too, so that it can share its frames without a copy.
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 9e95e9ed7b3..148e417ec72 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -1593,11 +1593,19 @@ void ViewImplementation::update_interface_color_scheme()
});
}
+// Pages can't read the platform's settings, so Auto reaches them as what the platform asks for.
+static Web::CSS::PreferredContrast contrast_for_pages(Web::CSS::PreferredContrast contrast)
+{
+ if (contrast != Web::CSS::PreferredContrast::Auto)
+ return contrast;
+ return Application::the().platform_prefers_more_contrast() ? Web::CSS::PreferredContrast::More : Web::CSS::PreferredContrast::NoPreference;
+}
+
void ViewImplementation::set_preferred_contrast(Web::CSS::PreferredContrast contrast)
{
m_preferred_contrast = contrast;
traversable().for_each_hosting_page([&](WebContentPage& page) {
- page.async_set_preferred_contrast(contrast);
+ page.async_set_preferred_contrast(contrast_for_pages(contrast));
});
}
@@ -1651,7 +1659,7 @@ void ViewImplementation::send_preferences_to_page(WebContentPage& page)
{
send_system_theme_to_page(page);
page.async_set_preferred_color_scheme(preferred_color_scheme_for_page(page));
- page.async_set_preferred_contrast(m_preferred_contrast);
+ page.async_set_preferred_contrast(contrast_for_pages(m_preferred_contrast));
page.async_set_preferred_motion(motion_for_pages(m_preferred_motion));
page.async_set_preferred_languages(Application::settings().languages());
page.async_set_zoom_level(m_zoom_level);
diff --git a/Services/Hub/Application.cpp b/Services/Hub/Application.cpp
index d2ca60feb9f..d46353fd4eb 100644
--- a/Services/Hub/Application.cpp
+++ b/Services/Hub/Application.cpp
@@ -72,11 +72,14 @@ void Application::set_ui_info(Hub::UIInfo info)
{
auto scrollbar_track_press_jumps_changed = !m_ui_info.has_value() || m_ui_info->scrollbar_track_press_jumps != info.scrollbar_track_press_jumps;
auto prefers_reduced_motion_changed = !m_ui_info.has_value() || m_ui_info->prefers_reduced_motion != info.prefers_reduced_motion;
+ auto prefers_more_contrast_changed = !m_ui_info.has_value() || m_ui_info->prefers_more_contrast != info.prefers_more_contrast;
m_ui_info = move(info);
if (scrollbar_track_press_jumps_changed)
platform_scrollbar_track_press_behavior_changed();
if (prefers_reduced_motion_changed)
platform_motion_preference_changed();
+ if (prefers_more_contrast_changed)
+ platform_contrast_preference_changed();
}
View& Application::create_view(View::InitialState initial_state, Optional<WebView::CanonicalTraversable&> traversable)
@@ -586,6 +589,15 @@ bool Application::platform_prefers_reduced_motion() const
return WebView::Application::platform_prefers_reduced_motion();
}
+bool Application::platform_prefers_more_contrast() const
+{
+ if (m_host)
+ return m_host->platform_prefers_more_contrast();
+ if (auto const* info = ui_info())
+ return info->prefers_more_contrast;
+ return WebView::Application::platform_prefers_more_contrast();
+}
+
Vector<String> Application::platform_preferred_languages() const
{
if (m_host)
diff --git a/Services/Hub/Application.h b/Services/Hub/Application.h
index 2d7bfbaf3dc..7d1ffbfb4b2 100644
--- a/Services/Hub/Application.h
+++ b/Services/Hub/Application.h
@@ -38,6 +38,7 @@ public:
virtual bool platform_scrolls_elastically() const = 0;
virtual bool platform_scrollbar_track_press_jumps() const = 0;
virtual bool platform_prefers_reduced_motion() const = 0;
+ virtual bool platform_prefers_more_contrast() const = 0;
virtual Vector<String> platform_preferred_languages() const = 0;
virtual Optional<Gfx::GpuDeviceIdentity> platform_presentation_gpu_device() const = 0;
virtual Vector<u64> platform_presentation_drm_format_modifiers() const = 0;
@@ -122,6 +123,7 @@ private:
virtual bool platform_scrolls_elastically() const override;
virtual bool platform_scrollbar_track_press_jumps() const override;
virtual bool platform_prefers_reduced_motion() const override;
+ virtual bool platform_prefers_more_contrast() const override;
virtual Vector<String> platform_preferred_languages() const override;
virtual Optional<Gfx::GpuDeviceIdentity> platform_presentation_gpu_device() const override;
virtual Vector<u64> platform_presentation_drm_format_modifiers() const override;
diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipc
index d42f9656d97..ca893434de4 100644
--- a/Services/Hub/HubClient.ipc
+++ b/Services/Hub/HubClient.ipc
@@ -134,6 +134,7 @@ struct Hub::UIInfo {
bool scrolls_elastically;
bool scrollbar_track_press_jumps;
bool prefers_reduced_motion;
+ bool prefers_more_contrast;
Vector<String> preferred_languages;
Optional<Gfx::GpuDeviceIdentity> presentation_gpu_device;
Vector<u64> presentation_drm_format_modifiers;
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 127d9260cd5..71bc8048ded 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -725,6 +725,9 @@ ErrorOr<NonnullOwnPtr<Application>> Application::create(Main::Arguments const& a
QObject::connect(&SystemAppearance::the(), &SystemAppearance::reduced_motion_changed, [] {
hub().platform_motion_preference_changed();
});
+ QObject::connect(&SystemAppearance::the(), &SystemAppearance::contrast_changed, [] {
+ hub().platform_contrast_preference_changed();
+ });
#endif
return application;
}
@@ -774,6 +777,7 @@ Hub::UIInfo Application::current_ui_info() const
info.scrolls_elastically = platform_scrolls_elastically();
info.scrollbar_track_press_jumps = platform_scrollbar_track_press_jumps();
info.prefers_reduced_motion = platform_prefers_reduced_motion();
+ info.prefers_more_contrast = platform_prefers_more_contrast();
info.preferred_languages = platform_preferred_languages();
info.presentation_gpu_device = platform_presentation_gpu_device();
info.presentation_drm_format_modifiers = platform_presentation_drm_format_modifiers();
@@ -1004,6 +1008,7 @@ ErrorOr<void> Application::connect_to_hub(Main::Arguments const& arguments)
QObject::connect(&SystemAppearance::the(), &SystemAppearance::changed, send_ui_info);
QObject::connect(&SystemAppearance::the(), &SystemAppearance::interface_appearance_changed, send_ui_info);
QObject::connect(&SystemAppearance::the(), &SystemAppearance::reduced_motion_changed, send_ui_info);
+ QObject::connect(&SystemAppearance::the(), &SystemAppearance::contrast_changed, send_ui_info);
// The Hub may have started with what we told it before a change that came in the meantime.
send_ui_info();
s_browser_has_started = true;
@@ -1326,6 +1331,11 @@ bool Application::platform_prefers_reduced_motion() const
return SystemAppearance::prefers_reduced_motion();
}
+bool Application::platform_prefers_more_contrast() const
+{
+ return Theme::has_increased_contrast();
+}
+
Vector<String> Application::platform_preferred_languages() const
{
// The system's UI languages, most preferred first, as BCP 47 tags ("en-US", then "en").
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index ddf8dd9840d..1e2866c3af8 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -299,6 +299,7 @@ private:
virtual bool platform_scrolls_elastically() const override;
virtual bool platform_scrollbar_track_press_jumps() const override;
virtual bool platform_prefers_reduced_motion() const override;
+ virtual bool platform_prefers_more_contrast() const override;
virtual Vector<String> platform_preferred_languages() const override;
virtual Optional<Gfx::GpuDeviceIdentity> platform_presentation_gpu_device() const override;
virtual Vector<u64> platform_presentation_drm_format_modifiers() const override;
diff --git a/UI/Qt/SystemAppearance.cpp b/UI/Qt/SystemAppearance.cpp
index 1f73b13dbfa..aa507ed8915 100644
--- a/UI/Qt/SystemAppearance.cpp
+++ b/UI/Qt/SystemAppearance.cpp
@@ -60,6 +60,8 @@ SystemAppearance::SystemAppearance()
});
// The chrome's colors depend on the contrast preference, but changing it does not change the application palette.
QObject::connect(QGuiApplication::styleHints()->accessibility(), &QAccessibilityHints::contrastPreferenceChanged, this, notify_widgets_of_palette_change);
+ // Pages that follow the system's contrast preference learn of the change too.
+ QObject::connect(QGuiApplication::styleHints()->accessibility(), &QAccessibilityHints::contrastPreferenceChanged, this, &SystemAppearance::contrast_changed);
observe_platform_changes();
}
diff --git a/UI/Qt/SystemAppearance.h b/UI/Qt/SystemAppearance.h
index 8f41f835f65..d0c2d8dc362 100644
--- a/UI/Qt/SystemAppearance.h
+++ b/UI/Qt/SystemAppearance.h
@@ -46,6 +46,8 @@ signals:
void changed();
// The system started or stopped asking for less motion.
void reduced_motion_changed();
+ // The system started or stopped asking for more contrast.
+ void contrast_changed();
// Our windows switched between the light and dark appearance, whether the system or the interface theme switched
// them.
void interface_appearance_changed();