Mountain/Environment/WebviewProvider/
Configuration.rs1use std::collections::HashMap;
9
10use CommonLibrary::Error::CommonError::CommonError;
11use serde_json::{Value, json};
12use tauri::{Emitter, Manager};
13
14use super::super::{MountainEnvironment::MountainEnvironment, Utility};
15use crate::dev_log;
16
17pub(super) async fn set_webview_options_impl(
19 env:&MountainEnvironment,
20 handle:String,
21 options_value:Value,
22) -> Result<(), CommonError> {
23 dev_log!("extensions", "[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 dev_log!(
81 "extensions",
82 "[WebviewProvider] Setting HTML for Webview: {} ({} bytes)",
83 handle,
84 html.len()
85 );
86
87 if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
88 webview_window
89 .emit::<String>("sky://webview/set-html", html)
90 .map_err(|error| CommonError::IPCError { Description:format!("Failed to set Webview HTML: {}", error) })?;
91
92 Ok(())
93 } else {
94 Err(CommonError::WebviewNotFound { Handle:handle })
95 }
96}