Файли
openidea/src-tauri/tests/background_tab_appkit.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

71 рядки
2.7 KiB
Rust

#[cfg(not(target_os = "macos"))]
fn main() {}
#[cfg(target_os = "macos")]
#[path = "../src/background_tab.rs"]
mod background_tab;
#[cfg(target_os = "macos")]
#[link(name = "WebKit", kind = "framework")]
unsafe extern "C" {}
#[cfg(target_os = "macos")]
fn main() {
// nextest runs a harness-less binary as `--list --format terse [--ignored]`, then `<name> --exact --nocapture`.
let args: Vec<String> = std::env::args().collect();
if args.iter().any(|a| a == "--list") {
if !args.iter().any(|a| a == "--ignored") {
println!("background_tab_appkit: test");
}
return;
}
use background_tab::Return;
use objc2::{MainThreadMarker, msg_send, rc::Retained, runtime::AnyClass};
use objc2_app_kit::{
NSApplication, NSBackingStoreType, NSView, NSWindow, NSWindowDidBecomeKeyNotification, NSWindowStyleMask,
};
use objc2_foundation::{NSNotificationCenter, NSPoint, NSRect, NSSize};
use std::{cell::Cell, rc::Rc};
let mtm = MainThreadMarker::new().expect("main thread");
let _app = NSApplication::sharedApplication(mtm);
let rect = NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(400.0, 300.0));
let window = unsafe {
NSWindow::initWithContentRect_styleMask_backing_defer(
mtm.alloc(),
rect,
NSWindowStyleMask::Titled,
NSBackingStoreType::Buffered,
false,
)
};
let class = AnyClass::get(c"WKWebView").expect("WebKit is linked");
let web: Retained<NSView> = unsafe { msg_send![msg_send![class, alloc], initWithFrame: rect] };
window.contentView().unwrap().addSubview(&web);
let view = background_tab::web_view(&window).expect("web view in content view");
view.removeFromSuperview();
assert!(view.window().is_none());
// Not asked for its process ids: on a web view that never loaded a page, WebKit asserts in
// `_networkProcessIdentifier` (macOS 15), which a real tab never is.
let reason = Rc::new(Cell::new(None));
let observers = {
let (reason, window, view) = (reason.clone(), window.clone(), view.clone());
background_tab::on_return(&window.clone(), move |why| {
background_tab::put_back(&window, &view);
reason.set(Some(why));
})
};
unsafe {
NSNotificationCenter::defaultCenter()
.postNotificationName_object(NSWindowDidBecomeKeyNotification, Some(&window));
}
assert_eq!(reason.get(), Some(Return::Key));
assert!(view.window().is_some_and(|w| Retained::as_ptr(&w) == Retained::as_ptr(&window)));
for observer in &observers {
unsafe { NSNotificationCenter::defaultCenter().removeObserver(observer.as_ref()) };
}
println!("ok: re-attached synchronously inside the key notification");
}