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

72 рядки
2.5 KiB
Rust

//! AppKit side of a background tab's detached web view; no tauri types, so a test can include it.
use objc2::{
Message,
rc::Retained,
runtime::{AnyClass, ProtocolObject},
};
use objc2_app_kit::{
NSView, NSWindow, NSWindowDidBecomeKeyNotification, NSWindowDidChangeOcclusionStateNotification,
NSWindowOcclusionState, NSWindowOrderingMode,
};
use objc2_foundation::{NSNotificationCenter, NSObjectProtocol};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Return {
Key,
Visible,
}
pub fn web_view(native: &NSWindow) -> Option<Retained<NSView>> {
let class = AnyClass::get(c"WKWebView")?;
native.contentView()?.subviews().iter().find(|v| v.isKindOfClass(class))
}
/// At the bottom so the title bar views stay above it.
pub fn put_back(native: &NSWindow, view: &NSView) {
let Some(content) = native.contentView() else { return };
view.setFrame(content.bounds());
content.addSubview_positioned_relativeTo(view, NSWindowOrderingMode::Below, None);
native.makeFirstResponder(Some(view));
}
/// Calls `f` when the window becomes key, or visible (AppKit can bring a window back by itself,
/// e.g. when a tab it closes next to leaves the group). The observers have no queue, so `f` runs
/// synchronously inside the post. For the key notification that is inside `makeKeyWindow`, before the
/// window's first display; occlusion arrives when the window is already on screen.
pub fn on_return(
native: &NSWindow,
f: impl Fn(Return) + 'static,
) -> Vec<Retained<ProtocolObject<dyn NSObjectProtocol>>> {
let f = std::rc::Rc::new(f);
let center = NSNotificationCenter::defaultCenter();
let key = {
let f = f.clone();
let block = block2::RcBlock::new(move |_| f(Return::Key));
unsafe {
center.addObserverForName_object_queue_usingBlock(
Some(NSWindowDidBecomeKeyNotification),
Some(native),
None,
&block,
)
}
};
let visible = {
let window: Retained<NSWindow> = native.retain();
let block = block2::RcBlock::new(move |_| {
if window.occlusionState().contains(NSWindowOcclusionState::Visible) {
f(Return::Visible);
}
});
unsafe {
center.addObserverForName_object_queue_usingBlock(
Some(NSWindowDidChangeOcclusionStateNotification),
Some(native),
None,
&block,
)
}
};
vec![key, visible]
}