Mountain/Environment/WebviewProvider/
configuration.rs1use std::collections::HashMap;
9
10use CommonLibrary::Error::CommonError::CommonError;
11use log::debug;
12use serde_json::{Value, json};
13use tauri::{Emitter, Manager, WebviewWindow};
14
15use super::super::{MountainEnvironment::MountainEnvironment, Utility};
16
17pub(super) async fn set_webview_options_impl(
19 env:&MountainEnvironment,
20 handle:String,
21 options_value:Value,
22) -> Result<(), CommonError> {
23 debug!("[WebviewProvider] Setting options for Webview: {}", handle);
24
25 if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
26 let options_map:HashMap<String, Value> = serde_json::from_value(options_value.clone()).map_err(|error| {
27 CommonError::SerializationError { Description:format!("Failed to parse Webview options: {}", error) }
28 })?;
29
30 if let Some(title) = options_map.get("title").and_then(|v| v.as_str()) {
32 webview_window.set_title(title).map_err(|error| {
33 CommonError::UserInterfaceInteraction { Reason:format!("Failed to set Webview title: {}", error) }
34 })?;
35
36 {
38 let mut webview_guard = env
39 .ApplicationState
40 .Feature
41 .Webviews
42 .ActiveWebviews
43 .lock()
44 .map_err(Utility::MapApplicationStateLockErrorToCommonError)?;
45
46 if let Some(state) = webview_guard.get_mut(&handle) {
47 state.Title = title.to_string();
48 }
49 }
50 }
51
52 }
60
61 env.ApplicationHandle
63 .emit::<Value>(
64 "sky://webview/options-changed",
65 json!({ "Handle": handle, "Options": options_value }),
66 )
67 .map_err(|error| {
68 CommonError::IPCError { Description:format!("Failed to emit Webview options changed event: {}", error) }
69 })?;
70
71 Ok(())
72}
73
74pub(super) async fn set_webview_html_impl(
76 env:&MountainEnvironment,
77 handle:String,
78 html:String,
79) -> Result<(), CommonError> {
80 debug!("[WebviewProvider] Setting HTML for Webview: {} ({} bytes)", handle, html.len());
81
82 if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
83 webview_window
84 .emit::<String>("sky://webview/set-html", html)
85 .map_err(|error| CommonError::IPCError { Description:format!("Failed to set Webview HTML: {}", error) })?;
86
87 Ok(())
88 } else {
89 Err(CommonError::WebviewNotFound { Handle:handle })
90 }
91}