Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add dominator-rust template into tauri #576

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/package_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ impl PackageManager {
PackageManager::Cargo => &[
Template::Vanilla,
Template::Yew,
Template::Dominator,
Template::Leptos,
Template::Sycamore,
],
Expand All @@ -102,6 +103,7 @@ impl PackageManager {
PackageManager::Cargo => &[
Template::Vanilla,
Template::Yew,
Template::Dominator,
Template::Leptos,
Template::Sycamore,
],
Expand Down
13 changes: 9 additions & 4 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub enum Template {
Solid,
SolidTs,
Yew,
Dominator,
Leptos,
Sycamore,
Angular,
Expand Down Expand Up @@ -75,6 +76,7 @@ impl Display for Template {
Template::Solid => write!(f, "solid"),
Template::SolidTs => write!(f, "solid-ts"),
Template::Yew => write!(f, "yew"),
Template::Dominator => write!(f, "dominator"),
Template::Leptos => write!(f, "leptos"),
Template::Sycamore => write!(f, "sycamore"),
Template::Angular => write!(f, "angular"),
Expand All @@ -99,6 +101,7 @@ impl FromStr for Template {
"solid" => Ok(Template::Solid),
"solid-ts" => Ok(Template::SolidTs),
"yew" => Ok(Template::Yew),
"dominator" => Ok(Template::Dominator),
"leptos" => Ok(Template::Leptos),
"sycamore" => Ok(Template::Sycamore),
"angular" => Ok(Template::Angular),
Expand All @@ -125,6 +128,7 @@ impl Template {
Template::React => "React - (https://reactjs.org/)",
Template::Solid => "Solid - (https://www.solidjs.com/)",
Template::Yew => "Yew - (https://yew.rs/)",
Template::Dominator => "Dominator - (https://github.com/Pauan/rust-dominator)",
Template::Leptos => "Leptos - (https://github.com/leptos-rs/leptos)",
Template::Sycamore => "Sycamore - (https://sycamore-rs.netlify.app/)",
Template::Angular => "Angular - (https://angular.io/)",
Expand All @@ -147,6 +151,7 @@ impl<'a> Template {
Template::Solid,
Template::SolidTs,
Template::Yew,
Template::Dominator,
Template::Leptos,
Template::Sycamore,
Template::Angular,
Expand Down Expand Up @@ -217,23 +222,23 @@ impl<'a> Template {
| Template::Angular
| Template::Preact
| Template::PreactTs => PackageManager::NODE,
Template::Yew | Template::Leptos | Template::Sycamore => &[PackageManager::Cargo],
Template::Yew | Template::Dominator | Template::Leptos | Template::Sycamore => &[PackageManager::Cargo],
}
}

pub const fn needs_trunk(&self) -> bool {
matches!(self, Template::Sycamore | Template::Yew | Template::Leptos)
matches!(self, Template::Sycamore | Template::Yew | Template::Dominator | Template::Leptos)
}

pub const fn needs_tauri_cli(&self) -> bool {
matches!(
self,
Template::Sycamore | Template::Yew | Template::Leptos | Template::Vanilla
Template::Sycamore | Template::Yew | Template::Dominator | Template::Leptos | Template::Vanilla
)
}

pub const fn needs_wasm32_target(&self) -> bool {
matches!(self, Template::Sycamore | Template::Yew | Template::Leptos)
matches!(self, Template::Sycamore | Template::Yew | Template::Dominator | Template::Leptos)
}

pub fn render(
Expand Down
3 changes: 3 additions & 0 deletions templates/template-dominator/.taurignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/src
/public
/Cargo.toml
63 changes: 63 additions & 0 deletions templates/template-dominator/Cargo.toml.lte
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
[package]
name = "{% package_name %}-ui"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

# Enable a small amount of optimization in debug mode
[profile.dev]
opt-level = 1

# Enable high optimizations for dependencies, but not for our code:
[profile.dev.package."*"]
opt-level = 3

[dependencies]
wasm-bindgen = "0.2.84"
js-sys = "0.3.61"
wasm-bindgen-futures= "0.4.34"
gloo-events = "0.1.2"
gloo-timers = { version = "0.2.6", features = ["futures"] }
gloo-net = "0.2.6"
wasm-logger = { version = "0.2.0", optional = true }
serde-wasm-bindgen = "0.5.0"
serde = "1.0.159"
serde_json = "1.0.95"
anyhow = "1.0.70"
cfg-if = "1.0.0"
log = "0.4.17"
console_error_panic_hook = { version = "0.1.7", optional = true }
futures = "0.3.28"
dominator = "0.5.32"
futures-signals = "0.3.32"
once_cell = "1.17.1"
dominator_helpers = "0.7.2"
rand = "0.8.5"
getrandom = { version = "0.2.8", features = ["js"] }
ed25519-dalek = {version = "2.0.0-rc.3", features = ["serde", "rand_core"]}
hex = "0.4.3"

[dependencies.web-sys]
version = "0.3.61"
features = [
"console",
"MouseEvent",
"Document",
"Element",
"HtmlAnchorElement",
"HtmlElement",
"HtmlButtonElement",
"HtmlImageElement",
"Node",
"Window",
"Performance",
"HtmlFormElement",
]
[features]
default = []
dev = ["wasm-logger", "console_error_panic_hook"]
Comment on lines +18 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a huge amount of dependencies, are these all required or can we simplify this a bit?


[workspace]
members = ["src-tauri"]
7 changes: 7 additions & 0 deletions templates/template-dominator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Tauri + Dominator

This template should help get you started developing with Tauri and Dominator.

## Recommended IDE Setup

[VS Code](https://code.visualstudio.com/) + [Tauri](https://marketplace.visualstudio.com/items?itemName=tauri-apps.tauri-vscode) + [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer).
11 changes: 11 additions & 0 deletions templates/template-dominator/Trunk.toml.lte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[build]
target = "./index.html"

[watch]
ignore = ["./src-tauri"]

[serve]
address = "{% if mobile %}0.0.0.0{% else %}127.0.0.1{% endif %}"
port = 1420
open = false{% if mobile %}
ws_protocol = "ws"{% endif %}
10 changes: 10 additions & 0 deletions templates/template-dominator/_cta_manifest_
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright 2019-2022 Tauri Programme within The Commons Conservancy
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: MIT
devPath = http://localhost:1420
distDir = ../dist
withGlobalTauri = true

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should also provide a beforeDevCommand and beforeBuildCommand

[files]
tauri.svg = public/tauri.svg
styles.css = styles.css
10 changes: 10 additions & 0 deletions templates/template-dominator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>GUI</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
</body>
</html>
20 changes: 20 additions & 0 deletions templates/template-dominator/package.json.lte
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "{% package_name %}-ui",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use of a package.json file in a Rust project? we should remove this file

"version": "0.1.0",
"scripts": {
"start": "npm-run-all --parallel localmedia:serve trunk:serve:dev tauri:dev",
"localmedia:serve": "cd public && http-server -g -b --cors -p 9000",
"trunk:serve:dev": "cross-env RUSTFLAGS=--cfg=web_sys_unstable_apis trunk serve --features dev --watch .",
"tauri:dev": "tauri dev",
"build": "cross-env RUSTFLAGS=--cfg=web_sys_unstable_apis trunk build --release"
},
"dependencies": {
"@tauri-apps/api": "^1.4.0"
},
"devDependencies": {
"@tauri-apps/cli": "^1.5.9",
"cross-env": "^7.0.3",
"http-server": "^14.1.1",
"npm-run-all": "^4.1.5"
}
}
124 changes: 124 additions & 0 deletions templates/template-dominator/src/app.rs.lte
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use std::sync::Arc;

use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::to_value;
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
use dominator::DomBuilder;
pub use dominator::{clone, events, html, svg, with_node, Dom};
pub use futures_signals::{
map_ref,
signal::{Mutable, Signal, SignalExt},
signal_vec::{MutableVec, SignalVec, SignalVecExt},
};
use web_sys::HtmlInputElement;

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "{% if alpha %}core{% else %}tauri{% endif %}"])]
async fn invoke(cmd: &str, name: JsValue) -> JsValue;

#[wasm_bindgen(js_namespace = ["window", "__TAURI__", "{% if alpha %}core{% else %}tauri{% endif %}"], js_name = invoke)]
async fn invoke2(cmd: &str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this variant here

}

#[derive(Clone, Serialize, Deserialize)]
struct Args {
name: String,
}
Comment on lines +25 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename to GreetArgs so it can be consistent with other Rust templates

Suggested change
#[derive(Clone, Serialize, Deserialize)]
struct Args {
name: String,
}
#[derive(Serialize, Deserialize)]
struct GreetArgs<'a> {
name: &'a str,
}


#[derive(Clone, Serialize, Deserialize)]
pub struct App {
greet: Mutable<Option<String>>,
input: Mutable<String>
}

impl App {
pub fn new() -> Arc<Self> {
Arc::new(Self {
greet: Mutable::new(None),
input: Mutable::new(String::new())
})
}

pub fn render(app: Arc<Self>) -> Dom {
let state = app.clone();
// let new_msg =
// invoke("greet", to_value(&GreetArgs { name: &name.get() }).unwrap()).await;
Comment on lines +46 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this comment should be removed?

html!("div", {
.child(html!("div", {
.class(["body"])
.child(html!("div", {
.text("Welcome to the Tauri + Dominator starter template.")
}))
}))
.child(html!("div", {
.children(&mut [
html!("input" => HtmlInputElement, {
.prop_signal("value", state.input.signal_cloned())

.with_node!(element => {
.event(clone!(app => move |_: events::Input| {
log::info!("input: {}", element.value());
app.input.set(element.value());
}))
})
}),

html!("button", {
.text("Greet")
.event(clone!(app => move |_: events::Click| {
let app = app.clone();
let input = app.input.lock_ref();

if *input == "" {
app.clone().greet.set(None);

} else {

let input = input.to_string();
let args = Args {name: input.clone()};
let app = app.clone();
spawn_local(async move {
let name = invoke("greet", to_value(&args).unwrap()).await;
app.greet.set(name.as_string());
});
}
}))
}),

html!("div", {
.text_signal(app.greet.signal_ref(|greet| format!("{}", greet.clone().unwrap_or_default())))
}),

html!("button", {
.text("Greet2")
.event(clone!(app => move |_: events::Click| {
let app = app.clone();
let input = app.input.lock_ref();

if *input == "" {
app.clone().greet.set(None);

} else {

let input = input.to_string();
let args = Args {name: input.clone()};
let app = app.clone();
spawn_local(async move {
invoke2("greet2").await;
// app.greet.set(name.as_string());
});
}
}))
}),

html!("div", {
.text_signal(app.greet.signal_ref(|greet| format!("{}", greet.clone().unwrap_or_default())))
}),

])
}))
})
}
}
Comment on lines +48 to +124
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We like to give a similar structure in all the templates so this should be changed to match other templates, the final result should look something like this
image

Loading
Loading