Skip to content

Commit f5851ee

Browse files
Themayulucasfernog
andauthored
feat: Expose ScrollBarStyle webview option to tauri. (#14089)
* Expose `ScrollBarStyle` webview option to tauri. This commit exposes the scroll_bar_style option from wry via the tauri WebviewWindowBuilder API. By itself, the commit does not include changes to the configuration file or JavaScript APIs: These will be added in a later commit. * Fix a compile error on macOS and Linux. * Add `scroll_bar_style` to WindowConfig. This commit exposes the `scroll_bar_style` option in tauri.conf.json/ .json5/.toml as `scrollBarStyle` and `scroll-bar-style`. * Expose `scroll_bar_style` to JavaScript API. This commit exposes the `scroll_bar_style` in the options object passed to the JavaScript API `Webview` and `WebviewWindow` constructors. While testing this, I discovered that on Windows, attempting to create a webview from the JavaScript API will cause the hosting window to immediately hang if it attempts to use the same data directory as another webview without sharing the same environment options. This commit includes no mitigation for this behaviour, as I will be opening a separate issue about it at some point in the near future. * Document WebView2 environment requirements. This commit adds a message to the documentation for all components of the `scroll_bar_style` configuration option, telling users that all webviews that use the same data directory must use the same value for this option. * Fix formatting. * Add change files to .changes directory. * Remove `tauri-schema-generator` from change file. * Remove quotes from change tags. * Add tags to change files. I did not realise that these were needed, as the pull request that I used as my reference when building this feature did not have them. * update conf --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 66cb1db commit f5851ee

File tree

13 files changed

+285
-7
lines changed

13 files changed

+285
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tauri-apps/api": minor:feat
3+
---
4+
5+
Adds the `scrollBarStyle` option to the Webview and WebviewBuilder constructors.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": minor:feat
3+
"tauri-utils": minor:feat
4+
---
5+
6+
Adds the `scrollBarStyle` option to the window configuration.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"tauri-runtime-wry": minor:feat
3+
"tauri-runtime": minor:feat
4+
"tauri": minor:feat
5+
---
6+
7+
Adds the `scroll_bar_style` option to the Webview and WebviewWindow builders.
8+
The possible values for this option are gated behind conditional compilation
9+
flags, and will need to be applied using conditional compilation if customised.

crates/tauri-cli/config.schema.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,15 @@
593593
},
594594
"maxItems": 16,
595595
"minItems": 16
596+
},
597+
"scrollBarStyle": {
598+
"description": "Specifies the native scrollbar style to use with the webview.\n CSS styles that modify the scrollbar are applied on top of the native appearance configured here.\n\n Defaults to `default`, which is the browser default.\n\n ## Platform-specific\n\n - **Windows**:\n - `fluentOverlay` requires WebView2 Runtime version 125.0.2535.41 or higher,\n and does nothing on older versions.\n - This option must be given the same value for all webviews that target the same data directory.\n - **Linux / Android / iOS / macOS**: Unsupported. Only supports `Default` and performs no operation.",
599+
"default": "default",
600+
"allOf": [
601+
{
602+
"$ref": "#/definitions/ScrollBarStyle"
603+
}
604+
]
596605
}
597606
},
598607
"additionalProperties": false
@@ -1112,6 +1121,25 @@
11121121
}
11131122
]
11141123
},
1124+
"ScrollBarStyle": {
1125+
"description": "The scrollbar style to use in the webview.\n\n ## Platform-specific\n\n - **Windows**: This option must be given the same value for all webviews that target the same data directory.",
1126+
"oneOf": [
1127+
{
1128+
"description": "The scrollbar style to use in the webview.",
1129+
"type": "string",
1130+
"enum": [
1131+
"default"
1132+
]
1133+
},
1134+
{
1135+
"description": "Fluent UI style overlay scrollbars. **Windows Only**\n\n Requires WebView2 Runtime version 125.0.2535.41 or higher, does nothing on older versions,\n see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/?tabs=dotnetcsharp#10253541",
1136+
"type": "string",
1137+
"enum": [
1138+
"fluentOverlay"
1139+
]
1140+
}
1141+
]
1142+
},
11151143
"SecurityConfig": {
11161144
"description": "Security configuration.\n\n See more: <https://v2.tauri.app/reference/config/#securityconfig>",
11171145
"type": "object",

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use http::Request;
1818
use objc2::ClassType;
1919
use raw_window_handle::{DisplayHandle, HasDisplayHandle, HasWindowHandle};
2020

21+
#[cfg(windows)]
22+
use tauri_runtime::webview::ScrollBarStyle;
2123
use tauri_runtime::{
2224
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
2325
monitor::Monitor,
@@ -79,6 +81,8 @@ use tauri_utils::{
7981
Theme,
8082
};
8183
use url::Url;
84+
#[cfg(windows)]
85+
use wry::ScrollBarStyle as WryScrollBarStyle;
8286
use wry::{
8387
DragDropEvent as WryDragDropEvent, ProxyConfig, ProxyEndpoint, WebContext as WryWebContext,
8488
WebView, WebViewBuilder,
@@ -4833,6 +4837,13 @@ You may have it installed on another user account, but it is not available for t
48334837
TaoTheme::Light => wry::Theme::Light,
48344838
_ => wry::Theme::Light,
48354839
});
4840+
4841+
webview_builder =
4842+
webview_builder.with_scroll_bar_style(match webview_attributes.scroll_bar_style {
4843+
ScrollBarStyle::Default => WryScrollBarStyle::Default,
4844+
ScrollBarStyle::FluentOverlay => WryScrollBarStyle::FluentOverlay,
4845+
_ => unreachable!(),
4846+
});
48364847
}
48374848

