livesplit_core/layout/editor/state.rs
1use super::Editor;
2use crate::platform::prelude::*;
3use crate::settings::SettingsDescription;
4use serde::{Deserialize, Serialize};
5
6/// Represents the current state of the Layout Editor in order to visualize it
7/// properly.
8#[derive(Serialize, Deserialize)]
9pub struct State {
10 /// The name of all the components in the layout.
11 pub components: Vec<String>,
12 /// Describes which actions are currently available.
13 pub buttons: Buttons,
14 /// The index of the currently selected component.
15 pub selected_component: u32,
16 /// A generic description of the settings available for the selected
17 /// component and their current values.
18 pub component_settings: SettingsDescription,
19 /// A generic description of the general settings available for the layout
20 /// and their current values.
21 pub general_settings: SettingsDescription,
22}
23
24/// Describes which actions are currently available. Depending on how many
25/// components exist and which one is selected, only some actions can be
26/// executed successfully.
27#[derive(Serialize, Deserialize)]
28pub struct Buttons {
29 /// Describes whether the currently selected component can be removed. If
30 /// there's only one component in the layout, it can't be removed.
31 pub can_remove: bool,
32 /// Describes whether the currently selected component can be moved up. If
33 /// the first component is selected, it can't be moved.
34 pub can_move_up: bool,
35 /// Describes whether the currently selected component can be moved down. If
36 /// the last component is selected, it can't be moved.
37 pub can_move_down: bool,
38}
39
40#[cfg(feature = "std")]
41impl State {
42 /// Encodes the state object's information as JSON.
43 pub fn write_json<W>(&self, writer: W) -> serde_json::Result<()>
44 where
45 W: std::io::Write,
46 {
47 serde_json::to_writer(writer, self)
48 }
49}
50
51impl Editor {
52 /// Calculates the Layout Editor's state in order to visualize it.
53 pub fn state(&self) -> State {
54 let components = self
55 .layout
56 .components
57 .iter()
58 .map(|c| c.name().into_owned())
59 .collect();
60
61 let buttons = Buttons {
62 can_remove: self.can_remove_component(),
63 can_move_up: self.can_move_component_up(),
64 can_move_down: self.can_move_component_down(),
65 };
66
67 State {
68 components,
69 buttons,
70 selected_component: self.selected_component as u32,
71 component_settings: self.layout.components[self.selected_component]
72 .settings_description(),
73 general_settings: self.layout.general_settings().settings_description(),
74 }
75 }
76}