Add menu functionality (#59)

* Added core::Channel and menu functionality. core::Channel may leak memory.

* Updated examples to v2 using Leptos.
This commit is contained in:
bicarlsen 2024-08-06 18:20:50 +02:00 committed by GitHub
parent 115009d4bf
commit ae49310ee1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 5712 additions and 4216 deletions

View file

@ -2,3 +2,6 @@
# will have compiled files and executables
/target/
# Generated by Tauri
# will have schema files for capabilities auto-completion
/gen/schemas

View file

@ -1,28 +1,20 @@
[package]
name = "tauri-sys-test"
name = "test-tauri-events"
version = "0.0.0"
description = "A Tauri App"
authors = ["you"]
license = ""
repository = ""
edition = "2021"
rust-version = "1.57"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[build-dependencies]
tauri-build = { version = "1.5.0", features = [] }
# tauri-build = { version = "2.0.0-rc", features = [] }
tauri-build = { path = "../../../../tauri/core/tauri-build", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri-plugin-log = {git = "https://github.com/tauri-apps/tauri-plugin-log", features = ["colored"] }
tauri = { version = "1.5.3", features = ["api-all", "updater"] }
[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
default = [ "custom-protocol" ]
# this feature is used for production builds where `devPath` points to the filesystem
# DO NOT remove this
custom-protocol = [ "tauri/custom-protocol" ]
tauri = { version = "2.0.0-rc", features = [] }
tauri-plugin-shell = "2.0.0-rc"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"

View file

@ -0,0 +1,17 @@
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"description": "Capability for the main window",
"windows": ["main"],
"permissions": [
"core:path:default",
"core:event:default",
"core:window:default",
"core:app:default",
"core:image:default",
"core:resources:default",
"core:menu:default",
"core:tray:default",
"shell:allow-open"
]
}

View file

@ -1,6 +0,0 @@
{
"build": {
"beforeDevCommand": "trunk serve --features ci",
"beforeBuildCommand": "trunk build --release --features ci"
}
}

View file

@ -1,70 +1,41 @@
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::sync::atomic::{AtomicBool, Ordering};
use tauri::{Manager, Runtime, State, Window};
use tauri_plugin_log::{LogTarget, LoggerBuilder};
struct Received(AtomicBool);
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn verify_receive(emitted: State<Received>) -> bool {
emitted.0.load(Ordering::Relaxed)
}
#[tauri::command]
async fn emit_event<R: Runtime>(win: Window<R>) -> Result<(), ()> {
let _ = win.emit("rust-event-once", "Hello World from Rust!");
Ok(())
}
#[tauri::command]
async fn emit_event_5_times<R: Runtime>(win: Window<R>) -> Result<(), ()> {
for i in 0..5 {
let _ = win.emit("rust-event-listen", i);
}
Ok(())
}
#[tauri::command]
fn exit_with_error(e: &str) -> bool {
eprintln!("{}", e);
std::process::exit(1);
}
use tauri::{Emitter, Manager};
fn main() {
let log_plugin = {
let targets = [
LogTarget::LogDir,
#[cfg(debug_assertions)]
LogTarget::Stdout,
#[cfg(debug_assertions)]
LogTarget::Webview,
];
LoggerBuilder::new().targets(targets).build()
};
logging::enable();
tauri::Builder::default()
.plugin(log_plugin)
.invoke_handler(tauri::generate_handler![verify_receive, emit_event, emit_event_5_times, exit_with_error])
.setup(|app| {
app.manage(Received(AtomicBool::new(false)));
let app_handle = app.handle();
app.listen_global("javascript-event", move |_| {
app_handle
.state::<Received>()
.0
.store(true, Ordering::Relaxed);
});
Ok(())
})
.invoke_handler(tauri::generate_handler![trigger_listen_events,])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
#[tauri::command]
fn trigger_listen_events(app: tauri::AppHandle) {
tracing::debug!("trigger_listen_event");
std::thread::spawn({
move || {
for i in 1..=100 {
app.emit("event::listen", i).unwrap();
std::thread::sleep(std::time::Duration::from_millis(500));
}
}
});
}
mod logging {
use tracing_subscriber::{filter::LevelFilter, fmt, prelude::*, Layer, Registry};
const MAX_LOG_LEVEL: LevelFilter = LevelFilter::DEBUG;
pub fn enable() {
let console_logger = fmt::layer()
.with_writer(std::io::stdout)
.pretty()
.with_filter(MAX_LOG_LEVEL);
let subscriber = Registry::default().with(console_logger);
tracing::subscriber::set_global_default(subscriber).unwrap();
}
}

View file

@ -1,71 +1,35 @@
{
"productName": "test-tauri-events",
"version": "0.0.0",
"identifier": "com.tauri.dev",
"build": {
"beforeDevCommand": "trunk serve",
"beforeBuildCommand": "trunk build --release",
"devPath": "http://localhost:1420",
"distDir": "../dist",
"withGlobalTauri": true
"devUrl": "http://localhost:1420",
"beforeBuildCommand": "trunk build",
"frontendDist": "../dist"
},
"package": {
"productName": "tauri-sys-test",
"version": "0.0.0"
},
"tauri": {
"allowlist": {
"all": true
},
"bundle": {
"active": true,
"category": "DeveloperTool",
"copyright": "",
"deb": {
"depends": []
},
"externalBin": [],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
],
"identifier": "com.tauri.dev",
"longDescription": "",
"macOS": {
"entitlements": null,
"exceptionDomain": "",
"frameworks": [],
"providerShortName": null,
"signingIdentity": null
},
"resources": [],
"shortDescription": "",
"targets": "all",
"windows": {
"certificateThumbprint": null,
"digestAlgorithm": "sha256",
"timestampUrl": ""
}
},
"security": {
"csp": null
},
"updater": {
"active": true,
"endpoints": [
"https://releases.myapp.com/{{target}}/{{current_version}}"
],
"dialog": true,
"pubkey": "YOUR_UPDATER_SIGNATURE_PUBKEY_HERE"
},
"app": {
"withGlobalTauri": true,
"windows": [
{
"fullscreen": false,
"height": 600,
"resizable": true,
"title": "tauri-sys testing suite",
"width": 800
"title": "test-tauri-events",
"width": 800,
"height": 600
}
],
"security": {
"csp": null
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
}
}