| | | 1 | | // SPDX-FileCopyrightText: 2026 Alper Çelik |
| | | 2 | | // SPDX-License-Identifier: MIT OR Apache-2.0 |
| | | 3 | | |
| | | 4 | | //! Webview operations (owned by the webview worker). |
| | | 5 | | //! |
| | | 6 | | //! Delegates to `bridge::webview::ffi` (`qt_webview.cpp`). |
| | | 7 | | //! |
| | | 8 | | //! # Units |
| | | 9 | | //! |
| | | 10 | | //! Qt and tauri disagree on pixel units: `QWidget::geometry()` and |
| | | 11 | | //! `QWidget::resize()/move()` are in device-independent (logical) pixels, while |
| | | 12 | | //! tauri's `Physical*` values are device pixels. The conversion uses the |
| | | 13 | | //! widget's `devicePixelRatioF()` (the *window's* dpr — correct under Wayland |
| | | 14 | | //! fractional scaling, where a `QScreen`'s dpr can differ). Getters convert |
| | | 15 | | //! Qt → physical in `qt_webview.cpp`; setters tell the C++ side whether the |
| | | 16 | | //! caller passed logical (`Size::Logical`, used as-is) or physical values |
| | | 17 | | //! (divided by the dpr), so the scale factor is applied in exactly one place. |
| | | 18 | | //! |
| | | 19 | | //! Qt widget geometry is integral *logical* pixels, so it is quantised to the |
| | | 20 | | //! dpr grid: a physical setter followed by a physical getter can differ by up |
| | | 21 | | //! to half a logical pixel (measured at dpr 1.5: `set_position(Physical(5, 7))` |
| | | 22 | | //! reads back as `(5, 8)`). That is Qt's granularity, not an invented value. |
| | | 23 | | //! Positions are relative to the window's client-area origin; sizes and |
| | | 24 | | //! positions of a widget that has not been attached to a window yet are |
| | | 25 | | //! relative to the screen. |
| | | 26 | | //! |
| | | 27 | | //! # Geometry ownership |
| | | 28 | | //! |
| | | 29 | | //! By default the webview is layout-managed: the window agent |
| | | 30 | | //! (`qt_window_attach_webview`) puts the view in the window's zero-margin |
| | | 31 | | //! `QVBoxLayout`, so it fills the client area and follows window resizes. |
| | | 32 | | //! `set_bounds`/`set_size`/`set_position` cannot be expressed through that |
| | | 33 | | //! layout, so the first such call detaches the view from it and Qt stops |
| | | 34 | | //! managing its geometry: afterwards the webview no longer resizes with its |
| | | 35 | | //! window (tauri's `set_auto_resize` is a no-op on this runtime) until it is |
| | | 36 | | //! re-attached with `reparent`. Getters always report the live widget geometry, |
| | | 37 | | //! whoever set it. |
| | | 38 | | |
| | | 39 | | use std::sync::atomic::{AtomicU64, Ordering}; |
| | | 40 | | |
| | | 41 | | use parking_lot::Mutex; |
| | | 42 | | use tauri_runtime::dpi::{PhysicalPosition, PhysicalSize, Position, Rect, Size}; |
| | | 43 | | use tauri_runtime::{Error, Result}; |
| | | 44 | | use tauri_runtime_qt_core as qtcore; |
| | | 45 | | use tauri_utils::config::Color; |
| | | 46 | | |
| | | 47 | | use crate::bridge::webview::ffi; |
| | | 48 | | |
| | 0 | 49 | | pub(crate) fn url(native: u64) -> Result<String> { |
| | 0 | 50 | | Ok(ffi::qt_webview_url(native)) |
| | 0 | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | pub(crate) fn navigate(native: u64, url: &str) -> Result<()> { |
| | 0 | 54 | | ffi::qt_webview_navigate(native, url); |
| | 0 | 55 | | Ok(()) |
| | 0 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | pub(crate) fn reload(native: u64) -> Result<()> { |
| | 0 | 59 | | ffi::qt_webview_reload(native); |
| | 0 | 60 | | Ok(()) |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | pub(crate) fn eval(native: u64, script: &str) -> Result<()> { |
| | 0 | 64 | | ffi::qt_webview_eval(native, script); |
| | 0 | 65 | | Ok(()) |
| | 0 | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | pub(crate) fn set_zoom(native: u64, factor: f64) -> Result<()> { |
| | 0 | 69 | | ffi::qt_webview_set_zoom(native, factor); |
| | 0 | 70 | | Ok(()) |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | pub(crate) fn set_background_color(native: u64, color: Option<Color>) -> Result<()> { |
| | 0 | 74 | | if let Some(Color(r, g, b, a)) = color { |
| | 0 | 75 | | ffi::qt_webview_set_background_color(native, r as i32, g as i32, b as i32, a as i32); |
| | 0 | 76 | | } |
| | 0 | 77 | | Ok(()) |
| | 0 | 78 | | } |
| | | 79 | | |
| | | 80 | | /// Places the webview at `bounds` (position + size), the same way |
| | | 81 | | /// `set_position` + `set_size` do; a `Rect` may mix logical and physical |
| | | 82 | | /// components, so each half is converted on its own. |
| | 0 | 83 | | pub(crate) fn set_bounds(native: u64, bounds: Rect) -> Result<()> { |
| | 0 | 84 | | set_position(native, bounds.position)?; |
| | 0 | 85 | | set_size(native, bounds.size) |
| | 0 | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | pub(crate) fn show(native: u64) -> Result<()> { |
| | 0 | 89 | | ffi::qt_webview_show(native); |
| | 0 | 90 | | Ok(()) |
| | 0 | 91 | | } |
| | | 92 | | |
| | 0 | 93 | | pub(crate) fn hide(native: u64) -> Result<()> { |
| | 0 | 94 | | ffi::qt_webview_hide(native); |
| | 0 | 95 | | Ok(()) |
| | 0 | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | pub(crate) fn close(native: u64) -> Result<()> { |
| | 0 | 99 | | ffi::qt_webview_close(native); |
| | | 100 | | // The view (and its page) is going away, so any `runJavaScript` result that |
| | | 101 | | // is still pending will never arrive: drop its callback instead of leaking |
| | | 102 | | // it for the lifetime of the process. |
| | 0 | 103 | | PENDING_EVAL |
| | 0 | 104 | | .lock() |
| | 0 | 105 | | .retain(|(_, webview, _)| *webview != native); |
| | 0 | 106 | | Ok(()) |
| | 0 | 107 | | } |
| | | 108 | | |
| | 0 | 109 | | pub(crate) fn print(native: u64) -> Result<()> { |
| | 0 | 110 | | ffi::qt_webview_print(native); |
| | 0 | 111 | | Ok(()) |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | #[cfg(any(debug_assertions, feature = "devtools"))] |
| | 0 | 115 | | pub(crate) fn open_devtools(native: u64) { |
| | 0 | 116 | | ffi::qt_webview_open_devtools(native); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | #[cfg(any(debug_assertions, feature = "devtools"))] |
| | 0 | 120 | | pub(crate) fn close_devtools(native: u64) { |
| | 0 | 121 | | ffi::qt_webview_close_devtools(native); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | #[cfg(any(debug_assertions, feature = "devtools"))] |
| | 0 | 125 | | pub(crate) fn is_devtools_open(native: u64) -> Result<bool> { |
| | 0 | 126 | | Ok(ffi::qt_webview_is_devtools_open(native)) |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | // ---- geometry ---- |
| | | 130 | | |
| | 0 | 131 | | pub(crate) fn bounds(native: u64) -> Result<Rect> { |
| | 0 | 132 | | let geometry = live_geometry(native)?; |
| | 0 | 133 | | Ok(qtcore::webview::geometry_rect( |
| | 0 | 134 | | geometry.x, |
| | 0 | 135 | | geometry.y, |
| | 0 | 136 | | geometry.width, |
| | 0 | 137 | | geometry.height, |
| | 0 | 138 | | )) |
| | 0 | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | pub(crate) fn position(native: u64) -> Result<PhysicalPosition<i32>> { |
| | 0 | 142 | | let geometry = live_geometry(native)?; |
| | 0 | 143 | | Ok(PhysicalPosition::new(geometry.x, geometry.y)) |
| | 0 | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | pub(crate) fn size(native: u64) -> Result<PhysicalSize<u32>> { |
| | 0 | 147 | | let geometry = live_geometry(native)?; |
| | 0 | 148 | | Ok(qtcore::webview::geometry_size( |
| | 0 | 149 | | geometry.width, |
| | 0 | 150 | | geometry.height, |
| | 0 | 151 | | )) |
| | 0 | 152 | | } |
| | | 153 | | |
| | 0 | 154 | | pub(crate) fn set_size(native: u64, size: Size) -> Result<()> { |
| | 0 | 155 | | let (width, height, physical) = qtcore::webview::size_params(size); |
| | 0 | 156 | | if ffi::qt_webview_set_size(native, width, height, physical) { |
| | 0 | 157 | | Ok(()) |
| | | 158 | | } else { |
| | 0 | 159 | | Err(missing(native)) |
| | | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | |
| | 0 | 163 | | pub(crate) fn set_position(native: u64, position: Position) -> Result<()> { |
| | | 164 | | // A webview is a child widget, so this positions it inside its own window. |
| | | 165 | | // That works on Wayland too (unlike positioning a *top-level window*, which |
| | | 166 | | // the protocol forbids): a child's placement is client-side. |
| | 0 | 167 | | let (x, y, physical) = qtcore::webview::position_params(position); |
| | 0 | 168 | | if ffi::qt_webview_set_position(native, x, y, physical) { |
| | 0 | 169 | | Ok(()) |
| | | 170 | | } else { |
| | 0 | 171 | | Err(missing(native)) |
| | | 172 | | } |
| | 0 | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | pub(crate) fn set_focus(native: u64) -> Result<()> { |
| | 0 | 176 | | if ffi::qt_webview_set_focus(native) { |
| | 0 | 177 | | Ok(()) |
| | | 178 | | } else { |
| | 0 | 179 | | Err(missing(native)) |
| | | 180 | | } |
| | 0 | 181 | | } |
| | | 182 | | |
| | | 183 | | // ---- script evaluation with a result callback ---- |
| | | 184 | | |
| | | 185 | | /// Monotonic id for the pending-eval registry; never reused, so a late result |
| | | 186 | | /// can never be delivered to a newer callback. |
| | | 187 | | static NEXT_EVAL_REQUEST: AtomicU64 = AtomicU64::new(1); |
| | | 188 | | |
| | | 189 | | /// `(request id, webview handle, callback)` for `runJavaScript` calls whose |
| | | 190 | | /// result has not arrived yet. `Vec` instead of a map so the whole registry can |
| | | 191 | | /// live in a `const`-initialisable `static`. |
| | | 192 | | static PENDING_EVAL: Mutex<Vec<(u64, u64, Box<dyn Fn(String) + Send + 'static>)>> = |
| | | 193 | | Mutex::new(Vec::new()); |
| | | 194 | | |
| | 0 | 195 | | pub(crate) fn eval_with_callback( |
| | 0 | 196 | | native: u64, |
| | 0 | 197 | | script: &str, |
| | 0 | 198 | | callback: impl Fn(String) + Send + 'static, |
| | 0 | 199 | | ) -> Result<()> { |
| | 0 | 200 | | let request_id = NEXT_EVAL_REQUEST.fetch_add(1, Ordering::Relaxed); |
| | 0 | 201 | | PENDING_EVAL |
| | 0 | 202 | | .lock() |
| | 0 | 203 | | .push((request_id, native, Box::new(callback))); |
| | | 204 | | |
| | 0 | 205 | | if ffi::qt_webview_eval_with_callback(native, request_id, script) { |
| | 0 | 206 | | Ok(()) |
| | | 207 | | } else { |
| | 0 | 208 | | PENDING_EVAL.lock().retain(|(id, _, _)| *id != request_id); |
| | 0 | 209 | | Err(missing(native)) |
| | | 210 | | } |
| | 0 | 211 | | } |
| | | 212 | | |
| | | 213 | | /// Delivers a `runJavaScript` result (`json`, see the bridge) to the callback |
| | | 214 | | /// registered for `request_id`. Called from Qt on the GUI thread. |
| | | 215 | | /// |
| | | 216 | | /// The callback runs outside the registry lock, so a callback that calls back |
| | | 217 | | /// into the webview API cannot deadlock; an unknown id (a result for a webview |
| | | 218 | | /// that was closed, or a duplicate) is dropped. |
| | 0 | 219 | | pub(crate) fn on_eval_result(request_id: u64, json: &str) { |
| | 0 | 220 | | let callback = { |
| | 0 | 221 | | let mut pending = PENDING_EVAL.lock(); |
| | 0 | 222 | | match pending.iter().position(|(id, _, _)| *id == request_id) { |
| | 0 | 223 | | Some(index) => Some(pending.swap_remove(index).2), |
| | 0 | 224 | | None => None, |
| | | 225 | | } |
| | | 226 | | }; |
| | | 227 | | |
| | 0 | 228 | | match callback { |
| | 0 | 229 | | Some(callback) => callback(json.to_owned()), |
| | | 230 | | None => { |
| | 0 | 231 | | crate::qtrace!( |
| | | 232 | | "[tauri-runtime-qt] eval result for unknown/expired request {request_id} ({json})" |
| | | 233 | | ); |
| | | 234 | | } |
| | | 235 | | } |
| | 0 | 236 | | } |
| | | 237 | | |
| | | 238 | | // ---- reparenting / profile data ---- |
| | | 239 | | |
| | 0 | 240 | | pub(crate) fn reparent(native: u64, window_native: u64) -> Result<()> { |
| | 0 | 241 | | if ffi::qt_webview_reparent(native, window_native) { |
| | 0 | 242 | | Ok(()) |
| | | 243 | | } else { |
| | 0 | 244 | | Err(Error::CreateWebview( |
| | 0 | 245 | | format!( |
| | 0 | 246 | | "tauri-runtime-qt: cannot reparent webview {native} into window {window_native} \ |
| | 0 | 247 | | (unknown webview or window)" |
| | 0 | 248 | | ) |
| | 0 | 249 | | .into(), |
| | 0 | 250 | | )) |
| | | 251 | | } |
| | 0 | 252 | | } |
| | | 253 | | |
| | | 254 | | /// Clears the browsing data Qt 6.11 exposes: HTTP cache, cookies and visited |
| | | 255 | | /// links; see the implementation comment in `qt_webview.cpp` for the stores it |
| | | 256 | | /// cannot touch (localStorage, IndexedDB, service workers) — Qt offers no API |
| | | 257 | | /// for those on a live profile. Fire-and-forget: Qt's cache clearing and cookie |
| | | 258 | | /// deletion are asynchronous. |
| | 0 | 259 | | pub(crate) fn clear_all_browsing_data(native: u64) -> Result<()> { |
| | 0 | 260 | | if ffi::qt_webview_clear_browsing_data(native) { |
| | 0 | 261 | | Ok(()) |
| | | 262 | | } else { |
| | 0 | 263 | | Err(missing(native)) |
| | | 264 | | } |
| | 0 | 265 | | } |
| | | 266 | | |
| | | 267 | | // ---- diagnostics ---- |
| | | 268 | | |
| | | 269 | | /// `TAURIQT_EVAL_DEBUG=1` smoke test for `eval_with_callback`: runs |
| | | 270 | | /// `document.title = 'eval-ok'` and prints whatever the page reports back. |
| | | 271 | | /// |
| | | 272 | | /// Triggered from `qt_webview.cpp` when the *first* webview finishes its first |
| | | 273 | | /// page load. Unset (the normal case) this returns immediately, so it costs one |
| | | 274 | | /// `getenv` per process. |
| | 21 | 275 | | pub(crate) fn debug_hook_first_load(native: u64) { |
| | 21 | 276 | | if std::env::var("TAURIQT_EVAL_DEBUG").ok().as_deref() != Some("1") { |
| | 21 | 277 | | return; |
| | 0 | 278 | | } |
| | | 279 | | |
| | 0 | 280 | | let result = eval_with_callback(native, "document.title = 'eval-ok'", |json| { |
| | 0 | 281 | | eprintln!("[tauri-runtime-qt] TAURIQT_EVAL_DEBUG eval_with_callback -> {json}"); |
| | 0 | 282 | | }); |
| | 0 | 283 | | match result { |
| | 0 | 284 | | Ok(()) => eprintln!( |
| | | 285 | | "[tauri-runtime-qt] TAURIQT_EVAL_DEBUG ran document.title = 'eval-ok' on webview {native}" |
| | | 286 | | ), |
| | 0 | 287 | | Err(error) => eprintln!("[tauri-runtime-qt] TAURIQT_EVAL_DEBUG failed: {error}"), |
| | | 288 | | } |
| | 21 | 289 | | } |
| | | 290 | | |
| | | 291 | | // ---- helpers ---- |
| | | 292 | | |
| | | 293 | | /// Live widget geometry in physical pixels, or `Err` for an unknown handle (Qt |
| | | 294 | | /// cannot answer truthfully then, and a made-up `0` would be a lie). |
| | 0 | 295 | | fn live_geometry(native: u64) -> Result<ffi::QtWebviewGeometry> { |
| | 0 | 296 | | let geometry = ffi::qt_webview_geometry(native); |
| | 0 | 297 | | if geometry.ok { |
| | 0 | 298 | | Ok(geometry) |
| | | 299 | | } else { |
| | 0 | 300 | | Err(missing(native)) |
| | | 301 | | } |
| | 0 | 302 | | } |
| | | 303 | | |
| | | 304 | | /// A handle that is no longer in the Qt registry. Tauri has no dedicated |
| | | 305 | | /// "webview not found" error; `Error::CreateWebview` is this crate's webview |
| | | 306 | | /// error carrier (see `ops`). |
| | 0 | 307 | | fn missing(native: u64) -> Error { |
| | 0 | 308 | | Error::CreateWebview(format!("tauri-runtime-qt: webview {native} does not exist").into()) |
| | 0 | 309 | | } |