diff options
author | Ngo Iok Ui (Wu Yu Wei) <yuweiwu@pm.me> | 2023-10-04 22:16:16 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-04 13:16:16 +0000 |
commit | 38a325cc1c2a079c42b0511c2e5e5aa88753144d (patch) | |
tree | 99c5d0c95d5bd23db923473d01b1a6edfe24e502 | |
parent | ffac882f8f57ffbdf2980cf00dd0894e3a13b461 (diff) | |
download | servo-38a325cc1c2a079c42b0511c2e5e5aa88753144d.tar.gz servo-38a325cc1c2a079c42b0511c2e5e5aa88753144d.zip |
Add shell.background-color.rgba to prefs (#30488)
* Add shell.transparent-background.enabled to prefs
* Rename config to background_color
* Rename to background-color and.rgba add PrefValue::Array variant
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | components/compositing/Cargo.toml | 1 | ||||
-rw-r--r-- | components/compositing/compositor.rs | 11 | ||||
-rw-r--r-- | components/config/pref_util.rs | 32 | ||||
-rw-r--r-- | components/config/prefs.rs | 15 | ||||
-rw-r--r-- | components/devtools/actors/preference.rs | 79 | ||||
-rw-r--r-- | components/webdriver_server/lib.rs | 5 | ||||
-rw-r--r-- | resources/prefs.json | 1 |
8 files changed, 108 insertions, 37 deletions
diff --git a/Cargo.lock b/Cargo.lock index cb5fbc98430..417992bdc68 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -915,6 +915,7 @@ dependencies = [ "profile_traits", "script_traits", "servo-media", + "servo_config", "servo_geometry", "servo_url", "style_traits", diff --git a/components/compositing/Cargo.toml b/components/compositing/Cargo.toml index ff02ad2e93f..b2fa4608669 100644 --- a/components/compositing/Cargo.toml +++ b/components/compositing/Cargo.toml @@ -35,6 +35,7 @@ num-traits = { workspace = true } pixels = { path = "../pixels", optional = true } profile_traits = { path = "../profile_traits" } script_traits = { path = "../script_traits" } +servo_config = { path = "../config" } servo-media = { git = "https://github.com/servo/media" } servo_geometry = { path = "../geometry" } servo_url = { path = "../url" } diff --git a/components/compositing/compositor.rs b/components/compositing/compositor.rs index 4798970a8be..59845759ca0 100644 --- a/components/compositing/compositor.rs +++ b/components/compositing/compositor.rs @@ -1873,7 +1873,7 @@ impl<Window: WindowMethods + ?Sized> IOCompositor<Window> { self.assert_gl_framebuffer_complete(); } - // Make the viewport white. + // Set the viewport background based on prefs. let viewport = self.embedder_coordinates.get_flipped_viewport(); gl.scissor( viewport.origin.x, @@ -1881,7 +1881,14 @@ impl<Window: WindowMethods + ?Sized> IOCompositor<Window> { viewport.size.width, viewport.size.height, ); - gl.clear_color(1.0, 1.0, 1.0, 1.0); + + let color = servo_config::pref!(shell.background_color.rgba); + gl.clear_color( + color[0] as f32, + color[1] as f32, + color[2] as f32, + color[3] as f32, + ); gl.enable(gleam::gl::SCISSOR_TEST); gl.clear(gleam::gl::COLOR_BUFFER_BIT); gl.disable(gleam::gl::SCISSOR_TEST); diff --git a/components/config/pref_util.rs b/components/config/pref_util.rs index 08de25cb399..11e2f7f94a8 100644 --- a/components/config/pref_util.rs +++ b/components/config/pref_util.rs @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::collections::HashMap; +use std::convert::TryInto; use std::fmt; use std::str::FromStr; use std::sync::RwLock; @@ -16,6 +17,7 @@ pub enum PrefValue { Int(i64), Str(String), Bool(bool), + Array(Vec<PrefValue>), Missing, } @@ -149,6 +151,36 @@ impl_from_pref! { PrefValue::Bool => bool, } +impl From<[f64; 4]> for PrefValue { + fn from(other: [f64; 4]) -> PrefValue { + PrefValue::Array(IntoIterator::into_iter(other).map(|v| v.into()).collect()) + } +} + +impl From<PrefValue> for [f64; 4] { + fn from(other: PrefValue) -> [f64; 4] { + match other { + PrefValue::Array(values) if values.len() == 4 => { + let mut f = values.into_iter().map(|v| v.try_into()); + if f.all(|v| v.is_ok()) { + let f = f.flatten().collect::<Vec<f64>>(); + return [f[0], f[1], f[2], f[3]]; + } else { + panic!( + "Cannot convert PrefValue to {:?}", + std::any::type_name::<[f64; 4]>() + ) + } + }, + _ => panic!( + "Cannot convert {:?} to {:?}", + other, + std::any::type_name::<[f64; 4]>() + ), + } + } +} + #[derive(Debug)] pub enum PrefError { NoSuchPref(String), diff --git a/components/config/prefs.rs b/components/config/prefs.rs index fd6de4dd2fd..0974ed19a9b 100644 --- a/components/config/prefs.rs +++ b/components/config/prefs.rs @@ -73,6 +73,17 @@ pub fn read_prefs_map(txt: &str) -> Result<HashMap<String, PrefValue>, PrefError Value::Number(n) if n.is_i64() => PrefValue::Int(n.as_i64().unwrap()), Value::Number(n) if n.is_f64() => PrefValue::Float(n.as_f64().unwrap()), Value::String(s) => PrefValue::Str(s.to_owned()), + Value::Array(v) => { + let mut array = v.iter().map(|v| PrefValue::from_json_value(v)); + if array.all(|v| v.is_some()) { + PrefValue::Array(array.flatten().collect()) + } else { + return Err(PrefError::InvalidValue(format!( + "Invalid value: {}", + pref_value + ))); + } + }, _ => { return Err(PrefError::InvalidValue(format!( "Invalid value: {}", @@ -478,6 +489,10 @@ mod gen { max_length: i64, }, shell: { + background_color: { + #[serde(rename = "shell.background-color.rgba")] + rgba: [f64; 4], + }, crash_reporter: { enabled: bool, }, diff --git a/components/devtools/actors/preference.rs b/components/devtools/actors/preference.rs index 860b476d250..75ce7e0364e 100644 --- a/components/devtools/actors/preference.rs +++ b/components/devtools/actors/preference.rs @@ -37,41 +37,50 @@ impl Actor for PreferenceActor { _id: StreamId, ) -> Result<ActorMessageStatus, ()> { let pref_value = pref_map().get(msg_type); - Ok(match pref_value { - PrefValue::Float(value) => { - let reply = FloatReply { - from: self.name(), - value: value, - }; - let _ = stream.write_json_packet(&reply); - ActorMessageStatus::Processed - }, - PrefValue::Int(value) => { - let reply = IntReply { - from: self.name(), - value: value, - }; - let _ = stream.write_json_packet(&reply); - ActorMessageStatus::Processed - }, - PrefValue::Str(value) => { - let reply = CharReply { - from: self.name(), - value: value, - }; - let _ = stream.write_json_packet(&reply); - ActorMessageStatus::Processed - }, - PrefValue::Bool(value) => { - let reply = BoolReply { - from: self.name(), - value: value, - }; - let _ = stream.write_json_packet(&reply); - ActorMessageStatus::Processed - }, - PrefValue::Missing => handle_missing_preference(self.name(), msg_type, stream), - }) + Ok(handle_preference_value( + pref_value, + self.name(), + msg_type, + stream, + )) + } +} + +fn handle_preference_value( + pref_value: PrefValue, + name: String, + msg_type: &str, + stream: &mut TcpStream, +) -> ActorMessageStatus { + match pref_value { + PrefValue::Float(value) => { + let reply = FloatReply { from: name, value }; + let _ = stream.write_json_packet(&reply); + ActorMessageStatus::Processed + }, + PrefValue::Int(value) => { + let reply = IntReply { from: name, value }; + let _ = stream.write_json_packet(&reply); + ActorMessageStatus::Processed + }, + PrefValue::Str(value) => { + let reply = CharReply { from: name, value }; + let _ = stream.write_json_packet(&reply); + ActorMessageStatus::Processed + }, + PrefValue::Bool(value) => { + let reply = BoolReply { from: name, value }; + let _ = stream.write_json_packet(&reply); + ActorMessageStatus::Processed + }, + PrefValue::Array(values) => { + let mut result = ActorMessageStatus::Processed; + for value in values { + result = handle_preference_value(value, name.clone(), msg_type, stream); + } + result + }, + PrefValue::Missing => handle_missing_preference(name, msg_type, stream), } } diff --git a/components/webdriver_server/lib.rs b/components/webdriver_server/lib.rs index 1c2682ded5f..50337ecaa0a 100644 --- a/components/webdriver_server/lib.rs +++ b/components/webdriver_server/lib.rs @@ -283,6 +283,11 @@ impl Serialize for WebDriverPrefValue { PrefValue::Float(f) => serializer.serialize_f64(f), PrefValue::Int(i) => serializer.serialize_i64(i), PrefValue::Missing => serializer.serialize_unit(), + PrefValue::Array(ref v) => v + .iter() + .map(|value| WebDriverPrefValue(value.clone())) + .collect::<Vec<WebDriverPrefValue>>() + .serialize(serializer), } } } diff --git a/resources/prefs.json b/resources/prefs.json index 1881a525087..ebd7d6824b0 100644 --- a/resources/prefs.json +++ b/resources/prefs.json @@ -110,6 +110,7 @@ "network.http-cache.disabled": false, "network.mime.sniff": false, "session-history.max-length": 20, + "shell.background-color.rgba": [1.0, 1.0, 1.0, 1.0], "shell.crash_reporter.enabled": false, "shell.homepage": "https://servo.org", "shell.keep_screen_on.enabled": false, |