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
This commit is contained in:
Ngo Iok Ui (Wu Yu Wei) 2023-10-04 22:16:16 +09:00 committed by GitHub
parent ffac882f8f
commit 38a325cc1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 108 additions and 37 deletions

View file

@ -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" }

View file

@ -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);

View file

@ -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),

View file

@ -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,
},

View file

@ -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),
}
}

View file

@ -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),
}
}
}