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 <lucas@tauri.studio>
This commit is contained in:
chip 2021-05-11 11:44:53 -07:00 committed by GitHub
parent 6dc416c544
commit caba4ed198
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 228 additions and 4 deletions

View File

@ -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",
]

View File

@ -0,0 +1,7 @@
{
"name": "params",
"version": "1.0.0",
"scripts": {
"tauri": "node ../../tooling/cli.js/bin/tauri"
}
}

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Params</title>
</head>
<body>
<h1>Simple custom `Params` types check.</h1>
</body>
</html>

10
examples/params/src-tauri/.gitignore vendored Normal file
View File

@ -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

View File

@ -0,0 +1 @@
../../../.license_template

View File

@ -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" ]

View File

@ -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);
}
}

View File

@ -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<Event = Event, Label = Window, MenuId = Menu, SystemTrayMenuId = SystemMenu>
{
}
impl<P> Params for P where
P: tauri::Params<Event = Event, Label = Window, MenuId = Menu, SystemTrayMenuId = SystemMenu>
{
}
#[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<Self, Self::Err> {
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<dyn std::error::Error>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
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<impl Params>) {
dbg!(window.label());
}
#[command]
fn send_foo(window: tauri::Window<impl Params>) {
window
.emit(&Event::Foo, ())
.expect("couldn't send Event::Foo");
}
fn main() {
tauri::Builder::<Event, Window, Menu, SystemMenu, _, Wry>::new()
.invoke_handler(tauri::generate_handler![log_window_label, send_foo])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@ -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
}
}
}