Файли
openidea/src-tauri/src/linux_webkit.rs
T
Ivan 3b488a7824 fix(macos): let background project tabs give their web content memory back (#7060)
A background project tab of a native tab group detaches its WKWebView from the window 30 s after it leaves the front, so WebKit frees the hidden page's graphics and caches. The view comes back when the tab is selected through our tab bar, when its window becomes key (Window menu, Close Project) or when AppKit makes it visible by itself; an inactive app's full-screen group waits for activation (#6937).

Every macOS with_webview caller goes through macos::with_web_view, which hands the retained view to a detached tab instead of panicking in wry; titlebar::set_height uses the NSWindow directly. clippy.toml disallows the raw with_webview calls. Covered by BackgroundTabs unit tests and an AppKit test of the synchronous re-attach on the key notification (it no longer asks a never-loaded web view for its process ids, which WebKit asserts on).

Fixes #6938

Co-authored-by: Pecnikas <[email protected]>

Co-authored-by: Pecnikas <[email protected]>
2026-12-01 03:20:08 +08:00

197 рядки
9.4 KiB
Rust
Неформатований Анотація Історія

Цей файл містить неоднозначні символи Юнікоду
Цей файл містить символи Юнікоду, які можна сплутати з іншими символами. Якщо ви вважаєте, що це зроблено навмисно, можете сміливо ігнорувати це попередження. Щоб показати їх, скористайтеся кнопкою Escape.
//! WebKitGTK's DMA-BUF renderer fails on Wayland with the NVIDIA driver: the window
//! never shows and GDK logs "Error 71 (Protocol error) dispatching to Wayland display".
//! Turning the driver's explicit sync off for this process avoids it and keeps the
//! renderer, so that is done before the first web view is created. Turning the DMA-BUF
//! renderer off also avoids it, but then WebKit paints on the CPU and a tool window drag
//! stalls the UI for seconds; setting `WEBKIT_DISABLE_DMABUF_RENDERER=1` still does that.
//!
//! On Wayland, GTK's default input method goes through the compositor (text-input-v3).
//! With IBus behind it, a dead key waiting for its second key makes WebKit collapse every
//! new selection in the window to its caret (Select All, mouse drags, text fields) until a
//! composition completes. GTK's own IBus module does not, so IBus sessions use that.
//! Fcitx's GTK module is the other way round on GNOME: Wayland gives it only coordinates
//! inside the window, so with several monitors its candidate window opens on the wrong
//! one. Through the compositor (Mutter hands text-input to Fcitx's IBus frontend) it
//! follows the caret, so GNOME Wayland sessions drop `GTK_IM_MODULE=fcitx`.
//!
//! WSLg's Wayland compositor has no usable GPU path for WebKitGTK (Mesa reports an
//! incompatible driver and the window stays blank); its X server works, so WSL uses X11
//! unless `GDK_BACKEND` is set.
const DISABLE_DMABUF: &str = "WEBKIT_DISABLE_DMABUF_RENDERER";
const DISABLE_EXPLICIT_SYNC: &str = "__NV_DISABLE_EXPLICIT_SYNC";
const IM_MODULE: &str = "GTK_IM_MODULE";
/// Run before other threads start (`set_var`).
#[cfg(target_os = "linux")]
pub fn configure() {
if use_x11_on_wsl(|k| std::env::var(k).ok()) {
tracing::info!("WSLg: GDK_BACKEND=x11");
unsafe { std::env::set_var("GDK_BACKEND", "x11") };
}
let nvidia = ["/proc/driver/nvidia/version", "/sys/module/nvidia"].iter().any(|p| std::path::Path::new(p).exists());
if disable_explicit_sync(|k| std::env::var(k).ok(), nvidia) {
tracing::info!("Wayland with the NVIDIA driver: {DISABLE_EXPLICIT_SYNC}=1");
unsafe { std::env::set_var(DISABLE_EXPLICIT_SYNC, "1") };
}
if use_ibus_module(|k| std::env::var(k).ok(), gtk_ibus_module_installed) {
tracing::info!("Wayland with IBus: {IM_MODULE}=ibus");
unsafe { std::env::set_var(IM_MODULE, "ibus") };
}
if drop_fcitx_module(|k| std::env::var(k).ok()) {
tracing::info!("GNOME Wayland with Fcitx: input method through the compositor");
unsafe { std::env::remove_var(IM_MODULE) };
}
}
/// GNOME on Wayland with Fcitx's GTK module selected (the session default `im-config` and
/// Fcitx's setup write).
fn drop_fcitx_module(var: impl Fn(&str) -> Option<String>) -> bool {
let gnome = var("XDG_CURRENT_DESKTOP").is_some_and(|d| d.split(':').any(|d| d.eq_ignore_ascii_case("gnome")));
gnome && var(IM_MODULE).is_some_and(|m| matches!(m.trim(), "fcitx" | "fcitx5")) && gdk_wayland(&var)
}
/// Under WSL with WSLg's X server available, unless the user chose a GDK backend.
fn use_x11_on_wsl(var: impl Fn(&str) -> Option<String>) -> bool {
let set = |k: &str| var(k).is_some_and(|v| !v.is_empty());
(set("WSL_DISTRO_NAME") || set("WSL_INTEROP")) && set("DISPLAY") && !set("GDK_BACKEND")
}
/// GTK 3's IBus input method module (`ibus-gtk3`), wherever the distribution keeps it.
#[cfg(target_os = "linux")]
fn gtk_ibus_module_installed() -> bool {
const MODULE: &str = "gtk-3.0/3.0.0/immodules/im-ibus.so";
let lib_dirs = ["/usr/lib", "/usr/lib64", "/usr/local/lib"];
lib_dirs.iter().any(|dir| {
let dir = std::path::Path::new(dir);
dir.join(MODULE).exists()
|| std::fs::read_dir(dir)
.into_iter()
.flatten()
.flatten()
.any(|multiarch| multiarch.path().join(MODULE).exists())
})
}
/// A Wayland session whose input method is IBus, unless the user chose a GTK module.
fn use_ibus_module(var: impl Fn(&str) -> Option<String>, module_installed: impl FnOnce() -> bool) -> bool {
if var(IM_MODULE).is_some_and(|v| !v.is_empty()) || !gdk_wayland(&var) {
return false;
}
let ibus = var("XMODIFIERS").is_some_and(|v| v.split_whitespace().any(|m| m == "@im=ibus"));
ibus && module_installed()
}
/// Applies Appearance › Smooth scrolling to every new web view
#[cfg(target_os = "linux")]
pub fn smooth_scrolling_plugin() -> tauri::plugin::TauriPlugin<tauri::Wry> {
use tauri::Manager;
tauri::plugin::Builder::new("linux-smooth-scrolling")
.on_webview_ready(|webview| {
let enabled = webview
.try_state::<crate::state::AppState>()
.is_some_and(|s| s.global_settings.effective().appearance.smooth_scrolling);
#[allow(clippy::disallowed_methods)]
let _ = webview.with_webview(move |platform| set_smooth_scrolling(&platform, enabled));
})
.build()
}
#[cfg(target_os = "linux")]
pub fn set_smooth_scrolling(platform: &tauri::webview::PlatformWebview, enabled: bool) {
use webkit2gtk::{SettingsExt, WebViewExt};
if let Some(settings) = platform.inner().settings() {
settings.set_enable_smooth_scrolling(enabled);
}
}
/// A Wayland session on the NVIDIA driver, unless the user already chose a workaround
/// (either variable set keeps their setting).
fn disable_explicit_sync(var: impl Fn(&str) -> Option<String>, nvidia: bool) -> bool {
if !nvidia || var(DISABLE_DMABUF).is_some() || var(DISABLE_EXPLICIT_SYNC).is_some() {
return false;
}
gdk_wayland(&var)
}
/// GDK will open a Wayland display (it tries the backends of `GDK_BACKEND` in order).
fn gdk_wayland(var: &impl Fn(&str) -> Option<String>) -> bool {
let wayland_display = var("WAYLAND_DISPLAY").is_some_and(|v| !v.is_empty());
let backend =
var("GDK_BACKEND").is_none_or(|b| matches!(b.split(',').next().map(str::trim), Some("wayland" | "*" | "")));
wayland_display && backend
}
#[cfg(test)]
mod tests {
use super::*;
fn decide(vars: &[(&str, &str)], nvidia: bool) -> bool {
disable_explicit_sync(|k| vars.iter().find(|(name, _)| *name == k).map(|(_, v)| v.to_string()), nvidia)
}
#[test]
fn turns_explicit_sync_off_on_wayland_with_nvidia_only() {
assert!(decide(&[("WAYLAND_DISPLAY", "wayland-0")], true));
assert!(decide(&[("WAYLAND_DISPLAY", "wayland-0"), ("GDK_BACKEND", "wayland,x11")], true));
assert!(!decide(&[("WAYLAND_DISPLAY", "wayland-0")], false));
assert!(!decide(&[("DISPLAY", ":0")], true));
assert!(!decide(&[("WAYLAND_DISPLAY", "")], true));
assert!(!decide(&[("WAYLAND_DISPLAY", "wayland-0"), ("GDK_BACKEND", "x11")], true));
}
#[test]
fn keeps_the_users_own_setting() {
assert!(!decide(&[("WAYLAND_DISPLAY", "wayland-0"), (DISABLE_DMABUF, "1")], true));
assert!(!decide(&[("WAYLAND_DISPLAY", "wayland-0"), (DISABLE_EXPLICIT_SYNC, "0")], true));
}
fn decide_im(vars: &[(&str, &str)], installed: bool) -> bool {
use_ibus_module(|k| vars.iter().find(|(name, _)| *name == k).map(|(_, v)| v.to_string()), || installed)
}
#[test]
fn uses_the_ibus_module_on_wayland_with_ibus_only() {
let gnome = [("WAYLAND_DISPLAY", "wayland-0"), ("XMODIFIERS", "@im=ibus")];
assert!(decide_im(&gnome, true));
assert!(decide_im(&[gnome[0], gnome[1], (IM_MODULE, "")], true));
assert!(!decide_im(&gnome, false));
assert!(!decide_im(&[("WAYLAND_DISPLAY", "wayland-0"), ("XMODIFIERS", "@im=fcitx")], true));
assert!(!decide_im(&[("WAYLAND_DISPLAY", "wayland-0")], true));
assert!(!decide_im(&[("DISPLAY", ":0"), ("XMODIFIERS", "@im=ibus")], true));
assert!(!decide_im(&[gnome[0], gnome[1], ("GDK_BACKEND", "x11")], true));
}
fn env(vars: &[(&'static str, &'static str)]) -> impl Fn(&str) -> Option<String> {
let vars = vars.to_vec();
move |k| vars.iter().find(|(name, _)| *name == k).map(|(_, v)| v.to_string())
}
#[test]
fn gnome_wayland_drops_the_fcitx_module() {
let gnome = [("XDG_CURRENT_DESKTOP", "ubuntu:GNOME"), ("WAYLAND_DISPLAY", "wayland-0"), (IM_MODULE, "fcitx")];
assert!(drop_fcitx_module(env(&gnome)));
assert!(drop_fcitx_module(env(&[gnome[0], gnome[1], (IM_MODULE, "fcitx5")])));
assert!(!drop_fcitx_module(env(&[("XDG_CURRENT_DESKTOP", "KDE"), gnome[1], gnome[2]])));
assert!(!drop_fcitx_module(env(&[gnome[0], gnome[2], ("DISPLAY", ":0")])), "X11 session");
assert!(!drop_fcitx_module(env(&[gnome[0], gnome[1], gnome[2], ("GDK_BACKEND", "x11")])));
assert!(!drop_fcitx_module(env(&[gnome[0], gnome[1], (IM_MODULE, "ibus")])));
}
#[test]
fn wsl_uses_x11_unless_a_backend_is_chosen() {
let wslg = [("WSL_DISTRO_NAME", "Ubuntu-24.04"), ("WAYLAND_DISPLAY", "wayland-0"), ("DISPLAY", ":0")];
assert!(use_x11_on_wsl(env(&wslg)));
assert!(use_x11_on_wsl(env(&[("WSL_INTEROP", "/run/WSL/1_interop"), ("DISPLAY", ":0")])));
assert!(!use_x11_on_wsl(env(&[wslg[0], wslg[1], wslg[2], ("GDK_BACKEND", "wayland")])));
assert!(!use_x11_on_wsl(env(&[wslg[0], wslg[1]])), "no X server");
assert!(!use_x11_on_wsl(env(&[wslg[1], wslg[2]])), "not WSL");
}
#[test]
fn keeps_the_users_input_method_module() {
let vars = [("WAYLAND_DISPLAY", "wayland-0"), ("XMODIFIERS", "@im=ibus"), (IM_MODULE, "wayland")];
assert!(!decide_im(&vars, true));
}
}