Skip to content

Commit

Permalink
Fixed issue where text_styles were not being serialized correctly.
Browse files Browse the repository at this point in the history
Added error message to the serialization macro so that when properties there will be at least some output.
  • Loading branch information
jacobsky committed Jun 4, 2022
1 parent 6daf989 commit c14cd10
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 10 deletions.
57 changes: 56 additions & 1 deletion egui-theme/src/tests/ser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use egui::FontData;
use egui::{Style, TextStyle, FontId, FontFamily, FontData, FontDefinitions};

use crate::EguiTheme;
#[test]
Expand Down Expand Up @@ -132,3 +132,58 @@ fn test_custom_font_serialization() {
"Nacelle data does not exist in the serialized string"
);
}

#[test]
fn test_text_style() {
let mut style = Style::default();
let mut fonts = FontDefinitions::default();

const FONT_NAME: &str = "NacelleFontData";
fonts.font_data.insert(
FONT_NAME.to_owned(),
FontData::from_static(include_bytes!("test-fonts/Nacelle-Regular.otf")),
);
fonts.families.insert(
egui::FontFamily::Name(FONT_NAME.into()),
vec![FONT_NAME.to_owned()],
);

fonts.families.insert(FontFamily::Name("NacelleFontFamily".into()), vec![FONT_NAME.to_owned()]);
style.text_styles.insert(TextStyle::Name("NacelleStyle".into()), FontId::new(12.0, FontFamily::Name("NacelleFontFamily".into())));

let theme = EguiTheme::new(style, fonts);
let serialized = serde_json::to_string(&theme).expect("serialization failed");
assert!(serialized.contains("NacelleFontData"), "Nacelle Font Data was not serialized correctly");
assert!(serialized.contains("NacelleFontFamily"), "FontFamily was not serialized correctly");
assert!(serialized.contains("NacelleStyle"), "TextStyle was not serialized correctly");

let deserialized = serde_json::from_str::<EguiTheme>(serialized.as_str()).expect("deserialization failed");
let (de_style, _fonts) = deserialized.extract();
assert!(de_style.text_styles().contains(&TextStyle::Body), "text style `Body` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Small), "text style `Small` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Monospace), "text style `Monospace` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Button), "text style `Button` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Heading), "text style `Heading` does not exist");
assert!(de_style.text_styles().contains(&TextStyle::Name("NacelleStyle".into())), "text style `NacelleStyle` does not exist");
assert!(de_style.text_styles.get(&TextStyle::Name("NacelleStyle".into())).is_some(), "could not get the text_style");
assert_eq!(*de_style.text_styles.get(&TextStyle::Name("NacelleStyle".into())).unwrap(), FontId::new(12.0, FontFamily::Name("NacelleFontFamily".into())), "FontStyle not deserialized");
}
#[test]
fn test_colors() {
let mut style = Style::default();
let fg_stroke = egui::Stroke::new(1f32, egui::Color32::TRANSPARENT);
style.visuals.widgets.noninteractive.fg_stroke = fg_stroke.clone();
style.visuals.widgets.inactive.bg_fill = egui::Color32::LIGHT_RED;

let theme = EguiTheme::new(style, FontDefinitions::default());
let serialized = serde_json::to_string(&theme).expect("serialization failed");
let deserialized = serde_json::from_str::<EguiTheme>(serialized.as_str()).expect("deserialization failed");
let (de_style, _fonts) = deserialized.extract();

assert_eq!(
de_style.visuals.widgets.noninteractive.fg_stroke, fg_stroke.clone(), "stroke doesn't match"
);
assert_eq!(
de_style.visuals.widgets.inactive.bg_fill, egui::Color32::LIGHT_RED, "Color doesn't match"
);
}
2 changes: 0 additions & 2 deletions egui-theme/src/theme/fonts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ pub fn from_fonts(
serde_json::to_value(font_data).expect("serialization error occurred"),
);

