use super::key_value;
use crate::{
analysis::{delta, state_helper},
comparison,
platform::prelude::*,
settings::{Color, Field, Gradient, SemanticColor, SettingsDescription, Value},
timing::{
formatter::{Accuracy, Delta, TimeFormatter},
Snapshot,
},
GeneralLayoutSettings,
};
use alloc::borrow::Cow;
use core::fmt::Write;
use serde::{Deserialize, Serialize};
#[cfg(test)]
mod tests;
#[derive(Default, Clone)]
pub struct Component {
settings: Settings,
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Settings {
pub background: Gradient,
pub comparison_override: Option<String>,
pub display_two_rows: bool,
pub label_color: Option<Color>,
pub drop_decimals: bool,
pub accuracy: Accuracy,
}
impl Default for Settings {
fn default() -> Self {
Self {
background: key_value::DEFAULT_GRADIENT,
comparison_override: None,
display_two_rows: false,
label_color: None,
drop_decimals: true,
accuracy: Accuracy::Tenths,
}
}
}
impl Component {
pub fn new() -> Self {
Default::default()
}
pub const fn with_settings(settings: Settings) -> Self {
Self { settings }
}
pub const fn settings(&self) -> &Settings {
&self.settings
}
pub fn settings_mut(&mut self) -> &mut Settings {
&mut self.settings
}
pub fn name(&self) -> Cow<'static, str> {
if let Some(comparison) = &self.settings.comparison_override {
format!("Delta ({comparison})").into()
} else {
"Delta".into()
}
}
pub fn update_state(
&self,
state: &mut key_value::State,
timer: &Snapshot<'_>,
layout_settings: &GeneralLayoutSettings,
) {
let comparison = comparison::resolve(&self.settings.comparison_override, timer);
let text = comparison.unwrap_or_else(|| timer.current_comparison());
let comparison = comparison::or_current(comparison, timer);
let (delta, use_live_delta) = delta::calculate(timer, comparison);
let mut index = timer.current_split_index();
if !use_live_delta {
index = index.and_then(|i| i.checked_sub(1));
}
let semantic_color = if let Some(index) = index {
state_helper::split_color(
timer,
delta,
index,
true,
false,
comparison,
timer.current_timing_method(),
)
} else {
SemanticColor::Default
};
let value_color = Some(semantic_color.visualize(layout_settings));
state.background = self.settings.background;
state.key_color = self.settings.label_color;
state.value_color = value_color;
state.key.clear();
state.key.push_str(text);
state.value.clear();
let _ = write!(
state.value,
"{}",
Delta::custom(self.settings.drop_decimals, self.settings.accuracy).format(delta),
);
state.key_abbreviations.clear();
if let Some(abbreviation) = comparison::try_shorten(text) {
state.key_abbreviations.push(abbreviation.into());
}
state.display_two_rows = self.settings.display_two_rows;
state.updates_frequently = use_live_delta;
}
pub fn state(
&self,
timer: &Snapshot<'_>,
layout_settings: &GeneralLayoutSettings,
) -> key_value::State {
let mut state = Default::default();
self.update_state(&mut state, timer, layout_settings);
state
}
pub fn settings_description(&self) -> SettingsDescription {
SettingsDescription::with_fields(vec![
Field::new("Background".into(), self.settings.background.into()),
Field::new(
"Comparison".into(),
self.settings.comparison_override.clone().into(),
),
Field::new(
"Display 2 Rows".into(),
self.settings.display_two_rows.into(),
),
Field::new("Label Color".into(), self.settings.label_color.into()),
Field::new("Drop Decimals".into(), self.settings.drop_decimals.into()),
Field::new("Accuracy".into(), self.settings.accuracy.into()),
])
}
pub fn set_value(&mut self, index: usize, value: Value) {
match index {
0 => self.settings.background = value.into(),
1 => self.settings.comparison_override = value.into(),
2 => self.settings.display_two_rows = value.into(),
3 => self.settings.label_color = value.into(),
4 => self.settings.drop_decimals = value.into(),
5 => self.settings.accuracy = value.into(),
_ => panic!("Unsupported Setting Index"),
}
}
}