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