tilepad_plugin_sdk/
protocol.rs

1use serde::{Deserialize, Serialize};
2use uuid::Uuid;
3
4pub type PluginId = String;
5pub type ActionId = String;
6
7pub type ProfileId = Uuid;
8pub type FolderId = Uuid;
9pub type DeviceId = Uuid;
10pub type TileId = Uuid;
11pub type JsonObject = serde_json::Map<String, serde_json::Value>;
12
13#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
14pub struct InspectorContext {
15    pub profile_id: ProfileId,
16    pub folder_id: FolderId,
17
18    pub plugin_id: PluginId,
19    pub action_id: ActionId,
20
21    pub tile_id: TileId,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
25pub struct DisplayContext {
26    pub device_id: DeviceId,
27    pub plugin_id: PluginId,
28    pub action_id: ActionId,
29    pub tile_id: TileId,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
33pub struct TileInteractionContext {
34    pub device_id: DeviceId,
35
36    pub plugin_id: PluginId,
37    pub action_id: ActionId,
38
39    pub tile_id: TileId,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct DeepLinkContext {
44    pub url: String,
45    pub host: Option<String>,
46    pub path: String,
47    pub query: Option<String>,
48    pub fragment: Option<String>,
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct TileModel {
53    /// Unique ID of the tile
54    pub id: TileId,
55
56    /// Configuration for the tile and how it appears in the UI
57    pub config: TileConfig,
58
59    /// Properties / settings defined on this specific tile
60    pub properties: JsonObject,
61
62    /// ID of the folder this tile is within
63    pub folder_id: FolderId,
64
65    /// ID of the plugin the `action_id` is apart of
66    pub plugin_id: PluginId,
67    /// ID of the action within the plugin to execute
68    pub action_id: ActionId,
69
70    /// Position of the tile
71    pub position: TilePosition,
72}
73
74#[derive(Debug, Clone, Serialize, Deserialize)]
75pub struct TileConfig {
76    /// Icon to use
77    pub icon: TileIcon,
78    /// Label to display on top of the tile
79    pub label: TileLabel,
80}
81
82#[derive(Debug, Clone, Serialize, Deserialize)]
83pub struct TileIconOptions {
84    pub padding: u32,
85    pub background_color: String,
86    pub border_color: String,
87}
88
89#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
90pub struct TilePosition {
91    /// Row within the UI to display at
92    pub row: u32,
93    /// Column within the UI to display at
94    pub column: u32,
95    /// Number of rows to span
96    pub row_span: u32,
97    /// Number of columns to span
98    pub column_span: u32,
99}
100
101#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
102#[serde(tag = "type")]
103pub enum TileIcon {
104    /// No icon
105    #[default]
106    None,
107
108    /// Icon from a specific plugin path
109    PluginIcon {
110        /// ID of the plugin the icon is from
111        plugin_id: PluginId,
112        /// Path to the icon file
113        icon: String,
114    },
115
116    /// Use an icon from an icon pack
117    IconPack {
118        /// ID of the icon pack
119        pack_id: String,
120        /// Path to the icon file
121        path: String,
122    },
123
124    // Image at some remote URL
125    Url {
126        src: String,
127    },
128}
129
130#[derive(Default, Debug, Clone, Serialize, Deserialize)]
131#[serde(default)]
132pub struct TileLabel {
133    pub enabled: Option<bool>,
134    pub label: Option<String>,
135    pub align: Option<LabelAlign>,
136
137    pub font: Option<String>,
138    pub font_size: Option<u32>,
139
140    pub bold: Option<bool>,
141    pub italic: Option<bool>,
142    pub underline: Option<bool>,
143    pub outline: Option<bool>,
144
145    pub color: Option<String>,
146    pub outline_color: Option<String>,
147}
148
149#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
150pub enum LabelAlign {
151    #[default]
152    Bottom,
153    Middle,
154    Top,
155}
156
157/// Plugin message coming from the client side
158#[derive(Debug, Serialize)]
159#[serde(tag = "type")]
160pub(crate) enum ClientPluginMessage {
161    /// Register the current plugin with the server
162    RegisterPlugin { plugin_id: PluginId },
163
164    /// Request the current plugin properties
165    GetProperties,
166
167    /// Set the properties for the plugin (Partial update)
168    SetProperties {
169        properties: serde_json::Value,
170
171        /// Whether to treat the properties update as a partial update
172        partial: bool,
173    },
174
175    /// Send data to the current inspector window
176    SendToInspector {
177        /// Inspector context
178        ctx: InspectorContext,
179        /// Message to send the inspector
180        message: serde_json::Value,
181    },
182
183    /// Send data to a specific display
184    SendToDisplay {
185        /// Inspector context
186        ctx: DisplayContext,
187        /// Message to send the display
188        message: serde_json::Value,
189    },
190
191    /// Open a URL
192    OpenUrl { url: String },
193
194    /// Request the current properties for a tile
195    GetTileProperties {
196        /// ID of the tile to get properties for
197        tile_id: TileId,
198    },
199
200    /// Set the current properties for a tile
201    SetTileProperties {
202        /// ID of the tile to set properties for
203        tile_id: TileId,
204        /// Properties for the tile
205        properties: serde_json::Value,
206        /// Whether to treat the properties update as a partial update
207        partial: bool,
208    },
209
210    /// Set the current icon for a tile
211    SetTileIcon { tile_id: TileId, icon: TileIcon },
212
213    /// Set the current label for a tile
214    SetTileLabel { tile_id: TileId, label: TileLabel },
215
216    /// Get all currently visible tiles
217    GetVisibleTiles,
218
219    /// Display an icon on connected devices
220    DisplayIndicator {
221        /// ID of the device to display on
222        device_id: Uuid,
223        /// ID of the tile to display it on
224        tile_id: Uuid,
225        /// Indicator to display
226        indicator: DeviceIndicator,
227        /// Duration in milliseconds to display the
228        /// indicator for
229        duration: u32,
230    },
231}
232
233/// Plugin message coming from the server side
234#[derive(Debug, Clone, Deserialize)]
235#[serde(tag = "type")]
236pub(crate) enum ServerPluginMessage {
237    /// Plugin has registered with the server
238    Registered {
239        #[allow(unused)]
240        plugin_id: PluginId,
241    },
242
243    /// Properties received from the server
244    Properties { properties: serde_json::Value },
245
246    /// Tile was clicked on a remote device
247    TileClicked {
248        ctx: TileInteractionContext,
249        properties: serde_json::Value,
250    },
251
252    /// Got a message from the inspector
253    RecvFromInspector {
254        ctx: InspectorContext,
255        message: serde_json::Value,
256    },
257
258    /// Got a message from a display
259    RecvFromDisplay {
260        ctx: DisplayContext,
261        message: serde_json::Value,
262    },
263
264    /// Inspector was opened
265    InspectorOpen { ctx: InspectorContext },
266
267    /// Inspector was closed
268    InspectorClose { ctx: InspectorContext },
269
270    /// Received a deep link message for the plugin
271    DeepLink { ctx: DeepLinkContext },
272
273    /// Properties requested for a tile
274    TileProperties {
275        tile_id: TileId,
276        properties: serde_json::Value,
277    },
278
279    /// Selection of tiles for a device has changed
280    DeviceTiles {
281        /// ID of the device that changes
282        device_id: DeviceId,
283        /// Tiles that are now visible on the device
284        tiles: Vec<TileModel>,
285    },
286
287    VisibleTiles {
288        /// Tiles that are currently visible
289        tiles: Vec<TileModel>,
290    },
291}
292
293#[derive(Debug, Serialize, Deserialize)]
294pub enum DeviceIndicator {
295    Error,
296    Success,
297    Warning,
298    Loading,
299    /// Clear the active indicator
300    None,
301}