fix(core): allow wry to be an optional dep again (fix #1841) (#1854)

This commit is contained in:
chip 2021-05-17 20:42:02 -07:00 committed by GitHub
parent 2881ccc329
commit 3d8dcbbf81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 153 additions and 88 deletions

View 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`.

View File

@ -25,7 +25,6 @@ use crate::runtime::menu::Menu;
#[cfg(feature = "system-tray")] #[cfg(feature = "system-tray")]
use crate::runtime::{menu::SystemTrayMenuItem, Icon}; use crate::runtime::{menu::SystemTrayMenuItem, Icon};
use crate::manager::DefaultArgs;
#[cfg(feature = "updater")] #[cfg(feature = "updater")]
use crate::updater; use crate::updater;
@ -51,12 +50,14 @@ impl<I: MenuId> SystemTrayEvent<I> {
} }
} }
/// A menu event that was triggered on a window. crate::manager::default_args! {
#[cfg(feature = "menu")] /// A menu event that was triggered on a window.
#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))] #[cfg(feature = "menu")]
pub struct WindowMenuEvent<P: Params = DefaultArgs> { #[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))]
pub(crate) menu_item_id: P::MenuId, pub struct WindowMenuEvent<P: Params> {
pub(crate) window: Window<P>, pub(crate) menu_item_id: P::MenuId,
pub(crate) window: Window<P>,
}
} }
#[cfg(feature = "menu")] #[cfg(feature = "menu")]
@ -72,10 +73,12 @@ impl<P: Params> WindowMenuEvent<P> {
} }
} }
/// A window event that was triggered on the specified window. crate::manager::default_args! {
pub struct GlobalWindowEvent<P: Params = DefaultArgs> { /// A window event that was triggered on the specified window.
pub(crate) event: WindowEvent, pub struct GlobalWindowEvent<P: Params> {
pub(crate) window: Window<P>, pub(crate) event: WindowEvent,
pub(crate) window: Window<P>,
}
} }
impl<P: Params> GlobalWindowEvent<P> { impl<P: Params> GlobalWindowEvent<P> {
@ -90,9 +93,11 @@ impl<P: Params> GlobalWindowEvent<P> {
} }
} }
/// A handle to the currently running application. crate::manager::default_args! {
pub struct AppHandle<P: Params = DefaultArgs> { /// A handle to the currently running application.
manager: WindowManager<P>, pub struct AppHandle<P: Params> {
manager: WindowManager<P>,
}
} }
impl<P: Params> Manager<P> for AppHandle<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. 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 = DefaultArgs> { /// This type implements [`Manager`] which allows for manipulation of global application items.
runtime: P::Runtime, pub struct App<P: Params> {
manager: WindowManager<P>, runtime: P::Runtime,
manager: WindowManager<P>,
}
} }
impl<P: Params> Manager<P> for App<P> {} impl<P: Params> Manager<P> for App<P> {}

View File

@ -2,7 +2,6 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
use crate::manager::DefaultArgs;
use crate::{ use crate::{
api::rpc::{format_callback, format_callback_result}, api::rpc::{format_callback, format_callback_result},
app::App, app::App,
@ -35,13 +34,15 @@ impl PageLoadPayload {
} }
} }
/// The message and resolver given to a custom command. crate::manager::default_args! {
pub struct Invoke<P: Params = DefaultArgs> { /// The message and resolver given to a custom command.
/// The message passed. pub struct Invoke<P: Params> {
pub message: InvokeMessage<P>, /// The message passed.
pub message: InvokeMessage<P>,
/// The resolver of the message. /// The resolver of the message.
pub resolver: InvokeResolver<P>, pub resolver: InvokeResolver<P>,
}
} }
/// Error response from an [`InvokeMessage`]. /// Error response from an [`InvokeMessage`].
@ -111,11 +112,13 @@ impl From<InvokeError> for InvokeResponse {
} }
} }
/// Resolver of a invoke message. crate::manager::default_args! {
pub struct InvokeResolver<P: Params = DefaultArgs> { /// Resolver of a invoke message.
window: Window<P>, pub struct InvokeResolver<P: Params> {
pub(crate) callback: String, window: Window<P>,
pub(crate) error: String, pub(crate) callback: String,
pub(crate) error: String,
}
} }
impl<P: Params> InvokeResolver<P> { impl<P: Params> InvokeResolver<P> {
@ -229,16 +232,18 @@ impl<P: Params> InvokeResolver<P> {
} }
} }
/// An invoke message. crate::manager::default_args! {
pub struct InvokeMessage<P: Params = DefaultArgs> { /// An invoke message.
/// The window that received the invoke message. pub struct InvokeMessage<P: Params> {
pub(crate) window: Window<P>, /// The window that received the invoke message.
/// Application managed state. pub(crate) window: Window<P>,
pub(crate) state: Arc<StateManager>, /// Application managed state.
/// The RPC command. pub(crate) state: Arc<StateManager>,
pub(crate) command: String, /// The RPC command.
/// The JSON argument passed on the invoke message. pub(crate) command: String,
pub(crate) payload: JsonValue, /// The JSON argument passed on the invoke message.
pub(crate) payload: JsonValue,
}
} }
impl<P: Params> InvokeMessage<P> { impl<P: Params> InvokeMessage<P> {

View File

@ -2,6 +2,9 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT // 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::{ use crate::{
api::{ api::{
assets::Assets, assets::Assets,
@ -71,41 +74,78 @@ pub(crate) fn tauri_event<Event: Tag>(tauri_event: &str) -> Event {
}) })
} }
pub struct InnerWindowManager<P: Params = DefaultArgs> { crate::manager::default_args! {
windows: Mutex<HashMap<P::Label, Window<P>>>, pub struct InnerWindowManager<P: Params> {
plugins: Mutex<PluginStore<P>>, windows: Mutex<HashMap<P::Label, Window<P>>>,
listeners: Listeners<P::Event, P::Label>, plugins: Mutex<PluginStore<P>>,
pub(crate) state: Arc<StateManager>, listeners: Listeners<P::Event, P::Label>,
pub(crate) state: Arc<StateManager>,
/// The JS message handler. /// The JS message handler.
invoke_handler: Box<InvokeHandler<P>>, invoke_handler: Box<InvokeHandler<P>>,
/// The page load hook, invoked when the webview performs a navigation. /// The page load hook, invoked when the webview performs a navigation.
on_page_load: Box<OnPageLoad<P>>, on_page_load: Box<OnPageLoad<P>>,
config: Arc<Config>, config: Arc<Config>,
assets: Arc<P::Assets>, assets: Arc<P::Assets>,
default_window_icon: Option<Vec<u8>>, default_window_icon: Option<Vec<u8>>,
/// A list of salts that are valid for the current application. /// A list of salts that are valid for the current application.
salts: Mutex<HashSet<Uuid>>, salts: Mutex<HashSet<Uuid>>,
package_info: PackageInfo, package_info: PackageInfo,
/// The webview protocols protocols available to all windows. /// The webview protocols protocols available to all windows.
uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>, uri_scheme_protocols: HashMap<String, Arc<CustomProtocol>>,
/// The menu set to all windows. /// The menu set to all windows.
#[cfg(feature = "menu")] #[cfg(feature = "menu")]
menu: Vec<Menu<P::MenuId>>, menu: Vec<Menu<P::MenuId>>,
/// Maps runtime id to a strongly typed menu id. /// Maps runtime id to a strongly typed menu id.
#[cfg(feature = "menu")] #[cfg(feature = "menu")]
menu_ids: HashMap<u32, P::MenuId>, menu_ids: HashMap<u32, P::MenuId>,
/// Menu event listeners to all windows. /// Menu event listeners to all windows.
#[cfg(feature = "menu")] #[cfg(feature = "menu")]
menu_event_listeners: Arc<Vec<GlobalMenuEventListener<P>>>, menu_event_listeners: Arc<Vec<GlobalMenuEventListener<P>>>,
/// Window event listeners to all windows. /// Window event listeners to all windows.
window_event_listeners: Arc<Vec<GlobalWindowEventListener<P>>>, 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. /// This type should always match `Builder::default()`, otherwise the default type is useless.
#[cfg(feature = "wry")]
pub(crate) type DefaultArgs = pub(crate) type DefaultArgs =
Args<String, String, String, String, crate::api::assets::EmbeddedAssets, crate::Wry>; 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; type Runtime = R;
} }
pub struct WindowManager<P: Params = DefaultArgs> { crate::manager::default_args! {
pub inner: Arc<InnerWindowManager<P>>, pub struct WindowManager<P: Params> {
#[allow(clippy::type_complexity)] pub inner: Arc<InnerWindowManager<P>>,
_marker: Args<P::Event, P::Label, P::MenuId, P::SystemTrayMenuId, P::Assets, P::Runtime>, #[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> { impl<P: Params> Clone for WindowManager<P> {

View File

@ -4,7 +4,6 @@
//! Extend Tauri functionality. //! Extend Tauri functionality.
use crate::manager::DefaultArgs;
use crate::{api::config::PluginConfig, App, Invoke, PageLoadPayload, Params, Window}; use crate::{api::config::PluginConfig, App, Invoke, PageLoadPayload, Params, Window};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use std::collections::HashMap; use std::collections::HashMap;
@ -45,9 +44,11 @@ pub trait Plugin<P: Params>: Send {
fn extend_api(&mut self, invoke: Invoke<P>) {} fn extend_api(&mut self, invoke: Invoke<P>) {}
} }
/// Plugin collection type. crate::manager::default_args! {
pub(crate) struct PluginStore<P: Params = DefaultArgs> { /// Plugin collection type.
store: HashMap<&'static str, Box<dyn Plugin<P>>>, pub(crate) struct PluginStore<P: Params> {
store: HashMap<&'static str, Box<dyn Plugin<P>>>,
}
} }
impl<P: Params> Default for PluginStore<P> { impl<P: Params> Default for PluginStore<P> {

View File

@ -8,7 +8,7 @@ use crate::{
api::config::WindowUrl, api::config::WindowUrl,
command::{CommandArg, CommandItem}, command::{CommandArg, CommandItem},
event::{Event, EventHandler}, event::{Event, EventHandler},
manager::{DefaultArgs, WindowManager}, manager::WindowManager,
runtime::{ runtime::{
monitor::Monitor as RuntimeMonitor, monitor::Monitor as RuntimeMonitor,
tag::{TagRef, ToJsString}, tag::{TagRef, ToJsString},
@ -92,16 +92,19 @@ impl Monitor {
} }
// TODO: expand these docs since this is a pretty important type // TODO: expand these docs since this is a pretty important type
/// A webview window managed by Tauri. 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. /// This type also implements [`Manager`] which allows you to manage other windows attached to
pub struct Window<P: Params = DefaultArgs> { /// the same application.
/// The webview window created by the runtime. pub struct Window<P: Params> {
window: DetachedWindow<P>, /// The webview window created by the runtime.
/// ok
window: DetachedWindow<P>,
/// The manager to associate this webview window with. /// The manager to associate this webview window with.
manager: WindowManager<P>, manager: WindowManager<P>,
}
} }
impl<P: Params> Clone for Window<P> { impl<P: Params> Clone for Window<P> {