livesplit_core/component/
blank_space.rs

1//! Provides the Blank Space Component and relevant types for using it. The
2//! Blank Space Component is simply an empty component that doesn't show
3//! anything other than a background. It mostly serves as padding between other
4//! components.
5
6use crate::platform::prelude::*;
7use crate::settings::{Field, Gradient, SettingsDescription, Value};
8use serde::{Deserialize, Serialize};
9
10/// The Blank Space Component is simply an empty component that doesn't show
11/// anything other than a background. It mostly serves as padding between other
12/// components.
13#[derive(Default, Clone)]
14pub struct Component {
15    settings: Settings,
16}
17
18/// The Settings for this component.
19#[derive(Clone, Serialize, Deserialize)]
20#[serde(default)]
21pub struct Settings {
22    /// The background shown behind the component.
23    pub background: Gradient,
24    /// The size of the component.
25    pub size: u32,
26}
27
28impl Default for Settings {
29    fn default() -> Self {
30        Self {
31            background: Gradient::Transparent,
32            size: 24,
33        }
34    }
35}
36
37/// The state object describes the information to visualize for this component.
38#[derive(Default, Serialize, Deserialize)]
39pub struct State {
40    /// The background shown behind the component.
41    pub background: Gradient,
42    /// The size of the component.
43    pub size: u32,
44}
45
46#[cfg(feature = "std")]
47impl State {
48    /// Encodes the state object's information as JSON.
49    pub fn write_json<W>(&self, writer: W) -> serde_json::Result<()>
50    where
51        W: std::io::Write,
52    {
53        serde_json::to_writer(writer, self)
54    }
55}
56
57impl Component {
58    /// Creates a new Blank Space Component.
59    pub fn new() -> Self {
60        Default::default()
61    }
62
63    /// Creates a new Blank Space Component with the given settings.
64    pub const fn with_settings(settings: Settings) -> Self {
65        Self { settings }
66    }
67
68    /// Accesses the settings of the component.
69    pub const fn settings(&self) -> &Settings {
70        &self.settings
71    }
72
73    /// Grants mutable access to the settings of the component.
74    pub fn settings_mut(&mut self) -> &mut Settings {
75        &mut self.settings
76    }
77
78    /// Accesses the name of the component.
79    pub const fn name(&self) -> &'static str {
80        "Blank Space"
81    }
82
83    /// Updates the component's state.
84    pub fn update_state(&self, state: &mut State) {
85        state.background = self.settings.background;
86        state.size = self.settings.size;
87    }
88
89    /// Calculates the component's state.
90    pub const fn state(&self) -> State {
91        State {
92            background: self.settings.background,
93            size: self.settings.size,
94        }
95    }
96
97    /// Accesses a generic description of the settings available for this
98    /// component and their current values.
99    pub fn settings_description(&self) -> SettingsDescription {
100        SettingsDescription::with_fields(vec![
101            Field::new("Background".into(), self.settings.background.into()),
102            Field::new("Size".into(), u64::from(self.settings.size).into()),
103        ])
104    }
105
106    /// Sets a setting's value by its index to the given value.
107    ///
108    /// # Panics
109    ///
110    /// This panics if the type of the value to be set is not compatible with
111    /// the type of the setting's value. A panic can also occur if the index of
112    /// the setting provided is out of bounds.
113    pub fn set_value(&mut self, index: usize, value: Value) {
114        match index {
115            0 => self.settings.background = value.into(),
116            1 => self.settings.size = value.into_uint().unwrap() as _,
117            _ => panic!("Unsupported Setting Index"),
118        }
119    }
120}