streamdeck_oxide/view/mod.rs
1//! View system for Stream Deck applications.
2//!
3//! This module provides types and traits for creating and managing
4//! views in a Stream Deck application.
5
6mod button;
7mod matrix;
8mod manager;
9pub mod customizable;
10
11// Re-export public items
12pub use self::button::{Button, ButtonState};
13pub use self::matrix::ButtonMatrix;
14pub use self::manager::DisplayManager;
15
16use std::sync::Arc;
17use tokio::sync::mpsc;
18use generic_array::ArrayLength;
19
20use crate::navigation::NavigationEntry;
21
22/// A trait for views in a Stream Deck application.
23///
24/// This trait is implemented by types that represent different views
25/// or screens in a Stream Deck application. It provides methods for
26/// rendering the view, handling button clicks, and fetching state.
27#[async_trait::async_trait]
28pub trait View<W, H, C, N>: Send + Sync + 'static
29where
30 W: ArrayLength,
31 H: ArrayLength,
32 C: Send + Clone + Sync + 'static,
33 N: NavigationEntry<W, H, C>,
34{
35 /// Render the view to a button matrix.
36 ///
37 /// This method returns a button matrix that can be used to render
38 /// the buttons for this view.
39 async fn render(&self) -> Result<ButtonMatrix<W, H>, Box<dyn std::error::Error>>
40 where
41 W: ArrayLength,
42 H: ArrayLength;
43
44 /// Handle a button click.
45 ///
46 /// This method is called when a button is clicked. It takes the
47 /// application context, the button index, and a sender for navigation
48 /// events.
49 async fn on_click(
50 &self,
51 context: &C,
52 index: u8,
53 navigation: Arc<mpsc::Sender<N>>,
54 ) -> Result<(), Box<dyn std::error::Error>>;
55
56 /// Fetch state for all buttons in the view.
57 ///
58 /// This method is called to fetch the state for all buttons in the view.
59 /// It takes the application context.
60 async fn fetch_all(&self, _context: &C) -> Result<(), Box<dyn std::error::Error>>;
61}
62
63/// A trait for view state.
64///
65/// This trait is implemented by types that represent the state of a view.
66/// It provides a method for creating a new instance of the state.
67pub trait ViewState: Send + Sync + 'static {
68 /// Create a new instance of the view state.
69 fn new() -> Self;
70}