48384849
#[cfg(windows)]

crates/tauri-runtime/src/webview.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use crate::{window::is_label_valid, Rect, Runtime, UserEvent};
1010

1111
use http::Request;
1212
use tauri_utils::config::{
13-
BackgroundThrottlingPolicy, Color, WebviewUrl, WindowConfig, WindowEffectsConfig,
13+
BackgroundThrottlingPolicy, Color, ScrollBarStyle as ConfigScrollBarStyle, WebviewUrl,
14+
WindowConfig, WindowEffectsConfig,
1415
};
1516
use url::Url;
1617

@@ -170,6 +171,26 @@ pub enum NewWindowResponse {
170171
Deny,
171172
}
172173

174+
/// The scrollbar style to use in the webview.
175+
///
176+
/// ## Platform-specific
177+
///
178+
/// - **Windows**: This option must be given the same value for all webviews that target the same data directory.
179+
#[non_exhaustive]
180+
#[derive(Debug, Clone, Copy, Default)]
181+
pub enum ScrollBarStyle {
182+
#[default]
183+
/// The default scrollbar style for the webview.
184+
Default,
185+
186+
#[cfg(windows)]
187+
/// Fluent UI style overlay scrollbars. **Windows Only**
188+
///
189+
/// Requires WebView2 Runtime version 125.0.2535.41 or higher, does nothing on older versions,
190+
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/?tabs=dotnetcsharp#10253541
191+
FluentOverlay,
192+
}
193+
173194
/// A webview that has yet to be built.
174195
pub struct PendingWebview<T: UserEvent, R: Runtime<T>> {
175196
/// The label that the webview will be named.
@@ -340,6 +361,7 @@ pub struct WebviewAttributes {
340361
/// on macOS and iOS there is a link preview on long pressing links, this is enabled by default.
341362
/// see https://docs.rs/objc2-web-kit/latest/objc2_web_kit/struct.WKWebView.html#method.allowsLinkPreview
342363
pub allow_link_preview: bool,
364+
pub scroll_bar_style: ScrollBarStyle,
343365
/// Allows overriding the the keyboard accessory view on iOS.
344366
/// Returning `None` effectively removes the view.
345367
///
@@ -404,7 +426,13 @@ impl From<&WindowConfig> for WebviewAttributes {
404426
.use_https_scheme(config.use_https_scheme)
405427
.browser_extensions_enabled(config.browser_extensions_enabled)
406428
.background_throttling(config.background_throttling.clone())
407-
.devtools(config.devtools);
429+
.devtools(config.devtools)
430+
.scroll_bar_style(match config.scroll_bar_style {
431+
ConfigScrollBarStyle::Default => ScrollBarStyle::Default,
432+
#[cfg(windows)]
433+
ConfigScrollBarStyle::FluentOverlay => ScrollBarStyle::FluentOverlay,
434+
_ => ScrollBarStyle::Default,
435+
});
408436

409437
#[cfg(any(not(target_os = "macos"), feature = "macos-private-api"))]
410438
{
@@ -478,6 +506,7 @@ impl WebviewAttributes {
478506
background_throttling: None,
479507
javascript_disabled: false,
480508
allow_link_preview: true,
509+
scroll_bar_style: ScrollBarStyle::Default,
481510
#[cfg(target_os = "ios")]
482511
input_accessory_view_builder: None,
483512
#[cfg(windows)]
@@ -750,6 +779,25 @@ impl WebviewAttributes {
750779
self.background_throttling = policy;
751780
self
752781
}
782+
783+
/// Specifies the native scrollbar style to use with the webview.
784+
/// CSS styles that modify the scrollbar are applied on top of the native appearance configured here.
785+
///
786+
/// Defaults to [`ScrollBarStyle::Default`], which is the browser default.
787+
///
788+
/// ## Platform-specific
789+
///
790+
/// - **Windows**:
791+
/// - [`ScrollBarStyle::FluentOverlay`] requires WebView2 Runtime version 125.0.2535.41 or higher,
792+
/// and does nothing on older versions.
793+
/// - This option must be given the same value for all webviews that target the same data directory. Use
794+
/// [`WebviewAttributes::data_directory`] to change data directories if needed.
795+
/// - **Linux / Android / iOS / macOS**: Unsupported. Only supports `Default` and performs no operation.
796+
#[must_use]
797+
pub fn scroll_bar_style(mut self, style: ScrollBarStyle) -> Self {
798+
self.scroll_bar_style = style;
799+
self
800+
}
753801
}
754802

755803
/// IPC handler.

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,15 @@
593593
},
594594
"maxItems": 16,
595595
"minItems": 16
596+
},
597+
"scrollBarStyle": {
598+
"description": "Specifies the native scrollbar style to use with the webview.\n CSS styles that modify the scrollbar are applied on top of the native appearance configured here.\n\n Defaults to `default`, which is the browser default.\n\n ## Platform-specific\n\n - **Windows**:\n - `fluentOverlay` requires WebView2 Runtime version 125.0.2535.41 or higher,\n and does nothing on older versions.\n - This option must be given the same value for all webviews that target the same data directory.\n - **Linux / Android / iOS / macOS**: Unsupported. Only supports `Default` and performs no operation.",
599+
"default": "default",
600+
"allOf": [
601+
{
602+
"$ref": "#/definitions/ScrollBarStyle"
603+
}
604+
]
596605
}
597606
},
598607
"additionalProperties": false
@@ -1112,6 +1121,25 @@
11121121
}
11131122
]
11141123
},
1124+
"ScrollBarStyle": {
1125+
"description": "The scrollbar style to use in the webview.\n\n ## Platform-specific\n\n - **Windows**: This option must be given the same value for all webviews that target the same data directory.",
1126+
"oneOf": [
1127+
{
1128+
"description": "The scrollbar style to use in the webview.",
1129+
"type": "string",
1130+
"enum": [
1131+
"default"
1132+
]
1133+
},
1134+
{
1135+
"description": "Fluent UI style overlay scrollbars. **Windows Only**\n\n Requires WebView2 Runtime version 125.0.2535.41 or higher, does nothing on older versions,\n see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/?tabs=dotnetcsharp#10253541",
1136+
"type": "string",
1137+
"enum": [
1138+
"fluentOverlay"
1139+
]
1140+
}
1141+
]
1142+
},
11151143
"SecurityConfig": {
11161144
"description": "Security configuration.\n\n See more: <https://v2.tauri.app/reference/config/#securityconfig>",
11171145
"type": "object",

crates/tauri-utils/src/config.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,27 @@ pub enum PreventOverflowConfig {
15861586
Margin(PreventOverflowMargin),
15871587
}
15881588

1589+
/// The scrollbar style to use in the webview.
1590+
///
1591+
/// ## Platform-specific
1592+
///
1593+
/// - **Windows**: This option must be given the same value for all webviews that target the same data directory.
1594+
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Default)]
1595+
#[cfg_attr(feature = "schema", derive(JsonSchema))]
1596+
#[serde(rename_all = "camelCase", deny_unknown_fields)]
1597+
#[non_exhaustive]
1598+
pub enum ScrollBarStyle {
1599+
#[default]
1600+
/// The scrollbar style to use in the webview.
1601+
Default,
1602+
1603+
/// Fluent UI style overlay scrollbars. **Windows Only**
1604+
///
1605+
/// Requires WebView2 Runtime version 125.0.2535.41 or higher, does nothing on older versions,
1606+
/// see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/?tabs=dotnetcsharp#10253541
1607+
FluentOverlay,
1608+
}
1609+
15891610
/// The window configuration object.
15901611
///
15911612
/// See more: <https://v2.tauri.app/reference/config/#windowconfig>
@@ -1919,6 +1940,21 @@ pub struct WindowConfig {
19191940
/// - **Windows / Linux / Android**: Unsupported.
19201941
#[serde(default, alias = "data-store-identifier")]
19211942
pub data_store_identifier: Option<[u8; 16]>,
1943+
1944+
/// Specifies the native scrollbar style to use with the webview.
1945+
/// CSS styles that modify the scrollbar are applied on top of the native appearance configured here.
1946+
///
1947+
/// Defaults to `default`, which is the browser default.
1948+
///
1949+
/// ## Platform-specific
1950+
///
1951+
/// - **Windows**:
1952+
/// - `fluentOverlay` requires WebView2 Runtime version 125.0.2535.41 or higher,
1953+
/// and does nothing on older versions.
1954+
/// - This option must be given the same value for all webviews that target the same data directory.
1955+
/// - **Linux / Android / iOS / macOS**: Unsupported. Only supports `Default` and performs no operation.
1956+
#[serde(default, alias = "scroll-bar-style")]
1957+
pub scroll_bar_style: ScrollBarStyle,
19221958
}
19231959

