Skip to content

Commit

Permalink
refactor: move runtime to tauri-runtime crate (#1751)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 9, 2021
1 parent e849495 commit 665ec1d
Show file tree
Hide file tree
Showing 25 changed files with 879 additions and 699 deletions.
13 changes: 11 additions & 2 deletions .changes/config.json
Expand Up @@ -214,6 +214,14 @@
"path": "./core/tauri-utils",
"manager": "rust"
},
"tauri-runtime": {
"path": "./core/tauri-runtime",
"manager": "rust",
"dependencies": [
"tauri-utils"
],
"postversion": "node ../../.scripts/sync-prerelease.js ${ release.type }"
},
"tauri-codegen": {
"path": "./core/tauri-codegen",
"manager": "rust",
Expand Down Expand Up @@ -241,7 +249,8 @@
"manager": "rust",
"dependencies": [
"tauri-macros",
"tauri-utils"
"tauri-utils",
"tauri-runtime"
],
"postversion": "node ../../.scripts/sync-cli-metadata.js ${ pkg.pkg } ${ release.type }"
},
Expand Down Expand Up @@ -276,4 +285,4 @@
"manager": "javascript"
}
}
}
}
6 changes: 6 additions & 0 deletions .changes/runtime-crate.md
@@ -0,0 +1,6 @@
---
"tauri-runtime": patch
"tauri": patch
---

`tauri-runtime` crate initial release.
31 changes: 31 additions & 0 deletions .scripts/sync-prerelease.js
@@ -0,0 +1,31 @@
#!/usr/bin/env node
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

/*
This script is solely intended to be run as part of the `covector version` step to
keep the `tauri-release` crate version without the `beta` or `beta-rc` suffix.
*/

const { readFileSync, writeFileSync } = require("fs")

const runtimeManifestPath = '../../core/tauri-runtime/Cargo.toml'
const dependencyManifestPaths = ['../../core/tauri/Cargo.toml']
const changelogPath = '../../core/tauri-runtime/CHANGELOG.md'

const bump = process.argv[2]

let runtimeManifest = readFileSync(runtimeManifestPath, "utf-8")
runtimeManifest = runtimeManifest.replace(/version = "(\d+\.\d+\.\d+)-[^0-9\.]+\.0"/, 'version = "$1"')
writeFileSync(runtimeManifestPath, runtimeManifest)

let changelog = readFileSync(changelogPath, "utf-8")
changelog = changelog.replace(/(\d+\.\d+\.\d+)-[^0-9\.]+\.0/, '$1')
writeFileSync(changelogPath, changelog)

