mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-02-06 13:37:09 +00:00
refactor(core): do not panic on invalid window labels,#3544 (#3596)
This commit is contained in:
parent
4d0e2eccd9
commit
64e0054299
5
.changes/builder-create-window-result.md
Normal file
5
.changes/builder-create-window-result.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
**Breaking change:** The `Builder#create_window` API now returns a Result validating the window label.
|
||||
5
.changes/label-validation.md
Normal file
5
.changes/label-validation.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
Return an error when creating a window with an invalid label instead of panicking.
|
||||
5
.changes/runtime-window-creation-result.md
Normal file
5
.changes/runtime-window-creation-result.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"tauri-runtime": patch
|
||||
---
|
||||
|
||||
The `PendingWindow::new` and `PendingWindow::with_config` functions now return `Result<Self>` validating the window label.
|
||||
@ -102,6 +102,9 @@ pub enum Error {
|
||||
/// Failed to create window.
|
||||
#[error("failed to create window")]
|
||||
CreateWindow,
|
||||
/// The given window label is invalid.
|
||||
#[error("Window labels must only include alphanumeric characters, `-`, `/`, `:` and `_`.")]
|
||||
InvalidWindowLabel,
|
||||
/// Failed to send message to webview.
|
||||
#[error("failed to send message to the webview")]
|
||||
FailedToSendMessage,
|
||||
|
||||
@ -128,23 +128,26 @@ impl<R: Runtime> PendingWindow<R> {
|
||||
window_builder: <R::Dispatcher as Dispatch>::WindowBuilder,
|
||||
webview_attributes: WebviewAttributes,
|
||||
label: impl Into<String>,
|
||||
) -> Self {
|
||||
) -> crate::Result<Self> {
|
||||
let mut menu_ids = HashMap::new();
|
||||
if let Some(menu) = window_builder.get_menu() {
|
||||
get_menu_ids(&mut menu_ids, menu);
|
||||
}
|
||||
let label = label.into();
|
||||
assert_label_is_valid(&label);
|
||||
Self {
|
||||
window_builder,
|
||||
webview_attributes,
|
||||
uri_scheme_protocols: Default::default(),
|
||||
label,
|
||||
ipc_handler: None,
|
||||
file_drop_handler: None,
|
||||
url: "tauri://localhost".to_string(),
|
||||
menu_ids: Arc::new(Mutex::new(menu_ids)),
|
||||
js_event_listeners: Default::default(),
|
||||
if !is_label_valid(&label) {
|
||||
Err(crate::Error::InvalidWindowLabel)
|
||||
} else {
|
||||
Ok(Self {
|
||||
window_builder,
|
||||
webview_attributes,
|
||||
uri_scheme_protocols: Default::default(),
|
||||
label,
|
||||
ipc_handler: None,
|
||||
file_drop_handler: None,
|
||||
url: "tauri://localhost".to_string(),
|
||||
menu_ids: Arc::new(Mutex::new(menu_ids)),
|
||||
js_event_listeners: Default::default(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -153,24 +156,27 @@ impl<R: Runtime> PendingWindow<R> {
|
||||
window_config: WindowConfig,
|
||||
webview_attributes: WebviewAttributes,
|
||||
label: impl Into<String>,
|
||||
) -> Self {
|
||||
) -> crate::Result<Self> {
|
||||
let window_builder = <<R::Dispatcher as Dispatch>::WindowBuilder>::with_config(window_config);
|
||||
let mut menu_ids = HashMap::new();
|
||||
if let Some(menu) = window_builder.get_menu() {
|
||||
get_menu_ids(&mut menu_ids, menu);
|
||||
}
|
||||
let label = label.into();
|
||||
assert_label_is_valid(&label);
|
||||
Self {
|
||||
window_builder,
|
||||
webview_attributes,
|
||||
uri_scheme_protocols: Default::default(),
|
||||
label,
|
||||
ipc_handler: None,
|
||||
file_drop_handler: None,
|
||||
url: "tauri://localhost".to_string(),
|
||||
menu_ids: Arc::new(Mutex::new(menu_ids)),
|
||||
js_event_listeners: Default::default(),
|
||||
if !is_label_valid(&label) {
|
||||
Err(crate::Error::InvalidWindowLabel)
|
||||
} else {
|
||||
Ok(Self {
|
||||
window_builder,
|
||||
webview_attributes,
|
||||
uri_scheme_protocols: Default::default(),
|
||||
label,
|
||||
ipc_handler: None,
|
||||
file_drop_handler: None,
|
||||
url: "tauri://localhost".to_string(),
|
||||
menu_ids: Arc::new(Mutex::new(menu_ids)),
|
||||
js_event_listeners: Default::default(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -382,7 +382,7 @@ macro_rules! shared_app_impl {
|
||||
window_builder,
|
||||
webview_attributes,
|
||||
label,
|
||||
))
|
||||
)?)
|
||||
}
|
||||
|
||||
#[cfg(feature = "system-tray")]
|
||||
@ -869,8 +869,12 @@ impl<R: Runtime> Builder<R> {
|
||||
/// return (win, webview);
|
||||
/// });
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub fn create_window<F>(mut self, label: impl Into<String>, url: WindowUrl, setup: F) -> Self
|
||||
pub fn create_window<F>(
|
||||
mut self,
|
||||
label: impl Into<String>,
|
||||
url: WindowUrl,
|
||||
setup: F,
|
||||
) -> crate::Result<Self>
|
||||
where
|
||||
F: FnOnce(
|
||||
<R::Dispatcher as Dispatch>::WindowBuilder,
|
||||
@ -888,8 +892,8 @@ impl<R: Runtime> Builder<R> {
|
||||
window_builder,
|
||||
webview_attributes,
|
||||
label,
|
||||
));
|
||||
self
|
||||
)?);
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
/// Adds the icon configured on `tauri.conf.json` to the system tray with the specified menu items.
|
||||
@ -1117,7 +1121,7 @@ impl<R: Runtime> Builder<R> {
|
||||
config,
|
||||
webview_attributes,
|
||||
label,
|
||||
));
|
||||
)?);
|
||||
}
|
||||
|
||||
#[cfg(any(windows, target_os = "linux"))]
|
||||
|
||||
@ -204,7 +204,7 @@ impl<R: Runtime> Window<R> {
|
||||
window_builder,
|
||||
webview_attributes,
|
||||
label,
|
||||
))
|
||||
)?)
|
||||
}
|
||||
|
||||
pub(crate) fn invoke_responder(&self) -> Arc<InvokeResponder<R>> {
|
||||
|
||||
@ -24,6 +24,7 @@ fn main() {
|
||||
(window_builder.title("Tauri - Rust"), webview_attributes)
|
||||
},
|
||||
)
|
||||
.unwrap() // safe to unwrap: window label is valid
|
||||
.run(tauri::generate_context!(
|
||||
"../../examples/multiwindow/src-tauri/tauri.conf.json"
|
||||
))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user