livesplit_core/component/key_value.rs
1//! Provides the state for key value based components. Examples of these
2//! components include the Previous Segment and the Possible Time Save
3//! components. They all share the same visual appearance and thus use the same
4//! state object representation.
5
6use crate::{
7 platform::prelude::*,
8 settings::{Color, Gradient, SemanticColor},
9};
10use alloc::borrow::Cow;
11use serde::{Deserialize, Serialize};
12
13/// The state object describes the information to visualize for a key value
14/// based component.
15#[derive(Default, Serialize, Deserialize)]
16pub struct State {
17 /// The background shown behind the component.
18 pub background: Gradient,
19 /// The color of the key. If `None` is specified, the color is taken from
20 /// the layout.
21 pub key_color: Option<Color>,
22 /// The color of the value. If `None` is specified, the color is taken from
23 /// the layout.
24 pub value_color: Option<Color>,
25 /// The semantic coloring information the value carries.
26 pub semantic_color: SemanticColor,
27 /// The key to visualize.
28 pub key: String,
29 /// The value to visualize.
30 pub value: String,
31 /// Specifies additional abbreviations for the key that can be used instead
32 /// of the key, if there is not enough space to show the whole key.
33 pub key_abbreviations: Vec<Cow<'static, str>>,
34 /// Specifies whether to display the key and the value in two separate rows.
35 pub display_two_rows: bool,
36 /// This value indicates whether the value is currently frequently being
37 /// updated. This can be used for rendering optimizations.
38 pub updates_frequently: bool,
39}
40
41#[cfg(feature = "std")]
42impl State {
43 /// Encodes the state object's information as JSON.
44 pub fn write_json<W>(&self, writer: W) -> serde_json::Result<()>
45 where
46 W: std::io::Write,
47 {
48 serde_json::to_writer(writer, self)
49 }
50}
51
52pub(super) const DEFAULT_GRADIENT: Gradient = Gradient::Vertical(
53 Color {
54 red: 1.0,
55 green: 1.0,
56 blue: 1.0,
57 alpha: 0.06,
58 },
59 Color {
60 red: 1.0,
61 green: 1.0,
62 blue: 1.0,
63 alpha: 0.005,
64 },
65);