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

custom plugin example #49

Merged
merged 5 commits into from
Sep 12, 2023
Merged
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

Expand Down
56 changes: 40 additions & 16 deletions Cargo.lock

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

18 changes: 10 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ rustdoc-args = ["--cfg", "docsrs"]

[features]
default = []
javascript = []
javascript = ["specta/typescript"]
typescript = ["specta/typescript"]

[dependencies]
specta = { workspace = true, features = ["functions", "tauri"] }
tauri-specta-macros = { path = "./macros" }
serde = "1.0.188"
serde = { workspace = true }
serde_json = "1.0.106"
tauri = "1.4.1"
tauri = { workspace = true }
thiserror = "1.0.48"
heck = "0.4.1"
indoc = "2.0.3"
Expand All @@ -36,11 +36,13 @@ doc-comment = "0.3.3"

[workspace]
members = [
"example/src-tauri",
"macros"
"examples/app/src-tauri",
"examples/custom-plugin/app/src-tauri",
"examples/custom-plugin/plugin",
"macros",
]


[workspace.dependencies]
# specta = { version = "=2.0.0-rc.1" }
specta = { git = "https://github.com/oscartbeaumont/specta", rev = "5948d80f2551780eda2c7bf38450fc796c74cfbf" }
specta = { version = "=2.0.0-rc.2" }
serde = "1.0.188"
tauri = "1.4.1"
5 changes: 5 additions & 0 deletions examples/app/dist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
# This is done so that Tauri never complains that '../dist does not exist'
File renamed without changes.
3 changes: 1 addition & 2 deletions example/package.json → examples/app/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "example",
"name": "tauri-specta-example-app",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "pnpm tauri dev",
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "example"
name = "tauri-specta-example-app"
version = "0.0.0"
description = "A Tauri App"
authors = ["you"]
Expand All @@ -17,8 +17,8 @@ serde_json = "1.0"
specta.workspace = true
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.4.1", features = ["api-all"] }
tauri-specta = { path = "../../", features = ["typescript", "javascript"] }
tauri-specta = { path = "../../../", features = ["typescript", "javascript"] }

[features]
default = [ "custom-protocol" ]
custom-protocol = [ "tauri/custom-protocol" ]
default = ["custom-protocol"]
custom-protocol = ["tauri/custom-protocol"]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions examples/custom-plugin/app/dist/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
# This is done so that Tauri never complains that '../dist does not exist'
17 changes: 17 additions & 0 deletions examples/custom-plugin/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<link rel="icon" type="image/svg+xml" href="/src/assets/logo.svg" />
<title>Tauri + Solid + Typescript App</title>
</head>

<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>

<script src="/src/index.tsx" type="module"></script>
</body>
</html>
24 changes: 24 additions & 0 deletions examples/custom-plugin/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "tauri-specta-example-custom-plugin-app",
"version": "0.0.0",
"description": "",
"scripts": {
"start": "vite",
"dev": "vite",
"build": "vite build",
"serve": "vite preview",
"tauri": "tauri"
},
"license": "MIT",
"dependencies": {
"solid-js": "^1.7.8",
"@tauri-apps/api": "^1.4.0",
"tauri-specta-custom-plugin": "workspace:*"
},
"devDependencies": {
"typescript": "^5.0.2",
"vite": "^4.4.4",
"vite-plugin-solid": "^2.7.0",
"@tauri-apps/cli": "^1.4.0"
}
}
6 changes: 6 additions & 0 deletions examples/custom-plugin/app/public/tauri.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions examples/custom-plugin/app/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions examples/custom-plugin/app/src-tauri/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Generated by Cargo
# will have compiled files and executables
/target/

24 changes: 24 additions & 0 deletions examples/custom-plugin/app/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
name = "tauri-specta-example-custom-plugin-app"
version = "0.0.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
tauri-build = { version = "1.4", features = [] }

[dependencies]
tauri = { version = "1.4", features = ["shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tauri-specta-example-custom-plugin = { path = "../../plugin" }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
# DO NOT REMOVE!!
custom-protocol = ["tauri/custom-protocol"]
3 changes: 3 additions & 0 deletions examples/custom-plugin/app/src-tauri/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
tauri_build::build()
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/custom-plugin/app/src-tauri/icons/32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
9 changes: 9 additions & 0 deletions examples/custom-plugin/app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

fn main() {
tauri::Builder::default()
.plugin(tauri_specta_example_custom_plugin::init())
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
46 changes: 46 additions & 0 deletions examples/custom-plugin/app/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"build": {
"beforeDevCommand": "pnpm dev",
"beforeBuildCommand": "pnpm build",
"devPath": "http://localhost:1420",
"distDir": "../dist",
"withGlobalTauri": false
},
"package": {
"productName": "app",
"version": "0.0.0"
},
"tauri": {
"allowlist": {
"all": false,
"shell": {
"all": false,
"open": true
}
},
"bundle": {
"active": true,
"targets": "all",
"identifier": "com.tauri.dev",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/[email protected]",
"icons/icon.icns",
"icons/icon.ico"
]
},
"security": {
"csp": null
},
"windows": [
{
"fullscreen": false,
"resizable": true,
"title": "app",
"width": 800,
"height": 600
}
]
}
}
50 changes: 50 additions & 0 deletions examples/custom-plugin/app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Match, Switch, createSignal, onCleanup } from "solid-js";
import { events, addNumbers } from "tauri-specta-custom-plugin";

function App() {
const [latestNumber, setLatestNumber] = createSignal<number | null>(null);

const [generatedNumber, setGeneratedNumber] = createSignal<
{ type: "loading" } | { type: "loaded"; value: number } | null
>(null);

events.randomNumber
.listen((e) => setLatestNumber(e.payload))
.then((unlisten) => onCleanup(unlisten));

return (
<div>
<p>Latest Random Number: {latestNumber()}</p>
<button
disabled={generatedNumber()?.type === "loading"}
onClick={() => {
setGeneratedNumber({ type: "loading" });

addNumbers(
Math.floor(Math.random() * 10),
Math.floor(Math.random() * 10)
)
.then((value) => setGeneratedNumber({ type: "loaded", value }))
.catch(() => setGeneratedNumber(null));
}}
>
Generate Random Number
</button>
<Switch fallback="Generate a number!">
<Match when={generatedNumber()?.type === "loading"}>
Loading number...
</Match>
<Match
when={(() => {
const n = generatedNumber();
return n?.type === "loaded" && n.value;
})()}
>
{(value) => value()}
</Match>
</Switch>
</div>
);
}

export default App;
6 changes: 6 additions & 0 deletions examples/custom-plugin/app/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* @refresh reload */
import { render } from "solid-js/web";

import App from "./App";

render(() => <App />, document.getElementById("root") as HTMLElement);
Loading
Loading