Tauri教程-实战篇-第三节 定义系统托盘、日志

请添加图片描述

“如果结果不如你所愿,就在尘埃落定前奋力一搏。”——《夏目友人帐》
“有些事不是看到了希望才去坚持,而是因为坚持才会看到希望。”——《十宗罪》
“维持现状意味着空耗你的努力和生命。”——纪伯伦

Tauri 技术教程 * 第六章 Tauri的实战教程
第三节 定义系统托盘、日志

一. 前言

本章节我们将学习如何为我们的应用添加系统托盘功能和服务日志。

二. 修改项目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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Elcker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值