for (const dependencyManifestPath of dependencyManifestPaths) {
let dependencyManifest = readFileSync(dependencyManifestPath, "utf-8")
dependencyManifest = dependencyManifest.replace(/tauri-runtime = { version = "(\d+\.\d+\.\d+)-[^0-9\.]+\.0"/, 'tauri-runtime = { version = "$1"')
writeFileSync(dependencyManifestPath, dependencyManifest)
}
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -2,6 +2,7 @@
members = [
# core
"core/tauri",
"core/tauri-runtime",
"core/tauri-macros",
"core/tauri-utils",
"core/tauri-build",
Expand Down
19 changes: 19 additions & 0 deletions core/tauri-runtime/Cargo.toml
@@ -0,0 +1,19 @@
[package]
name = "tauri-runtime"
version = "0.0.0"
authors = [ "Tauri Programme within The Commons Conservancy" ]
categories = [ "gui", "web-programming" ]
license = "Apache-2.0 OR MIT"
homepage = "https://tauri.studio"
repository = "https://github.com/tauri-apps/tauri"
description = "Runtime for Tauri applications"
edition = "2018"

[dependencies]
serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0"
thiserror = "1.0"
uuid = { version = "0.8.2", features = [ "v4" ] }
tauri-utils = { version = "1.0.0-beta-rc.1", path = "../tauri-utils" }
wry = { git = "https://github.com/tauri-apps/wry", rev = "6bc97aff525644b83a3a00537316c46d7afb985b" }
image = "0.23"
File renamed without changes.
Expand Up @@ -5,23 +5,19 @@
//! The [`wry`] Tauri [`Runtime`].

use crate::{
api::config::WindowConfig,
runtime::{
menu::{CustomMenuItem, Menu, MenuId, MenuItem, SystemTrayMenuItem},
webview::{
FileDropEvent, FileDropHandler, RpcRequest, WebviewRpcHandler, WindowBuilder,
WindowBuilderBase,
},
window::{
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
DetachedWindow, MenuEvent, PendingWindow, WindowEvent,
},
Dispatch, Monitor, Params, Runtime, SystemTrayEvent,
menu::{CustomMenuItem, Menu, MenuId, MenuItem, SystemTrayMenuItem},
webview::{
FileDropEvent, FileDropHandler, RpcRequest, WebviewRpcHandler, WindowBuilder, WindowBuilderBase,
},
window::{
dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size},
DetachedWindow, MenuEvent, PendingWindow, WindowEvent,
},
Icon,
Dispatch, Icon, Monitor, Params, Runtime, SystemTrayEvent,
};

use image::{GenericImageView, Pixel};
use tauri_utils::config::WindowConfig;
use uuid::Uuid;
use wry::{
application::{
Expand Down Expand Up @@ -1043,15 +1039,15 @@ fn create_webview<M: Params<Runtime = Wry>>(
) -> crate::Result<WebView> {
let PendingWindow {
webview_attributes,
window_attributes,
window_builder,
rpc_handler,
file_drop_handler,
label,
url,
..
} = pending;

let window = window_attributes.build(event_loop).unwrap();
let window = window_builder.build(event_loop).unwrap();
let mut webview_builder = WebViewBuilder::new(window)
.map_err(|e| crate::Error::CreateWebview(Box::new(e)))?
.with_url(&url)
Expand Down
77 changes: 69 additions & 8 deletions core/tauri/src/runtime/mod.rs → core/tauri-runtime/src/lib.rs
Expand Up @@ -4,15 +4,12 @@

//! Internal runtime between Tauri and the underlying webview runtime.

use crate::{
runtime::window::{DetachedWindow, PendingWindow},
Icon, Params, WindowBuilder,
};
use std::path::PathBuf;

use tauri_utils::assets::Assets;
use uuid::Uuid;

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

use menu::{MenuId, SystemTrayMenuItem};
use monitor::Monitor;
use tag::Tag;
use webview::WindowBuilder;
use window::{
dpi::{PhysicalPosition, PhysicalSize, Position, Size},
MenuEvent, WindowEvent,
DetachedWindow, MenuEvent, PendingWindow, WindowEvent,
};

#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
/// Failed to create webview.
#[error("failed to create webview: {0}")]
CreateWebview(Box<dyn std::error::Error + Send>),
/// Failed to create window.
#[error("failed to create window")]
CreateWindow,
/// Failed to send message to webview.
#[error("failed to send message to the webview")]
FailedToSendMessage,
/// Failed to serialize/deserialize.
#[error("JSON error: {0}")]
Json(#[from] serde_json::Error),
/// Encountered an error creating the app system tray,
#[error("error encountered during tray setup: {0}")]
SystemTray(Box<dyn std::error::Error + Send>),
/// Failed to load window icon.
#[error("invalid icon: {0}")]
InvalidIcon(Box<dyn std::error::Error + Send>),
}

/// Result type.
pub type Result<T> = std::result::Result<T, Error>;

#[doc(hidden)]
pub mod private {
pub trait ParamsBase {}
}

/// Types associated with the running Tauri application.
pub trait Params: private::ParamsBase + 'static {
/// The event type used to create and listen to events.
type Event: Tag;

/// The type used to determine the name of windows.
type Label: Tag;

/// The type used to determine window menu ids.
type MenuId: MenuId;

/// The type used to determine system tray menu ids.
type SystemTrayMenuId: MenuId;

/// Assets that Tauri should serve from itself.
type Assets: Assets;

/// The underlying webview runtime used by the Tauri application.
type Runtime: Runtime;
}

/// A icon definition.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum Icon {
/// Icon from file path.
File(PathBuf),
/// Icon from raw bytes.
Raw(Vec<u8>),
}

/// A system tray event.
pub struct SystemTrayEvent {
pub(crate) menu_item_id: u32,
pub menu_item_id: u32,
}

/// The webview runtime interface.
Expand Down
18 changes: 11 additions & 7 deletions core/tauri/src/runtime/menu.rs → core/tauri-runtime/src/menu.rs
Expand Up @@ -8,16 +8,18 @@ use std::{
hash::{Hash, Hasher},
};

use serde::Serialize;

/// A type that can be derived into a menu id.
pub trait MenuId: Hash + Eq + Debug + Clone + Send + Sync + 'static {}
pub trait MenuId: Serialize + Hash + Eq + Debug + Clone + Send + Sync + 'static {}

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

/// A window menu.
#[derive(Debug, Clone)]
pub struct Menu<I: MenuId> {
pub(crate) title: String,
pub(crate) items: Vec<MenuItem<I>>,
pub title: String,
pub items: Vec<MenuItem<I>>,
}

impl<I: MenuId> Menu<I> {
Expand All @@ -29,11 +31,12 @@ impl<I: MenuId> Menu<I> {
}
}
}

/// A custom menu item.
#[derive(Debug, Clone)]
pub struct CustomMenuItem<I: MenuId> {
pub(crate) id: I,
pub(crate) name: String,
pub id: I,
pub name: String,
}

impl<I: MenuId> CustomMenuItem<I> {
Expand All @@ -43,7 +46,8 @@ impl<I: MenuId> CustomMenuItem<I> {
Self { id, name: title }
}

pub(crate) fn id_value(&self) -> u32 {
#[doc(hidden)]
pub fn id_value(&self) -> u32 {
let mut s = DefaultHasher::new();
self.id.hash(&mut s);
s.finish() as u32
Expand Down
File renamed without changes.
Expand Up @@ -71,7 +71,7 @@ use std::{
/// let event: Event = "tauri://file-drop".parse().unwrap();
///
/// // show that this event type can be represented as a Tag, a requirement for using it in Tauri.
/// fn is_file_drop(tag: impl tauri::runtime::tag::Tag) {
/// fn is_file_drop(tag: impl tauri_runtime::tag::Tag) {
/// assert_eq!("tauri://file-drop", tag.to_string());
/// }
///
Expand Down Expand Up @@ -113,7 +113,7 @@ where
///
/// We don't want downstream users to implement this trait so that [`Tag`]s cannot be turned into
/// invalid JavaScript - regardless of their content.
pub(crate) trait ToJsString {
pub trait ToJsString {
fn to_js_string(&self) -> crate::Result<String>;
}

Expand All @@ -125,7 +125,7 @@ impl<D: Display> ToJsString for D {
}

/// Turn any collection of [`Tag`]s into a JavaScript array of strings.
pub(crate) fn tags_to_javascript_array(tags: &[impl Tag]) -> crate::Result<String> {
pub fn tags_to_javascript_array(tags: &[impl Tag]) -> crate::Result<String> {
let tags: Vec<String> = tags.iter().map(ToString::to_string).collect();
Ok(serde_json::to_string(&tags)?)
}

0 comments on commit 665ec1d

Please sign in to comment.