
往期回顾
- Rust 实战四 | Traui2+Vue3+Rspack 开发桌面应用:通配符掩码计算器
- Rust 实战三 | HTTP 服务开发及 Web 框架推荐
- Rust 实战二 | 开发简易版命令行工具 grep
- Rust 实战一 | 用 RustRover 开发猜数字游戏
- Rust 安装与版本更新
代码开源地址: github.com/0604hx/rust...、通配符掩码计算器
🛠️ 完善功能
在上一篇博文,我们还有发送到记事本
功能没有实现。该功能完成的操作内容有:
- 将用户输入的IP、掩码以及计算的结果写入到
txt
文件 - 调用系统默认程序打开上述文件
rust
use tauri_plugin_opener::OpenerExt;
#[tauri::command]
fn save_txt(ip:String, mask:String, ips:Vec<String>, filename: Option<String>, app: tauri::AppHandle)-> Result<String, String> {
// 获取当前 exe 路径
let exe_path = std::env::current_exe().map_err(|e| e.to_string())?;
// 获取 exe 所在目录
let exe_dir = exe_path.parent().ok_or("无法获取程序所在目录")?;
let filename = filename.unwrap_or_else(|| "temp.txt".to_string());
let file_path = exe_dir.join(&filename);
let mut lines = String::new();
lines.push_str(&format!("{}\n\n", app.package_info().name.clone()));
lines.push_str(&format!("程序作者\t{}\n", app.package_info().authors));
lines.push_str("开源代码\thttps://gitcode.com/ssrc0604hx/tauri-wildcard-mask\n");
lines.push_str("\n");
lines.push_str(&format!("IP地址\t{}\n", ip));
lines.push_str(&format!("掩码地址\t{}\n", mask));
lines.push_str("匹配地址\n");
for row in ips {
lines.push_str(&format!("\t\t{}\n", row));
}
fs::write(&file_path, lines).map_err(|e| e.to_string())?;
if let Err(e) = app.opener().open_path(file_path.to_string_lossy().to_string(), None::<&str>) {
eprintln!("打开文件{}失败:{}", file_path.display(), e);
}
Ok(filename)
}
执行后的,在 exe
同级目录下自动创建temp.txt
文件,内容如下:
🏷️ 配置图标
我们的程序写好后,就可以打包给更多人使用。不过,在此之前,我们应该考虑配置应用的图标。应用图标
作为用户与应用交互的 "第一印象",其重要性远超单纯的视觉标识,它在用户体验、品牌传播、市场竞争等多个维度都扮演着关键角色。
当然,你也可以直接用 Tauri 默认的图标😄。
我的图标源自iconfont(关键词计算
),可参考以下步骤下载不同尺寸的 PNG 图片。
打包时用到的图标尺寸在
src-tauri\tauri.conf.json
文件中可以查到:
json
"bundle": {
"active": true,
"targets": ["app"],
"icon": [
"icons/32x32.png",
"icons/128x128.png",
"icons/128x128@2x.png",
"icons/icon.icns",
"icons/icon.ico"
]
}
然后可以到www.aconvert.com将 PNG 转换为 ico、icns 格式的文件。最后,我们将用到的图标文件放置在icons
目录下。
☠️ build 后被识别为威胁
通过pnpm tauri build
打包(我本地耗时3分半左右)后,在release
内运行没有问题,可是拷贝到其他目录再次运行,就会被识别为威胁,程序被自动清理😂。 经排查,发现是
src-tauri\Cargo.toml
开启了opt-level = "z"
极限压缩 exe 体积导致(个人主观判定),将配置换成s
后,没有被拦截(此时体积增大 450KB 左右)。

ChatGPT 给出的建议有:
- 换优化级
- 用
opt-level = "3"
或"s"
,避免"z"
的极限压缩
- 用
- 给 exe 签名
- 使用
signtool
或第三方证书签名,SmartScreen 和 Defender 都会降低误报概率
- 使用
- 改变嵌入资源方式
- 部分前端资源外置(减少 exe 中的高熵数据块)
- 提交误报
- 上传到微软误报申诉:www.microsoft.com/en-us/wdsi/...