streamdeck_oxide/view/
button.rs

1//! Button types for the view system.
2//!
3//! This module provides types for representing buttons in the view system.
4
5use crate::Theme;
6
7/// The state of a button.
8#[derive(Clone, Copy)]
9pub enum ButtonState {
10    /// The default state.
11    Default,
12    /// The button is pressed.
13    Pressed,
14    /// The button is active.
15    Active,
16    /// The button is inactive.
17    Inactive,
18    /// The button is in an error state.
19    Error,
20}
21
22/// A button in the view system.
23///
24/// This struct represents a button in the view system. It contains
25/// the text, icon, and state of the button.
26#[derive(Clone)]
27pub struct Button {
28    /// The text to display on the button.
29    pub(crate) text: String,
30    /// The icon to display on the button.
31    pub(crate) icon: Option<&'static str>,
32    /// The state of the button.
33    pub(crate) state: ButtonState,
34    /// Alternative theme
35    pub(crate) theme: Option<Theme>,
36}
37
38impl Button {
39    /// Create a new button with the given text, icon, and state.
40    pub fn new(text: String, icon: Option<&'static str>, state: ButtonState) -> Self {
41        Button {
42            text,
43            icon,
44            state,
45            theme: None,
46        }
47    }
48
49    /// Create a new button with the given text.
50    pub fn text(text: String) -> Self {
51        Button {
52            text,
53            icon: None,
54            state: ButtonState::Default,
55            theme: None,
56        }
57    }
58
59    /// Create a new button with the given text and icon.
60    pub fn with_icon(text: String, icon: &'static str) -> Self {
61        Button {
62            text,
63            icon: Some(icon),
64            state: ButtonState::Default,
65            theme: None,
66        }
67    }
68
69    /// Create a new button with the given text and state.
70    pub fn with_state(text: String, state: ButtonState) -> Self {
71        Button {
72            text,
73            icon: None,
74            state,
75            theme: None,
76        }
77    }
78
79    /// Create a new button with the given text, icon, and state.
80    pub fn with_icon_and_state(text: String, icon: &'static str, state: ButtonState) -> Self {
81        Button {
82            text,
83            icon: Some(icon),
84            state,
85            theme: None,
86        }
87    }
88
89    /// Update the text of the button.
90    pub fn updated_text(&self, text: String) -> Self {
91        Button {
92            text,
93            icon: self.icon,
94            state: self.state,
95            theme: self.theme.clone(),
96        }
97    }
98
99    /// Update the icon of the button.
100    pub fn updated_icon(&self, icon: &'static str) -> Self {
101        Button {
102            text: self.text.clone(),
103            icon: Some(icon),
104            state: self.state,
105            theme: self.theme.clone(),
106        }
107    }
108
109    /// Update the state of the button.
110    pub fn updated_state(&self, state: ButtonState) -> Self {
111        Button {
112            text: self.text.clone(),
113            icon: self.icon,
114            state,
115            theme: self.theme.clone(),
116        }
117    }
118
119    /// Update the theme of the button.
120    pub fn with_theme(self, theme: Theme) -> Self {
121        Button {
122            theme: Some(theme),
123            ..self
124        }
125    }
126}
127
128impl Default for Button {
129    fn default() -> Self {
130        Button {
131            text: "".to_string(),
132            icon: None,
133            state: ButtonState::Default,
134            theme: None,
135        }
136    }
137}