Skip to content

Commit a2d36b8

Browse files
feat: disable background throttling (#12181)
Co-authored-by: Fabian-Lars <github@fabianlars.de>
1 parent 5a3647b commit a2d36b8

File tree

19 files changed

+350
-64
lines changed

19 files changed

+350
-64
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'tauri-cli': 'minor:enhance'
3+
'tauri-runtime': 'minor:enhance'
4+
'tauri-runtime-wry': 'minor:enhance'
5+
'tauri-utils': 'minor:enhance'
6+
'tauri': 'minor:enhance'
7+
'@tauri-apps/api': 'minor:enhance'
8+
'@tauri-apps/cli': 'minor:enhance'
9+
---
10+
11+
Add an option to change the default background throttling policy (currently for WebKit only).

Cargo.lock

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri-cli/config.schema.json

+37
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,17 @@
516516
"type": "null"
517517
}
518518
]
519+
},
520+
"backgroundThrottling": {
521+
"description": "Change the default background throttling behaviour.\n\n By default, browsers use a suspend policy that will throttle timers and even unload\n the whole tab (view) to free resources after roughly 5 minutes when a view became\n minimized or hidden. This will pause 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",
522+
"anyOf": [
523+
{
524+
"$ref": "#/definitions/BackgroundThrottlingPolicy"
525+
},
526+
{
527+
"type": "null"
528+
}
529+
]
519530
}
520531
},
521532
"additionalProperties": false
@@ -948,6 +959,32 @@
948959
}
949960
]
950961
},
962+
"BackgroundThrottlingPolicy": {
963+
"description": "Background throttling policy.",
964+
"oneOf": [
965+
{
966+
"description": "A policy where background throttling is disabled",
967+
"type": "string",
968+
"enum": [
969+
"disabled"
970+
]
971+
},
972+
{
973+
"description": "A policy where a web view that’s not in a window fully suspends tasks. This is usually the default behavior in case no policy is set.",
974+
"type": "string",
975+
"enum": [
976+
"suspend"
977+
]
978+
},
979+
{
980+
"description": "A policy where a web view that’s not in a window limits processing, but does not fully suspend tasks.",
981+
"type": "string",
982+
"enum": [
983+
"throttle"
984+
]
985+
}
986+
]
987+
},
951988
"SecurityConfig": {
952989
"description": "Security configuration.\n\n See more: <https://v2.tauri.app/reference/config/#securityconfig>",
953990
"type": "object",

crates/tauri-cli/schema.json

+9
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@
465465
"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.",
466466
"default": false,
467467
"type": "boolean"
468+
},
469+
"backgroundThrottling": {
470+
"description": "Change the default background throttling behaviour.\n\n By default, browsers use a suspend policy that will throttle timers and even unload\n the whole tab (view) to free resources after roughly 5 minutes when a view became\n minimized or hidden. This will pause 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",
471+
"type": "string",
472+
"enum": [
473+
"disabled",
474+
"throttle",
475+
"suspend"
476+
]
468477
}
469478
},
470479
"additionalProperties": false

crates/tauri-cli/tauri.config.schema.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,15 @@
465465
"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.",
466466
"default": false,
467467
"type": "boolean"
468+
},
469+
"backgroundThrottling": {
470+
"description": "Change the default background throttling behaviour.\n\n By default, browsers use a suspend policy that will throttle timers and even unload\n the whole tab (view) to free resources after roughly 5 minutes when a view became\n minimized or hidden. This will pause 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",
471+
"type": "string",
472+
"enum": [
473+
"disabled",
474+
"throttle",
475+
"suspend"
476+
]
468477
}
469478
},
470479
"additionalProperties": false
@@ -2995,4 +3004,4 @@
29953004
"additionalProperties": true
29963005
}
29973006
}
2998-
}
3007+
}

crates/tauri-runtime-wry/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ rustc-args = ["--cfg", "docsrs"]
1717
rustdoc-args = ["--cfg", "docsrs"]
1818