println!("{:?}", &families);
// Workaround due to FontFamily not properly serializing to "String" when attempting to serialize the BTreeMap<FontFamily, Vec<String>>
let families = {
families
Expand All @@ -45,7 +44,6 @@ pub fn from_fonts(
serde_json::to_value(families).expect("serialization error occurred"),
);

println!("{:?}", hash_map.get(FAMILIES_KEY));
hash_map
}

Expand Down
39 changes: 32 additions & 7 deletions egui-theme/src/theme/style.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use egui::Style;
use std::collections::HashMap;

const TEXT_STYLES_KEY: &str = "text_styles";

macro_rules! ser {
($collection:ident, $style:ident, $prop:ident) => {
if let Ok(value) = serde_json::to_value($style.$prop.to_owned()) {
$collection.insert(stringify!($prop).to_owned(), value);
}
match serde_json::to_value($style.$prop.to_owned()) {
Ok(value) => { let _ = $collection.insert(stringify!($prop).to_owned(), value); },
Err(error) => { println!("{}", error); },
}
};
($collection:ident, $style:ident, $prop:ident, $sub_prop:ident) => {
if let Ok(value) = serde_json::to_value($style.$prop.$sub_prop.to_owned()) {
$collection.insert(stringify!($prop.$sub_prop).to_owned(), value);
match serde_json::to_value($style.$prop.$sub_prop.to_owned()) {
Ok(value) => { let _ = $collection.insert(stringify!($prop.$sub_prop).to_owned(), value); },
Err(error) => { println!("{}", error); },
}
};
}
Expand Down Expand Up @@ -37,11 +41,22 @@ macro_rules! de {
pub fn from_style(style: Style) -> HashMap<String, super::ThemeValue> {
let mut hash_map = HashMap::new();

{
let text_styles = style.text_styles.iter()
.map(|(k, v)| (k.to_owned(), v.to_owned() ))
.collect::<Vec<_>>();
if let Ok(value) = serde_json::to_value(text_styles) {
hash_map.insert(TEXT_STYLES_KEY.to_owned(), value);
}
}
// Text Styles are a special case due to being a map that must serialize to a string.
// ser!(hash_map, style, text_styles);

ser!(hash_map, style, override_text_style);
ser!(hash_map, style, override_font_id);
ser!(hash_map, style, text_styles);
ser!(hash_map, style, wrap);


ser!(hash_map, style, animation_time);
ser!(hash_map, style, explanation_tooltips);

Expand Down Expand Up @@ -86,10 +101,19 @@ pub fn from_style(style: Style) -> HashMap<String, super::ThemeValue> {
/// Helper function to deserialize the `egui::Style`
pub fn to_style(hash_map: HashMap<String, super::ThemeValue>) -> Style {
let mut style = Style::default();
// Special case due to json requiring String keys
{
hash_map.get(TEXT_STYLES_KEY).map(|value| {
if let Ok(values) = serde_json::from_value::<Vec<(egui::TextStyle, egui::FontId)>>(value.to_owned()) {
for (key, value) in values {
style.text_styles.insert(key, value);
}
}
});
}

de!(hash_map, style, override_text_style);
de!(hash_map, style, override_font_id);
de!(hash_map, style, text_styles);
de!(hash_map, style, wrap);

de!(hash_map, style, animation_time);
Expand All @@ -112,6 +136,7 @@ pub fn to_style(hash_map: HashMap<String, super::ThemeValue>) -> Style {
de!(hash_map, style, interaction, resize_grab_radius_side);
de!(hash_map, style, interaction, resize_grab_radius_corner);
de!(hash_map, style, interaction, show_tooltips_only_when_still);

de!(hash_map, style, visuals, dark_mode);
de!(hash_map, style, visuals, override_text_color);
de!(hash_map, style, visuals, widgets);
Expand Down

0 comments on commit c14cd10

Please sign in to comment.