livesplit_core/component/
current_comparison.rs1use super::key_value;
6use crate::{
7 platform::prelude::*,
8 settings::{Color, Field, Gradient, SettingsDescription, Value},
9 Timer,
10};
11use serde::{Deserialize, Serialize};
12
13#[derive(Default, Clone)]
16pub struct Component {
17 settings: Settings,
18}
19
20#[derive(Clone, Serialize, Deserialize)]
22#[serde(default)]
23pub struct Settings {
24 pub background: Gradient,
26 pub display_two_rows: bool,
29 pub label_color: Option<Color>,
32 pub value_color: Option<Color>,
35}
36
37impl Default for Settings {
38 fn default() -> Self {
39 Self {
40 background: key_value::DEFAULT_GRADIENT,
41 display_two_rows: false,
42 label_color: None,
43 value_color: None,
44 }
45 }
46}
47
48impl Component {
49 pub fn new() -> Self {
51 Default::default()
52 }
53
54 pub const fn with_settings(settings: Settings) -> Self {
56 Self { settings }
57 }
58
59 pub const fn settings(&self) -> &Settings {
61 &self.settings
62 }
63
64 pub fn settings_mut(&mut self) -> &mut Settings {
66 &mut self.settings
67 }
68
69 pub const fn name(&self) -> &'static str {
71 "Current Comparison"
72 }
73
74 pub fn update_state(&self, state: &mut key_value::State, timer: &Timer) {
76 state.background = self.settings.background;
77 state.key_color = self.settings.label_color;
78 state.value_color = self.settings.value_color;
79 state.semantic_color = Default::default();
80
81 state.key.clear();
82 state.key.push_str("Comparing Against");
83
84 state.value.clear();
85 state.value.push_str(timer.current_comparison());
86
87 state.key_abbreviations.clear();
88 state.key_abbreviations.push("Comparison".into());
89
90 state.display_two_rows = self.settings.display_two_rows;
91 state.updates_frequently = false;
92 }
93
94 pub fn state(&self, timer: &Timer) -> key_value::State {
96 let mut state = Default::default();
97 self.update_state(&mut state, timer);
98 state
99 }
100
101 pub fn settings_description(&self) -> SettingsDescription {
104 SettingsDescription::with_fields(vec![
105 Field::new("Background".into(), self.settings.background.into()),
106 Field::new(
107 "Display 2 Rows".into(),
108 self.settings.display_two_rows.into(),
109 ),
110 Field::new("Label Color".into(), self.settings.label_color.into()),
111 Field::new("Value Color".into(), self.settings.value_color.into()),
112 ])
113 }
114
115 pub fn set_value(&mut self, index: usize, value: Value) {
123 match index {
124 0 => self.settings.background = value.into(),
125 1 => self.settings.display_two_rows = value.into(),
126 2 => self.settings.label_color = value.into(),
127 3 => self.settings.value_color = value.into(),
128 _ => panic!("Unsupported Setting Index"),
129 }
130 }
131}