1919
[dependencies]
20-
wry = { version = "0.48", default-features = false, features = [
20+
wry = { version = "0.48.1", default-features = false, features = [
2121
"drag-drop",
2222
"protocol",
2323
"os-webview",

crates/tauri-runtime-wry/src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -4187,6 +4187,20 @@ fn create_webview<T: UserEvent>(
41874187
webview_builder = webview_builder.with_https_scheme(webview_attributes.use_https_scheme);
41884188
}
41894189

4190+
if let Some(background_throttling) = webview_attributes.background_throttling {
4191+
webview_builder = webview_builder.with_background_throttling(match background_throttling {
4192+
tauri_utils::config::BackgroundThrottlingPolicy::Disabled => {
4193+
wry::BackgroundThrottlingPolicy::Disabled
4194+
}
4195+
tauri_utils::config::BackgroundThrottlingPolicy::Suspend => {
4196+
wry::BackgroundThrottlingPolicy::Suspend
4197+
}
4198+
tauri_utils::config::BackgroundThrottlingPolicy::Throttle => {
4199+
wry::BackgroundThrottlingPolicy::Throttle
4200+
}
4201+
});
4202+
}
4203+
41904204
if let Some(color) = webview_attributes.background_color {
41914205
webview_builder = webview_builder.with_background_color(color.into());
41924206
}

crates/tauri-runtime/src/webview.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
use crate::{window::is_label_valid, Rect, Runtime, UserEvent};
88

99
use http::Request;
10-
use tauri_utils::config::{Color, WebviewUrl, WindowConfig, WindowEffectsConfig};
10+
use tauri_utils::config::{
11+
BackgroundThrottlingPolicy, Color, WebviewUrl, WindowConfig, WindowEffectsConfig,
12+
};
1113
use url::Url;
1214

1315
use std::{
@@ -215,6 +217,7 @@ pub struct WebviewAttributes {
215217
pub use_https_scheme: bool,
216218
pub devtools: Option<bool>,
217219
pub background_color: Option<Color>,
220+
pub background_throttling: Option<BackgroundThrottlingPolicy>,
218221
}
219222

220223
impl From<&WindowConfig> for WebviewAttributes {
@@ -225,7 +228,9 @@ impl From<&WindowConfig> for WebviewAttributes {
225228
.zoom_hotkeys_enabled(config.zoom_hotkeys_enabled)
226229
.use_https_scheme(config.use_https_scheme)
227230
.browser_extensions_enabled(config.browser_extensions_enabled)
231+
.background_throttling(config.background_throttling.clone())
228232
.devtools(config.devtools);
233+
229234
#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
230235
{
231236
builder = builder.transparent(config.transparent);
@@ -279,6 +284,7 @@ impl WebviewAttributes {
279284
use_https_scheme: false,
280285
devtools: None,
281286
background_color: None,
287+
background_throttling: None,
282288
}
283289
}
284290

@@ -444,6 +450,26 @@ impl WebviewAttributes {
444450
self.background_color = Some(color);
445451
self
446452
}
453+
454+
/// Change the default background throttling behaviour.
455+
///
456+
/// By default, browsers use a suspend policy that will throttle timers and even unload
457+
/// the whole tab (view) to free resources after roughly 5 minutes when a view became
458+
/// minimized or hidden. This will pause all tasks until the documents visibility state
459+
/// changes back from hidden to visible by bringing the view back to the foreground.
460+
///
461+
/// ## Platform-specific
462+
///
463+
/// - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.
464+
/// - **iOS**: Supported since version 17.0+.
465+
/// - **macOS**: Supported since version 14.0+.
466+
///
467+
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
468+
#[must_use]
469+
pub fn background_throttling(mut self, policy: Option<BackgroundThrottlingPolicy>) -> Self {
470+
self.background_throttling = policy;
471+
self
472+
}
447473
}
448474

449475
/// IPC handler.

crates/tauri-schema-generator/schemas/config.schema.json

+37
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,17 @@
516516
"type": "null"
517517
}
518518
]
519+
},
520+
"backgroundThrottling": {
521+
"description": "Change the default background throttling behaviour.\n\n By default, browsers use a suspend policy that will throttle timers and even unload\n the whole tab (view) to free resources after roughly 5 minutes when a view became\n minimized or hidden. This will pause 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",
522+
"anyOf": [
523+
{
524+
"$ref": "#/definitions/BackgroundThrottlingPolicy"
525+
},
526+
{
527+
"type": "null"
528+
}
529+
]
519530
}
520531
},
521532
"additionalProperties": false
@@ -948,6 +959,32 @@
948959
}
949960
]
950961
},
962+
"BackgroundThrottlingPolicy": {
963+
"description": "Background throttling policy.",
964+
"oneOf": [
965+
{
966+
"description": "A policy where background throttling is disabled",
967+
"type": "string",
968+
"enum": [
969+
"disabled"
970+
]
971+
},
972+
{
973+
"description": "A policy where a web view that’s not in a window fully suspends tasks. This is usually the default behavior in case no policy is set.",
974+
"type": "string",
975+
"enum": [
976+
"suspend"
977+
]
978+
},
979+
{
980+
"description": "A policy where a web view that’s not in a window limits processing, but does not fully suspend tasks.",
981+
"type": "string",
982+
"enum": [
983+
"throttle"
984+
]
985+
}
986+
]
987+
},
951988
"SecurityConfig": {
952989
"description": "Security configuration.\n\n See more: <https://v2.tauri.app/reference/config/#securityconfig>",
953990
"type": "object",

crates/tauri-utils/src/config.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,19 @@ impl schemars::JsonSchema for Color {
14201420
}
14211421
}
14221422

