Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: disable background throttling #1445

Merged
Merged
4 changes: 2 additions & 2 deletions .changes/disable-background-throttling.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
"wry": minor
'wry': 'minor:enhance'
FabianLars marked this conversation as resolved.
Show resolved Hide resolved
---

Add an option to disable background throttling (currently for WebKit only).
Add an option to disable background throttling (currently for WebKit only).
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,9 @@ pub struct WebViewAttributes<'a> {
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android**: Unsupported yet. But workarounds like a pending WebLock transaction might suffice.
/// - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.
/// - **iOS / macOS**: Supported since version 17.0+.
FabianLars marked this conversation as resolved.
Show resolved Hide resolved
///
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
pub disable_background_throttling: bool,
bastiankistner marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -1202,7 +1204,9 @@ impl<'a> WebViewBuilder<'a> {
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android**: Unsupported yet. But workarounds like a pending WebLock transaction might suffice.
/// - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.
/// - **iOS / macOS**: Supported since version 17.0+.
FabianLars marked this conversation as resolved.
Show resolved Hide resolved
///
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
pub fn with_disable_background_throttling(self, disable: bool) -> Self {
self.and_then(|mut b| {
Expand Down
22 changes: 14 additions & 8 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,14 +349,20 @@ impl InnerWebView {

// disable background throttling if attributes.disable_background_throttling is true
// which works for iOS 17.0+,iPadOS 17.0+,Mac Catalyst 17.0+, macOS 14.0+, visionOS 1.0+
if attributes.disable_background_throttling {
// Set inactive scheduling policy to None enum value (2)
_preference.setValue_forKey(
Some(&NSNumber::numberWithInt(
WKInactiveSchedulingPolicy::None.0.try_into().unwrap(),
)),
ns_string!("inactiveSchedulingPolicy"),
);
#[cfg(any(target_os = "ios", target_os = "macos"))]
{
let is_supported_os = (cfg!(target_os = "ios") && os_major_version >= 17)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could replace is_supported_os with custom_data_store_available from above since that has the same version requirement but i'm not sure if that's good for visibility 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do so, but indeed, that could leave the impression that it has something to do with the custom data store.

|| (cfg!(target_os = "macos") && os_major_version >= 14);

if is_supported_os && attributes.disable_background_throttling {
// Set inactive scheduling policy to None enum value (2)
_preference.setValue_forKey(
Some(&NSNumber::numberWithInt(
WKInactiveSchedulingPolicy::None.0.try_into().unwrap(),
)),
ns_string!("inactiveSchedulingPolicy"),
);
}
}

#[cfg(target_os = "macos")]
Expand Down
Loading