freya_webview/
registry.rs

1//! WebView types and configuration.
2
3use std::sync::Arc;
4
5use freya_core::prelude::UseId;
6
7/// Unique identifier for a WebView instance.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub struct WebViewId(pub usize);
10
11impl WebViewId {
12    /// Generate a new unique WebView ID.
13    pub fn new() -> Self {
14        Self(UseId::<Self>::get_in_hook())
15    }
16}
17
18impl Default for WebViewId {
19    fn default() -> Self {
20        Self::new()
21    }
22}
23
24/// Callback type for WebView customization.
25pub type WebViewCallback =
26    Arc<Box<dyn Fn(wry::WebViewBuilder) -> wry::WebViewBuilder + Send + 'static>>;
27
28/// Configuration for a WebView instance.
29#[derive(Clone, Default)]
30pub struct WebViewConfig {
31    /// The URL to load in the WebView.
32    pub url: String,
33    /// Whether the WebView background should be transparent.
34    pub transparent: bool,
35    /// Custom user agent string.
36    pub user_agent: Option<String>,
37    /// Optional callback called when the WebView is created.
38    pub on_created: Option<WebViewCallback>,
39}
40
41impl PartialEq for WebViewConfig {
42    fn eq(&self, other: &Self) -> bool {
43        self.url == other.url
44            && self.transparent == other.transparent
45            && self.user_agent == other.user_agent
46            && match (&self.on_created, &other.on_created) {
47                (None, None) => true,
48                (Some(a), Some(b)) => std::sync::Arc::ptr_eq(a, b),
49                _ => false,
50            }
51    }
52}
53
54impl std::fmt::Debug for WebViewConfig {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        f.debug_struct("WebViewConfig")
57            .field("url", &self.url)
58            .field("transparent", &self.transparent)
59            .field("user_agent", &self.user_agent)
60            .field(
61                "on_created",
62                &self.on_created.as_ref().map(|_| "<callback>"),
63            )
64            .finish()
65    }
66}