From d12cb17d73f4261d3292544e651495c0891f4763 Mon Sep 17 00:00:00 2001 From: Oriol Brufau Date: Wed, 17 May 2023 00:31:25 +0200 Subject: [PATCH] style: -moz-toolbar-prefers-color-scheme for dark theme detection This adds a new @media query -moz-toolbar-prefers-color-scheme which works like prefers-color-scheme but is set based on the browser theme rather than the OS theme. The background colour of the toolbar is used to determine the theme dark/light preference. This will be used for in-content common.css pages and other UI elements that include that stylesheet in the browser-chrome through shadow DOM. The end result is that about: pages, infobars, and modals will now "match" the browser theme (just light/dark mode, not LWT theming support). Differential Revision: https://phabricator.services.mozilla.com/D111486 --- components/style/gecko/media_features.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/components/style/gecko/media_features.rs b/components/style/gecko/media_features.rs index 287ab6a1797..f6703bc9b3f 100644 --- a/components/style/gecko/media_features.rs +++ b/components/style/gecko/media_features.rs @@ -406,6 +406,21 @@ fn eval_prefers_color_scheme(device: &Device, query_value: Option) -> bool { + let prefers_color_scheme = if static_prefs::pref!("browser.theme.dark-toolbar-theme") { + PrefersColorScheme::Dark + } else { + PrefersColorScheme::Light + }; + + match query_value { + Some(v) => prefers_color_scheme == v, + None => true, + } +} + bitflags! { /// https://drafts.csswg.org/mediaqueries-4/#mf-interaction struct PointerCapabilities: u8 { @@ -624,7 +639,7 @@ macro_rules! bool_pref_feature { /// to support new types in these entries and (2) ensuring that either /// nsPresContext::MediaFeatureValuesChanged is called when the value that /// would be returned by the evaluator function could change. -pub static MEDIA_FEATURES: [MediaFeatureDescription; 61] = [ +pub static MEDIA_FEATURES: [MediaFeatureDescription; 62] = [ feature!( atom!("width"), AllowsRanges::Yes, @@ -834,6 +849,12 @@ pub static MEDIA_FEATURES: [MediaFeatureDescription; 61] = [ Evaluator::BoolInteger(eval_moz_non_native_content_theme), ParsingRequirements::CHROME_AND_UA_ONLY, ), + feature!( + atom!("-moz-toolbar-prefers-color-scheme"), + AllowsRanges::No, + keyword_evaluator!(eval_toolbar_prefers_color_scheme, PrefersColorScheme), + ParsingRequirements::CHROME_AND_UA_ONLY, + ), lnf_int_feature!(atom!("-moz-scrollbar-start-backward"), ScrollArrowStyle, get_scrollbar_start_backward), lnf_int_feature!(atom!("-moz-scrollbar-start-forward"), ScrollArrowStyle, get_scrollbar_start_forward),