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 #12181

Open
wants to merge 16 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changes/disable-background-throttling.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'tauri-cli': 'minor:enhance'
'tauri-runtime': 'minor:enhance'
'tauri-runtime-wry': 'minor:enhance'
'tauri-utils': 'minor:enhance'
'tauri': 'minor:enhance'
'@tauri-apps/api': 'minor:enhance'
'@tauri-apps/cli': 'minor:enhance'
---

Add an option to change the default background throttling policy (currently for WebKit only).
5 changes: 2 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions crates/tauri-cli/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,17 @@
"type": "null"
}
]
},
"backgroundThrottling": {
"description": "Change the default background throttling behaviour.\n\n By default, browsers throttle timers and even unload the whole tab (view) to free resources after roughly 5 minutes when\n a view became minimized or hidden. This will permanently suspend all tasks until the documents visibility state\n changes back from hidden to visible by bringing the view back to the foreground.\n\n ## Platform-specific\n\n - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.\n - **iOS**: Supported since version 17.0+.\n - **macOS**: Supported since version 14.0+.\n\n see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578",
"anyOf": [
{
"$ref": "#/definitions/BackgroundThrottlingPolicy"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
Expand Down Expand Up @@ -948,6 +959,32 @@
}
]
},
"BackgroundThrottlingPolicy": {
"description": "Background throttling policy.",
"oneOf": [
{
"description": "A policy where background throttling is disabled",
"type": "string",
"enum": [
"disabled"
]
},
{
"description": "A policy where a web view that’s not in a window fully suspends tasks.",
"type": "string",
"enum": [
"suspend"
]
},
{
"description": "A policy where a web view that’s not in a window limits processing, but does not fully suspend tasks.",
"type": "string",
"enum": [
"throttle"
]
}
]
},
"SecurityConfig": {
"description": "Security configuration.\n\n See more: <https://v2.tauri.app/reference/config/#securityconfig>",
"type": "object",
Expand Down
9 changes: 9 additions & 0 deletions crates/tauri-cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,15 @@
"description": "Whether page zooming by hotkeys is enabled\n\n ## Platform-specific:\n\n - **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting.\n - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,\n 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission\n\n - **Android / iOS**: Unsupported.",
"default": false,
"type": "boolean"
},
"backgroundThrottling": {
"description": "Change the default background throttling behaviour.\n\n By default, browsers throttle timers and even unload the whole tab (view) to free resources after roughly 5 minutes when\n a view became minimized or hidden. This will permanently suspend all tasks until the documents visibility state\n changes back from hidden to visible by bringing the view back to the foreground.\n\n ## Platform-specific\n\n - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.\n - **iOS**: Supported since version 17.0+.\n - **macOS**: Supported since version 14.0+.\n\n see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578",
"type": "string",
"enum": [
"disabled",
"throttle",
"suspend"
]
}
},
"additionalProperties": false
Expand Down
11 changes: 10 additions & 1 deletion crates/tauri-cli/tauri.config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,15 @@
"description": "Whether page zooming by hotkeys is enabled\n\n ## Platform-specific:\n\n - **Windows**: Controls WebView2's [`IsZoomControlEnabled`](https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/winrt/microsoft_web_webview2_core/corewebview2settings?view=webview2-winrt-1.0.2420.47#iszoomcontrolenabled) setting.\n - **MacOS / Linux**: Injects a polyfill that zooms in and out with `ctrl/command` + `-/=`,\n 20% in each step, ranging from 20% to 1000%. Requires `webview:allow-set-webview-zoom` permission\n\n - **Android / iOS**: Unsupported.",
"default": false,
"type": "boolean"
},
"backgroundThrottling": {
"description": "Change the default background throttling behaviour.\n\n By default, browsers throttle timers and even unload the whole tab (view) to free resources after roughly 5 minutes when\n a view became minimized or hidden. This will permanently suspend all tasks until the documents visibility state\n changes back from hidden to visible by bringing the view back to the foreground.\n\n ## Platform-specific\n\n - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.\n - **iOS**: Supported since version 17.0+.\n - **macOS**: Supported since version 14.0+.\n\n see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578",
"type": "string",
"enum": [
"disabled",
"throttle",
"suspend"
]
}
},
"additionalProperties": false
Expand Down Expand Up @@ -2995,4 +3004,4 @@
"additionalProperties": true
}
}
}
}
2 changes: 1 addition & 1 deletion crates/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ rustc-args = ["--cfg", "docsrs"]
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
wry = { version = "0.48", default-features = false, features = [
wry = { version = "0.48.1", default-features = false, features = [
"drag-drop",
"protocol",
"os-webview",
Expand Down
4 changes: 4 additions & 0 deletions crates/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4187,6 +4187,10 @@ fn create_webview<T: UserEvent>(
webview_builder = webview_builder.with_https_scheme(webview_attributes.use_https_scheme);
}

if let Some(background_throttling) = webview_attributes.background_throttling {
webview_builder = webview_builder.with_background_throttling(background_throttling.into());
}

if let Some(color) = webview_attributes.background_color {
webview_builder = webview_builder.with_background_color(color.into());
}
Expand Down
27 changes: 26 additions & 1 deletion crates/tauri-runtime/src/webview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use crate::{window::is_label_valid, Rect, Runtime, UserEvent};

use http::Request;
use tauri_utils::config::{Color, WebviewUrl, WindowConfig, WindowEffectsConfig};
use tauri_utils::config::{
BackgroundThrottlingPolicy, Color, WebviewUrl, WindowConfig, WindowEffectsConfig,
};
use url::Url;

use std::{
Expand Down Expand Up @@ -215,6 +217,7 @@ pub struct WebviewAttributes {
pub use_https_scheme: bool,
pub devtools: Option<bool>,
pub background_color: Option<Color>,
pub background_throttling: Option<BackgroundThrottlingPolicy>,
}

impl From<&WindowConfig> for WebviewAttributes {
Expand All @@ -225,7 +228,9 @@ impl From<&WindowConfig> for WebviewAttributes {
.zoom_hotkeys_enabled(config.zoom_hotkeys_enabled)
.use_https_scheme(config.use_https_scheme)
.browser_extensions_enabled(config.browser_extensions_enabled)
.background_throttling(config.background_throttling.clone())
.devtools(config.devtools);

#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
{
builder = builder.transparent(config.transparent);
Expand Down Expand Up @@ -279,6 +284,7 @@ impl WebviewAttributes {
use_https_scheme: false,
devtools: None,
background_color: None,
background_throttling: None,
}
}

Expand Down Expand Up @@ -444,6 +450,25 @@ impl WebviewAttributes {
self.background_color = Some(color);
self
}

/// Change the default background throttling behaviour.
///
/// By default, browsers throttle timers and even unload the whole tab (view) to free resources after roughly 5 minutes when
/// a view became minimized or hidden. This will permanently suspend all tasks until the documents visibility state
/// changes back from hidden to visible by bringing the view back to the foreground.
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.
/// - **iOS**: Supported since version 17.0+.
/// - **macOS**: Supported since version 14.0+.
///
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
#[must_use]
pub fn background_throttling(mut self, policy: Option<BackgroundThrottlingPolicy>) -> Self {
self.background_throttling = policy;
self
}
}

/// IPC handler.
Expand Down
37 changes: 37 additions & 0 deletions crates/tauri-schema-generator/schemas/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,17 @@
"type": "null"
}
]
},
"backgroundThrottling": {
"description": "Change the default background throttling behaviour.\n\n By default, browsers throttle timers and even unload the whole tab (view) to free resources after roughly 5 minutes when\n a view became minimized or hidden. This will permanently suspend all tasks until the documents visibility state\n changes back from hidden to visible by bringing the view back to the foreground.\n\n ## Platform-specific\n\n - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.\n - **iOS**: Supported since version 17.0+.\n - **macOS**: Supported since version 14.0+.\n\n see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578",
"anyOf": [
{
"$ref": "#/definitions/BackgroundThrottlingPolicy"
},
{
"type": "null"
}
]
}
},
"additionalProperties": false
Expand Down Expand Up @@ -948,6 +959,32 @@
}
]
},
"BackgroundThrottlingPolicy": {
"description": "Background throttling policy.",
"oneOf": [
{
"description": "A policy where background throttling is disabled",
"type": "string",
"enum": [
"disabled"
]
},
{
"description": "A policy where a web view that’s not in a window fully suspends tasks.",
"type": "string",
"enum": [
"suspend"
]
},
{
"description": "A policy where a web view that’s not in a window limits processing, but does not fully suspend tasks.",
"type": "string",
"enum": [
"throttle"
]
}
]
},
"SecurityConfig": {
"description": "Security configuration.\n\n See more: <https://v2.tauri.app/reference/config/#securityconfig>",
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions crates/tauri-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ edition.workspace = true
rust-version.workspace = true

[dependencies]
wry = { version = "0.48.1", default-features = false }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
thiserror = "2"
Expand Down
55 changes: 54 additions & 1 deletion crates/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,29 @@ impl schemars::JsonSchema for Color {
}
}

