Skip to content

Commit 665ec1d

Browse files
authored
refactor: move runtime to tauri-runtime crate (#1751)
1 parent e849495 commit 665ec1d

File tree

25 files changed

+879
-699
lines changed

25 files changed

+879
-699
lines changed

.changes/config.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,14 @@
214214
"path": "./core/tauri-utils",
215215
"manager": "rust"
216216
},
217+
"tauri-runtime": {
218+
"path": "./core/tauri-runtime",
219+
"manager": "rust",
220+
"dependencies": [
221+
"tauri-utils"
222+
],
223+
"postversion": "node ../../.scripts/sync-prerelease.js ${ release.type }"
224+
},
217225
"tauri-codegen": {
218226
"path": "./core/tauri-codegen",
219227
"manager": "rust",
@@ -241,7 +249,8 @@
241249
"manager": "rust",
242250
"dependencies": [
243251
"tauri-macros",
244-
"tauri-utils"
252+
"tauri-utils",
253+
"tauri-runtime"
245254
],
246255
"postversion": "node ../../.scripts/sync-cli-metadata.js ${ pkg.pkg } ${ release.type }"
247256
},
@@ -276,4 +285,4 @@
276285
"manager": "javascript"
277286
}
278287
}
279-
}
288+
}

.changes/runtime-crate.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": patch
3+
"tauri": patch
4+
---
5+
6+
`tauri-runtime` crate initial release.

