Skip to content

Commit

Permalink
fix(core): allow wry to be an optional dep again (fix #1841) (#1854)
Browse files Browse the repository at this point in the history
  • Loading branch information
chippers authored May 18, 2021
1 parent 2881ccc commit 3d8dcbb
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 89 deletions.
7 changes: 7 additions & 0 deletions .changes/internal-default-args.md
Original file line number Diff line number Diff line change
@@ -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`.
47 changes: 27 additions & 20 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")]
Expand All @@ -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> {
Expand All @@ -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> {}
Expand All @@ -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> {}
Expand Down
49 changes: 27 additions & 22 deletions core/tauri/src/hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`].
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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> {
Expand Down
106 changes: 74 additions & 32 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>;

Expand Down Expand Up @@ -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> {
Expand Down
9 changes: 5 additions & 4 deletions core/tauri/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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> {
Expand Down
25 changes: 14 additions & 11 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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>,

/// The manager to associate this webview window with.
manager: WindowManager<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>,
}
}

impl<P: Params> Clone for Window<P> {
Expand Down

0 comments on commit 3d8dcbb

Please sign in to comment.