Mountain/Environment/WebviewProvider/
Configuration.rs1use std::collections::HashMap;
9
10use CommonLibrary::{Error::CommonError::CommonError, IPC::SkyEvent::SkyEvent};
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
21 handle:String,
22
23 options_value:Value,
24) -> Result<(), CommonError> {
25 dev_log!("extensions", "[WebviewProvider] Setting options for Webview: {}", handle);
26
27 if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
28 let options_map:HashMap<String, Value> = serde_json::from_value(options_value.clone()).map_err(|error| {
29 CommonError::SerializationError { Description:format!("Failed to parse Webview options: {}", error) }
30 })?;
31
32 if let Some(title) = options_map.get("title").and_then(|v| v.as_str()) {
34 webview_window.set_title(title).map_err(|error| {
35 CommonError::UserInterfaceInteraction { Reason:format!("Failed to set Webview title: {}", error) }
36 })?;
37
38 {
40 let mut webview_guard = env
41 .ApplicationState
42 .Feature
43 .Webviews
44 .ActiveWebviews
45 .lock()
46 .map_err(Utility::ErrorMapping::MapApplicationStateLockErrorToCommonError)?;
47
48 if let Some(state) = webview_guard.get_mut(&handle) {
49 state.Title = title.to_string();
50 }
51 }
52 }
53
54 }
62
63 env.ApplicationHandle
65 .emit::<Value>(
66 SkyEvent::WebviewOptionsChanged.AsStr(),
67 json!({ "Handle": handle, "Options": options_value }),
68 )
69 .map_err(|error| {
70 CommonError::IPCError { Description:format!("Failed to emit Webview options changed event: {}", error) }
71 })?;
72
73 Ok(())
74}
75
76pub(super) async fn set_webview_html_impl(
78 env:&MountainEnvironment,
79
80 handle:String,
81
82 html:String,
83) -> Result<(), CommonError> {
84 dev_log!(
85 "extensions",
86 "[WebviewProvider] Setting HTML for Webview: {} ({} bytes)",
87 handle,
88 html.len()
89 );
90
91 if let Some(webview_window) = env.ApplicationHandle.get_webview_window(&handle) {
92 webview_window
93 .emit::<String>(SkyEvent::WebviewSetHTML.AsStr(), html)
94 .map_err(|error| CommonError::IPCError { Description:format!("Failed to set Webview HTML: {}", error) })?;
95
96 Ok(())
97 } else {
98 Err(CommonError::WebviewNotFound { Handle:handle })
99 }
100}