From 90a65bcf0190a156d58187f398a5f88b6e69ded5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 18 Jun 2023 14:12:34 +0200 Subject: [PATCH] style: Inherit used color-scheme from embedder elements This allows popups and sidebars to use the chrome preferred color-scheme. This moves the responsibility of setting the content-preferred color scheme to the appropriate browsers to the front-end (via tabs.css). We still return the PreferredColorSchemeForContent() when there's no pres context (e.g., for display:none in-process iframes). We could potentially move a bunch of the pres-context data to the document instead, but that should be acceptable IMO as for general web content there's no behavior change in any case. Differential Revision: https://phabricator.services.mozilla.com/D142578 --- components/style/custom_properties.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/components/style/custom_properties.rs b/components/style/custom_properties.rs index 7be700ec350..2efcda9ef15 100644 --- a/components/style/custom_properties.rs +++ b/components/style/custom_properties.rs @@ -64,6 +64,20 @@ fn get_safearea_inset_right(device: &Device) -> VariableValue { VariableValue::pixels(device.safe_area_insets().right) } +fn get_content_preferred_color_scheme(device: &Device) -> VariableValue { + use crate::gecko::media_features::PrefersColorScheme; + let prefers_color_scheme = unsafe { + crate::gecko_bindings::bindings::Gecko_MediaFeatures_PrefersColorScheme( + device.document(), + /* use_content = */ true, + ) + }; + VariableValue::ident(match prefers_color_scheme { + PrefersColorScheme::Light => "light", + PrefersColorScheme::Dark => "dark", + }) +} + static ENVIRONMENT_VARIABLES: [EnvironmentVariable; 4] = [ make_variable!(atom!("safe-area-inset-top"), get_safearea_inset_top), make_variable!(atom!("safe-area-inset-bottom"), get_safearea_inset_bottom), @@ -99,7 +113,7 @@ macro_rules! lnf_int_variable { }}; } -static CHROME_ENVIRONMENT_VARIABLES: [EnvironmentVariable; 5] = [ +static CHROME_ENVIRONMENT_VARIABLES: [EnvironmentVariable; 6] = [ lnf_int_variable!( atom!("-moz-gtk-csd-titlebar-radius"), TitlebarRadius, @@ -121,6 +135,7 @@ static CHROME_ENVIRONMENT_VARIABLES: [EnvironmentVariable; 5] = [ GTKCSDMaximizeButtonPosition, integer ), + make_variable!(atom!("-moz-content-preferred-color-scheme"), get_content_preferred_color_scheme), ]; impl CssEnvironment { @@ -328,6 +343,11 @@ impl VariableValue { }) } + /// Create VariableValue from an int. + fn ident(ident: &'static str) -> Self { + Self::from_token(Token::Ident(ident.into())) + } + /// Create VariableValue from a float amount of CSS pixels. fn pixels(number: f32) -> Self { // FIXME (https://github.com/servo/rust-cssparser/issues/266):