Skip to content

Commit 27a7810

Browse files
authored
feat(core): add default Args to all types exposing Params (#1777)
1 parent b5f8912 commit 27a7810

File tree

7 files changed

+26
-13
lines changed

7 files changed

+26
-13
lines changed

.changes/default-params-type.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Adds the default types used with `Builder::default()` to items that expose `Params` in their type. This allows you to
6+
skip specifying a generic parameter to types like `Window<P>` if you use the default type.

core/tauri/src/app.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::runtime::menu::Menu;
2525
#[cfg(feature = "system-tray")]
2626
use crate::runtime::{menu::SystemTrayMenuItem, Icon};
2727

28+
use crate::manager::DefaultArgs;
2829
#[cfg(feature = "updater")]
2930
use crate::updater;
3031

@@ -53,7 +54,7 @@ impl<I: MenuId> SystemTrayEvent<I> {
5354
/// A menu event that was triggered on a window.
5455
#[cfg(feature = "menu")]
5556
#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
56-
pub struct WindowMenuEvent<P: Params> {
57+
pub struct WindowMenuEvent<P: Params = DefaultArgs> {
5758
pub(crate) menu_item_id: P::MenuId,
5859
pub(crate) window: Window<P>,
5960
}
@@ -72,7 +73,7 @@ impl<P: Params> WindowMenuEvent<P> {
7273
}
7374

7475
/// A window event that was triggered on the specified window.
75-
pub struct GlobalWindowEvent<P: Params> {
76+
pub struct GlobalWindowEvent<P: Params = DefaultArgs> {
7677
pub(crate) event: WindowEvent,
7778
pub(crate) window: Window<P>,
7879
}
@@ -90,7 +91,7 @@ impl<P: Params> GlobalWindowEvent<P> {
9091
}
9192

9293
/// A handle to the currently running application.
93-
pub struct AppHandle<P: Params> {
94+
pub struct AppHandle<P: Params = DefaultArgs> {
9495
manager: WindowManager<P>,
9596
}
9697

@@ -104,7 +105,7 @@ impl<P: Params> ManagerBase<P> for AppHandle<P> {
104105
/// The instance of the currently running application.
105106
///
106107
/// This type implements [`Manager`] which allows for manipulation of global application items.
107-
pub struct App<P: Params> {
108+
pub struct App<P: Params = DefaultArgs> {
108109
runtime: P::Runtime,
109110
manager: WindowManager<P>,
110111
}

core/tauri/src/hooks.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5+
use crate::manager::DefaultArgs;
56
use crate::{
67
api::rpc::{format_callback, format_callback_result},
78
app::App,
@@ -35,7 +36,7 @@ impl PageLoadPayload {
3536
}
3637

3738
/// The message and resolver given to a custom command.
38-
pub struct Invoke<P: Params> {
39+
pub struct Invoke<P: Params = DefaultArgs> {
3940
/// The message passed.
4041
pub message: InvokeMessage<P>,
4142

@@ -111,7 +112,7 @@ impl From<InvokeError> for InvokeResponse {
111112
}
112113

113114
/// Resolver of a invoke message.
114-
pub struct InvokeResolver<P: Params> {
115+
pub struct InvokeResolver<P: Params = DefaultArgs> {
115116
window: Window<P>,
116117
pub(crate) callback: String,
117118
pub(crate) error: String,
@@ -229,7 +230,7 @@ impl<P: Params> InvokeResolver<P> {
229230
}
230231

231232
/// An invoke message.
232-
pub struct InvokeMessage<P: Params> {
233+
pub struct InvokeMessage<P: Params = DefaultArgs> {
233234
/// The window that received the invoke message.
234235
pub(crate) window: Window<P>,
235236
/// Application managed state.

core/tauri/src/manager.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) fn tauri_event<Event: Tag>(tauri_event: &str) -> Event {
7171
})
7272
}
7373

74-
pub struct InnerWindowManager<P: Params> {
74+
pub struct InnerWindowManager<P: Params = DefaultArgs> {
7575
windows: Mutex<HashMap<P::Label, Window<P>>>,
7676
plugins: Mutex<PluginStore<P>>,
7777
listeners: Listeners<P::Event, P::Label>,
@@ -105,6 +105,10 @@ pub struct InnerWindowManager<P: Params> {
105105
window_event_listeners: Arc<Vec<GlobalWindowEventListener<P>>>,
106106
}
107107

108+
/// This type should always match `Builder::default()`, otherwise the default type is useless.
109+
pub(crate) type DefaultArgs =
110+
Args<String, String, String, String, crate::api::assets::EmbeddedAssets, crate::Wry>;
111+
108112
/// A [Zero Sized Type] marker representing a full [`Params`].
109113
///
110114
/// [Zero Sized Type]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#zero-sized-types-zsts
@@ -147,7 +151,7 @@ impl<E: Tag, L: Tag, MID: MenuId, TID: MenuId, A: Assets, R: Runtime> Params
147151
type Runtime = R;
148152
}
149153

150-
pub struct WindowManager<P: Params> {
154+
pub struct WindowManager<P: Params = DefaultArgs> {
151155
pub inner: Arc<InnerWindowManager<P>>,
152156
#[allow(clippy::type_complexity)]
153157
_marker: Args<P::Event, P::Label, P::MenuId, P::SystemTrayMenuId, P::Assets, P::Runtime>,

core/tauri/src/plugin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
//! Extend Tauri functionality.
66
7+
use crate::manager::DefaultArgs;
78
use crate::{api::config::PluginConfig, App, Invoke, PageLoadPayload, Params, Window};
89
use serde_json::Value as JsonValue;
910
use std::collections::HashMap;
@@ -45,7 +46,7 @@ pub trait Plugin<P: Params>: Send {
4546
}
4647

4748
/// Plugin collection type.
48-
pub(crate) struct PluginStore<P: Params> {
49+
pub(crate) struct PluginStore<P: Params = DefaultArgs> {
4950
store: HashMap<&'static str, Box<dyn Plugin<P>>>,
5051
}
5152

core/tauri/src/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
api::config::WindowUrl,
99
command::{CommandArg, CommandItem},
1010
event::{Event, EventHandler},
11-
manager::WindowManager,
11+
manager::{DefaultArgs, WindowManager},
1212
runtime::{
1313
monitor::Monitor as RuntimeMonitor,
1414
tag::{TagRef, ToJsString},
@@ -96,7 +96,7 @@ impl Monitor {
9696
///
9797
/// This type also implements [`Manager`] which allows you to manage other windows attached to
9898
/// the same application.
99-
pub struct Window<P: Params> {
99+
pub struct Window<P: Params = DefaultArgs> {
100100
/// The webview window created by the runtime.
101101
window: DetachedWindow<P>,
102102

examples/commands/src-tauri/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ enum MyError {
2626

2727
// ------------------------ Commands using Window ------------------------
2828
#[command]
29-
fn window_label(window: Window<impl Params<Label = String>>) {
29+
fn window_label(window: Window) {
3030
println!("window label: {}", window.label());
3131
}
3232

0 commit comments

Comments
 (0)