tilepad_plugin_sdk/
protocol.rs

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