tauri_plugin_prevent_default/platform/
windows.rs

1use tauri::{Runtime, Webview};
2use webview2_com::Microsoft::Web::WebView2::Win32::{
3  ICoreWebView2Settings3, ICoreWebView2Settings4, ICoreWebView2Settings5, ICoreWebView2Settings6,
4};
5use windows::core::Interface;
6
7#[must_use]
8#[derive(Clone, Debug, Default)]
9pub struct PlatformOptions {
10  browser_accelerator_keys: Option<bool>,
11  built_in_error_page: Option<bool>,
12  default_context_menus: Option<bool>,
13  default_script_dialogs: Option<bool>,
14  dev_tools: Option<bool>,
15  general_autofill: Option<bool>,
16  host_objects: Option<bool>,
17  password_autosave: Option<bool>,
18  pinch_zoom: Option<bool>,
19  script: Option<bool>,
20  swipe_navigation: Option<bool>,
21  web_message: Option<bool>,
22  zoom_control: Option<bool>,
23}
24
25impl PlatformOptions {
26  pub fn new() -> Self {
27    Self::default()
28  }
29
30  /// Determines whether browser-specific accelerator keys are enabled.
31  ///
32  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.arebrowseracceleratorkeysenabled>
33  pub fn browser_accelerator_keys(mut self, enabled: bool) -> Self {
34    self.browser_accelerator_keys = Some(enabled);
35    self
36  }
37
38  /// Determines whether to disable built in error page for navigation failure and render process failure.
39  ///
40  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.isbuiltinerrorpageenabled>
41  pub fn built_in_error_page(mut self, enabled: bool) -> Self {
42    self.built_in_error_page = Some(enabled);
43    self
44  }
45
46  /// Determines whether the default context menus are shown to the user in WebView.
47  ///
48  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.aredefaultcontextmenusenabled>
49  pub fn default_context_menus(mut self, enabled: bool) -> Self {
50    self.default_context_menus = Some(enabled);
51    self
52  }
53
54  /// Determines whether WebView renders the default JavaScript dialog box.
55  ///
56  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.aredefaultscriptdialogsenabled>
57  pub fn default_script_dialogs(mut self, enabled: bool) -> Self {
58    self.default_script_dialogs = Some(enabled);
59    self
60  }
61
62  /// Determines whether the user is able to use the context menu or keyboard shortcuts to open the DevTools window.
63  ///
64  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.aredevtoolsenabled>
65  pub fn dev_tools(mut self, enabled: bool) -> Self {
66    self.dev_tools = Some(enabled);
67    self
68  }
69
70  /// Determines whether general form information will be saved and autofilled.
71  ///
72  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.isgeneralautofillenabled>
73  pub fn general_autofill(mut self, enabled: bool) -> Self {
74    self.general_autofill = Some(enabled);
75    self
76  }
77
78  /// Determines whether host objects are accessible from the page in WebView.
79  ///
80  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.arehostobjectsallowed>
81  pub fn host_objects(mut self, enabled: bool) -> Self {
82    self.host_objects = Some(enabled);
83    self
84  }
85
86  /// Determines whether password information will be autosaved.
87  ///
88  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.ispasswordautosaveenabled>
89  pub fn password_autosave(mut self, enabled: bool) -> Self {
90    self.password_autosave = Some(enabled);
91    self
92  }
93
94  /// Determines the ability of the end users to use pinching motions on touch input enabled devices to scale the web content in the WebView.
95  ///
96  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.ispinchzoomenabled>
97  pub fn pinch_zoom(mut self, enabled: bool) -> Self {
98    self.pinch_zoom = Some(enabled);
99    self
100  }
101
102  /// Determines whether running JavaScript is enabled in all future navigations in the WebView.
103  ///
104  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.isscriptenabled>
105  pub fn script(mut self, enabled: bool) -> Self {
106    self.script = Some(enabled);
107    self
108  }
109
110  /// Determines whether the end user can use swiping gesture on touch input enabled devices to navigate in the WebView.
111  ///
112  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.isswipenavigationenabled>
113  pub fn swipe_navigation(mut self, enabled: bool) -> Self {
114    self.swipe_navigation = Some(enabled);
115    self
116  }
117
118  /// Determines whether communication from the host to the top-level HTML document of the WebView is allowed.
119  ///
120  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.iswebmessageenabled>
121  pub fn web_message(mut self, enabled: bool) -> Self {
122    self.web_message = Some(enabled);
123    self
124  }
125
126  /// Determines whether the user is able to impact the zoom of the WebView.
127  ///
128  /// <https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2settings.iszoomcontrolenabled>
129  pub fn zoom_control(mut self, enabled: bool) -> Self {
130    self.zoom_control = Some(enabled);
131    self
132  }
133}
134
135#[allow(clippy::needless_pass_by_value)]
136pub(crate) fn on_webview_ready<R>(webview: &Webview<R>, options: PlatformOptions)
137where
138  R: Runtime,
139{
140  let _ = webview.with_webview(move |platform_webview| unsafe {
141    let Ok(webview2) = platform_webview.controller().CoreWebView2() else {
142      return;
143    };
144
145    let Ok(settings) = webview2.Settings() else {
146      return;
147    };
148
149    if let Some(built_in_error_page) = options.built_in_error_page {
150      let _ = settings.SetIsBuiltInErrorPageEnabled(built_in_error_page);
151    }
152
153    if let Some(default_context_menus) = options.default_context_menus {
154      let _ = settings.SetAreDefaultContextMenusEnabled(default_context_menus);
155    }
156
157    if let Some(default_script_dialogs) = options.default_script_dialogs {
158      let _ = settings.SetAreDefaultScriptDialogsEnabled(default_script_dialogs);
159    }
160
161    if let Some(dev_tools) = options.dev_tools {
162      let _ = settings.SetAreDevToolsEnabled(dev_tools);
163    }
164
165    if let Some(host_objects) = options.host_objects {
166      let _ = settings.SetAreHostObjectsAllowed(host_objects);
167    }
168
169    if let Some(script) = options.script {
170      let _ = settings.SetIsScriptEnabled(script);
171    }
172
173    if let Some(web_message) = options.web_message {
174      let _ = settings.SetIsWebMessageEnabled(web_message);
175    }
176
177    if let Some(zoom_control) = options.zoom_control {
178      let _ = settings.SetIsZoomControlEnabled(zoom_control);
179    }
180
181    if let Some(browser_accelerator_keys) = options.browser_accelerator_keys {
182      if let Ok(settings3) = settings.cast::<ICoreWebView2Settings3>() {
183        let _ = settings3.SetAreBrowserAcceleratorKeysEnabled(browser_accelerator_keys);
184      }
185    }
186
187    if let Ok(settings4) = settings.cast::<ICoreWebView2Settings4>() {
188      if let Some(general_autofill) = options.general_autofill {
189        let _ = settings4.SetIsGeneralAutofillEnabled(general_autofill);
190      }
191
192      if let Some(password_autosave) = options.password_autosave {
193        let _ = settings4.SetIsPasswordAutosaveEnabled(password_autosave);
194      }
195    }
196
197    if let Some(pinch_zoom) = options.pinch_zoom {
198      if let Ok(settings5) = settings.cast::<ICoreWebView2Settings5>() {
199        let _ = settings5.SetIsPinchZoomEnabled(pinch_zoom);
200      }
201    }
202
203    if let Some(swipe_navigation) = options.swipe_navigation {
204      if let Ok(settings6) = settings.cast::<ICoreWebView2Settings6>() {
205        let _ = settings6.SetIsSwipeNavigationEnabled(swipe_navigation);
206      }
207    }
208  });
209}