feat: 支持4种OpenBridge主题切换,统一项目命名和端口配置

- 添加 OpenBridge 四种主题切换 (day/bright/dusk/night)
- 实现 DJB2 hash 算法生成项目专用端口 (14323)
- 统一项目名称为 openbridge-token-usage-viewer
- 更新 Tauri 应用名称、sidecar 命名和窗口标题
- 开发服务器端口从 3000 改为基于项目名称的稳定端口
This commit is contained in:
2026-01-22 13:02:43 +08:00
parent d077dfdd90
commit b520cdaf35
10 changed files with 189 additions and 36 deletions

22
src-tauri/Cargo.lock generated
View File

@@ -47,17 +47,6 @@ version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]]
name = "app-desktop"
version = "0.1.0"
dependencies = [
"serde",
"tauri",
"tauri-build",
"tauri-plugin-shell",
"tokio",
]
[[package]]
name = "atk"
version = "0.18.2"
@@ -2071,6 +2060,17 @@ dependencies = [
"pathdiff",
]
[[package]]
name = "openbridge-token-usage-viewer"
version = "0.1.0"
dependencies = [
"serde",
"tauri",
"tauri-build",
"tauri-plugin-shell",
"tokio",
]
[[package]]
name = "option-ext"
version = "0.2.0"

View File

@@ -1,7 +1,7 @@
[package]
name = "app-desktop"
name = "openbridge-token-usage-viewer"
version = "0.1.0"
description = "A Tauri App"
description = "OpenBridge Token Usage Viewer"
authors = ["imbytecat"]
edition = "2021"
@@ -11,7 +11,7 @@ edition = "2021"
# The `_lib` suffix may seem redundant but it is necessary
# to make the lib name unique and wouldn't conflict with the bin name.
# This seems to be only an issue on Windows, see https://github.com/rust-lang/cargo/issues/8519
name = "app_desktop_lib"
name = "openbridge_token_usage_viewer_lib"
crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies]

View File

@@ -2,5 +2,5 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
fn main() {
app_desktop_lib::run()
openbridge_token_usage_viewer_lib::run()
}

View File

@@ -5,14 +5,32 @@ use tauri::Manager;
use tauri_plugin_shell::process::{CommandChild, CommandEvent};
use tauri_plugin_shell::ShellExt;
// ===== 项目配置 =====
/// 项目名称 - 用于生成稳定端口和 sidecar 命名
const PROJECT_NAME: &str = "openbridge-token-usage-viewer";
/// DJB2 Hash 算法 - 将项目名称转换为稳定端口
fn djb2_hash(s: &str) -> u32 {
let mut hash: u32 = 5381;
for c in s.bytes() {
hash = hash.wrapping_shl(5).wrapping_add(hash).wrapping_add(c as u32);
}
hash
}
/// 计算项目专用端口 (范围: 10000-60000)
fn get_project_port() -> u16 {
const PORT_MIN: u16 = 10000;
const PORT_RANGE: u32 = 50000;
PORT_MIN + (djb2_hash(PROJECT_NAME) % PORT_RANGE) as u16
}
// ===== 配置常量 =====
/// Sidecar App 启动超时时间(秒)
const STARTUP_TIMEOUT_SECS: u64 = 30;
/// 默认起始端口
const DEFAULT_PORT: u16 = 3000;
/// 端口扫描范围(从起始端口开始扫描的端口数量)
const PORT_SCAN_RANGE: u16 = 100;
@@ -23,7 +41,7 @@ const DEFAULT_WINDOW_WIDTH: f64 = 1200.0;
const DEFAULT_WINDOW_HEIGHT: f64 = 800.0;
/// 窗口标题
const WINDOW_TITLE: &str = "Tauri App";
const WINDOW_TITLE: &str = "OpenBridge Token Usage Viewer";
// ===== 数据结构 =====
@@ -72,15 +90,20 @@ fn show_error_dialog(message: &str) {
pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
// 检测是否为开发模式
let is_dev = cfg!(debug_assertions);
// 获取项目专用端口
let project_port = get_project_port();
if is_dev {
// 开发模式:直接创建窗口连接到 Vite 开发服务器
println!("🔧 开发模式");
println!("📌 项目: {}", PROJECT_NAME);
println!("📌 端口: {}", project_port);
let dev_url = format!("http://localhost:{}", project_port);
match tauri::WebviewWindowBuilder::new(
&app_handle,
"main",
tauri::WebviewUrl::External("http://localhost:3000".parse().unwrap()),
tauri::WebviewUrl::External(dev_url.parse().unwrap()),
)
.title(WINDOW_TITLE)
.inner_size(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT)
@@ -99,13 +122,14 @@ pub fn spawn_sidecar(app_handle: tauri::AppHandle) {
// 生产模式:启动 sidecar 二进制
tauri::async_runtime::spawn(async move {
println!("🚀 生产模式");
println!("📌 项目: {}", PROJECT_NAME);
// 查找可用端口
let port = find_available_port(DEFAULT_PORT).await;
println!("使用端口: {}", port);
// 查找可用端口 (从项目端口开始扫描)
let port = find_available_port(project_port).await;
println!("📌 端口: {}", port);
// 启动 sidecar
let sidecar = match app_handle.shell().sidecar("app") {
// 启动 sidecar (使用项目名称作为 sidecar 名称)
let sidecar = match app_handle.shell().sidecar(PROJECT_NAME) {
Ok(cmd) => cmd.env("PORT", port.to_string()),
Err(e) => {
eprintln!("✗ 无法找到 sidecar: {}", e);

View File

@@ -1,8 +1,8 @@
{
"$schema": "https://schema.tauri.app/config/2",
"productName": "app-desktop",
"productName": "openbridge-token-usage-viewer",
"version": "0.1.0",
"identifier": "com.imbytecat.app-desktop",
"identifier": "com.imbytecat.openbridge-token-usage-viewer",
"app": {
"withGlobalTauri": true,
"windows": [],
@@ -20,6 +20,6 @@
"icons/icon.icns",
"icons/icon.ico"
],
"externalBin": ["binaries/app"]
"externalBin": ["binaries/openbridge-token-usage-viewer"]
}
}