mirror of
https://github.com/tauri-apps/tauri.git
synced 2024-11-28 12:27:16 +03:00
feat(core): add default Args to all types exposing Params (#1777)
This commit is contained in:
parent
b5f8912122
commit
27a7810767
6
.changes/default-params-type.md
Normal file
6
.changes/default-params-type.md
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
Adds the default types used with `Builder::default()` to items that expose `Params` in their type. This allows you to
|
||||
skip specifying a generic parameter to types like `Window<P>` if you use the default type.
|
@ -25,6 +25,7 @@ use crate::runtime::menu::Menu;
|
||||
#[cfg(feature = "system-tray")]
|
||||
use crate::runtime::{menu::SystemTrayMenuItem, Icon};
|
||||
|
||||
use crate::manager::DefaultArgs;
|
||||
#[cfg(feature = "updater")]
|
||||
use crate::updater;
|
||||
|
||||
@ -53,7 +54,7 @@ impl<I: MenuId> SystemTrayEvent<I> {
|
||||
/// A menu event that was triggered on a window.
|
||||
#[cfg(feature = "menu")]
|
||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
|
||||
pub struct WindowMenuEvent<P: Params> {
|
||||
pub struct WindowMenuEvent<P: Params = DefaultArgs> {
|
||||
pub(crate) menu_item_id: P::MenuId,
|
||||
pub(crate) window: Window<P>,
|
||||
}
|
||||
@ -72,7 +73,7 @@ impl<P: Params> WindowMenuEvent<P> {
|
||||
}
|
||||
|
||||
/// A window event that was triggered on the specified window.
|
||||
pub struct GlobalWindowEvent<P: Params> {
|
||||
pub struct GlobalWindowEvent<P: Params = DefaultArgs> {
|
||||
pub(crate) event: WindowEvent,
|
||||
pub(crate) window: Window<P>,
|
||||
}
|
||||
@ -90,7 +91,7 @@ impl<P: Params> GlobalWindowEvent<P> {
|
||||
}
|
||||
|
||||
/// A handle to the currently running application.
|
||||
pub struct AppHandle<P: Params> {
|
||||
pub struct AppHandle<P: Params = DefaultArgs> {
|
||||
manager: WindowManager<P>,
|
||||
}
|
||||
|
||||
@ -104,7 +105,7 @@ impl<P: Params> ManagerBase<P> for AppHandle<P> {
|
||||
/// The instance of the currently running application.
|
||||
///
|
||||
/// This type implements [`Manager`] which allows for manipulation of global application items.
|
||||
pub struct App<P: Params> {
|
||||
pub struct App<P: Params = DefaultArgs> {
|
||||
runtime: P::Runtime,
|
||||
manager: WindowManager<P>,
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use crate::manager::DefaultArgs;
|
||||
use crate::{
|
||||
api::rpc::{format_callback, format_callback_result},
|
||||
app::App,
|
||||
@ -35,7 +36,7 @@ impl PageLoadPayload {
|
||||
}
|
||||
|
||||
/// The message and resolver given to a custom command.
|
||||
pub struct Invoke<P: Params> {
|
||||
pub struct Invoke<P: Params = DefaultArgs> {
|
||||
/// The message passed.
|
||||
pub message: InvokeMessage<P>,
|
||||
|
||||
@ -111,7 +112,7 @@ impl From<InvokeError> for InvokeResponse {
|
||||
}
|
||||
|
||||
/// Resolver of a invoke message.
|
||||
pub struct InvokeResolver<P: Params> {
|
||||
pub struct InvokeResolver<P: Params = DefaultArgs> {
|
||||
window: Window<P>,
|
||||
pub(crate) callback: String,
|
||||
pub(crate) error: String,
|
||||
@ -229,7 +230,7 @@ impl<P: Params> InvokeResolver<P> {
|
||||
}
|
||||
|
||||
/// An invoke message.
|
||||
pub struct InvokeMessage<P: Params> {
|
||||
pub struct InvokeMessage<P: Params = DefaultArgs> {
|
||||
/// The window that received the invoke message.
|
||||
pub(crate) window: Window<P>,
|
||||
/// Application managed state.
|
||||
|
@ -71,7 +71,7 @@ pub(crate) fn tauri_event<Event: Tag>(tauri_event: &str) -> Event {
|
||||
})
|
||||
}
|
||||
|
||||
pub struct InnerWindowManager<P: Params> {
|
||||
pub struct InnerWindowManager<P: Params = DefaultArgs> {
|
||||
windows: Mutex<HashMap<P::Label, Window<P>>>,
|
||||
plugins: Mutex<PluginStore<P>>,
|
||||
listeners: Listeners<P::Event, P::Label>,
|
||||
@ -105,6 +105,10 @@ pub struct InnerWindowManager<P: Params> {
|
||||
window_event_listeners: Arc<Vec<GlobalWindowEventListener<P>>>,
|
||||
}
|
||||
|
||||
/// This type should always match `Builder::default()`, otherwise the default type is useless.
|
||||
pub(crate) type DefaultArgs =
|
||||
Args<String, String, String, String, crate::api::assets::EmbeddedAssets, crate::Wry>;
|
||||
|
||||
/// A [Zero Sized Type] marker representing a full [`Params`].
|
||||
///
|
||||
/// [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
|
||||
type Runtime = R;
|
||||
}
|
||||
|
||||
pub struct WindowManager<P: Params> {
|
||||
pub struct WindowManager<P: Params = DefaultArgs> {
|
||||
pub inner: Arc<InnerWindowManager<P>>,
|
||||
#[allow(clippy::type_complexity)]
|
||||
_marker: Args<P::Event, P::Label, P::MenuId, P::SystemTrayMenuId, P::Assets, P::Runtime>,
|
||||
|
@ -4,6 +4,7 @@
|
||||
|
||||
//! Extend Tauri functionality.
|
||||
|
||||
use crate::manager::DefaultArgs;
|
||||
use crate::{api::config::PluginConfig, App, Invoke, PageLoadPayload, Params, Window};
|
||||
use serde_json::Value as JsonValue;
|
||||
use std::collections::HashMap;
|
||||
@ -45,7 +46,7 @@ pub trait Plugin<P: Params>: Send {
|
||||
}
|
||||
|
||||
/// Plugin collection type.
|
||||
pub(crate) struct PluginStore<P: Params> {
|
||||
pub(crate) struct PluginStore<P: Params = DefaultArgs> {
|
||||
store: HashMap<&'static str, Box<dyn Plugin<P>>>,
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ use crate::{
|
||||
api::config::WindowUrl,
|
||||
command::{CommandArg, CommandItem},
|
||||
event::{Event, EventHandler},
|
||||
manager::WindowManager,
|
||||
manager::{DefaultArgs, WindowManager},
|
||||
runtime::{
|
||||
monitor::Monitor as RuntimeMonitor,
|
||||
tag::{TagRef, ToJsString},
|
||||
@ -96,7 +96,7 @@ impl Monitor {
|
||||
///
|
||||
/// This type also implements [`Manager`] which allows you to manage other windows attached to
|
||||
/// the same application.
|
||||
pub struct Window<P: Params> {
|
||||
pub struct Window<P: Params = DefaultArgs> {
|
||||
/// The webview window created by the runtime.
|
||||
window: DetachedWindow<P>,
|
||||
|
||||
|
@ -26,7 +26,7 @@ enum MyError {
|
||||
|
||||
// ------------------------ Commands using Window ------------------------
|
||||
#[command]
|
||||
fn window_label(window: Window<impl Params<Label = String>>) {
|
||||
fn window_label(window: Window) {
|
||||
println!("window label: {}", window.label());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user