Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add version check for background throttling
Browse files Browse the repository at this point in the history
bastiankistner committed Jan 7, 2025
1 parent d468637 commit 96279eb
Showing 3 changed files with 22 additions and 12 deletions.
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'
---

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
@@ -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+.
///
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
pub disable_background_throttling: bool,
}
@@ -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+.
///
/// 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| {
22 changes: 14 additions & 8 deletions src/wkwebview/mod.rs
Original file line number Diff line number Diff line change
@@ -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)
|| (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")]

0 comments on commit 96279eb

Please sign in to comment.