1423+
/// Background throttling policy.
1424+
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
1425+
#[cfg_attr(feature = "schema", derive(JsonSchema))]
1426+
#[serde(rename_all = "camelCase", deny_unknown_fields)]
1427+
pub enum BackgroundThrottlingPolicy {
1428+
/// A policy where background throttling is disabled
1429+
Disabled,
1430+
/// A policy where a web view that’s not in a window fully suspends tasks. This is usually the default behavior in case no policy is set.
1431+
Suspend,
1432+
/// A policy where a web view that’s not in a window limits processing, but does not fully suspend tasks.
1433+
Throttle,
1434+
}
1435+
14231436
/// The window effects configuration object
14241437
#[skip_serializing_none]
14251438
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize, Default)]
@@ -1687,6 +1700,23 @@ pub struct WindowConfig {
16871700
/// - **Windows**: On Windows 8 and newer, if alpha channel is not `0`, it will be ignored for the webview layer.
16881701
#[serde(alias = "background-color")]
16891702
pub background_color: Option<Color>,
1703+
1704+
/// Change the default background throttling behaviour.
1705+
///
1706+
/// By default, browsers use a suspend policy that will throttle timers and even unload
1707+
/// the whole tab (view) to free resources after roughly 5 minutes when a view became
1708+
/// minimized or hidden. This will pause all tasks until the documents visibility state
1709+
/// changes back from hidden to visible by bringing the view back to the foreground.
1710+
///
1711+
/// ## Platform-specific
1712+
///
1713+
/// - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.
1714+
/// - **iOS**: Supported since version 17.0+.
1715+
/// - **macOS**: Supported since version 14.0+.
1716+
///
1717+
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
1718+
#[serde(default, alias = "background-throttling")]
1719+
pub background_throttling: Option<BackgroundThrottlingPolicy>,
16901720
}
16911721

16921722
impl Default for WindowConfig {
@@ -1739,6 +1769,7 @@ impl Default for WindowConfig {
17391769
use_https_scheme: false,
17401770
devtools: None,
17411771
background_color: None,
1772+
background_throttling: None,
17421773
}
17431774
}
17441775
}
@@ -2858,6 +2889,17 @@ mod build {
28582889
}
28592890
}
28602891

2892+
impl ToTokens for BackgroundThrottlingPolicy {
2893+
fn to_tokens(&self, tokens: &mut TokenStream) {
2894+
let prefix = quote! { ::tauri::utils::config::BackgroundThrottlingPolicy };
2895+
tokens.append_all(match self {
2896+
Self::Disabled => quote! { #prefix::Disabled },
2897+
Self::Throttle => quote! { #prefix::Throttle },
2898+
Self::Suspend => quote! { #prefix::Suspend },
2899+
})
2900+
}
2901+
}
2902+
28612903
impl ToTokens for crate::Theme {
28622904
fn to_tokens(&self, tokens: &mut TokenStream) {
28632905
let prefix = quote! { ::tauri::utils::Theme };
@@ -3004,6 +3046,7 @@ mod build {
30043046
let use_https_scheme = self.use_https_scheme;
30053047
let devtools = opt_lit(self.devtools.as_ref());
30063048
let background_color = opt_lit(self.background_color.as_ref());
3049+
let background_throttling = opt_lit(self.background_throttling.as_ref());
30073050

30083051
literal_struct!(
30093052
tokens,
@@ -3054,7 +3097,8 @@ mod build {
30543097
browser_extensions_enabled,
30553098
use_https_scheme,
30563099
devtools,
3057-
background_color
3100+
background_color,
3101+
background_throttling
30583102
);
30593103
}
30603104
}

crates/tauri/scripts/bundle.global.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri/src/webview/mod.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use tauri_runtime::{
2323
WebviewDispatch,
2424
};
2525
pub use tauri_utils::config::Color;
26-
use tauri_utils::config::{WebviewUrl, WindowConfig};
26+
use tauri_utils::config::{BackgroundThrottlingPolicy, WebviewUrl, WindowConfig};
2727
pub use url::Url;
2828

2929
use crate::{
@@ -867,6 +867,26 @@ fn main() {
867867
self.webview_attributes.background_color = Some(color);
868868
self
869869
}
870+
871+
/// Change the default background throttling behaviour.
872+
///
873+
/// By default, browsers use a suspend policy that will throttle timers and even unload
874+
/// the whole tab (view) to free resources after roughly 5 minutes when a view became
875+
/// minimized or hidden. This will pause all tasks until the documents visibility state
876+
/// changes back from hidden to visible by bringing the view back to the foreground.
877+
///
878+
/// ## Platform-specific
879+
///
880+
/// - **Linux / Windows / Android**: Unsupported. Workarounds like a pending WebLock transaction might suffice.
881+
/// - **iOS**: Supported since version 17.0+.
882+
/// - **macOS**: Supported since version 14.0+.
883+
///
884+
/// see https://github.com/tauri-apps/tauri/issues/5250#issuecomment-2569380578
885+
#[must_use]
886+
pub fn background_throttling(mut self, policy: BackgroundThrottlingPolicy) -> Self {
887+
self.webview_attributes.background_throttling = Some(policy);
888+
self
889+
}
870890
}
871891

872892
/// Webview.

0 commit comments

Comments
 (0)