tauri_runtime_verso/
webview.rs

1#![allow(unused_variables)]
2
3use tauri_runtime::{
4    Error, Result, UserEvent, WebviewDispatch, WebviewEventId,
5    dpi::{PhysicalPosition, PhysicalSize, Position, Size},
6    window::{WebviewEvent, WindowId},
7};
8use url::Url;
9use verso::VersoviewController;
10
11use std::{
12    fmt::{self, Debug},
13    sync::{Arc, Mutex},
14};
15
16use crate::{RuntimeContext, VersoRuntime};
17
18/// The Tauri [`WebviewDispatch`] for [`VersoRuntime`].
19#[derive(Clone)]
20pub struct VersoWebviewDispatcher<T: UserEvent> {
21    pub(crate) id: u32,
22    pub(crate) context: RuntimeContext<T>,
23    pub(crate) webview: Arc<Mutex<VersoviewController>>,
24}
25
26impl<T: UserEvent> Debug for VersoWebviewDispatcher<T> {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        f.debug_struct("VersoWebviewDispatcher")
29            .field("id", &self.id)
30            .field("context", &self.context)
31            .field("webview", &"VersoviewController")
32            .finish()
33    }
34}
35
36impl<T: UserEvent> WebviewDispatch<T> for VersoWebviewDispatcher<T> {
37    type Runtime = VersoRuntime<T>;
38
39    fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
40        self.context.run_on_main_thread(f)
41    }
42
43    /// Unsupported, has no effect when called, the callback will not be called
44    fn on_webview_event<F: Fn(&WebviewEvent) + Send + 'static>(&self, f: F) -> WebviewEventId {
45        self.context.next_window_event_id()
46    }
47
48    /// Unsupported, has no effect when called, the callback will not be called
49    fn with_webview<F: FnOnce(Box<dyn std::any::Any>) + Send + 'static>(&self, f: F) -> Result<()> {
50        Ok(())
51    }
52
53    /// Unsupported, has no effect when called
54    fn set_zoom(&self, scale_factor: f64) -> Result<()> {
55        Ok(())
56    }
57
58    fn eval_script<S: Into<String>>(&self, script: S) -> Result<()> {
59        self.webview
60            .lock()
61            .unwrap()
62            .execute_script(script.into())
63            .map_err(|_| Error::FailedToSendMessage)?;
64        Ok(())
65    }
66
67    fn url(&self) -> Result<String> {
68        Ok(self
69            .webview
70            .lock()
71            .unwrap()
72            .get_current_url()
73            .map_err(|_| Error::FailedToSendMessage)?
74            .to_string())
75    }
76
77    fn bounds(&self) -> Result<tauri_runtime::dpi::Rect> {
78        Ok(tauri_runtime::dpi::Rect {
79            position: self.position()?.into(),
80            size: self.size()?.into(),
81        })
82    }
83
84    fn position(&self) -> Result<PhysicalPosition<i32>> {
85        Ok(PhysicalPosition { x: 0, y: 0 })
86    }
87
88    fn size(&self) -> Result<PhysicalSize<u32>> {
89        let size = self
90            .webview
91            .lock()
92            .unwrap()
93            .get_inner_size()
94            .map_err(|_| Error::FailedToSendMessage)?;
95        Ok(size)
96    }
97
98    fn navigate(&self, url: Url) -> Result<()> {
99        self.webview
100            .lock()
101            .unwrap()
102            .navigate(url)
103            .map_err(|_| Error::FailedToSendMessage)?;
104        Ok(())
105    }
106
107    /// Unsupported, has no effect when called
108    fn print(&self) -> Result<()> {
109        Ok(())
110    }
111
112    /// Unsupported, has no effect when called,
113    /// the versoview controls both the webview and the window
114    /// use the method from the parent window instead
115    fn close(&self) -> Result<()> {
116        Ok(())
117    }
118
119    /// Unsupported, has no effect when called,
120    /// the versoview controls both the webview and the window
121    /// use the method from the parent window instead
122    fn set_bounds(&self, bounds: tauri_runtime::dpi::Rect) -> Result<()> {
123        Ok(())
124    }
125
126    /// Unsupported, has no effect when called,
127    /// the versoview controls both the webview and the window
128    /// use the method from the parent window instead
129    fn set_size(&self, _size: Size) -> Result<()> {
130        Ok(())
131    }
132
133    /// Unsupported, has no effect when called,
134    /// the versoview controls both the webview and the window
135    /// use the method from the parent window instead
136    fn set_position(&self, _position: Position) -> Result<()> {
137        Ok(())
138    }
139
140    /// Unsupported, has no effect when called,
141    /// the versoview controls both the webview and the window
142    /// use the method from the parent window instead
143    fn set_focus(&self) -> Result<()> {
144        Ok(())
145    }
146
147    /// Unsupported, has no effect when called
148    fn reparent(&self, window_id: WindowId) -> Result<()> {
149        Ok(())
150    }
151
152    /// Unsupported, has no effect when called
153    fn set_auto_resize(&self, auto_resize: bool) -> Result<()> {
154        Ok(())
155    }
156
157    /// Unsupported, has no effect when called
158    fn clear_all_browsing_data(&self) -> Result<()> {
159        Ok(())
160    }
161
162    /// Unsupported, has no effect when called,
163    /// the versoview controls both the webview and the window
164    /// use the method from the parent window instead
165    fn hide(&self) -> Result<()> {
166        Ok(())
167    }
168
169    /// Unsupported, has no effect when called,
170    /// the versoview controls both the webview and the window
171    /// use the method from the parent window instead
172    fn show(&self) -> Result<()> {
173        Ok(())
174    }
175
176    /// Unsupported, has no effect when called
177    fn set_background_color(&self, color: Option<tauri_utils::config::Color>) -> Result<()> {
178        Ok(())
179    }
180
181    /// Unsupported, has no effect when called
182    #[cfg(debug_assertions)]
183    fn open_devtools(&self) {}
184
185    /// Unsupported, has no effect when called
186    #[cfg(debug_assertions)]
187    fn close_devtools(&self) {}
188
189    /// Always false since we don't have devtools built-in
190    #[cfg(debug_assertions)]
191    fn is_devtools_open(&self) -> Result<bool> {
192        Ok(false)
193    }
194
195    fn reload(&self) -> Result<()> {
196        self.webview
197            .lock()
198            .unwrap()
199            .reload()
200            .map_err(|_| Error::FailedToSendMessage)?;
201        Ok(())
202    }
203
204    /// Unsupported, always returns an empty vector
205    fn cookies_for_url(&self, url: Url) -> Result<Vec<tauri_runtime::Cookie<'static>>> {
206        Ok(Vec::new())
207    }
208
209    /// Unsupported, always returns an empty vector
210    fn cookies(&self) -> Result<Vec<tauri_runtime::Cookie<'static>>> {
211        Ok(Vec::new())
212    }
213}