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

labelprinter 是一个完整的 Tauri 应用 (有自己的 tauri.conf.jsonbuild.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.rstauri.conf.jsonicons/、前端产物这四样,缺一不可。
    • 但因为它仍是 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.rsgenerate_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.rsuse hwcollect::... 即可。

相关推荐
孙启超2 小时前
【AI开发之Rust】第 8 课:泛型、trait 与 trait 对象
开发语言·后端·rust
Punchline2782 小时前
解决 Windows 环境变量 Path 设置时提示“字符太长”的完整指南
windows·环境变量·path
蓝宝石的傻话2 小时前
MiBee Eye Notebook:把闲置笔记本变成一台带麦克风的网络摄像头
数码相机·rust
chushiyunen3 小时前
claude code笔记(二)、claude桌面端(内网不好用)
windows
d_benhua3 小时前
以太网测试步骤
windows
小灰灰搞电子3 小时前
Rust Once 、OnceLock、LazyLock 一次性初始化详解
开发语言·后端·rust
冯一川4 小时前
DeepSeek在Windows系统上部署
windows·python
qq_452396234 小时前
第八篇:《智能指针与内部可变性:Rust 的进阶内存管理》
rust
解道Jdon5 小时前
JDK 27发布:紧凑对象头、G1默认、量子安全TLS
ide·windows·git·svn·eclipse·github·visual studio