#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(default)]
pub struct Panel {
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
pub color: [f32; 4],
pub corner_radius: f32,
pub title: String,
pub title_font: String,
pub title_color: [f32; 3],
pub title_scale: f32,
pub padding: f32,
}
impl Default for Panel {
fn default() -> Self {
Self {
x: 0.0,
y: 0.0,
width: 400.0,
height: 300.0,
color: [0.08, 0.09, 0.12, 0.96],
corner_radius: 8.0,
title: String::new(),
title_font: String::new(),
title_color: [0.95, 0.95, 0.97],
title_scale: 1.0,
padding: 16.0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_blank_panel_is_a_rounded_near_opaque_dark_container() {
let p = Panel::default();
assert_eq!((p.width, p.height), (400.0, 300.0));
assert_eq!(p.corner_radius, 8.0);
assert_eq!(p.padding, 16.0);
assert_eq!(p.title_scale, 1.0);
assert!(p.title.is_empty());
assert!(p.title_font.is_empty());
assert_eq!(p.color[3], 0.96);
}
#[test]
fn an_authored_panel_parses_and_round_trips_through_postcard() {
let p: Panel = serde_json::from_str(
r#"{"x":20,"y":30,"width":520,"height":360,"color":[0,0,0,1],
"corner_radius":0,"title":"Outliner","title_font":"body",
"title_color":[1,1,1],"title_scale":1.2,"padding":8}"#,
)
.unwrap();
assert_eq!(p.title, "Outliner");
assert_eq!(p.corner_radius, 0.0);
let bytes = postcard::to_allocvec(&p).unwrap();
let back: Panel = postcard::from_bytes(&bytes).unwrap();
assert_eq!((back.x, back.y), (20.0, 30.0));
assert_eq!((back.width, back.height), (520.0, 360.0));
assert_eq!(back.color, [0.0, 0.0, 0.0, 1.0]);
assert_eq!(back.title_font, "body");
assert_eq!(back.title_color, [1.0, 1.0, 1.0]);
assert_eq!(back.title_scale, 1.2);
assert_eq!(back.padding, 8.0);
}
}