“如果结果不如你所愿,就在尘埃落定前奋力一搏。”——《夏目友人帐》
“有些事不是看到了希望才去坚持,而是因为坚持才会看到希望。”——《十宗罪》
“维持现状意味着空耗你的努力和生命。”——纪伯伦
第三节 定义系统托盘、日志
一. 前言
本章节我们将学习如何为我们的应用添加系统托盘功能和服务日志。
二. 修改项目src-tauri 目录结构
1. 修改后的目录结构如下
- commnd 存放项目封装的命令
- pligins 用于处理命令的函数引入
- main 进行全局配置
2. 完善项目代码
-
修改Cargo.toml
[package] name = "tv_task" version = "0.1.0" description = "A Tauri App" authors = ["elcker"] edition = "2021" [build-dependencies] tauri-build = { version = "2", features = [] } [dependencies] tauri = { version = "2", features = [ "tray-icon" ] } tauri-plugin-opener = "2" serde = { version = "1", features = ["derive"] } serde_json = "1"
-
main.rs
// Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] mod plugins; mod command; use plugins::KoiTauriPlugin; use command::KoiTauriCommand; fn main() { tauri::Builder::default() .init_plugin() .init_cmd() .run(tauri::generate_context!()) .expect("启动应用程序出错.."); }
-
commid.rs
mod test; use tauri::Runtime; pub trait KoiTauriCommand { fn init_cmd(self) -> Self; } impl<R: Runtime> KoiTauriCommand for tauri::Builder<R> { // 初始化插件 fn init_cmd(self) -> Self { self.invoke_handler(tauri::generate_handler![ test::greet ]) } }
-
command 下创建 test 文件 用于测试
#[tauri::command] pub fn greet(name: &str) -> String { format!("Hello, {}! You've been greeted from Rust!", name) }
-
-
plugins.rs
use tauri::Runtime; pub trait KoiTauriPlugin { fn init_plugin(self) -> Self; } impl<R: Runtime> KoiTauriPlugin for tauri::Builder<R> { // 初始化插件 fn init_plugin(self) -> Self { self.plugin(tauri_plugin_opener::init()) } }
-
home.vue 验证修改后的目录结构 src/views/home/index.vue
<template> <div> <el-container> <el-header> <h1>控制台效果</h1> </el-header> <el-main> <el-row> <el-col :span="24"> <el-card> <div class="console"> <div class="console-line" v-for="(line, index) in consoleLines" :key