html_view_shared/lib.rs
1//! Shared types and protocol definitions for html_view.
2//!
3//! This crate defines the wire protocol between the API crate and the Tauri app,
4//! including all request and response types that cross the process boundary.
5
6use serde::{Deserialize, Serialize};
7use std::path::PathBuf;
8use url::Url;
9use uuid::Uuid;
10
11/// Protocol version for compatibility checking between library and viewer.
12///
13/// This follows semantic versioning:
14/// - Major version: Breaking changes to the protocol
15/// - Minor version: Backward-compatible additions
16/// - Patch version: Bug fixes that don't affect the protocol
17pub const PROTOCOL_VERSION: &str = env!("CARGO_PKG_VERSION");
18
19/// Complete request structure sent to the Tauri viewer application.
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct ViewerRequest {
22 /// Unique identifier for this viewer instance.
23 pub id: Uuid,
24
25 /// The content to display in the viewer.
26 pub content: ViewerContent,
27
28 /// Window configuration options.
29 pub window: WindowOptions,
30
31 /// Behaviour and security options.
32 pub behaviour: BehaviourOptions,
33
34 /// Environment and runtime options.
35 pub environment: EnvironmentOptions,
36
37 /// Dialog configuration.
38 pub dialog: DialogOptions,
39
40 /// Optional path to command file for runtime updates.
41 #[serde(default)]
42 pub command_path: Option<PathBuf>,
43}
44
45/// The type of content to display in the viewer.
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(tag = "type", rename_all = "snake_case")]
48pub enum ViewerContent {
49 /// Plain inline HTML string.
50 InlineHtml {
51 /// The HTML content to display.
52 html: String,
53
54 /// Optional base directory used to resolve relative paths in the HTML,
55 /// for example when the HTML refers to local assets.
56 base_dir: Option<PathBuf>,
57 },
58
59 /// A single local file, usually an HTML file.
60 LocalFile {
61 /// Path to the HTML file.
62 path: PathBuf,
63 },
64
65 /// A directory that contains a static HTML application, such as
66 /// index.html, JS bundles, CSS, and assets.
67 AppDir {
68 /// Root directory of the application.
69 root: PathBuf,
70
71 /// The entry HTML file relative to root, defaults to "index.html".
72 entry: Option<String>,
73 },
74
75 /// A remote URL. Only allowed if enabled in BehaviourOptions.
76 RemoteUrl {
77 /// The URL to load.
78 url: Url,
79 },
80}
81
82/// Window configuration options.
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct WindowOptions {
85 /// Window title. If None, a default title is used.
86 pub title: Option<String>,
87
88 /// Window width in logical pixels.
89 pub width: Option<u32>,
90
91 /// Window height in logical pixels.
92 pub height: Option<u32>,
93
94 /// Initial X position.
95 pub x: Option<i32>,
96
97 /// Initial Y position.
98 pub y: Option<i32>,
99
100 /// Whether the window can be resized.
101 pub resizable: bool,
102
103 /// Whether the window starts maximised.
104 pub maximised: bool,
105
106 /// Whether the window starts in fullscreen mode.
107 pub fullscreen: bool,
108
109 /// Whether to show window decorations (title bar, border).
110 pub decorations: bool,
111
112 /// Whether the window background is transparent.
113 pub transparent: bool,
114
115 /// Whether the window should always be on top of other windows.
116 pub always_on_top: bool,
117
118 /// Window theme ("light", "dark", or "system").
119 #[deprecated(since = "0.1.1", note = "Use theme_enum instead")]
120 pub theme: Option<String>,
121
122 /// Window theme preference (type-safe enum).
123 pub theme_enum: Option<WindowTheme>,
124
125 /// Background color in hex format (e.g., "#FFFFFF").
126 pub background_color: Option<String>,
127
128 /// Toolbar configuration.
129 pub toolbar: ToolbarOptions,
130}
131
132impl Default for WindowOptions {
133 fn default() -> Self {
134 Self {
135 title: Some("HTML Viewer".to_string()),
136 width: Some(1024),
137 height: Some(768),
138 x: None,
139 y: None,
140 resizable: true,
141 maximised: false,
142 fullscreen: false,
143 decorations: true,
144 transparent: false,
145 always_on_top: false,
146 #[allow(deprecated)]
147 theme: None,
148 theme_enum: None,
149 background_color: None,
150 toolbar: ToolbarOptions::default(),
151 }
152 }
153}
154
155/// Toolbar configuration options.
156#[derive(Debug, Clone, Serialize, Deserialize, Default)]
157pub struct ToolbarOptions {
158 /// Whether to show the custom toolbar.
159 pub show: bool,
160
161 /// Title text to display in the toolbar.
162 pub title_text: Option<String>,
163
164 /// Background color of the toolbar (hex).
165 pub background_color: Option<String>,
166
167 /// Text color of the toolbar (hex).
168 pub text_color: Option<String>,
169
170 /// List of buttons to show in the toolbar.
171 pub buttons: Vec<ToolbarButton>,
172}
173
174/// A button in the custom toolbar.
175#[derive(Debug, Clone, Serialize, Deserialize)]
176pub struct ToolbarButton {
177 /// Unique identifier for the button action.
178 pub id: String,
179
180 /// Text to display on the button.
181 pub label: String,
182
183 /// Optional icon name (e.g. from a standard set).
184 pub icon: Option<String>,
185}
186
187/// Window theme options.
188#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
189#[serde(rename_all = "lowercase")]
190pub enum WindowTheme {
191 /// Light theme.
192 Light,
193 /// Dark theme.
194 Dark,
195 /// System theme (follows OS preference).
196 #[default]
197 System,
198}
199
200/// Behaviour and security configuration.
201#[derive(Debug, Clone, Serialize, Deserialize, Default)]
202pub struct BehaviourOptions {
203 /// Whether navigation away from the initial content is allowed.
204 pub allow_external_navigation: bool,
205
206 /// Optional allowlist of hostnames that can be navigated to.
207 /// Only applies if allow_external_navigation is true.
208 pub allowed_domains: Option<Vec<String>>,
209
210 /// Whether devtools are enabled.
211 pub enable_devtools: bool,
212
213 /// Whether remote URL loading is permitted at all.
214 pub allow_remote_content: bool,
215
216 /// Whether system notifications are allowed.
217 pub allow_notifications: bool,
218}
219
220/// Dialog configuration options.
221#[derive(Debug, Clone, Serialize, Deserialize, Default)]
222pub struct DialogOptions {
223 /// Whether file dialogs are allowed.
224 pub allow_file_dialogs: bool,
225
226 /// Whether message dialogs are allowed.
227 pub allow_message_dialogs: bool,
228}
229
230/// Environment and runtime configuration.
231#[derive(Debug, Clone, Serialize, Deserialize, Default)]
232pub struct EnvironmentOptions {
233 /// Optional working directory for resolving relative paths.
234 pub working_dir: Option<PathBuf>,
235
236 /// Optional timeout in seconds after which the viewer will auto-close.
237 pub timeout_seconds: Option<u64>,
238}
239
240/// Exit status returned by the viewer application.
241#[derive(Debug, Clone, Serialize, Deserialize)]
242pub struct ViewerExitStatus {
243 /// The unique identifier matching the original request.
244 pub id: Uuid,
245
246 /// The reason the viewer exited.
247 pub reason: ViewerExitReason,
248
249 /// The protocol version of the viewer application.
250 /// This is used to check compatibility with the library.
251 #[serde(default = "default_version")]
252 pub viewer_version: String,
253}
254
255/// Default version for backward compatibility with old viewers that don't report version.
256fn default_version() -> String {
257 "0.0.0".to_string()
258}
259
260/// The reason the viewer exited.
261#[derive(Debug, Clone, Serialize, Deserialize)]
262#[serde(tag = "reason", rename_all = "snake_case")]
263pub enum ViewerExitReason {
264 /// The user closed the window.
265 ClosedByUser,
266
267 /// The viewer timed out according to EnvironmentOptions.
268 TimedOut,
269
270 /// An error occurred.
271 Error {
272 /// Error message.
273 message: String,
274 },
275}
276
277/// Commands that can be sent to a running viewer.
278#[derive(Debug, Clone, Serialize, Deserialize)]
279#[serde(tag = "type", rename_all = "snake_case")]
280pub enum ViewerCommand {
281 /// Refresh the displayed content.
282 Refresh {
283 /// Sequence number for command ordering.
284 seq: u64,
285 /// New content to display.
286 content: ViewerContent,
287 },
288}
289
290/// Response to a viewer command.
291#[derive(Debug, Clone, Serialize, Deserialize)]
292pub struct ViewerCommandResponse {
293 /// Sequence number matching the command.
294 pub seq: u64,
295 /// Whether the command succeeded.
296 pub success: bool,
297 /// Error message if unsuccessful.
298 pub error: Option<String>,
299}