CommonLibrary/Webview/WebviewProvider.rs
1// File: Common/Source/Webview/WebviewProvider.rs
2// Role: Defines the abstract service trait for creating and managing Webviews.
3// Responsibilities:
4// - Provide a contract for creating, disposing, and revealing Webview panels.
5// - Define methods for setting a Webview's content (HTML) and options (title,
6
7// icon).
8// - Define a method for posting messages to a Webview's content script.
9
10//! # WebviewProvider Trait
11//!
12//! Defines the abstract service trait for creating and managing Webviews.
13
14use async_trait::async_trait;
15use serde_json::Value;
16
17use crate::{Environment::Environment::Environment, Error::CommonError::CommonError};
18
19/// An abstract service contract for an environment component that can manage
20/// Webview panels.
21///
22/// This trait defines all the operations necessary for creating Webview-based
23/// UI, setting their content, and managing their lifecycle, abstracting away
24/// the specific UI framework (e.g., Tauri, Electron) being used by the host.
25#[async_trait]
26pub trait WebviewProvider: Environment + Send + Sync {
27 /// Creates a new Webview panel.
28 ///
29 /// # Parameters
30 /// * `ExtensionDataValue`: DTO containing information about the extension
31 /// creating the panel.
32 /// * `ViewType`: A unique string identifying the type of the Webview.
33 /// * `Title`: The initial title for the Webview panel.
34 /// * `ShowOptionsValue`: DTO specifying the view column to show the panel
35 /// in.
36 /// * `PanelOptionsValue`: DTO specifying behavior options for the panel
37 /// (e.g., enable scripts).
38 /// * `ContentOptionsValue`: DTO specifying content options (e.g., local
39 /// resource roots).
40 ///
41 /// # Returns
42 /// A `Result` containing a unique handle (string) for the new Webview, or
43 /// a `CommonError` on failure.
44 async fn CreateWebviewPanel(
45 &self,
46
47 // DTO: WebviewExtensionDescriptionDTO
48 ExtensionDataValue:Value,
49
50 ViewType:String,
51
52 Title:String,
53
54 // DTO: WebviewShowOptionsDTO
55 ShowOptionsValue:Value,
56
57 // DTO: WebviewPanelOptionsDTO
58 PanelOptionsValue:Value,
59
60 // DTO: WebviewContentOptionsDTO
61 ContentOptionsValue:Value,
62 ) -> Result<String, CommonError>;
63
64 /// Disposes of a Webview panel, removing it from the UI.
65 ///
66 /// # Parameters
67 /// * `Handle`: The unique handle of the Webview panel to dispose.
68 async fn DisposeWebviewPanel(&self, Handle:String) -> Result<(), CommonError>;
69
70 /// Reveals an existing Webview panel, bringing it to the front.
71 ///
72 /// # Parameters
73 /// * `Handle`: The unique handle of the Webview panel to reveal.
74 /// * `ShowOptionsValue`: DTO specifying the view column to show the panel
75 /// in.
76 async fn RevealWebviewPanel(
77 &self,
78
79 Handle:String,
80
81 // DTO: WebviewShowOptionsDTO
82 ShowOptionsValue:Value,
83 ) -> Result<(), CommonError>;
84
85 /// Sets various options for a Webview panel, such as its title and icon
86 /// path.
87 ///
88 /// # Parameters
89 /// * `Handle`: The unique handle of the Webview panel to update.
90 /// * `OptionsValue`: A DTO (`WebviewPanelOptionsUpdateDTO`) containing the
91 /// options to set.
92 async fn SetWebviewOptions(&self, Handle:String, OptionsValue:Value) -> Result<(), CommonError>;
93
94 /// Sets the HTML content of a Webview panel.
95 // # Parameters
96 /// * `Handle`: The unique handle of the Webview panel.
97 /// * `HTML`: The HTML string to set as the content.
98 async fn SetWebviewHTML(&self, Handle:String, HTML:String) -> Result<(), CommonError>;
99
100 /// Posts a message from the extension host to the Webview content script.
101 ///
102 /// # Parameters
103 /// * `Handle`: The unique handle of the target Webview panel.
104 /// * `Message`: The JSON-serializable message to post.
105 ///
106 /// # Returns
107 /// `Ok(true)` if the message was posted successfully.
108 async fn PostMessageToWebview(&self, Handle:String, Message:Value) -> Result<bool, CommonError>;
109}