|
| 1 | +//! Keep Tauri browser children out of the main WebView's vertical GTK layout. |
| 2 | +
|
| 3 | +use gtk::prelude::*; |
| 4 | +use gtk::{OverlaySignals, Widget}; |
| 5 | +use tauri::{LogicalPosition, LogicalSize, Webview, WebviewWindow, Window}; |
| 6 | +use tokio::sync::oneshot; |
| 7 | + |
| 8 | +const OVERLAY_NAME: &str = "openbitfun-browser-overlay"; |
| 9 | +const BOUNDS_KEY: &str = "openbitfun-browser-overlay-bounds"; |
| 10 | + |
| 11 | +pub(crate) fn install(window: &WebviewWindow) -> tauri::Result<()> { |
| 12 | + window.with_webview(|native| { |
| 13 | + let main = native.inner(); |
| 14 | + let Some(parent) = main.parent().and_downcast::<gtk::Box>() else { |
| 15 | + log::error!("Main WebView is missing its GTK container"); |
| 16 | + return; |
| 17 | + }; |
| 18 | + |
| 19 | + let overlay = gtk::Overlay::new(); |
| 20 | + overlay.set_widget_name(OVERLAY_NAME); |
| 21 | + overlay.connect_get_child_position(|_, child| { |
| 22 | + // SAFETY: This module exclusively uses this key for a Rectangle. |
| 23 | + // GTK calls this on its main thread and we copy the value immediately. |
| 24 | + unsafe { |
| 25 | + child |
| 26 | + .data::<gtk::gdk::Rectangle>(BOUNDS_KEY) |
| 27 | + .map(|value| *value.as_ref()) |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + // Only the primary child contributes to the window's size request. |
| 32 | + parent.remove(&main); |
| 33 | + overlay.add(&main); |
| 34 | + parent.pack_start(&overlay, true, true, 0); |
| 35 | + overlay.show(); |
| 36 | + }) |
| 37 | +} |
| 38 | + |
| 39 | +pub(crate) async fn create_child( |
| 40 | + window: Window, |
| 41 | + builder: tauri::webview::WebviewBuilder<tauri::Wry>, |
| 42 | + position: LogicalPosition<f64>, |
| 43 | + size: LogicalSize<f64>, |
| 44 | +) -> Result<Webview, String> { |
| 45 | + let (sender, receiver) = oneshot::channel(); |
| 46 | + let host = window.clone(); |
| 47 | + window |
| 48 | + .run_on_main_thread(move || { |
| 49 | + // Attach and hide in the same GTK iteration as Tauri creates the |
| 50 | + // child so the vertical box cannot lay it out or paint it first. |
| 51 | + let webview = host.add_child(builder.focused(false), position, size); |
| 52 | + let result = webview |
| 53 | + .map_err(|error| format!("failed to create browser webview: {error}")) |
| 54 | + .and_then(|webview| { |
| 55 | + let (attached, mut attachment) = oneshot::channel(); |
| 56 | + // Tauri dispatches with_webview inline on its main thread. |
| 57 | + let result = webview |
| 58 | + .with_webview(move |native| { |
| 59 | + let result = attach_child(&native.inner().upcast(), position, size); |
| 60 | + let _ = attached.send(result); |
| 61 | + }) |
| 62 | + .map_err(|error| format!("failed to access browser webview: {error}")) |
| 63 | + .and_then(|()| { |
| 64 | + attachment.try_recv().map_err(|_| { |
| 65 | + "browser webview GTK attachment did not run".to_string() |
| 66 | + })? |
| 67 | + }); |
| 68 | + if let Err(error) = result { |
| 69 | + let _ = webview.close(); |
| 70 | + return Err(error); |
| 71 | + } |
| 72 | + Ok(webview) |
| 73 | + }); |
| 74 | + let _ = sender.send(result); |
| 75 | + }) |
| 76 | + .map_err(|error| format!("failed to schedule browser webview creation: {error}"))?; |
| 77 | + receiver |
| 78 | + .await |
| 79 | + .map_err(|_| "browser webview creation was canceled".to_string())? |
| 80 | +} |
| 81 | + |
| 82 | +fn attach_child( |
| 83 | + child: &Widget, |
| 84 | + position: LogicalPosition<f64>, |
| 85 | + size: LogicalSize<f64>, |
| 86 | +) -> Result<(), String> { |
| 87 | + child.hide(); |
| 88 | + let parent = child |
| 89 | + .parent() |
| 90 | + .and_downcast::<gtk::Box>() |
| 91 | + .ok_or_else(|| "browser webview is missing its GTK container".to_string())?; |
| 92 | + let overlay = parent |
| 93 | + .children() |
| 94 | + .into_iter() |
| 95 | + .filter_map(|widget| widget.downcast::<gtk::Overlay>().ok()) |
| 96 | + .find(|overlay| overlay.widget_name() == OVERLAY_NAME) |
| 97 | + .ok_or_else(|| "browser webview is missing its GTK overlay".to_string())?; |
| 98 | + |
| 99 | + save_bounds(child, position, size); |
| 100 | + parent.remove(child); |
| 101 | + overlay.add_overlay(child); |
| 102 | + Ok(()) |
| 103 | +} |
| 104 | + |
| 105 | +pub(crate) async fn set_bounds( |
| 106 | + webview: &Webview, |
| 107 | + position: LogicalPosition<f64>, |
| 108 | + size: LogicalSize<f64>, |
| 109 | +) -> Result<(), String> { |
| 110 | + let (sender, receiver) = oneshot::channel(); |
| 111 | + webview |
| 112 | + .with_webview(move |native| { |
| 113 | + let child: Widget = native.inner().upcast(); |
| 114 | + let result = child |
| 115 | + .parent() |
| 116 | + .and_downcast::<gtk::Overlay>() |
| 117 | + .ok_or_else(|| "browser webview is missing its GTK overlay".to_string()) |
| 118 | + .map(|overlay| { |
| 119 | + save_bounds(&child, position, size); |
| 120 | + overlay.queue_allocate(); |
| 121 | + }); |
| 122 | + let _ = sender.send(result); |
| 123 | + }) |
| 124 | + .map_err(|error| format!("set bounds failed: {error}"))?; |
| 125 | + receiver |
| 126 | + .await |
| 127 | + .map_err(|_| "browser webview bounds update was canceled".to_string())? |
| 128 | +} |
| 129 | + |
| 130 | +fn save_bounds(child: &Widget, position: LogicalPosition<f64>, size: LogicalSize<f64>) { |
| 131 | + let position = position.cast::<i32>(); |
| 132 | + let size = size.cast::<i32>(); |
| 133 | + let bounds = gtk::gdk::Rectangle::new(position.x, position.y, size.width, size.height); |
| 134 | + // SAFETY: Only this module accesses this key, always with a Rectangle. |
| 135 | + unsafe { child.set_data(BOUNDS_KEY, bounds) } |
| 136 | +} |
0 commit comments