Skip to content

Commit 3d8dcbb

Browse files
authored
fix(core): allow wry to be an optional dep again (fix #1841) (#1854)
1 parent 2881ccc commit 3d8dcbb

6 files changed

Lines changed: 154 additions & 89 deletions

File tree

.changes/internal-default-args.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
(internal): allow `wry` dependency to be optional again while keeping default args.
6+
code that wishes to expose a struct with a default arg should use the `crate::manager::default_args!` macro to declare
7+
the struct, so that it can automatically feature-gate `DefaultArgs` behind using `wry`.

core/tauri/src/app.rs

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

28-
use crate::manager::DefaultArgs;
2928
#[cfg(feature = "updater")]
3029
use crate::updater;
3130

@@ -51,12 +50,14 @@ impl<I: MenuId> SystemTrayEvent<I> {
5150
}
5251
}
5352

54-
/// A menu event that was triggered on a window.
55-
#[cfg(feature = "menu")]
56-
#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
57-
pub struct WindowMenuEvent<P: Params = DefaultArgs> {
58-
pub(crate) menu_item_id: P::MenuId,
59-
pub(crate) window: Window<P>,
53+
crate::manager::default_args! {
54+
/// A menu event that was triggered on a window.
55+
#[cfg(feature = "menu")]
56+
#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
57+
pub struct WindowMenuEvent<P: Params> {
58+
pub(crate) menu_item_id: P::MenuId,
59+
pub(crate) window: Window<P>,
60+
}
6061
}
6162

6263
#[cfg(feature = "menu")]
@@ -72,10 +73,12 @@ impl<P: Params> WindowMenuEvent<P> {
7273
}
7374
}
7475

