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;
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	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		// Update title
31		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			// Update state
37			{
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		// Set the webview panel's icon by storing the icon path in the
53		// webview state in ApplicationState.Feature.Webviews. Validate the
54		// path exists, convert to appropriate format (Url or string),
55		// update the UI to display the icon in the tab bar or title
56		// area, and emit an event to refresh the frontend
57		// representation. The icon path can be a theme-aware icon path or a
58		// custom image file URI.
59	}
60
61	// Emit options changed event
62	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
74/// Sets the HTML content of a Webview.
75pub(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}