use crate::platform::prelude::*;
use crate::settings::{Field, Gradient, SettingsDescription, Value};
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 size: u32,
}
impl Default for Settings {
fn default() -> Self {
Self {
background: Gradient::Transparent,
size: 24,
}
}
}
#[derive(Default, Serialize, Deserialize)]
pub struct State {
pub background: Gradient,
pub size: u32,
}
#[cfg(feature = "std")]
impl State {
pub fn write_json<W>(&self, writer: W) -> serde_json::Result<()>
where
W: std::io::Write,
{
serde_json::to_writer(writer, self)
}
}
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 const fn name(&self) -> &'static str {
"Blank Space"
}
pub fn update_state(&self, state: &mut State) {
state.background = self.settings.background;
state.size = self.settings.size;
}
pub const fn state(&self) -> State {
State {
background: self.settings.background,
size: self.settings.size,
}
}
pub fn settings_description(&self) -> SettingsDescription {
SettingsDescription::with_fields(vec![
Field::new("Background".into(), self.settings.background.into()),
Field::new("Size".into(), u64::from(self.settings.size).into()),
])
}
pub fn set_value(&mut self, index: usize, value: Value) {
match index {
0 => self.settings.background = value.into(),
1 => self.settings.size = value.into_uint().unwrap() as _,
_ => panic!("Unsupported Setting Index"),
}
}
}