75-
/// A window event that was triggered on the specified window.
76-
pub struct GlobalWindowEvent<P: Params = DefaultArgs> {
77-
pub(crate) event: WindowEvent,
78-
pub(crate) window: Window<P>,
76+
crate::manager::default_args! {
77+
/// A window event that was triggered on the specified window.
78+
pub struct GlobalWindowEvent<P: Params> {
79+
pub(crate) event: WindowEvent,
80+
pub(crate) window: Window<P>,
81+
}
7982
}
8083

8184
impl<P: Params> GlobalWindowEvent<P> {
@@ -90,9 +93,11 @@ impl<P: Params> GlobalWindowEvent<P> {
9093
}
9194
}
9295

93-
/// A handle to the currently running application.
94-
pub struct AppHandle<P: Params = DefaultArgs> {
95-
manager: WindowManager<P>,
96+
crate::manager::default_args! {
97+
/// A handle to the currently running application.
98+
pub struct AppHandle<P: Params> {
99+
manager: WindowManager<P>,
100+
}
96101
}
97102

98103
impl<P: Params> Manager<P> for AppHandle<P> {}
@@ -102,12 +107,14 @@ impl<P: Params> ManagerBase<P> for AppHandle<P> {
102107
}
103108
}
104109

105-
/// The instance of the currently running application.
106-
///
107-
/// This type implements [`Manager`] which allows for manipulation of global application items.
108-
pub struct App<P: Params = DefaultArgs> {
109-
runtime: P::Runtime,
110-
manager: WindowManager<P>,
110+
crate::manager::default_args! {
111+
/// The instance of the currently running application.
112+
///
113+
/// This type implements [`Manager`] which allows for manipulation of global application items.
114+
pub struct App<P: Params> {
115+
runtime: P::Runtime,
116+
manager: WindowManager<P>,
117+
}
111118
}
112119

113120
impl<P: Params> Manager<P> for App<P> {}

core/tauri/src/hooks.rs

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

5-
use crate::manager::DefaultArgs;
65
use crate::{
76
api::rpc::{format_callback, format_callback_result},
87
app::App,
@@ -35,13 +34,15 @@ impl PageLoadPayload {
3534
}
3635
}
3736

38-
/// The message and resolver given to a custom command.
39-
pub struct Invoke<P: Params = DefaultArgs> {
40-
/// The message passed.
41-
pub message: InvokeMessage<P>,
37+
crate::manager::default_args! {
38+
/// The message and resolver given to a custom command.
39+
pub struct Invoke<P: Params> {
40+
/// The message passed.
41+
pub message: InvokeMessage<P>,
4242

43-
/// The resolver of the message.
44-
pub resolver: InvokeResolver<P>,
43+
/// The resolver of the message.
44+
pub resolver: InvokeResolver<P>,
45+
}
4546
}
4647

4748
/// Error response from an [`InvokeMessage`].
@@ -111,11 +112,13 @@ impl From<InvokeError> for InvokeResponse {
111112
}
112113
}
113114

114-
/// Resolver of a invoke message.
115-
pub struct InvokeResolver<P: Params = DefaultArgs> {
116-
window: Window<P>,
117-
pub(crate) callback: String,
118-
pub(crate) error: String,
115+
crate::manager::default_args! {
116+
/// Resolver of a invoke message.
117+
pub struct InvokeResolver<P: Params> {
118+
window: Window<P>,
119+
pub(crate) callback: String,
120+
pub(crate) error: String,
121+
}
119122
}
120123

121124
impl<P: Params> InvokeResolver<P> {
@@ -229,16 +232,18 @@ impl<P: Params> InvokeResolver<P> {
229232
}
230233
}
231234

232-
/// An invoke message.
233-
pub struct InvokeMessage<P: Params = DefaultArgs> {
234-
/// The window that received the invoke message.
235-
pub(crate) window: Window<P>,
236-
/// Application managed state.
237-
pub(crate) state: Arc<StateManager>,
238-
/// The RPC command.
239-
pub(crate) command: String,
240-
/// The JSON argument passed on the invoke message.
241-
pub(crate) payload: JsonValue,
235+
crate::manager::default_args! {
236+
/// An invoke message.
237+
pub struct InvokeMessage<P: Params> {
238+
/// The window that received the invoke message.
239+
pub(crate) window: Window<P>,
240+
/// Application managed state.
241+
pub(crate) state: Arc<StateManager>,
242+
/// The RPC command.
243+
pub(crate) command: String,
244+
/// The JSON argument passed on the invoke message.
245+
pub(crate) payload: JsonValue,
246+
}
242247
}
243248

244249
impl<P: Params> InvokeMessage<P> {

core/tauri/src/manager.rs

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

5+
// we re-export the default_args! macro as pub(crate) so we can use it easily from other modules
6+
#![allow(clippy::single_component_path_imports)]
7+
58
use crate::{
69
api::{
710
assets::Assets,
@@ -71,41 +74,78 @@ pub(crate) fn tauri_event<Event: Tag>(tauri_event: &str) -> Event {
7174
})
7275
}
7376

74-
pub struct InnerWindowManager<P: Params = DefaultArgs> {
75-
windows: Mutex<HashMap<P::Label, Window<P>>>,
76-
plugins: Mutex<PluginStore<P>>,
77-
listeners: Listeners<P::Event, P::Label>,
78-
pub(crate) state: Arc<StateManager>,
77+
crate::manager::default_args! {
78+
pub struct InnerWindowManager<P: Params> {
79+
windows: Mutex<HashMap<P::Label, Window<P>>>,
80+
plugins: Mutex<PluginStore<P>>,
81+
listeners: Listeners<P::Event, P::Label>,
82+
pub(crate) state: Arc<StateManager>,
7983

80-
/// The JS message handler.
81-
invoke_handler: Box<InvokeHandler<P>>,
84+
/// The JS message handler.
85+
invoke_handler: Box<InvokeHandler<P>>,
8286

83-
/// The page load hook, invoked when the webview performs a navigation.
84-
on_page_load: Box<OnPageLoad<P>>,
87+
/// The page load hook, invoked when the webview performs a navigation.
88+
on_page_load: Box<OnPageLoad<P>>,
8589

86-
config: Arc<Config>,
87-
assets: Arc<P::Assets>,
88-
default_window_icon: Option<Vec<u8>>,
90+
config: Arc<Config>,
91+
assets: Arc<P::Assets>,
92+
default_window_icon: Option<Vec<u8>>,
8993

90-
/// A list of salts that are valid for the current application.
91-
salts: Mutex<HashSet<Uuid>>,
92-
package_info: PackageInfo,
93-
/// The webview protocols protocols available to all windows.
94-
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>,
95-
/// The menu set to all windows.
96-
#[cfg(feature = "menu")]
97-
menu: Vec<Menu<P::MenuId>>,
98-
/// Maps runtime id to a strongly typed menu id.
99-
#[cfg(feature = "menu")]
100-
menu_ids: HashMap<u32, P::MenuId>,
101-
/// Menu event listeners to all windows.
102-
#[cfg(feature = "menu")]
103-
menu_event_listeners: Arc<Vec<GlobalMenuEventListener<P>>>,
104-
/// Window event listeners to all windows.
105-
window_event_listeners: Arc<Vec<GlobalWindowEventListener<P>>>,
94+
/// A list of salts that are valid for the current application.
95+
salts: Mutex<HashSet<Uuid>>,
96+
package_info: PackageInfo,
97+
/// The webview protocols protocols available to all windows.
98+
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>,
99+
/// The menu set to all windows.
100+
#[cfg(feature = "menu")]
101+
menu: Vec<Menu<P::MenuId>>,
102+
/// Maps runtime id to a strongly typed menu id.
103+
#[cfg(feature = "menu")]
104+
menu_ids: HashMap<u32, P::MenuId>,
105+
/// Menu event listeners to all windows.
106+
#[cfg(feature = "menu")]
107+
menu_event_listeners: Arc<Vec<GlobalMenuEventListener<P>>>,
108+
/// Window event listeners to all windows.
109+
window_event_listeners: Arc<Vec<GlobalWindowEventListener<P>>>,
110+
}
106111
}
107112

113+
/// struct declaration using params + default args which includes optional feature wry
114+
macro_rules! default_args {
115+
(
116+
$(#[$attrs_struct:meta])*
117+
$vis_struct:vis struct $name:ident<$p:ident: $params:ident> {
118+
$(
119+
$(#[$attrs_field:meta])*
120+
$vis_field:vis $field:ident: $field_type:ty,
121+
)*
122+
}
123+
) => {
124+
$(#[$attrs_struct])*
125+
#[cfg(feature = "wry")]
126+
$vis_struct struct $name<$p: $params = crate::manager::DefaultArgs> {
127+
$(
128+
$(#[$attrs_field])*
129+
$vis_field $field: $field_type,
130+
)*
131+
}
132+
133+
$(#[$attrs_struct])*
134+
#[cfg(not(feature = "wry"))]
135+
$vis_struct struct $name<$p: $params> {
136+
$(
137+
$(#[$attrs_field])*
138+
$vis_field $field: $field_type,
139+
)*
140+
}
141+
};
142+
}
143+
144+
// export it to allow use from other modules
145+
pub(crate) use default_args;
146+
108147
/// This type should always match `Builder::default()`, otherwise the default type is useless.
148+
#[cfg(feature = "wry")]
109149
pub(crate) type DefaultArgs =
110150
Args<String, String, String, String, crate::api::assets::EmbeddedAssets, crate::Wry>;
111151

@@ -151,10 +191,12 @@ impl<E: Tag, L: Tag, MID: MenuId, TID: MenuId, A: Assets, R: Runtime> Params
151191
type Runtime = R;
152192
}
153193

154-
pub struct WindowManager<P: Params = DefaultArgs> {
155-
pub inner: Arc<InnerWindowManager<P>>,
156-
#[allow(clippy::type_complexity)]
157-
_marker: Args<P::Event, P::Label, P::MenuId, P::SystemTrayMenuId, P::Assets, P::Runtime>,
194+
crate::manager::default_args! {
195+
pub struct WindowManager<P: Params> {
196+
pub inner: Arc<InnerWindowManager<P>>,
197+
#[allow(clippy::type_complexity)]
198+
_marker: Args<P::Event, P::Label, P::MenuId, P::SystemTrayMenuId, P::Assets, P::Runtime>,
199+
}
158200
}
159201

160202
impl<P: Params> Clone for WindowManager<P> {

core/tauri/src/plugin.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
//! Extend Tauri functionality.
66
7-
use crate::manager::DefaultArgs;
87
use crate::{api::config::PluginConfig, App, Invoke, PageLoadPayload, Params, Window};
98
use serde_json::Value as JsonValue;
109
use std::collections::HashMap;
@@ -45,9 +44,11 @@ pub trait Plugin<P: Params>: Send {
4544
fn extend_api(&mut self, invoke: Invoke<P>) {}
4645
}
4746

48-
/// Plugin collection type.
49-
pub(crate) struct PluginStore<P: Params = DefaultArgs> {
50-
store: HashMap<&'static str, Box<dyn Plugin<P>>>,
47+
crate::manager::default_args! {
48+
/// Plugin collection type.
49+
pub(crate) struct PluginStore<P: Params> {
50+
store: HashMap<&'static str, Box<dyn Plugin<P>>>,
51+
}
5152
}
5253

5354
impl<P: Params> Default for PluginStore<P> {

core/tauri/src/window.rs

Lines changed: 14 additions & 11 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::{DefaultArgs, WindowManager},
11+
manager::WindowManager,
1212
runtime::{
1313
monitor::Monitor as RuntimeMonitor,
1414
tag::{TagRef, ToJsString},
@@ -92,16 +92,19 @@ impl Monitor {
9292
}
9393

9494
// TODO: expand these docs since this is a pretty important type
95-
/// A webview window managed by Tauri.
96-
///
97-
/// This type also implements [`Manager`] which allows you to manage other windows attached to
98-
/// the same application.
99-
pub struct Window<P: Params = DefaultArgs> {
100-
/// The webview window created by the runtime.
101-
window: DetachedWindow<P>,
102-
103-
/// The manager to associate this webview window with.
104-
manager: WindowManager<P>,
95+
crate::manager::default_args! {
96+
/// A webview window managed by Tauri.
97+
///
98+
/// This type also implements [`Manager`] which allows you to manage other windows attached to
99+
/// the same application.
100+
pub struct Window<P: Params> {
101+
/// The webview window created by the runtime.
102+
/// ok
103+
window: DetachedWindow<P>,
104+
105+
/// The manager to associate this webview window with.
106+
manager: WindowManager<P>,
107+
}
105108
}
106109

107110
impl<P: Params> Clone for Window<P> {

0 commit comments

Comments
 (0)