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)]
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)]
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)]
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)]
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)]
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
220/// Plugin message coming from the server side
221#[derive(Debug, Clone, Deserialize)]
222#[serde(tag = "type")]
223pub(crate) enum ServerPluginMessage {
224    /// Plugin has registered with the server
225    Registered {
226        #[allow(unused)]
227        plugin_id: PluginId,
228    },
229
230    /// Properties received from the server
231    Properties { properties: serde_json::Value },
232
233    /// Tile was clicked on a remote device
234    TileClicked {
235        ctx: TileInteractionContext,
236        properties: serde_json::Value,
237    },
238
239    /// Got a message from the inspector
240    RecvFromInspector {
241        ctx: InspectorContext,
242        message: serde_json::Value,
243    },
244
245    /// Got a message from a display
246    RecvFromDisplay {
247        ctx: DisplayContext,
248        message: serde_json::Value,
249    },
250
251    /// Inspector was opened
252    InspectorOpen { ctx: InspectorContext },
253
254    /// Inspector was closed
255    InspectorClose { ctx: InspectorContext },
256
257    /// Received a deep link message for the plugin
258    DeepLink { ctx: DeepLinkContext },
259
260    /// Properties requested for a tile
261    TileProperties {
262        tile_id: TileId,
263        properties: serde_json::Value,
264    },
265
266    /// Selection of tiles for a device has changed
267    DeviceTiles {
268        /// ID of the device that changes
269        device_id: DeviceId,
270        /// Tiles that are now visible on the device
271        tiles: Vec<TileModel>,
272    },
273
274    VisibleTiles {
275        /// Tiles that are currently visible
276        tiles: Vec<TileModel>,
277    },
278}