19241960
impl Default for WindowConfig {
@@ -1980,6 +2016,7 @@ impl Default for WindowConfig {
19802016
disable_input_accessory_view: false,
19812017
data_directory: None,
19822018
data_store_identifier: None,
2019+
scroll_bar_style: ScrollBarStyle::Default,
19832020
}
19842021
}
19852022
}
@@ -3430,6 +3467,17 @@ mod build {
34303467
}
34313468
}
34323469

3470+
impl ToTokens for ScrollBarStyle {
3471+
fn to_tokens(&self, tokens: &mut TokenStream) {
3472+
let prefix = quote! { ::tauri::utils::config::ScrollBarStyle };
3473+
3474+
tokens.append_all(match self {
3475+
Self::Default => quote! { #prefix::Default },
3476+
Self::FluentOverlay => quote! { #prefix::FluentOverlay },
3477+
})
3478+
}
3479+
}
3480+
34333481
impl ToTokens for WindowConfig {
34343482
fn to_tokens(&self, tokens: &mut TokenStream) {
34353483
let label = str_lit(&self.label);
@@ -3488,6 +3536,7 @@ mod build {
34883536
let disable_input_accessory_view = self.disable_input_accessory_view;
34893537
let data_directory = opt_lit(self.data_directory.as_ref().map(path_buf_lit).as_ref());
34903538
let data_store_identifier = opt_vec_lit(self.data_store_identifier, identity);
3539+
let scroll_bar_style = &self.scroll_bar_style;
34913540

34923541
literal_struct!(
34933542
tokens,
@@ -3547,7 +3596,8 @@ mod build {
35473596
allow_link_preview,
35483597
disable_input_accessory_view,
35493598
data_directory,
3550-
data_store_identifier
3599+
data_store_identifier,
3600+
scroll_bar_style
35513601
);
35523602
}
35533603
}

crates/tauri/scripts/bundle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/tauri/src/webview/mod.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use cookie;
1818
use http::HeaderMap;
1919
use serde::Serialize;
2020
use tauri_macros::default_runtime;
21-
pub use tauri_runtime::webview::{NewWindowFeatures, PageLoadEvent};
21+
pub use tauri_runtime::webview::{NewWindowFeatures, PageLoadEvent, ScrollBarStyle};
2222
// Remove this re-export in v3
2323
pub use tauri_runtime::Cookie;
2424
#[cfg(desktop)]
@@ -1165,6 +1165,25 @@ fn main() {
11651165
self
11661166
}
11671167

1168+
/// Specifies the native scrollbar style to use with the webview.
1169+
/// CSS styles that modify the scrollbar are applied on top of the native appearance configured here.
1170+
///
1171+
/// Defaults to [`ScrollBarStyle::Default`], which is the browser default.
1172+
///
1173+
/// ## Platform-specific
1174+
///
1175+
/// - **Windows**:
1176+
/// - [`ScrollBarStyle::FluentOverlay`] requires WebView2 Runtime version 125.0.2535.41 or higher,
1177+
/// and does nothing on older versions.
1178+
/// - This option must be given the same value for all webviews that target the same data directory. Use
1179+
/// [`WebviewBuilder::data_directory`] to change data directories if needed.
1180+
/// - **Linux / Android / iOS / macOS**: Unsupported. Only supports `Default` and performs no operation.
1181+
#[must_use]
1182+
pub fn scroll_bar_style(mut self, style: ScrollBarStyle) -> Self {
1183+
self.webview_attributes = self.webview_attributes.scroll_bar_style(style);
1184+
self
1185+
}
1186+
11681187
/// Whether to show a link preview when long pressing on links. Available on macOS and iOS only.
11691188
///
11701189
/// Default is true.

0 commit comments

Comments
 (0)