【A11】 labelprinter(Tauri 版)新建步骤

labelprinter 是一个完整的 Tauri 应用 (有自己的 tauri.conf.json、build.rs、前端资源),它仍然作为 workspace 的一个成员存在,独立运行、独立调试。

1. 建目录

在 workspace 根目录下执行:

bash 复制代码
cargo new labelprinter --bin

生成:

text 复制代码
labelprinter/
  Cargo.toml
  src/main.rs

2. 加入 workspace

默认自动加入workspace,检查根 Cargo.toml:

toml 复制代码
[workspace]
members = [
    "bridge",
    "a11core",
    "labelprinter",
]

[package]
name = "app"
version = "0.1.0"
edition = "2024"

[dependencies]
bridge = { path = "bridge" }
a11core = { path = "a11core" }
tauri = { version = "2", features = [] }

[build-dependencies]
tauri-build = { version = "2", features = [] }

注意:根 app 不需要依赖 labelprinter,保持它独立。

3. labelprinter/Cargo.toml

toml 复制代码
[package]
name = "labelprinter"
version = "0.1.0"
edition = "2024"
description = "资产标签打印"

[build-dependencies]
tauri-build = { version = "2", features = [] }

[dependencies]
tauri = { version = "2", features = [] }
# 后面接硬件采集 / Excel / DB 时再加:
# hwcollect = { path = "../hwcollect" }
# excelgen  = { path = "../excelgen" }
# pcdb      = { path = "../pcdb" }

[[bin]]
name = "labelprinter"
path = "src/main.rs"

要点:

  • tauri 放 [dependencies](运行时用),tauri-build 放 [build-dependencies](编译期用),二者不能混。
  • [[bin]] 显式写出来,避免以后加 lib.rs 时产生歧义。

4. labelprinter/build.rs

rust 复制代码
fn main() {
    tauri_build::build();
}

没有这个文件,tauri::generate_context!() 会报找不到上下文。

5. labelprinter/src/main.rs

rust 复制代码
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

