livesplit_core/component/
blank_space.rs1use crate::platform::prelude::*;
7use crate::settings::{Field, Gradient, SettingsDescription, Value};
8use serde::{Deserialize, Serialize};
9
10#[derive(Default, Clone)]
14pub struct Component {
15 settings: Settings,
16}
17
18#[derive(Clone, Serialize, Deserialize)]
20#[serde(default)]
21pub struct Settings {
22 pub background: Gradient,
24 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#[derive(Default, Serialize, Deserialize)]
39pub struct State {
40 pub background: Gradient,
42 pub size: u32,
44}
45
46#[cfg(feature = "std")]
47impl State {
48 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 pub fn new() -> Self {
60 Default::default()
61 }
62
63 pub const fn with_settings(settings: Settings) -> Self {
65 Self { settings }
66 }
67
68 pub const fn settings(&self) -> &Settings {
70 &self.settings
71 }
72
73 pub fn settings_mut(&mut self) -> &mut Settings {
75 &mut self.settings
76 }
77
78 pub const fn name(&self) -> &'static str {
80 "Blank Space"
81 }
82
83 pub fn update_state(&self, state: &mut State) {
85 state.background = self.settings.background;
86 state.size = self.settings.size;
87 }
88
89 pub const fn state(&self) -> State {
91 State {
92 background: self.settings.background,
93 size: self.settings.size,
94 }
95 }
96
97 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 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}