最近看到很多推荐Tauri的文章,顺便学习下写了个小工具,用到以下知识
- 前端 vite + vue3
- 后端 rust
后端
参照Tauri官网https://tauri.app/zh-cn/v1/guides/getting-started/setup/vite生成模板
获取系统信息使用 sysinfo 库, https://docs.rs/sysinfo/0.27.7/sysinfo/
总体Rust代码很少,主要是前端
main.rs代码
#![cfg_attr(
all(not(debug_assertions), target_os = "windows"),
windows_subsystem = "windows"
)]
use serde::{
Deserialize, Serialize};
use sysinfo::{
System, SystemExt, CpuExt};
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[derive(Serialize, Deserialize)]
struct SystemInfo {
memory_total: f32,
memory_used: f32,
cpu_used: f32,
hostname: String
}
#[tauri::command]
fn system_info() -> SystemInfo {
let mut sys = System::new_all();
sys.refresh_all();
SystemInfo {
memory_total: sys.total_memory() as f32 / (1024 * 1024 * 1024) as f32,
memory_used: sys.used_memory() as f32 / (1024 * 1024 * 1024) as f32,
cpu_used: sys.global_cpu_info().cpu_usage(),
hostname: sys.host_name().unwrap(),
}
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![system_info])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
前端
前端用到Bootstrap样式 + echarts, 定时调用rust中
system_info
函数更新
<script setup>
import