mirror of
https://github.com/tauri-apps/tauri.git
synced 2025-01-02 07:56:20 +03:00
parent
2881ccc329
commit
3d8dcbbf81
7
.changes/internal-default-args.md
Normal file
7
.changes/internal-default-args.md
Normal file
@ -0,0 +1,7 @@
|
||||
---
|
||||
"tauri": patch
|
||||
---
|
||||
|
||||
(internal): allow `wry` dependency to be optional again while keeping default args.
|
||||
code that wishes to expose a struct with a default arg should use the `crate::manager::default_args!` macro to declare
|
||||
the struct, so that it can automatically feature-gate `DefaultArgs` behind using `wry`.
|
@ -25,7 +25,6 @@ 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;
|
||||
|
||||
@ -51,12 +50,14 @@ 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 = DefaultArgs> {
|
||||
pub(crate) menu_item_id: P::MenuId,
|
||||
pub(crate) window: Window<P>,
|
||||
crate::manager::default_args! {
|
||||
/// 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(crate) menu_item_id: P::MenuId,
|
||||
pub(crate) window: Window<P>,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "menu")]
|
||||
@ -72,10 +73,12 @@ impl<P: Params> WindowMenuEvent<P> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A window event that was triggered on the specified window.
|
||||
pub struct GlobalWindowEvent<P: Params = DefaultArgs> {
|
||||
pub(crate) event: WindowEvent,
|
||||
pub(crate) window: Window<P>,
|
||||
crate::manager::default_args! {
|
||||
/// A window event that was triggered on the specified window.
|
||||
pub struct GlobalWindowEvent<P: Params> {
|
||||
pub(crate) event: WindowEvent,
|
||||
pub(crate) window: Window<P>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Params> GlobalWindowEvent<P> {
|
||||
@ -90,9 +93,11 @@ impl<P: Params> GlobalWindowEvent<P> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A handle to the currently running application.
|
||||
pub struct AppHandle<P: Params = DefaultArgs> {
|
||||
manager: WindowManager<P>,
|
||||
crate::manager::default_args! {
|
||||
/// A handle to the currently running application.
|
||||
pub struct AppHandle<P: Params> {
|
||||
manager: WindowManager<P>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Params> Manager<P> for AppHandle<P> {}
|
||||
@ -102,12 +107,14 @@ 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 = DefaultArgs> {
|
||||
runtime: P::Runtime,
|
||||
manager: WindowManager<P>,
|
||||
crate::manager::default_args! {
|
||||
/// The instance of the currently running application.
|
||||
///
|
||||
/// This type implements [`Manager`] which allows for manipulation of global application items.
|
||||
pub struct App<P: Params> {
|
||||
runtime: P::Runtime,
|
||||
manager: WindowManager<P>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Params> Manager<P> for App<P> {}
|
||||
|
@ -2,7 +2,6 @@
|
||||
// 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,13 +34,15 @@ impl PageLoadPayload {
|
||||
}
|
||||
}
|
||||
|
||||
/// The message and resolver given to a custom command.
|
||||
pub struct Invoke<P: Params = DefaultArgs> {
|
||||
/// The message passed.
|
||||
pub message: InvokeMessage<P>,
|
||||
crate::manager::default_args! {
|
||||
/// The message and resolver given to a custom command.
|
||||
pub struct Invoke<P: Params> {
|
||||
/// The message passed.
|
||||
pub message: InvokeMessage<P>,
|
||||
|
||||
/// The resolver of the message.
|
||||
pub resolver: InvokeResolver<P>,
|
||||
/// The resolver of the message.
|
||||
pub resolver: InvokeResolver<P>,
|
||||
}
|
||||
}
|
||||
|
||||
/// Error response from an [`InvokeMessage`].
|
||||
@ -111,11 +112,13 @@ impl From<InvokeError> for InvokeResponse {
|
||||
}
|
||||
}
|
||||
|
||||
/// Resolver of a invoke message.
|
||||
pub struct InvokeResolver<P: Params = DefaultArgs> {
|
||||
window: Window<P>,
|
||||
pub(crate) callback: String,
|
||||
pub(crate) error: String,
|
||||
crate::manager::default_args! {
|
||||
/// Resolver of a invoke message.
|
||||
pub struct InvokeResolver<P: Params> {
|
||||
window: Window<P>,
|
||||
pub(crate) callback: String,
|
||||
pub(crate) error: String,
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Params> InvokeResolver<P> {
|
||||
@ -229,16 +232,18 @@ impl<P: Params> InvokeResolver<P> {
|
||||
}
|
||||
}
|
||||
|
||||
/// An invoke message.
|
||||
pub struct InvokeMessage<P: Params = DefaultArgs> {
|
||||
/// The window that received the invoke message.
|
||||
pub(crate) window: Window<P>,
|
||||
/// Application managed state.
|
||||
pub(crate) state: Arc<StateManager>,
|
||||
/// The RPC command.
|
||||
pub(crate) command: String,
|
||||
/// The JSON argument passed on the invoke message.
|
||||
pub(crate) payload: JsonValue,
|
||||
crate::manager::default_args! {
|
||||
/// An invoke message.
|
||||
pub struct InvokeMessage<P: Params> {
|
||||
/// The window that received the invoke message.
|
||||
pub(crate) window: Window<P>,
|
||||
/// Application managed state.
|
||||
pub(crate) state: Arc<StateManager>,
|
||||
/// The RPC command.
|
||||
pub(crate) command: String,
|
||||
/// The JSON argument passed on the invoke message.
|
||||
pub(crate) payload: JsonValue,
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Params> InvokeMessage<P> {
|
||||
|
@ -2,6 +2,9 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// we re-export the default_args! macro as pub(crate) so we can use it easily from other modules
|
||||
#![allow(clippy::single_component_path_imports)]
|
||||
|
||||
use crate::{
|
||||
api::{
|
||||
assets::Assets,
|
||||
@ -71,41 +74,78 @@ pub(crate) fn tauri_event<Event: Tag>(tauri_event: &str) -> Event {
|
||||
})
|
||||
}
|
||||
|
||||
pub struct InnerWindowManager<P: Params = DefaultArgs> {
|
||||
windows: Mutex<HashMap<P::Label, Window<P>>>,
|
||||
plugins: Mutex<PluginStore<P>>,
|
||||
listeners: Listeners<P::Event, P::Label>,
|
||||
pub(crate) state: Arc<StateManager>,
|
||||
crate::manager::default_args! {
|
||||
pub struct InnerWindowManager<P: Params> {
|
||||
windows: Mutex<HashMap<P::Label, Window<P>>>,
|
||||
plugins: Mutex<PluginStore<P>>,
|
||||
listeners: Listeners<P::Event, P::Label>,
|
||||
pub(crate) state: Arc<StateManager>,
|
||||
|
||||
/// The JS message handler.
|
||||
invoke_handler: Box<InvokeHandler<P>>,
|
||||
/// The JS message handler.
|
||||
invoke_handler: Box<InvokeHandler<P>>,
|
||||
|
||||
/// The page load hook, invoked when the webview performs a navigation.
|
||||
on_page_load: Box<OnPageLoad<P>>,
|
||||
/// The page load hook, invoked when the webview performs a navigation.
|
||||
on_page_load: Box<OnPageLoad<P>>,
|
||||
|
||||
config: Arc<Config>,
|
||||
assets: Arc<P::Assets>,
|
||||
default_window_icon: Option<Vec<u8>>,
|
||||
config: Arc<Config>,
|
||||
assets: Arc<P::Assets>,
|
||||
default_window_icon: Option<Vec<u8>>,
|
||||
|
||||
/// A list of salts that are valid for the current application.
|
||||
salts: Mutex<HashSet<Uuid>>,
|
||||
package_info: PackageInfo,
|
||||
/// The webview protocols protocols available to all windows.
|
||||
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>,
|
||||
/// The menu set to all windows.
|
||||
#[cfg(feature = "menu")]
|
||||
menu: Vec<Menu<P::MenuId>>,
|
||||
/// Maps runtime id to a strongly typed menu id.
|
||||
#[cfg(feature = "menu")]
|
||||
menu_ids: HashMap<u32, P::MenuId>,
|
||||
/// Menu event listeners to all windows.
|
||||
#[cfg(feature = "menu")]
|
||||
menu_event_listeners: Arc<Vec<GlobalMenuEventListener<P>>>,
|
||||
/// Window event listeners to all windows.
|
||||
window_event_listeners: Arc<Vec<GlobalWindowEventListener<P>>>,
|
||||
/// A list of salts that are valid for the current application.
|
||||
salts: Mutex<HashSet<Uuid>>,
|
||||
package_info: PackageInfo,
|
||||
/// The webview protocols protocols available to all windows.
|
||||
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>,
|
||||
/// The menu set to all windows.
|
||||
#[cfg(feature = "menu")]
|
||||
menu: Vec<Menu<P::MenuId>>,
|
||||
/// Maps runtime id to a strongly typed menu id.
|
||||
#[cfg(feature = "menu")]
|
||||
menu_ids: HashMap<u32, P::MenuId>,
|
||||
/// Menu event listeners to all windows.
|
||||
#[cfg(feature = "menu")]
|
||||
menu_event_listeners: Arc<Vec<GlobalMenuEventListener<P>>>,
|
||||
/// Window event listeners to all windows.
|
||||
window_event_listeners: Arc<Vec<GlobalWindowEventListener<P>>>,
|
||||
}
|
||||
}
|
||||
|
||||
/// struct declaration using params + default args which includes optional feature wry
|
||||
macro_rules! default_args {
|
||||
(
|
||||
$(#[$attrs_struct:meta])*
|
||||
$vis_struct:vis struct $name:ident<$p:ident: $params:ident> {
|
||||
$(
|
||||
$(#[$attrs_field:meta])*
|
||||
$vis_field:vis $field:ident: $field_type:ty,
|
||||
)*
|
||||
}
|
||||
) => {
|
||||
$(#[$attrs_struct])*
|
||||
#[cfg(feature = "wry")]
|
||||
$vis_struct struct $name<$p: $params = crate::manager::DefaultArgs> {
|
||||
$(
|
||||
$(#[$attrs_field])*
|
||||
$vis_field $field: $field_type,
|
||||
)*
|
||||
}
|
||||
|
||||
$(#[$attrs_struct])*
|
||||
#[cfg(not(feature = "wry"))]
|
||||
$vis_struct struct $name<$p: $params> {
|
||||
$(
|
||||
$(#[$attrs_field])*
|
||||
$vis_field $field: $field_type,
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// export it to allow use from other modules
|
||||
pub(crate) use default_args;
|
||||
|
||||
/// This type should always match `Builder::default()`, otherwise the default type is useless.
|
||||
#[cfg(feature = "wry")]
|
||||
pub(crate) type DefaultArgs =
|
||||
Args<String, String, String, String, crate::api::assets::EmbeddedAssets, crate::Wry>;
|
||||
|
||||
@ -151,10 +191,12 @@ impl<E: Tag, L: Tag, MID: MenuId, TID: MenuId, A: Assets, R: Runtime> Params
|
||||
type Runtime = R;
|
||||
}
|
||||
|
||||
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>,
|
||||
crate::manager::default_args! {
|
||||
pub struct WindowManager<P: Params> {
|
||||
pub inner: Arc<InnerWindowManager<P>>,
|
||||
#[allow(clippy::type_complexity)]
|
||||
_marker: Args<P::Event, P::Label, P::MenuId, P::SystemTrayMenuId, P::Assets, P::Runtime>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Params> Clone for WindowManager<P> {
|
||||
|
@ -4,7 +4,6 @@
|
||||
|
||||
//! 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,9 +44,11 @@ pub trait Plugin<P: Params>: Send {
|
||||
fn extend_api(&mut self, invoke: Invoke<P>) {}
|
||||
}
|
||||
|
||||
/// Plugin collection type.
|
||||
pub(crate) struct PluginStore<P: Params = DefaultArgs> {
|
||||
store: HashMap<&'static str, Box<dyn Plugin<P>>>,
|
||||
crate::manager::default_args! {
|
||||
/// Plugin collection type.
|
||||
pub(crate) struct PluginStore<P: Params> {
|
||||
store: HashMap<&'static str, Box<dyn Plugin<P>>>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Params> Default for PluginStore<P> {
|
||||
|
@ -8,7 +8,7 @@ use crate::{
|
||||
api::config::WindowUrl,
|
||||
command::{CommandArg, CommandItem},
|
||||
event::{Event, EventHandler},
|
||||
manager::{DefaultArgs, WindowManager},
|
||||
manager::WindowManager,
|
||||
runtime::{
|
||||
monitor::Monitor as RuntimeMonitor,
|
||||
tag::{TagRef, ToJsString},
|
||||
@ -92,16 +92,19 @@ impl Monitor {
|
||||
}
|
||||
|
||||
// TODO: expand these docs since this is a pretty important type
|
||||
/// A webview window managed by Tauri.
|
||||
///
|
||||
/// This type also implements [`Manager`] which allows you to manage other windows attached to
|
||||
/// the same application.
|
||||
pub struct Window<P: Params = DefaultArgs> {
|
||||
/// The webview window created by the runtime.
|
||||
window: DetachedWindow<P>,
|
||||
crate::manager::default_args! {
|
||||
/// A webview window managed by Tauri.
|
||||
///
|
||||
/// This type also implements [`Manager`] which allows you to manage other windows attached to
|
||||
/// the same application.
|
||||
pub struct Window<P: Params> {
|
||||
/// The webview window created by the runtime.
|
||||
/// ok
|
||||
window: DetachedWindow<P>,
|
||||
|
||||
/// The manager to associate this webview window with.
|
||||
manager: WindowManager<P>,
|
||||
/// The manager to associate this webview window with.
|
||||
manager: WindowManager<P>,
|
||||
}
|
||||
}
|
||||
|
||||
impl<P: Params> Clone for Window<P> {
|
||||
|
Loading…
Reference in New Issue
Block a user