/// Background throttling policy.
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub enum BackgroundThrottlingPolicy {
/// A policy where background throttling is disabled
Disabled,
/// A policy where a web view that’s not in a window fully suspends tasks.
Suspend,
/// A policy where a web view that’s not in a window limits processing, but does not fully suspend tasks.
Throttle,
}

impl From<BackgroundThrottlingPolicy> for wry::BackgroundThrottlingPolicy {
fn from(policy: BackgroundThrottlingPolicy) -> Self {
match policy {
BackgroundThrottlingPolicy::Disabled => Self::Disabled,
BackgroundThrottlingPolicy::Throttle => Self::Throttle,
BackgroundThrottlingPolicy::Suspend => Self::Suspend,
}
}
}

/// The window effects configuration object
#[skip_serializing_none]
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
Expand Down Expand Up @@ -1687,6 +1710,22 @@ pub struct WindowConfig {
/// - **Windows**: On Windows 8 and newer, if alpha channel is not `0`, it will be ignored for the webview layer.
#[serde(alias = "background-color")]
pub background_color: Option<Color>,

/// Change the default background throttling behaviour.
///
/// By default, browsers throttle timers and even unload the whole tab (view) to free resources after roughly 5 minutes when
/// a view became minimized or hidden. This will permanently suspend all tasks until the documents visibility state
/// changes back from hidden to visible by bringing the view back to the foreground.
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.
/// - **iOS**: Supported since version 17.0+.
/// - **macOS**: Supported since version 14.0+.
///
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
#[serde(default, alias = "background-throttling")]
pub background_throttling: Option<BackgroundThrottlingPolicy>,
}