.scripts/sync-prerelease.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
3+
// SPDX-License-Identifier: Apache-2.0
4+
// SPDX-License-Identifier: MIT
5+
6+
/*
7+
This script is solely intended to be run as part of the `covector version` step to
8+
keep the `tauri-release` crate version without the `beta` or `beta-rc` suffix.
9+
*/
10+
11+
const { readFileSync, writeFileSync } = require("fs")
12+
13+
const runtimeManifestPath = '../../core/tauri-runtime/Cargo.toml'
14+
const dependencyManifestPaths = ['../../core/tauri/Cargo.toml']
15+
const changelogPath = '../../core/tauri-runtime/CHANGELOG.md'
16+
17+
const bump = process.argv[2]
18+
19+
let runtimeManifest = readFileSync(runtimeManifestPath, "utf-8")
20+
runtimeManifest = runtimeManifest.replace(/version = "(\d+\.\d+\.\d+)-[^0-9\.]+\.0"/, 'version = "$1"')
21+
writeFileSync(runtimeManifestPath, runtimeManifest)
22+
23+
let changelog = readFileSync(changelogPath, "utf-8")
24+
changelog = changelog.replace(/(\d+\.\d+\.\d+)-[^0-9\.]+\.0/, '$1')
25+
writeFileSync(changelogPath, changelog)
26+
27+
for (const dependencyManifestPath of dependencyManifestPaths) {
28+
let dependencyManifest = readFileSync(dependencyManifestPath, "utf-8")
29+
dependencyManifest = dependencyManifest.replace(/tauri-runtime = { version = "(\d+\.\d+\.\d+)-[^0-9\.]+\.0"/, 'tauri-runtime = { version = "$1"')
30+
writeFileSync(dependencyManifestPath, dependencyManifest)
31+
}

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
members = [
33
# core
44
"core/tauri",
5+
"core/tauri-runtime",
56
"core/tauri-macros",
67
"core/tauri-utils",
78
"core/tauri-build",

core/tauri-runtime/Cargo.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "tauri-runtime"
3+
version = "0.0.0"
4+
authors = [ "Tauri Programme within The Commons Conservancy" ]
5+
categories = [ "gui", "web-programming" ]
6+
license = "Apache-2.0 OR MIT"
7+
homepage = "https://tauri.studio"
8+
repository = "https://github.com/tauri-apps/tauri"
9+
description = "Runtime for Tauri applications"
10+
edition = "2018"
11+
12+
[dependencies]
13+
serde = { version = "1.0", features = [ "derive" ] }
14+
serde_json = "1.0"
15+
thiserror = "1.0"
16+
uuid = { version = "0.8.2", features = [ "v4" ] }
17+
tauri-utils = { version = "1.0.0-beta-rc.1", path = "../tauri-utils" }
18+
wry = { git = "https://github.com/tauri-apps/wry", rev = "6bc97aff525644b83a3a00537316c46d7afb985b" }
19+
image = "0.23"
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,19 @@
55
//! The [`wry`] Tauri [`Runtime`].
66
77
use crate::{
8-
api::config::WindowConfig,
9-
runtime::{
10-
menu::{CustomMenuItem, Menu, MenuId, MenuItem, SystemTrayMenuItem},
11-
webview::{
12-
FileDropEvent, FileDropHandler, RpcRequest, WebviewRpcHandler, WindowBuilder,
13-
WindowBuilderBase,
14-
},
15-
window::{
16-
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
17-
DetachedWindow, MenuEvent, PendingWindow, WindowEvent,
18-
},
19-
Dispatch, Monitor, Params, Runtime, SystemTrayEvent,
8+
menu::{CustomMenuItem, Menu, MenuId, MenuItem, SystemTrayMenuItem},
9+
webview::{
10+
FileDropEvent, FileDropHandler, RpcRequest, WebviewRpcHandler, WindowBuilder, WindowBuilderBase,
11+
},
12+
window::{
13+
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
14+
DetachedWindow, MenuEvent, PendingWindow, WindowEvent,
2015
},
21-
Icon,
16+
Dispatch, Icon, Monitor, Params, Runtime, SystemTrayEvent,
2217
};
2318

2419
use image::{GenericImageView, Pixel};
20+
use tauri_utils::config::WindowConfig;
2521
use uuid::Uuid;
2622
use wry::{
2723
application::{
@@ -1043,15 +1039,15 @@ fn create_webview<M: Params<Runtime = Wry>>(
10431039
) -> crate::Result<WebView> {
10441040
let PendingWindow {
10451041
webview_attributes,
1046-
window_attributes,
1042+
window_builder,
10471043
rpc_handler,
10481044
file_drop_handler,
10491045
label,
10501046
url,
10511047
..
10521048
} = pending;
10531049

1054-
let window = window_attributes.build(event_loop).unwrap();
1050+
let window = window_builder.build(event_loop).unwrap();
10551051
let mut webview_builder = WebViewBuilder::new(window)
10561052
.map_err(|e| crate::Error::CreateWebview(Box::new(e)))?
10571053
.with_url(&url)
Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
//! Internal runtime between Tauri and the underlying webview runtime.
66
7-
use crate::{
8-
runtime::window::{DetachedWindow, PendingWindow},
9-
Icon, Params, WindowBuilder,
10-
};
7+
use std::path::PathBuf;
8+
9+
use tauri_utils::assets::Assets;
1110
use uuid::Uuid;
1211

13-
pub(crate) mod app;
1412
pub mod flavors;
15-
pub(crate) mod manager;
1613
/// Create window and system tray menus.
1714
pub mod menu;
1815
/// Types useful for interacting with a user's monitors.
@@ -23,14 +20,78 @@ pub mod window;
2320

2421
use menu::{MenuId, SystemTrayMenuItem};
2522
use monitor::Monitor;
23+
use tag::Tag;
24+
use webview::WindowBuilder;
2625
use window::{
2726
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
28-
MenuEvent, WindowEvent,
27+
DetachedWindow, MenuEvent, PendingWindow, WindowEvent,
2928
};
3029

30+
#[derive(Debug, thiserror::Error)]
31+
#[non_exhaustive]
32+
pub enum Error {
33+
/// Failed to create webview.
34+
#[error("failed to create webview: {0}")]
35+
CreateWebview(Box<dyn std::error::Error + Send>),
36+
/// Failed to create window.
37+
#[error("failed to create window")]
38+
CreateWindow,
39+
/// Failed to send message to webview.
40+
#[error("failed to send message to the webview")]
41+
FailedToSendMessage,
42+
/// Failed to serialize/deserialize.
43+
#[error("JSON error: {0}")]
44+
Json(#[from] serde_json::Error),
45+
/// Encountered an error creating the app system tray,
46+
#[error("error encountered during tray setup: {0}")]
47+
SystemTray(Box<dyn std::error::Error + Send>),
48+
/// Failed to load window icon.
49+
#[error("invalid icon: {0}")]
50+
InvalidIcon(Box<dyn std::error::Error + Send>),
51+
}
52+
53+
/// Result type.
54+
pub type Result<T> = std::result::Result<T, Error>;
55+
56+
#[doc(hidden)]
57+
pub mod private {
58+
pub trait ParamsBase {}
59+
}
60+
61+
/// Types associated with the running Tauri application.
62+
pub trait Params: private::ParamsBase + 'static {
63+
/// The event type used to create and listen to events.
64+
type Event: Tag;
65+
66+
/// The type used to determine the name of windows.
67+
type Label: Tag;
68+
69+
/// The type used to determine window menu ids.
70+
type MenuId: MenuId;
71+
72+
/// The type used to determine system tray menu ids.
73+
type SystemTrayMenuId: MenuId;
74+
75+
/// Assets that Tauri should serve from itself.
76+
type Assets: Assets;
77+
78+
/// The underlying webview runtime used by the Tauri application.
79+
type Runtime: Runtime;
80+
}
81+
82+
/// A icon definition.
83+
#[derive(Debug, Clone)]
84+
#[non_exhaustive]
85+
pub enum Icon {
86+
/// Icon from file path.
87+
File(PathBuf),
88+
/// Icon from raw bytes.
89+
Raw(Vec<u8>),
90+
}
91+
3192
/// A system tray event.
3293
pub struct SystemTrayEvent {
33-
pub(crate) menu_item_id: u32,
94+
pub menu_item_id: u32,
3495
}
3596

3697
/// The webview runtime interface.
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ use std::{
88
hash::{Hash, Hasher},
99
};
1010

11+
use serde::Serialize;
12+
1113
/// A type that can be derived into a menu id.
12-
pub trait MenuId: Hash + Eq + Debug + Clone + Send + Sync + 'static {}
14+
pub trait MenuId: Serialize + Hash + Eq + Debug + Clone + Send + Sync + 'static {}
1315

14-
impl<T> MenuId for T where T: Hash + Eq + Debug + Clone + Send + Sync + 'static {}
16+
impl<T> MenuId for T where T: Serialize + Hash + Eq + Debug + Clone + Send + Sync + 'static {}
1517

1618
/// A window menu.
1719
#[derive(Debug, Clone)]
1820
pub struct Menu<I: MenuId> {
19-
pub(crate) title: String,
20-
pub(crate) items: Vec<MenuItem<I>>,
21+
pub title: String,
22+
pub items: Vec<MenuItem<I>>,
2123
}
2224

2325
impl<I: MenuId> Menu<I> {
@@ -29,11 +31,12 @@ impl<I: MenuId> Menu<I> {
2931
}
3032
}
3133
}
34+
3235
/// A custom menu item.
3336
#[derive(Debug, Clone)]
3437
pub struct CustomMenuItem<I: MenuId> {
35-
pub(crate) id: I,
36-
pub(crate) name: String,
38+
pub id: I,
39+
pub name: String,
3740
}
3841

3942
impl<I: MenuId> CustomMenuItem<I> {
@@ -43,7 +46,8 @@ impl<I: MenuId> CustomMenuItem<I> {
4346
Self { id, name: title }
4447
}
4548

46-
pub(crate) fn id_value(&self) -> u32 {
49+
#[doc(hidden)]
50+
pub fn id_value(&self) -> u32 {
4751
let mut s = DefaultHasher::new();
4852
self.id.hash(&mut s);
4953
s.finish() as u32

0 commit comments

Comments
 (0)