fn main() {
    tauri::Builder::default()
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

generate_context!() 在编译期会读 labelprinter/tauri.conf.json,所以配置文件必须在 labelprinter/ 目录下(不是 workspace 根)。

6. labelprinter/tauri.conf.json

最小可用版本:

json 复制代码
{
  "$schema": "https://schema.tauri.app/config/2",
  "productName": "labelprinter",
  "version": "0.1.0",
  "identifier": "com.example.labelprinter",
  "build": {
    "frontendDist": "https://weida.dothantech.com/#/editor",
    "devUrl": "https://weida.dothantech.com/#/editor"
  },
  "app": {
    "windows": [
      {
        "title": "资产标签打印",
        "width": 1280,
        "height": 900
      }
    ],
    "security": {
      "csp": null
    }
  },
  "bundle": {
    "active": true,
    "targets": "all",
    "icon": [
      "icons/32x32.png",
      "icons/128x128.png",
      "icons/icon.ico",
      "icons/icon.icns"
    ]
  }
}

frontendDist 指向 labelprinter/../dist,也就是 workspace 根下的 dist/;你也可以改成自己的前端产物路径,或者用 devUrl 指向 http://localhost:1420 做开发。

7. 图标(必须)

Tauri 要求 icons/ 至少有几个占位图标,否则 tauri build 会失败。最省事的做法:

bash 复制代码
cd labelprinter
cargo tauri icon path/to/any.png

或者手动放几个:

  • labelprinter/icons/32x32.png
  • labelprinter/icons/128x128.png
  • labelprinter/icons/icon.ico(Windows)
  • labelprinter/icons/icon.icns(macOS)

只是 cargo run 调试时,缺图标一般不致命,但一旦做打包就会报错,所以最好一开始放上。

8. 前端产物

因为 frontendDist 指向 ../dist,要么:

  • 放一个静态占位 :在 workspace 根建 dist/index.html,内容随便:
html 复制代码
<!doctype html>
<html><body><h1>labelprinter</h1></body></html>
  • 或改成 devUrl 指向你已有的前端 dev server。

调试纯后端逻辑(采集/Excel/DB/打印)时,用这个占位 HTML 完全够,不需要真前端。

9. 目录最终结构

text 复制代码
app/
  Cargo.toml
  src/main.rs           # 主 app(也是 Tauri 应用)
  build.rs
  tauri.conf.json
  ...
  labelprinter/
    Cargo.toml
    build.rs
    tauri.conf.json
    icons/
      32x32.png
      128x128.png
      icon.ico
      icon.icns
    src/main.rs
  dist/
    index.html          # 可选的占位前端

10. 运行与调试

bash 复制代码
# 只跑 labelprinter 这个 Tauri 应用
cargo run -p labelprinter

# 只编译它,不跑
cargo check -p labelprinter

# 跑主程序(app)
cargo run -p app

# 全 workspace 检查
cargo check --workspace

关键点解说

  1. 为什么 labelprinter 要独立 tauri.conf.json?

    Tauri 的 generate_context!() 宏是按 CARGO_MANIFEST_DIR 解析配置的,每个 Tauri 应用都必须有自己的一份。workspace 里的其他成员(bridge、a11core)不需要,因为它们不是 Tauri 应用。

  2. 和主 app 的关系

    • 两个都是独立 Tauri 应用,各跑各的。
    • labelprinter 想复用主 app 的业务逻辑,通过 lib crate(bridge / a11core) ,而不是直接 use app::...。
    • 现在主 app 也不依赖 labelprinter,双向解耦。
  3. 和最初的"纯 bin"方案差异

    • 纯 bin 方案的 [[bin]] 只跑命令行逻辑,简单。
    • Tauri 版多了 build.rs、tauri.conf.json、icons/、前端产物这四样,缺一不可。
    • 但因为它仍是 workspace 成员,cargo run -p labelprinter 用法完全一样,不影响主 app。
  4. 将来并入 app 的路径

    等 labelprinter 调试成熟,有两种"人工并入"方式:

    • 把业务逻辑搬成 lib :把采集/Excel/DB/打印的代码抽到 bridge 或新的 labelcore lib,让主 app 直接调用,Tauri 界面部分可以保留成独立成员或整体删掉。
    • 整体并入主 app :把 labelprinter/src/* 搬进 app/src/label/,把 labelprinter 窗口作为主 app 的一个页面。此时注意别让两个 tauri.conf.json 冲突------主 app 只用它自己的。
  5. 常见坑

    • tauri-build 写进 [dependencies] 而不是 [build-dependencies] → 编译报错。
    • 忘记 build.rs → generate_context! 找不到上下文。
    • tauri.conf.json 放在 workspace 根 → generate_context! 找不到。
    • 缺 icons/ → cargo run 可能能过,tauri build 一定失败。
    • frontendDist 路径写错 → 启动时白屏或报 frontendDist not found。

按这个步骤建完,cargo run -p labelprinter 就能弹出一个独立窗口,和其他成员互不干扰。后续把 hwcollect/excelgen/pcdb 从"概念"落成真 crate 时,只要在 labelprinter/Cargo.toml 里加上 path 依赖,main.rs 里 use hwcollect::... 即可。

相关推荐
SomeOtherTime37 分钟前
番外篇(第 12 讲):走向工业界深水区——从物理定律看 Rust 在真实工程中的“降维打击”与“阿喀琉斯之踵”
开发语言·rust
Kapaseker2 小时前
Rust 桌面开发到底怎么选?先看这篇文章!
rust
奶油话梅糖2 小时前
容器启停 2.5 毫秒:11 天 1,700 Star 的 Kiri,用一个 900 KB 的 PID 1 干掉了 runc 和 dockerd
前端·人工智能·rust
wflynn15 小时前
GitHub 今日推荐|kiri:clone3 系统调用直达容器,2.5 毫秒启动无 runc 中间层
docker·rust·kubernetes·开源·github·containers
SomeOtherTime18 小时前
第 4 讲:突破静态枷锁——智能指针(Box/Rc/Arc)与内部可变性(Cell/RefCell/UnsafeCell)的内存暗道
rust
xx z19 小时前
directx修复工具下载(DirectX Repair 增强版 使用说明)
windows·系统·工具·软件
xx z21 小时前
pcl2启动器下载(Minecraft PCL2 启动器 最新版 安装与使用说明)
windows·游戏·工具·软件
凯瑟琳.奥古斯特1 天前
我开源了一个 Windows 截图取字工具:全局热键 + 离线 OCR + 标注打码,不上传任何数据
windows·ocr
SomeOtherTime1 天前
第 9 讲:异步并发与 Pin 的底层解剖——async/await 无栈协程状态机脱糖、自引用死锁的终极救赎与 Future/Waker 驱动模型
rust
wflynn2 天前
GitHub 今日推荐|Stela:在 PC 上直接启动 macOS 的翻译层
rust·开源·github