use super::key_value;
use crate::{
analysis::possible_time_save,
comparison,
platform::prelude::*,
settings::{Color, Field, Gradient, SettingsDescription, Value},
timing::{
formatter::{Accuracy, SegmentTime, TimeFormatter},
Snapshot,
},
TimerPhase,
};
use alloc::borrow::Cow;
use core::fmt::Write as FmtWrite;
use serde::{Deserialize, Serialize};
#[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 total_possible_time_save: bool,
pub label_color: Option<Color>,
pub value_color: Option<Color>,
pub accuracy: Accuracy,
}
impl Default for Settings {
fn default() -> Self {
Self {
background: key_value::DEFAULT_GRADIENT,
comparison_override: None,
display_two_rows: false,
total_possible_time_save: false,
label_color: None,
value_color: None,
accuracy: Accuracy::Hundredths,
}
}
}
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> {
self.text(
self.settings
.comparison_override
.as_ref()
.map(String::as_ref),
)
}
fn text(&self, comparison: Option<&str>) -> Cow<'static, str> {
let text = if self.settings.total_possible_time_save {
"Total Possible Time Save"
} else {
"Possible Time Save"
};
let mut text = Cow::from(text);
if let Some(comparison) = comparison {
write!(text.to_mut(), " ({})", comparison::shorten(comparison)).unwrap();
}
text
}
pub fn update_state(&self, state: &mut key_value::State, timer: &Snapshot<'_>) {
let segment_index = timer.current_split_index();
let current_phase = timer.current_phase();
let comparison = comparison::resolve(&self.settings.comparison_override, timer);
let text = self.text(comparison);
let comparison = comparison::or_current(comparison, timer);
let (time, updates_frequently) = if self.settings.total_possible_time_save {
let (time, updates_frequently) =
possible_time_save::calculate_total(timer, segment_index.unwrap_or(0), comparison);
(Some(time), updates_frequently)
} else if current_phase == TimerPhase::Running || current_phase == TimerPhase::Paused {
possible_time_save::calculate(timer, segment_index.unwrap(), comparison, false)
} else {
(None, false)
};
state.background = self.settings.background;
state.key_color = self.settings.label_color;
state.value_color = self.settings.value_color;
state.semantic_color = Default::default();
state.key.clear();
state.key.push_str(&text);
state.value.clear();
let _ = write!(
state.value,
"{}",
SegmentTime::with_accuracy(self.settings.accuracy).format(time)
);
state.key_abbreviations.clear();
if self.settings.total_possible_time_save {
state
.key_abbreviations
.push("Total Possible Time Save".into());
}
state.key_abbreviations.push("Possible Time Save".into());
state.key_abbreviations.push("Poss. Time Save".into());
state.key_abbreviations.push("Time Save".into());
state.display_two_rows = self.settings.display_two_rows;
state.updates_frequently = updates_frequently;
}
pub fn state(&self, timer: &Snapshot<'_>) -> key_value::State {
let mut state = Default::default();
self.update_state(&mut state, timer);
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(
"Show Total Possible Time Save".into(),
self.settings.total_possible_time_save.into(),
),
Field::new("Label Color".into(), self.settings.label_color.into()),
Field::new("Value Color".into(), self.settings.value_color.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.total_possible_time_save = value.into(),
4 => self.settings.label_color = value.into(),
5 => self.settings.value_color = value.into(),
6 => self.settings.accuracy = value.into(),
_ => panic!("Unsupported Setting Index"),
}
}
}