Skip to content

Commit fd8fab5

Browse files
authored
refactor(core): remove Params and replace with strings (#2191)
* refactor(core): remove `Params` and replace with strings * add tauri-utils to changelog * update default runtime macro to accept type and feature * remove accidental default feature addition * remove changefile todo items that have no futher action * fix clippy warning * update changefile * finish change file * fix splashscreen example * fix markdown typo [skip ci] * remove final uses of `Params` * add license header to new runtime module in tauri-macros * update plugin guide to use runtime instead of params
1 parent 074caa3 commit fd8fab5

File tree

46 files changed

+855
-1409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+855
-1409
lines changed

.changes/weak-typing.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
"tauri": patch
3+
"tauri-runtime": patch
4+
"tauri-runtime-wry": patch
5+
"tauri-macros": patch
6+
"tauri-utils": patch
7+
---
8+
9+
`Params` has been removed, along with all the associated types on it. Functions that previously accepted those
10+
associated types now accept strings instead. Type that used a generic parameter `Params` now use `Runtime` instead. If
11+
you use the `wry` feature, then types with a `Runtime` generic parameter should default to `Wry`, letting you omit the
12+
explicit type and let the compiler infer it instead.
13+
14+
`tauri`:
15+
16+
* See `Params` note
17+
* If you were using `Params` inside a function parameter or definition, all references to it have been replaced with a
18+
simple runtime that defaults to `Wry`. If you are not using a custom runtime, just remove `Params` from the definition
19+
of functions/items that previously took it. If you are using a custom runtime, you _may_ need to pass the runtime type
20+
to these functions.
21+
* If you were using custom types for `Params` (uncommon and if you don't understand you probably were not using it), all
22+
methods that were previously taking the custom type now takes an `Into<String>` or a `&str`. The types were already
23+
required to be string-able, so just make sure to convert it into a string before passing it in if this breaking change
24+
affects you.
25+
26+
`tauri-macros`:
27+
28+
* (internal) Added private `default_runtime` proc macro to allow us to give item definitions a custom runtime only when
29+
the specified feature is enabled.
30+
31+
`tauri-runtime`:
32+
33+
* See `Params` note
34+
* Removed `Params`, `MenuId`, `Tag`, `TagRef`.
35+
* Added `menu::{MenuHash, MenuId, MenuIdRef}` as type aliases for the internal type that menu types now use.
36+
* All previous menu items that had a `MenuId` generic now use the underlying `MenuId` type without a generic.
37+
* `Runtime`, `RuntimeHandle`, and `Dispatch` have no more generic parameter on `create_window(...)` and instead use the
38+
`Runtime` type directly
39+
* `Runtime::system_tray` has no more `MenuId` generic and uses the string based `SystemTray` type directly.
40+
* (internal) `CustomMenuItem::id_value()` is now hashed on creation and exposed as the `id` field with type `MenuHash`.
41+
42+
`tauri-runtime-wry`:
43+
44+
* See `Params` note
45+
* update menu and runtime related types to the ones changed in `tauri-runtime`.
46+
47+
`tauri-utils`:
48+
49+
* `Assets::get` signature has changed to take a `&AssetKey` instead of `impl Into<AssetKey>` to become trait object
50+
safe.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ members = [
1515
"examples/helloworld/src-tauri",
1616
"examples/multiwindow/src-tauri",
1717
"examples/navigation/src-tauri",
18-
"examples/params/src-tauri",
1918
"examples/splashscreen/src-tauri",
2019
"examples/state/src-tauri",
2120
"examples/sidecar/src-tauri",

core/tauri-macros/src/lib.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
extern crate proc_macro;
66
use crate::context::ContextItems;
77
use proc_macro::TokenStream;
8-
use syn::parse_macro_input;
8+
use syn::{parse_macro_input, DeriveInput};
99

1010
mod command;
11+
mod runtime;
1112

1213
#[macro_use]
1314
mod context;
@@ -60,3 +61,16 @@ pub fn generate_context(items: TokenStream) -> TokenStream {
6061
let path = parse_macro_input!(items as ContextItems);
6162
context::generate_context(path).into()
6263
}
64+
65+
/// Adds the default type for the last parameter (assumed to be runtime) for a specific feature.
66+
///
67+
/// e.g. To default the runtime generic to type `crate::Wry` when the `wry` feature is enabled, the
68+
/// syntax would look like `#[default_runtime(crate::Wry, wry)`. This is **always** set for the last
69+
/// generic, so make sure the last generic is the runtime when using this macro.
70+
#[doc(hidden)]
71+
#[proc_macro_attribute]
72+
pub fn default_runtime(attributes: TokenStream, input: TokenStream) -> TokenStream {
73+
let attributes = parse_macro_input!(attributes as runtime::Attributes);
74+
let input = parse_macro_input!(input as DeriveInput);
75+
runtime::default_runtime(attributes, input).into()
76+
}

core/tauri-macros/src/runtime.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
use proc_macro2::TokenStream;
6+
use quote::quote;
7+
use syn::parse::{Parse, ParseStream};
8+
use syn::{parse_quote, DeriveInput, GenericParam, Ident, Token, Type, TypeParam};
9+
10+
/// The default runtime type to enable when the provided feature is enabled.
11+
pub(crate) struct Attributes {
12+
default_type: Type,
13+
feature: Ident,
14+
}
15+
16+
impl Parse for Attributes {
17+
fn parse(input: ParseStream) -> syn::Result<Self> {
18+
let default_type = input.parse()?;
19+
input.parse::<Token![,]>()?;
20+
Ok(Attributes {
21+
default_type,
22+
feature: input.parse()?,
23+
})
24+
}
25+
}
26+
27+
pub(crate) fn default_runtime(attributes: Attributes, input: DeriveInput) -> TokenStream {
28+
// create a new copy to manipulate for the wry feature flag
29+
let mut wry = input.clone();
30+
let wry_runtime = wry
31+
.generics
32+
.params
33+
.last_mut()
34+
.expect("default_runtime requires the item to have at least 1 generic parameter");
35+
36+
// set the default value of the last generic parameter to the provided runtime type
37+
match wry_runtime {
38+
GenericParam::Type(
39+
param @ TypeParam {
40+
eq_token: None,
41+
default: None,
42+
..
43+
},
44+
) => {
45+
param.eq_token = Some(parse_quote!(=));
46+
param.default = Some(attributes.default_type);
47+
}
48+
_ => {
49+
panic!("DefaultRuntime requires the last parameter to not have a default value")
50+
}
51+
};
52+
53+
let feature = attributes.feature.to_string();
54+
55+
quote!(
56+
#[cfg(feature = #feature)]
57+
#wry
58+
59+
#[cfg(not(feature = #feature))]
60+
#input
61+
)
62+
}

core/tauri-runtime-wry/src/lib.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use tauri_runtime::{
1313
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
1414
DetachedWindow, PendingWindow, WindowEvent,
1515
},
16-
ClipboardManager, Dispatch, Error, GlobalShortcutManager, Icon, Params, Result, RunEvent,
17-
RunIteration, Runtime, RuntimeHandle, UserAttentionType,
16+
ClipboardManager, Dispatch, Error, GlobalShortcutManager, Icon, Result, RunEvent, RunIteration,
17+
Runtime, RuntimeHandle, UserAttentionType,
1818
};
1919

2020
#[cfg(feature = "menu")]
@@ -407,7 +407,7 @@ pub struct WindowBuilderWrapper {
407407
inner: WryWindowBuilder,
408408
center: bool,
409409
#[cfg(feature = "menu")]
410-
menu: Menu<u16>,
410+
menu: Menu,
411411
}
412412

413413
// safe since `menu_items` are read only here
@@ -454,7 +454,7 @@ impl WindowBuilder for WindowBuilderWrapper {
454454
}
455455

456456
#[cfg(feature = "menu")]
457-
fn menu<I: MenuId>(mut self, menu: Menu<I>) -> Self {
457+
fn menu(mut self, menu: Menu) -> Self {
458458
self.menu = convert_menu_id(Menu::new(), menu);
459459
self
460460
}
@@ -898,10 +898,10 @@ impl Dispatch for WryDispatcher {
898898

899899
// Creates a window by dispatching a message to the event loop.
900900
// Note that this must be called from a separate thread, otherwise the channel will introduce a deadlock.
901-
fn create_window<P: Params<Runtime = Self::Runtime>>(
901+
fn create_window(
902902
&mut self,
903-
pending: PendingWindow<P>,
904-
) -> Result<DetachedWindow<P>> {
903+
pending: PendingWindow<Self::Runtime>,
904+
) -> Result<DetachedWindow<Self::Runtime>> {
905905
let (tx, rx) = channel();
906906
let label = pending.label.clone();
907907
let context = self.context.clone();
@@ -1203,10 +1203,10 @@ impl RuntimeHandle for WryHandle {
12031203

12041204
// Creates a window by dispatching a message to the event loop.
12051205
// Note that this must be called from a separate thread, otherwise the channel will introduce a deadlock.
1206-
fn create_window<P: Params<Runtime = Self::Runtime>>(
1206+
fn create_window(
12071207
&self,
1208-
pending: PendingWindow<P>,
1209-
) -> Result<DetachedWindow<P>> {
1208+
pending: PendingWindow<Self::Runtime>,
1209+
) -> Result<DetachedWindow<Self::Runtime>> {
12101210
let (tx, rx) = channel();
12111211
let label = pending.label.clone();
12121212
let dispatcher_context = self.dispatcher_context.clone();
@@ -1306,10 +1306,7 @@ impl Runtime for Wry {
13061306
self.clipboard_manager_handle.clone()
13071307
}
13081308

1309-
fn create_window<P: Params<Runtime = Self>>(
1310-
&self,
1311-
pending: PendingWindow<P>,
1312-
) -> Result<DetachedWindow<P>> {
1309+
fn create_window(&self, pending: PendingWindow<Self>) -> Result<DetachedWindow<Self>> {
13131310
let label = pending.label.clone();
13141311
let proxy = self.event_loop.create_proxy();
13151312
let webview = create_webview(
@@ -1347,7 +1344,7 @@ impl Runtime for Wry {
13471344
}
13481345

13491346
#[cfg(feature = "system-tray")]
1350-
fn system_tray<I: MenuId>(&self, system_tray: SystemTray<I>) -> Result<Self::TrayHandler> {
1347+
fn system_tray(&self, system_tray: SystemTray) -> Result<Self::TrayHandler> {
13511348
let icon = system_tray
13521349
.icon
13531350
.expect("tray icon not set")
@@ -1904,10 +1901,10 @@ fn center_window(window: &Window) -> Result<()> {
19041901
}
19051902
}
19061903

1907-
fn create_webview<P: Params<Runtime = Wry>>(
1904+
fn create_webview(
19081905
event_loop: &EventLoopWindowTarget<Message>,
19091906
context: DispatcherContext,
1910-
pending: PendingWindow<P>,
1907+
pending: PendingWindow<Wry>,
19111908
) -> Result<WebviewWrapper> {
19121909
#[allow(unused_mut)]
19131910
let PendingWindow {
@@ -1983,7 +1980,7 @@ fn create_webview<P: Params<Runtime = Wry>>(
19831980
.map_err(|e| Error::CreateWebview(Box::new(e)))?;
19841981

19851982
Ok(WebviewWrapper {
1986-
label: format!("{}", label),
1983+
label,
19871984
inner: webview,
19881985
#[cfg(feature = "menu")]
19891986
menu_items,
@@ -1993,10 +1990,10 @@ fn create_webview<P: Params<Runtime = Wry>>(
19931990
}
19941991

19951992
/// Create a wry rpc handler from a tauri rpc handler.
1996-
fn create_rpc_handler<P: Params<Runtime = Wry>>(
1993+
fn create_rpc_handler(
19971994
context: DispatcherContext,
1998-
label: P::Label,
1999-
handler: WebviewRpcHandler<P>,
1995+
label: String,
1996+
handler: WebviewRpcHandler<Wry>,
20001997
) -> Box<dyn Fn(&Window, WryRpcRequest) -> Option<RpcResponse> + 'static> {
20011998
Box::new(move |window, request| {
20021999
handler(
@@ -2014,10 +2011,10 @@ fn create_rpc_handler<P: Params<Runtime = Wry>>(
20142011
}
20152012

20162013
/// Create a wry file drop handler from a tauri file drop handler.
2017-
fn create_file_drop_handler<P: Params<Runtime = Wry>>(
2014+
fn create_file_drop_handler(
20182015
context: DispatcherContext,
2019-
label: P::Label,
2020-
handler: FileDropHandler<P>,
2016+
label: String,
2017+
handler: FileDropHandler<Wry>,
20212018
) -> Box<dyn Fn(&Window, WryFileDropEvent) -> bool + 'static> {
20222019
Box::new(move |window, event| {
20232020
handler(

core/tauri-runtime-wry/src/menu.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use tauri_runtime::{
88
SystemTrayMenuEntry, SystemTrayMenuItem, TrayHandle,
99
},
1010
window::MenuEvent,
11-
Icon, MenuId, SystemTrayEvent,
11+
Icon, SystemTrayEvent,
1212
};
1313
pub use wry::application::{
1414
event::TrayEvent,
@@ -31,6 +31,9 @@ pub use wry::application::platform::macos::{
3131
#[cfg(feature = "system-tray")]
3232
use crate::{Error, Message, Result, TrayMessage};
3333

34+
#[cfg(feature = "menu")]
35+
use tauri_runtime::menu::MenuHash;
36+
3437
use uuid::Uuid;
3538

3639
use std::{
@@ -145,12 +148,12 @@ impl From<NativeImage> for NativeImageWrapper {
145148

146149
pub struct MenuItemAttributesWrapper<'a>(pub WryMenuItemAttributes<'a>);
147150

148-
impl<'a, I: MenuId> From<&'a CustomMenuItem<I>> for MenuItemAttributesWrapper<'a> {
149-
fn from(item: &'a CustomMenuItem<I>) -> Self {
151+
impl<'a> From<&'a CustomMenuItem> for MenuItemAttributesWrapper<'a> {
152+
fn from(item: &'a CustomMenuItem) -> Self {
150153
let mut attributes = WryMenuItemAttributes::new(&item.title)
151154
.with_enabled(item.enabled)
152155
.with_selected(item.selected)
153-
.with_id(WryMenuId(item.id_value()));
156+
.with_id(WryMenuId(item.id));
154157
if let Some(accelerator) = item.keyboard_accelerator.as_ref() {
155158
attributes = attributes.with_accelerators(&accelerator.parse().expect("invalid accelerator"));
156159
}
@@ -195,11 +198,11 @@ impl From<SystemTrayMenuItem> for MenuItemWrapper {
195198
}
196199

197200
#[cfg(feature = "menu")]
198-
pub fn convert_menu_id<I: MenuId>(mut new_menu: Menu<u16>, menu: Menu<I>) -> Menu<u16> {
201+
pub fn convert_menu_id(mut new_menu: Menu, menu: Menu) -> Menu {
199202
for item in menu.items {
200203
match item {
201204
MenuEntry::CustomItem(c) => {
202-
let mut item = CustomMenuItem::new(c.id_value(), c.title);
205+
let mut item = CustomMenuItem::new(c.id_str, c.title);
203206
#[cfg(target_os = "macos")]
204207
if let Some(native_image) = c.native_image {
205208
item = item.native_image(native_image);
@@ -229,8 +232,8 @@ pub fn convert_menu_id<I: MenuId>(mut new_menu: Menu<u16>, menu: Menu<I>) -> Men
229232

230233
#[cfg(feature = "menu")]
231234
pub fn to_wry_menu(
232-
custom_menu_items: &mut HashMap<u16, WryCustomMenuItem>,
233-
menu: Menu<u16>,
235+
custom_menu_items: &mut HashMap<MenuHash, WryCustomMenuItem>,
236+
menu: Menu,
234237
) -> MenuBar {
235238
let mut wry_menu = MenuBar::new();
236239
for item in menu.items {
@@ -262,22 +265,21 @@ pub fn to_wry_menu(
262265
}
263266

264267
#[cfg(feature = "system-tray")]
265-
pub fn to_wry_context_menu<I: MenuId>(
266-
custom_menu_items: &mut HashMap<u16, WryCustomMenuItem>,
267-
menu: SystemTrayMenu<I>,
268+
pub fn to_wry_context_menu(
269+
custom_menu_items: &mut HashMap<MenuHash, WryCustomMenuItem>,
270+
menu: SystemTrayMenu,
268271
) -> WryContextMenu {
269272
let mut tray_menu = WryContextMenu::new();
270273
for item in menu.items {
271274
match item {
272275
SystemTrayMenuEntry::CustomItem(c) => {
273276
#[allow(unused_mut)]
274277
let mut item = tray_menu.add_item(MenuItemAttributesWrapper::from(&c).0);
275-
let id = c.id_value();
276278
#[cfg(target_os = "macos")]
277279
if let Some(native_image) = c.native_image {
278280
item.set_native_image(NativeImageWrapper::from(native_image).0);
279281
}
280-
custom_menu_items.insert(id, item);
282+
custom_menu_items.insert(c.id, item);
281283
}
282284
SystemTrayMenuEntry::NativeItem(i) => {
283285
tray_menu.add_native_item(MenuItemWrapper::from(i).0);

0 commit comments

Comments
 (0)