impl Default for WindowConfig {
Expand Down Expand Up @@ -1739,6 +1778,7 @@ impl Default for WindowConfig {
use_https_scheme: false,
devtools: None,
background_color: None,
background_throttling: None,
}
}
}
Expand Down Expand Up @@ -2858,6 +2898,17 @@ mod build {
}
}

impl ToTokens for BackgroundThrottlingPolicy {
fn to_tokens(&self, tokens: &mut TokenStream) {
let prefix = quote! { ::tauri::utils::config::BackgroundThrottlingPolicy };
tokens.append_all(match self {
Self::Disabled => quote! { #prefix::Disabled },
Self::Throttle => quote! { #prefix::Throttle },
Self::Suspend => quote! { #prefix::Suspend },
})
}
}

impl ToTokens for crate::Theme {
fn to_tokens(&self, tokens: &mut TokenStream) {
let prefix = quote! { ::tauri::utils::Theme };
Expand Down Expand Up @@ -3004,6 +3055,7 @@ mod build {
let use_https_scheme = self.use_https_scheme;
let devtools = opt_lit(self.devtools.as_ref());
let background_color = opt_lit(self.background_color.as_ref());
let background_throttling = opt_lit(self.background_throttling.as_ref());

literal_struct!(
tokens,
Expand Down Expand Up @@ -3054,7 +3106,8 @@ mod build {
browser_extensions_enabled,
use_https_scheme,
devtools,
background_color
background_color,
background_throttling
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/tauri/scripts/bundle.global.js

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion crates/tauri/src/webview/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tauri_runtime::{
WebviewDispatch,
};
pub use tauri_utils::config::Color;
use tauri_utils::config::{WebviewUrl, WindowConfig};
use tauri_utils::config::{BackgroundThrottlingPolicy, WebviewUrl, WindowConfig};
pub use url::Url;

use crate::{
Expand Down Expand Up @@ -867,6 +867,25 @@ fn main() {
self.webview_attributes.background_color = Some(color);
self
}

/// Change the default background throttling behaviour.
///
/// By default, browsers throttle timers and even unload the whole tab (view) to free resources after roughly 5 minutes when
/// a view became minimized or hidden. This will permanently suspend all tasks until the documents visibility state
/// changes back from hidden to visible by bringing the view back to the foreground.
///
/// ## Platform-specific
///
/// - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.
/// - **iOS**: Supported since version 17.0+.
/// - **macOS**: Supported since version 14.0+.
///
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
#[must_use]
pub fn background_throttling(mut self, policy: Option<BackgroundThrottlingPolicy>) -> Self {
self.webview_attributes.background_throttling = policy;
self
}
}

/// Webview.
Expand Down
Loading
Loading