From caba4ed1980c74805d7c83a8c11e6dacdb24290b Mon Sep 17 00:00:00 2001 From: chip Date: Tue, 11 May 2021 11:44:53 -0700 Subject: [PATCH] feat(examples): barebones custom param type example (#1780) * feat(examples): barebones custom param type example * cargo +nightly fmt * fix build * add required imports Co-authored-by: Lucas Nogueira --- Cargo.toml | 9 +- examples/params/package.json | 7 ++ examples/params/public/index.html | 12 +++ examples/params/src-tauri/.gitignore | 10 ++ examples/params/src-tauri/.license_template | 1 + examples/params/src-tauri/Cargo.toml | 16 +++ examples/params/src-tauri/build.rs | 14 +++ examples/params/src-tauri/src/main.rs | 107 ++++++++++++++++++++ examples/params/src-tauri/tauri.conf.json | 56 ++++++++++ 9 files changed, 228 insertions(+), 4 deletions(-) create mode 100644 examples/params/package.json create mode 100644 examples/params/public/index.html create mode 100644 examples/params/src-tauri/.gitignore create mode 100644 examples/params/src-tauri/.license_template create mode 100644 examples/params/src-tauri/Cargo.toml create mode 100644 examples/params/src-tauri/build.rs create mode 100644 examples/params/src-tauri/src/main.rs create mode 100644 examples/params/src-tauri/tauri.conf.json diff --git a/Cargo.toml b/Cargo.toml index a3a89ca1d..f6df2dbdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,12 +11,13 @@ members = [ # examples "examples/api/src-tauri", + "examples/commands/src-tauri", "examples/helloworld/src-tauri", "examples/multiwindow/src-tauri", - "examples/commands/src-tauri", - "examples/splashscreen/src-tauri/", - "examples/state/src-tauri/", - "examples/navigation/src-tauri/", + "examples/navigation/src-tauri", + "examples/params/src-tauri", + "examples/splashscreen/src-tauri", + "examples/state/src-tauri", # used to build updater artifacts "examples/updater/src-tauri", ] diff --git a/examples/params/package.json b/examples/params/package.json new file mode 100644 index 000000000..305ea3d20 --- /dev/null +++ b/examples/params/package.json @@ -0,0 +1,7 @@ +{ + "name": "params", + "version": "1.0.0", + "scripts": { + "tauri": "node ../../tooling/cli.js/bin/tauri" + } +} diff --git a/examples/params/public/index.html b/examples/params/public/index.html new file mode 100644 index 000000000..b7f3354c9 --- /dev/null +++ b/examples/params/public/index.html @@ -0,0 +1,12 @@ + + + + + + + Params + + +

Simple custom `Params` types check.

+ + diff --git a/examples/params/src-tauri/.gitignore b/examples/params/src-tauri/.gitignore new file mode 100644 index 000000000..270a92d27 --- /dev/null +++ b/examples/params/src-tauri/.gitignore @@ -0,0 +1,10 @@ +# Generated by Cargo +# will have compiled files and executables +/target/ +WixTools + +# These are backup files generated by rustfmt +**/*.rs.bk + +config.json +bundle.json diff --git a/examples/params/src-tauri/.license_template b/examples/params/src-tauri/.license_template new file mode 100644 index 000000000..860f08256 --- /dev/null +++ b/examples/params/src-tauri/.license_template @@ -0,0 +1 @@ +../../../.license_template \ No newline at end of file diff --git a/examples/params/src-tauri/Cargo.toml b/examples/params/src-tauri/Cargo.toml new file mode 100644 index 000000000..f3c64e767 --- /dev/null +++ b/examples/params/src-tauri/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "params" +version = "0.1.0" +description = "A simple Tauri Application showcasing custom Params types" +edition = "2018" + +[build-dependencies] +tauri-build = { path = "../../../core/tauri-build", features = [ "codegen" ] } + +[dependencies] +serde = { version = "1", features = [ "derive" ] } +tauri = { path = "../../../core/tauri", features = ["api-all"] } + +[features] +default = [ "custom-protocol" ] +custom-protocol = [ "tauri/custom-protocol" ] diff --git a/examples/params/src-tauri/build.rs b/examples/params/src-tauri/build.rs new file mode 100644 index 000000000..2ad1cb4e6 --- /dev/null +++ b/examples/params/src-tauri/build.rs @@ -0,0 +1,14 @@ +// Copyright 2019-2021 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +use tauri_build::{try_build, Attributes, WindowsAttributes}; + +fn main() { + if let Err(error) = try_build( + Attributes::new() + .windows_attributes(WindowsAttributes::new().window_icon_path("../../.icons/icon.ico")), + ) { + panic!("error found during tauri-build: {}", error); + } +} diff --git a/examples/params/src-tauri/src/main.rs b/examples/params/src-tauri/src/main.rs new file mode 100644 index 000000000..938991b30 --- /dev/null +++ b/examples/params/src-tauri/src/main.rs @@ -0,0 +1,107 @@ +// Copyright 2019-2021 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +#![cfg_attr( + all(not(debug_assertions), target_os = "windows"), + windows_subsystem = "windows" +)] + +use serde::Serialize; +use std::fmt; +use std::str::FromStr; +use tauri::{command, Wry}; + +trait Params: + tauri::Params +{ +} +impl

Params for P where + P: tauri::Params +{ +} + +#[derive(Debug, Clone, Hash, Eq, PartialEq)] +pub enum Event { + Foo, + Bar, + Unknown(String), +} + +impl fmt::Display for Event { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::Foo => "foo", + Self::Bar => "bar", + Self::Unknown(s) => &s, + }) + } +} + +impl FromStr for Event { + type Err = std::convert::Infallible; + + fn from_str(s: &str) -> Result { + Ok(match s { + "foo" => Self::Foo, + "bar" => Self::Bar, + other => Self::Unknown(other.to_string()), + }) + } +} + +#[derive(Debug, Clone, Hash, Eq, PartialEq)] +pub enum Window { + Main, +} + +impl fmt::Display for Window { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::Main => "main", + }) + } +} + +impl FromStr for Window { + type Err = Box; + + fn from_str(s: &str) -> Result { + if s == "main" { + Ok(Self::Main) + } else { + Err(format!("only expect main window label, found: {}", s).into()) + } + } +} + +#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize)] +pub enum Menu { + MenuFoo, + MenuBar, +} + +#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize)] +pub enum SystemMenu { + SystemFoo, + SystemBar, +} + +#[command] +fn log_window_label(window: tauri::Window) { + dbg!(window.label()); +} + +#[command] +fn send_foo(window: tauri::Window) { + window + .emit(&Event::Foo, ()) + .expect("couldn't send Event::Foo"); +} + +fn main() { + tauri::Builder::::new() + .invoke_handler(tauri::generate_handler![log_window_label, send_foo]) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} diff --git a/examples/params/src-tauri/tauri.conf.json b/examples/params/src-tauri/tauri.conf.json new file mode 100644 index 000000000..c00996bfe --- /dev/null +++ b/examples/params/src-tauri/tauri.conf.json @@ -0,0 +1,56 @@ +{ + "build": { + "distDir": "../public", + "devPath": "../public", + "beforeDevCommand": "", + "beforeBuildCommand": "" + }, + "tauri": { + "bundle": { + "active": true, + "targets": "all", + "identifier": "com.tauri.dev", + "icon": [ + "../../.icons/32x32.png", + "../../.icons/128x128.png", + "../../.icons/128x128@2x.png", + "../../.icons/icon.icns", + "../../.icons/icon.ico" + ], + "resources": [], + "externalBin": [], + "copyright": "", + "category": "DeveloperTool", + "shortDescription": "", + "longDescription": "", + "deb": { + "depends": [], + "useBootstrapper": false + }, + "macOS": { + "frameworks": [], + "minimumSystemVersion": "", + "useBootstrapper": false, + "exceptionDomain": "" + } + }, + "allowlist": { + "all": true + }, + "windows": [ + { + "title": "Welcome to Tauri!", + "width": 800, + "height": 600, + "resizable": true, + "fullscreen": false + } + ], + "security": { + "csp": "default-src blob: data: filesystem: ws: http: https: 'unsafe-eval' 'unsafe-inline'" + }, + "updater": { + "active": false + } + } +}