Skip to main content

Mountain/Environment/WebviewProvider/
Configuration.rs

1//! # WebviewProvider - Configuration Operations
2//!
3//! Implementation of webview configuration methods for
4//! [`MountainEnvironment`]
5//!
6//! Handles setting webview options and HTML content.
7
8use 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
17/// Configuration operations implementation for MountainEnvironment
18pub(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		// Update title
33		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			// Update state
39			{
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		// Set the webview panel's icon by storing the icon path in the
55		// webview state in ApplicationState.Feature.Webviews. Validate the
56		// path exists, convert to appropriate format (Url or string),
57		// update the UI to display the icon in the tab bar or title
58		// area, and emit an event to refresh the frontend
59		// representation. The icon path can be a theme-aware icon path or a
60		// custom image file URI.
61	}
62
63	// Emit options changed event
64	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
76/// Sets the HTML content of a Webview.
77pub(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}