From 3d8dcbbf8188b9e96d6b03dc984ca022eebf53e4 Mon Sep 17 00:00:00 2001 From: chip Date: Mon, 17 May 2021 20:42:02 -0700 Subject: [PATCH] fix(core): allow wry to be an optional dep again (fix #1841) (#1854) --- .changes/internal-default-args.md | 7 ++ core/tauri/src/app.rs | 47 +++++++------ core/tauri/src/hooks.rs | 49 +++++++------- core/tauri/src/manager.rs | 106 +++++++++++++++++++++--------- core/tauri/src/plugin.rs | 9 +-- core/tauri/src/window.rs | 23 ++++--- 6 files changed, 153 insertions(+), 88 deletions(-) create mode 100644 .changes/internal-default-args.md diff --git a/.changes/internal-default-args.md b/.changes/internal-default-args.md new file mode 100644 index 000000000..843ec299b --- /dev/null +++ b/.changes/internal-default-args.md @@ -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`. diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index da58ba702..437afb0f2 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -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 SystemTrayEvent { } } -/// A menu event that was triggered on a window. -#[cfg(feature = "menu")] -#[cfg_attr(doc_cfg, doc(cfg(feature = "menu")))] -pub struct WindowMenuEvent { - pub(crate) menu_item_id: P::MenuId, - pub(crate) window: Window

, +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 { + pub(crate) menu_item_id: P::MenuId, + pub(crate) window: Window

, + } } #[cfg(feature = "menu")] @@ -72,10 +73,12 @@ impl WindowMenuEvent

{ } } -/// A window event that was triggered on the specified window. -pub struct GlobalWindowEvent { - pub(crate) event: WindowEvent, - pub(crate) window: Window

, +crate::manager::default_args! { + /// A window event that was triggered on the specified window. + pub struct GlobalWindowEvent { + pub(crate) event: WindowEvent, + pub(crate) window: Window

, + } } impl GlobalWindowEvent

{ @@ -90,9 +93,11 @@ impl GlobalWindowEvent

{ } } -/// A handle to the currently running application. -pub struct AppHandle { - manager: WindowManager

, +crate::manager::default_args! { + /// A handle to the currently running application. + pub struct AppHandle { + manager: WindowManager

, + } } impl Manager

for AppHandle

{} @@ -102,12 +107,14 @@ impl ManagerBase

for AppHandle

{ } } -/// The instance of the currently running application. -/// -/// This type implements [`Manager`] which allows for manipulation of global application items. -pub struct App { - runtime: P::Runtime, - manager: WindowManager

, +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 { + runtime: P::Runtime, + manager: WindowManager

, + } } impl Manager

for App

{} diff --git a/core/tauri/src/hooks.rs b/core/tauri/src/hooks.rs index 8403c02b5..929abc520 100644 --- a/core/tauri/src/hooks.rs +++ b/core/tauri/src/hooks.rs @@ -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 { - /// The message passed. - pub message: InvokeMessage

, +crate::manager::default_args! { + /// The message and resolver given to a custom command. + pub struct Invoke { + /// The message passed. + pub message: InvokeMessage

, - /// The resolver of the message. - pub resolver: InvokeResolver

, + /// The resolver of the message. + pub resolver: InvokeResolver

, + } } /// Error response from an [`InvokeMessage`]. @@ -111,11 +112,13 @@ impl From for InvokeResponse { } } -/// Resolver of a invoke message. -pub struct InvokeResolver { - window: Window

, - pub(crate) callback: String, - pub(crate) error: String, +crate::manager::default_args! { + /// Resolver of a invoke message. + pub struct InvokeResolver { + window: Window

, + pub(crate) callback: String, + pub(crate) error: String, + } } impl InvokeResolver

{ @@ -229,16 +232,18 @@ impl InvokeResolver

{ } } -/// An invoke message. -pub struct InvokeMessage { - /// The window that received the invoke message. - pub(crate) window: Window

, - /// Application managed state. - pub(crate) state: Arc, - /// 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 { + /// The window that received the invoke message. + pub(crate) window: Window

, + /// Application managed state. + pub(crate) state: Arc, + /// The RPC command. + pub(crate) command: String, + /// The JSON argument passed on the invoke message. + pub(crate) payload: JsonValue, + } } impl InvokeMessage

{ diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index 2cd7ae42f..8552d10b2 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -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(tauri_event: &str) -> Event { }) } -pub struct InnerWindowManager { - windows: Mutex>>, - plugins: Mutex>, - listeners: Listeners, - pub(crate) state: Arc, +crate::manager::default_args! { + pub struct InnerWindowManager { + windows: Mutex>>, + plugins: Mutex>, + listeners: Listeners, + pub(crate) state: Arc, - /// The JS message handler. - invoke_handler: Box>, + /// The JS message handler. + invoke_handler: Box>, - /// The page load hook, invoked when the webview performs a navigation. - on_page_load: Box>, + /// The page load hook, invoked when the webview performs a navigation. + on_page_load: Box>, - config: Arc, - assets: Arc, - default_window_icon: Option>, + config: Arc, + assets: Arc, + default_window_icon: Option>, - /// A list of salts that are valid for the current application. - salts: Mutex>, - package_info: PackageInfo, - /// The webview protocols protocols available to all windows. - uri_scheme_protocols: HashMap>, - /// The menu set to all windows. - #[cfg(feature = "menu")] - menu: Vec>, - /// Maps runtime id to a strongly typed menu id. - #[cfg(feature = "menu")] - menu_ids: HashMap, - /// Menu event listeners to all windows. - #[cfg(feature = "menu")] - menu_event_listeners: Arc>>, - /// Window event listeners to all windows. - window_event_listeners: Arc>>, + /// A list of salts that are valid for the current application. + salts: Mutex>, + package_info: PackageInfo, + /// The webview protocols protocols available to all windows. + uri_scheme_protocols: HashMap>, + /// The menu set to all windows. + #[cfg(feature = "menu")] + menu: Vec>, + /// Maps runtime id to a strongly typed menu id. + #[cfg(feature = "menu")] + menu_ids: HashMap, + /// Menu event listeners to all windows. + #[cfg(feature = "menu")] + menu_event_listeners: Arc>>, + /// Window event listeners to all windows. + window_event_listeners: Arc>>, + } } +/// 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; @@ -151,10 +191,12 @@ impl Params type Runtime = R; } -pub struct WindowManager { - pub inner: Arc>, - #[allow(clippy::type_complexity)] - _marker: Args, +crate::manager::default_args! { + pub struct WindowManager { + pub inner: Arc>, + #[allow(clippy::type_complexity)] + _marker: Args, + } } impl Clone for WindowManager

{ diff --git a/core/tauri/src/plugin.rs b/core/tauri/src/plugin.rs index 27db547c4..383831182 100644 --- a/core/tauri/src/plugin.rs +++ b/core/tauri/src/plugin.rs @@ -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: Send { fn extend_api(&mut self, invoke: Invoke

) {} } -/// Plugin collection type. -pub(crate) struct PluginStore { - store: HashMap<&'static str, Box>>, +crate::manager::default_args! { + /// Plugin collection type. + pub(crate) struct PluginStore { + store: HashMap<&'static str, Box>>, + } } impl Default for PluginStore

{ diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index e2c573a6d..6f58db888 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -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 { - /// The webview window created by the runtime. - window: DetachedWindow

, +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 { + /// The webview window created by the runtime. + /// ok + window: DetachedWindow

, - /// The manager to associate this webview window with. - manager: WindowManager

, + /// The manager to associate this webview window with. + manager: WindowManager

, + } } impl Clone for Window

{