Skip to content

Commit ec27ca8

Browse files
authored
refactor(tauri): remove private params trait methods (#1484)
* refactor(tauri): remove private params trait methods * add changes file * remove newly unused trait in WindowManager unit test
1 parent f2d24ef commit ec27ca8

File tree

6 files changed

+148
-257
lines changed

6 files changed

+148
-257
lines changed

.changes/no-private-params-trait.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
internal refactoring of `Params` to allow for easier usage without a private trait with only 1 implementor.
6+
7+
`ParamsPrivate` -> `ParamsBase`
8+
`ManagerPrivate` -> `ManagerBase`
9+
(new) `Args`, crate only. Now implements `Params`/`ParamsBase`.
10+
`App` and `Window` use `WindowManager` directly

core/tauri/src/endpoints/event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
// SPDX-License-Identifier: MIT
44

5-
use crate::{endpoints::InvokeResponse, sealed::ManagerPrivate, Manager, Params, Window};
5+
use crate::{endpoints::InvokeResponse, sealed::ManagerBase, Manager, Params, Window};
66
use serde::Deserialize;
77

88
/// The API descriptor.

core/tauri/src/lib.rs

+16-113
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub struct Context<A: Assets> {
111111
}
112112

113113
/// Types associated with the running Tauri application.
114-
pub trait Params: sealed::ParamsPrivate<Self> {
114+
pub trait Params: sealed::ParamsBase {
115115
/// The event type used to create and listen to events.
116116
type Event: Tag;
117117

@@ -126,7 +126,9 @@ pub trait Params: sealed::ParamsPrivate<Self> {
126126
}
127127

128128
/// Manages a running application.
129-
pub trait Manager<M: Params>: sealed::ManagerPrivate<M> {
129+
///
130+
/// TODO: expand these docs
131+
pub trait Manager<M: Params>: sealed::ManagerBase<M> {
130132
/// The [`Config`] the manager was created with.
131133
fn config(&self) -> &Config {
132134
self.manager().config()
@@ -202,126 +204,27 @@ pub trait Manager<M: Params>: sealed::ManagerPrivate<M> {
202204
/// Prevent implementation details from leaking out of the [`Manager`] and [`Params`] traits.
203205
pub(crate) mod sealed {
204206
use super::Params;
205-
use crate::runtime::Runtime;
206-
use crate::{
207-
api::{config::Config, PackageInfo},
208-
event::{Event, EventHandler},
209-
hooks::{InvokeMessage, PageLoadPayload},
210-
runtime::window::{DetachedWindow, PendingWindow},
211-
Window,
212-
};
213-
use serde::Serialize;
214-
use std::collections::{HashMap, HashSet};
215-
use uuid::Uuid;
216-
217-
/// private manager api
218-
pub trait ParamsPrivate<M: Params>: Clone + Send + Sized + 'static {
219-
/// Pass messages not handled by modules or plugins to the running application
220-
fn run_invoke_handler(&self, message: InvokeMessage<M>);
221-
222-
/// Ran once for every window when the page is loaded.
223-
fn run_on_page_load(&self, window: Window<M>, payload: PageLoadPayload);
224-
225-
/// Pass a message to be handled by a plugin that expects the command.
226-
fn extend_api(&self, command: String, message: InvokeMessage<M>);
227-
228-
/// Initialize all the plugins attached to the [`Manager`].
229-
fn initialize_plugins(&self) -> crate::Result<()>;
230-
231-
/// Prepare a [`PendingWindow`] to be created by the [`Runtime`].
232-
///
233-
/// The passed labels should represent either all the windows in the manager. If the application
234-
/// has not yet been started, the passed labels should represent all windows that will be
235-
/// created before starting.
236-
fn prepare_window(
237-
&self,
238-
pending: PendingWindow<M>,
239-
labels: &[M::Label],
240-
) -> crate::Result<PendingWindow<M>>;
241-
242-
/// Attach a detached window to the manager.
243-
fn attach_window(&self, window: DetachedWindow<M>) -> Window<M>;
244-
245-
/// Emit an event to javascript windows that pass the predicate.
246-
fn emit_filter_internal<S: Serialize + Clone, F: Fn(&Window<Self>) -> bool>(
247-
&self,
248-
event: String,
249-
payload: Option<S>,
250-
filter: F,
251-
) -> crate::Result<()>;
252-
253-
/// Emit an event to javascript windows that pass the predicate.
254-
fn emit_filter<S: Serialize + Clone, F: Fn(&Window<M>) -> bool>(
255-
&self,
256-
event: M::Event,
257-
payload: Option<S>,
258-
predicate: F,
259-
) -> crate::Result<()>;
260-
261-
/// All current window labels existing.
262-
fn labels(&self) -> HashSet<M::Label>;
263-
264-
/// The configuration the [`Manager`] was built with.
265-
fn config(&self) -> &Config;
266-
267-
/// App package information.
268-
fn package_info(&self) -> &PackageInfo;
269-
270-
/// Remove the specified event handler.
271-
fn unlisten(&self, handler_id: EventHandler);
272-
273-
/// Trigger an event.
274-
fn trigger(&self, event: M::Event, window: Option<M::Label>, data: Option<String>);
275-
276-
/// Set up a listener to an event.
277-
fn listen<F: Fn(Event) + Send + 'static>(
278-
&self,
279-
event: M::Event,
280-
window: Option<M::Label>,
281-
handler: F,
282-
) -> EventHandler;
283-
284-
/// Set up a listener to and event that is automatically removed after called once.
285-
fn once<F: Fn(Event) + Send + 'static>(
286-
&self,
287-
event: M::Event,
288-
window: Option<M::Label>,
289-
handler: F,
290-
);
291-
292-
fn event_listeners_object_name(&self) -> String;
293-
fn event_queue_object_name(&self) -> String;
294-
fn event_emit_function_name(&self) -> String;
295-
296-
/// Generate a random salt and store it in the manager
297-
fn generate_salt(&self) -> Uuid;
298-
299-
/// Verify that the passed salt is a valid salt in the manager.
300-
fn verify_salt(&self, salt: String) -> bool;
301-
302-
/// Get a single managed window.
303-
fn get_window(&self, label: &M::Label) -> Option<Window<M>>;
304-
305-
/// Get all managed windows.
306-
fn windows(&self) -> HashMap<M::Label, Window<M>>;
307-
}
207+
use crate::runtime::{manager::WindowManager, Runtime};
208+
209+
/// No downstream implementations of [`Params`].
210+
pub trait ParamsBase: 'static {}
308211

309-
/// Represents either a running [`Runtime`] or a dispatcher to it.
310-
pub enum RuntimeOrDispatch<'m, M: Params> {
212+
/// A running [`Runtime`] or a dispatcher to it.
213+
pub enum RuntimeOrDispatch<'r, P: Params> {
311214
/// Mutable reference to the running [`Runtime`].
312-
Runtime(&'m mut M::Runtime),
215+
Runtime(&'r mut P::Runtime),
313216

314217
/// A dispatcher to the running [`Runtime`].
315-
Dispatch(<M::Runtime as Runtime>::Dispatcher),
218+
Dispatch(<P::Runtime as Runtime>::Dispatcher),
316219
}
317220

318-
/// Represents a managed handle to the application runner.
319-
pub trait ManagerPrivate<M: Params> {
221+
/// Managed handle to the application runtime.
222+
pub trait ManagerBase<P: Params> {
320223
/// The manager behind the [`Managed`] item.
321-
fn manager(&self) -> &M;
224+
fn manager(&self) -> &WindowManager<P>;
322225

323226
/// The runtime or runtime dispatcher of the [`Managed`] item.
324-
fn runtime(&mut self) -> RuntimeOrDispatch<'_, M>;
227+
fn runtime(&mut self) -> RuntimeOrDispatch<'_, P>;
325228
}
326229
}
327230

core/tauri/src/runtime/app.rs

+14-15
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ use crate::{
1010
flavors::wry::Wry, manager::WindowManager, tag::Tag, webview::Attributes,
1111
window::PendingWindow, Dispatch, Runtime,
1212
},
13-
sealed::{ManagerPrivate, ParamsPrivate, RuntimeOrDispatch},
13+
sealed::{ManagerBase, RuntimeOrDispatch},
1414
Context, Manager, Params, Window,
1515
};
1616

17+
use crate::runtime::manager::Args;
1718
#[cfg(feature = "updater")]
1819
use crate::updater;
1920

@@ -22,12 +23,12 @@ use crate::updater;
2223
/// This type implements [`Manager`] which allows for manipulation of global application items.
2324
pub struct App<P: Params> {
2425
runtime: P::Runtime,
25-
manager: P,
26+
manager: WindowManager<P>,
2627
}
2728

2829
impl<P: Params> Manager<P> for App<P> {}
29-
impl<P: Params> ManagerPrivate<P> for App<P> {
30-
fn manager(&self) -> &P {
30+
impl<P: Params> ManagerBase<P> for App<P> {
31+
fn manager(&self) -> &WindowManager<P> {
3132
&self.manager
3233
}
3334

@@ -104,19 +105,19 @@ where
104105
R: Runtime,
105106
{
106107
/// The JS message handler.
107-
invoke_handler: Box<InvokeHandler<WindowManager<E, L, A, R>>>,
108+
invoke_handler: Box<InvokeHandler<Args<E, L, A, R>>>,
108109

109110
/// The setup hook.
110-
setup: SetupHook<WindowManager<E, L, A, R>>,
111+
setup: SetupHook<Args<E, L, A, R>>,
111112

112113
/// Page load hook.
113-
on_page_load: Box<OnPageLoad<WindowManager<E, L, A, R>>>,
114+
on_page_load: Box<OnPageLoad<Args<E, L, A, R>>>,
114115

115116
/// windows to create when starting up.
116-
pending_windows: Vec<PendingWindow<WindowManager<E, L, A, R>>>,
117+
pending_windows: Vec<PendingWindow<Args<E, L, A, R>>>,
117118

118119
/// All passed plugins
119-
plugins: PluginStore<WindowManager<E, L, A, R>>,
120+
plugins: PluginStore<Args<E, L, A, R>>,
120121
}
121122

122123
impl<E, L, A, R> Builder<E, L, A, R>
@@ -140,7 +141,7 @@ where
140141
/// Defines the JS message handler callback.
141142
pub fn invoke_handler<F>(mut self, invoke_handler: F) -> Self
142143
where
143-
F: Fn(InvokeMessage<WindowManager<E, L, A, R>>) + Send + Sync + 'static,
144+
F: Fn(InvokeMessage<Args<E, L, A, R>>) + Send + Sync + 'static,
144145
{
145146
self.invoke_handler = Box::new(invoke_handler);
146147
self
@@ -149,9 +150,7 @@ where
149150
/// Defines the setup hook.
150151
pub fn setup<F>(mut self, setup: F) -> Self
151152
where
152-
F: Fn(&mut App<WindowManager<E, L, A, R>>) -> Result<(), Box<dyn std::error::Error>>
153-
+ Send
154-
+ 'static,
153+
F: Fn(&mut App<Args<E, L, A, R>>) -> Result<(), Box<dyn std::error::Error>> + Send + 'static,
155154
{
156155
self.setup = Box::new(setup);
157156
self
@@ -160,14 +159,14 @@ where
160159
/// Defines the page load hook.
161160
pub fn on_page_load<F>(mut self, on_page_load: F) -> Self
162161
where
163-
F: Fn(Window<WindowManager<E, L, A, R>>, PageLoadPayload) + Send + Sync + 'static,
162+
F: Fn(Window<Args<E, L, A, R>>, PageLoadPayload) + Send + Sync + 'static,
164163
{
165164
self.on_page_load = Box::new(on_page_load);
166165
self
167166
}
168167

169168
/// Adds a plugin to the runtime.
170-
pub fn plugin<P: Plugin<WindowManager<E, L, A, R>> + 'static>(mut self, plugin: P) -> Self {
169+
pub fn plugin<P: Plugin<Args<E, L, A, R>> + 'static>(mut self, plugin: P) -> Self {
171170
self.plugins.register(plugin);
172171
self
173172
}

0 commit comments

Comments
 (0)