2025-08-30 21:54:11 +08:00
|
|
|
|
mod app_config;
|
2025-10-01 21:23:55 +08:00
|
|
|
|
mod claude_plugin;
|
2025-10-08 22:34:58 +08:00
|
|
|
|
mod claude_mcp;
|
2025-10-09 21:08:42 +08:00
|
|
|
|
mod mcp;
|
2025-08-30 21:54:11 +08:00
|
|
|
|
mod codex_config;
|
2025-08-27 11:00:53 +08:00
|
|
|
|
mod commands;
|
2025-08-23 20:15:10 +08:00
|
|
|
|
mod config;
|
2025-10-05 23:33:07 +08:00
|
|
|
|
mod import_export;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
mod migration;
|
2025-08-23 20:15:10 +08:00
|
|
|
|
mod provider;
|
2025-09-20 21:20:07 +08:00
|
|
|
|
mod settings;
|
2025-08-23 20:15:10 +08:00
|
|
|
|
mod store;
|
feat: Implement Speed Test Function
* feat: add unified endpoint speed test for API providers
Add a comprehensive endpoint latency testing system that allows users to:
- Test multiple API endpoints concurrently
- Auto-select the fastest endpoint based on latency
- Add/remove custom endpoints dynamically
- View latency results with color-coded indicators
Backend (Rust):
- Implement parallel HTTP HEAD requests with configurable timeout
- Handle various error scenarios (timeout, connection failure, invalid URL)
- Return structured latency data with status codes
Frontend (React):
- Create interactive speed test UI component with auto-sort by latency
- Support endpoint management (add/remove custom endpoints)
- Extract and update Codex base_url from TOML configuration
- Integrate with provider presets for default endpoint candidates
This feature improves user experience when selecting optimal API endpoints,
especially useful for users with multiple provider options or proxy setups.
* refactor: convert endpoint speed test to modal dialog
- Transform EndpointSpeedTest component into a modal dialog
- Add "Advanced" button next to base URL input to open modal
- Support ESC key and backdrop click to close modal
- Apply Linear design principles: minimal styling, clean layout
- Remove unused showBaseUrlInput variable
- Implement same modal pattern for both Claude and Codex
* fix: prevent modal cascade closing when ESC is pressed
- Add state checks to prevent parent modal from closing when child modals (endpoint speed test or template wizard) are open
- Update ESC key handler dependencies to track all modal states
- Ensures only the topmost modal responds to ESC key
* refactor: unify speed test panel UI with project design system
UI improvements:
- Update modal border radius from rounded-lg to rounded-xl
- Unify header padding from px-6 py-4 to p-6
- Change speed test button color to blue theme (bg-blue-500) for consistency
- Update footer background from bg-gray-50 to bg-gray-100
- Style "Done" button as primary action button with blue theme
- Adjust footer button spacing and hover states
Simplify endpoint display:
- Remove endpoint labels (e.g., "Current Address", "Custom 1")
- Display only URL for cleaner interface
- Clean up all label-related logic:
* Remove label field from EndpointCandidate interface
* Remove label generation in buildInitialEntries function
* Remove label handling in useEffect merge logic
* Remove label generation in handleAddEndpoint
* Remove label parameters from claudeSpeedTestEndpoints
* Remove label parameters from codexSpeedTestEndpoints
* refactor: improve endpoint list UI consistency
- Show delete button for all endpoints on hover for uniform UI
- Change selected state to use blue theme matching main interface:
* Blue border (border-blue-500) for selected items
* Light blue background (bg-blue-50/dark:bg-blue-900/20)
* Blue indicator dot (bg-blue-500/dark:bg-blue-400)
- Switch from compact list (space-y-px) to card-based layout (space-y-2)
- Add rounded corners to each endpoint item for better visual separation
* feat: persist custom endpoints to settings.json
- Extend AppSettings to store custom endpoints for Claude and Codex
- Add Tauri commands: get/add/remove/update custom endpoints
- Update frontend API with endpoint persistence methods
- Modify EndpointSpeedTest to load/save custom endpoints via API
- Track endpoint last used time for future sorting/cleanup
- Store endpoints per app type in settings.json instead of localStorage
* - feat(types): add Provider.meta and ProviderMeta (snake_case) with custom_endpoints map
- feat(provider-form): persist custom endpoints on provider create by merging EndpointSpeedTest’s custom URLs into meta.custom_endpoints on submit
- feat(endpoint-speed-test): add onCustomEndpointsChange callback emitting normalized custom URLs; wire it for both Claude/Codex modals
- fix(api): send alias param names (app/appType/app_type and provider_id/providerId) in Tauri invokes to avoid “missing providerId” with older backends
- storage: custom endpoints are stored in ~/.cc-switch/config.json under providers[<id>].meta.custom_endpoints (not in settings.json)
- behavior: edit flow remains immediate writes; create flow now writes once via addProvider, removing the providerId dependency during creation
* feat: add endpoint candidates support and code formatting improvements
- Add endpointCandidates field to ProviderPreset and CodexProviderPreset interfaces
- Integrate preset endpoint candidates into speed test endpoint selection
- Add multiple endpoint options for PackyCode providers (Claude & Codex)
- Apply consistent code formatting (trailing commas, line breaks)
- Improve template value type safety and readability
* refactor: improve endpoint management button UX
Replace ambiguous "Advanced" text with intuitive "Manage & Test" label accompanied by Zap icon, making the endpoint management panel entry point more discoverable and self-explanatory for both Claude and Codex configurations.
* - merge: merge origin/main, resolve conflicts and preserve both feature sets
- feat(tauri): register import/export and file dialogs; keep endpoint speed test and custom endpoints
- feat(api): add updateTrayMenu and onProviderSwitched; wire import/export APIs
- feat(types): extend global API declarations (import/export)
- chore(presets): GLM preset supports both new and legacy model keys
- chore(rust): add chrono dependency; refresh lockfile
---------
Co-authored-by: Jason <farion1231@gmail.com>
2025-10-07 19:14:32 +08:00
|
|
|
|
mod speedtest;
|
2025-08-23 20:15:10 +08:00
|
|
|
|
|
|
|
|
|
|
use store::AppState;
|
2025-09-06 16:21:21 +08:00
|
|
|
|
use tauri::{
|
|
|
|
|
|
menu::{CheckMenuItem, Menu, MenuBuilder, MenuItem},
|
2025-09-14 21:55:41 +08:00
|
|
|
|
tray::{TrayIconBuilder, TrayIconEvent},
|
2025-09-06 16:21:21 +08:00
|
|
|
|
};
|
2025-09-29 17:03:13 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
use tauri::{ActivationPolicy, RunEvent};
|
2025-09-06 16:21:21 +08:00
|
|
|
|
use tauri::{Emitter, Manager};
|
|
|
|
|
|
|
|
|
|
|
|
/// 创建动态托盘菜单
|
|
|
|
|
|
fn create_tray_menu(
|
|
|
|
|
|
app: &tauri::AppHandle,
|
|
|
|
|
|
app_state: &AppState,
|
|
|
|
|
|
) -> Result<Menu<tauri::Wry>, String> {
|
|
|
|
|
|
let config = app_state
|
|
|
|
|
|
.config
|
|
|
|
|
|
.lock()
|
|
|
|
|
|
.map_err(|e| format!("获取锁失败: {}", e))?;
|
|
|
|
|
|
|
|
|
|
|
|
let mut menu_builder = MenuBuilder::new(app);
|
|
|
|
|
|
|
2025-09-14 16:07:51 +08:00
|
|
|
|
// 顶部:打开主界面
|
|
|
|
|
|
let show_main_item = MenuItem::with_id(app, "show_main", "打开主界面", true, None::<&str>)
|
|
|
|
|
|
.map_err(|e| format!("创建打开主界面菜单失败: {}", e))?;
|
|
|
|
|
|
menu_builder = menu_builder.item(&show_main_item).separator();
|
|
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
// 直接添加所有供应商到主菜单(扁平化结构,更简单可靠)
|
|
|
|
|
|
if let Some(claude_manager) = config.get_manager(&crate::app_config::AppType::Claude) {
|
|
|
|
|
|
// 添加Claude标题(禁用状态,仅作为分组标识)
|
|
|
|
|
|
let claude_header =
|
|
|
|
|
|
MenuItem::with_id(app, "claude_header", "─── Claude ───", false, None::<&str>)
|
|
|
|
|
|
.map_err(|e| format!("创建Claude标题失败: {}", e))?;
|
|
|
|
|
|
menu_builder = menu_builder.item(&claude_header);
|
|
|
|
|
|
|
|
|
|
|
|
if !claude_manager.providers.is_empty() {
|
|
|
|
|
|
for (id, provider) in &claude_manager.providers {
|
|
|
|
|
|
let is_current = claude_manager.current == *id;
|
|
|
|
|
|
let item = CheckMenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
format!("claude_{}", id),
|
|
|
|
|
|
&provider.name,
|
|
|
|
|
|
true,
|
|
|
|
|
|
is_current,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|e| format!("创建菜单项失败: {}", e))?;
|
|
|
|
|
|
menu_builder = menu_builder.item(&item);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 没有供应商时显示提示
|
|
|
|
|
|
let empty_hint = MenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
"claude_empty",
|
|
|
|
|
|
" (无供应商,请在主界面添加)",
|
|
|
|
|
|
false,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|e| format!("创建Claude空提示失败: {}", e))?;
|
|
|
|
|
|
menu_builder = menu_builder.item(&empty_hint);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Some(codex_manager) = config.get_manager(&crate::app_config::AppType::Codex) {
|
|
|
|
|
|
// 添加Codex标题(禁用状态,仅作为分组标识)
|
|
|
|
|
|
let codex_header =
|
|
|
|
|
|
MenuItem::with_id(app, "codex_header", "─── Codex ───", false, None::<&str>)
|
|
|
|
|
|
.map_err(|e| format!("创建Codex标题失败: {}", e))?;
|
|
|
|
|
|
menu_builder = menu_builder.item(&codex_header);
|
|
|
|
|
|
|
|
|
|
|
|
if !codex_manager.providers.is_empty() {
|
|
|
|
|
|
for (id, provider) in &codex_manager.providers {
|
|
|
|
|
|
let is_current = codex_manager.current == *id;
|
|
|
|
|
|
let item = CheckMenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
format!("codex_{}", id),
|
|
|
|
|
|
&provider.name,
|
|
|
|
|
|
true,
|
|
|
|
|
|
is_current,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|e| format!("创建菜单项失败: {}", e))?;
|
|
|
|
|
|
menu_builder = menu_builder.item(&item);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 没有供应商时显示提示
|
|
|
|
|
|
let empty_hint = MenuItem::with_id(
|
|
|
|
|
|
app,
|
|
|
|
|
|
"codex_empty",
|
|
|
|
|
|
" (无供应商,请在主界面添加)",
|
|
|
|
|
|
false,
|
|
|
|
|
|
None::<&str>,
|
|
|
|
|
|
)
|
|
|
|
|
|
.map_err(|e| format!("创建Codex空提示失败: {}", e))?;
|
|
|
|
|
|
menu_builder = menu_builder.item(&empty_hint);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 分隔符和退出菜单
|
|
|
|
|
|
let quit_item = MenuItem::with_id(app, "quit", "退出", true, None::<&str>)
|
|
|
|
|
|
.map_err(|e| format!("创建退出菜单失败: {}", e))?;
|
|
|
|
|
|
|
|
|
|
|
|
menu_builder = menu_builder.separator().item(&quit_item);
|
|
|
|
|
|
|
|
|
|
|
|
menu_builder
|
|
|
|
|
|
.build()
|
|
|
|
|
|
.map_err(|e| format!("构建菜单失败: {}", e))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-29 17:03:13 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
fn apply_tray_policy(app: &tauri::AppHandle, dock_visible: bool) {
|
|
|
|
|
|
let desired_policy = if dock_visible {
|
|
|
|
|
|
ActivationPolicy::Regular
|
|
|
|
|
|
} else {
|
|
|
|
|
|
ActivationPolicy::Accessory
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(err) = app.set_dock_visibility(dock_visible) {
|
|
|
|
|
|
log::warn!("设置 Dock 显示状态失败: {}", err);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(err) = app.set_activation_policy(desired_policy) {
|
|
|
|
|
|
log::warn!("设置激活策略失败: {}", err);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
/// 处理托盘菜单事件
|
|
|
|
|
|
fn handle_tray_menu_event(app: &tauri::AppHandle, event_id: &str) {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::info!("处理托盘菜单事件: {}", event_id);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
match event_id {
|
2025-09-14 16:07:51 +08:00
|
|
|
|
"show_main" => {
|
|
|
|
|
|
if let Some(window) = app.get_webview_window("main") {
|
2025-09-26 20:18:11 +08:00
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
|
{
|
|
|
|
|
|
let _ = window.set_skip_taskbar(false);
|
|
|
|
|
|
}
|
2025-09-14 16:07:51 +08:00
|
|
|
|
let _ = window.unminimize();
|
|
|
|
|
|
let _ = window.show();
|
|
|
|
|
|
let _ = window.set_focus();
|
2025-09-29 17:03:13 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
{
|
|
|
|
|
|
apply_tray_policy(app, true);
|
|
|
|
|
|
}
|
2025-09-14 16:07:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-09-06 16:21:21 +08:00
|
|
|
|
"quit" => {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::info!("退出应用");
|
2025-09-06 16:21:21 +08:00
|
|
|
|
app.exit(0);
|
|
|
|
|
|
}
|
|
|
|
|
|
id if id.starts_with("claude_") => {
|
|
|
|
|
|
let provider_id = id.strip_prefix("claude_").unwrap();
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::info!("切换到Claude供应商: {}", provider_id);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行切换
|
|
|
|
|
|
let app_handle = app.clone();
|
|
|
|
|
|
let provider_id = provider_id.to_string();
|
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
|
if let Err(e) = switch_provider_internal(
|
|
|
|
|
|
&app_handle,
|
|
|
|
|
|
crate::app_config::AppType::Claude,
|
|
|
|
|
|
provider_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
.await
|
2025-09-18 09:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
log::error!("切换Claude供应商失败: {}", e);
|
|
|
|
|
|
}
|
2025-09-06 16:21:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
id if id.starts_with("codex_") => {
|
|
|
|
|
|
let provider_id = id.strip_prefix("codex_").unwrap();
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::info!("切换到Codex供应商: {}", provider_id);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 执行切换
|
|
|
|
|
|
let app_handle = app.clone();
|
|
|
|
|
|
let provider_id = provider_id.to_string();
|
|
|
|
|
|
tauri::async_runtime::spawn(async move {
|
|
|
|
|
|
if let Err(e) = switch_provider_internal(
|
|
|
|
|
|
&app_handle,
|
|
|
|
|
|
crate::app_config::AppType::Codex,
|
|
|
|
|
|
provider_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
.await
|
2025-09-18 09:33:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
log::error!("切换Codex供应商失败: {}", e);
|
|
|
|
|
|
}
|
2025-09-06 16:21:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::warn!("未处理的菜单事件: {}", event_id);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-14 16:07:51 +08:00
|
|
|
|
//
|
|
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
/// 内部切换供应商函数
|
|
|
|
|
|
async fn switch_provider_internal(
|
|
|
|
|
|
app: &tauri::AppHandle,
|
|
|
|
|
|
app_type: crate::app_config::AppType,
|
|
|
|
|
|
provider_id: String,
|
|
|
|
|
|
) -> Result<(), String> {
|
|
|
|
|
|
if let Some(app_state) = app.try_state::<AppState>() {
|
|
|
|
|
|
// 在使用前先保存需要的值
|
|
|
|
|
|
let app_type_str = app_type.as_str().to_string();
|
|
|
|
|
|
let provider_id_clone = provider_id.clone();
|
|
|
|
|
|
|
|
|
|
|
|
crate::commands::switch_provider(
|
|
|
|
|
|
app_state.clone().into(),
|
|
|
|
|
|
Some(app_type),
|
|
|
|
|
|
None,
|
|
|
|
|
|
None,
|
|
|
|
|
|
provider_id,
|
|
|
|
|
|
)
|
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
|
|
// 切换成功后重新创建托盘菜单
|
|
|
|
|
|
if let Ok(new_menu) = create_tray_menu(app, app_state.inner()) {
|
|
|
|
|
|
if let Some(tray) = app.tray_by_id("main") {
|
|
|
|
|
|
if let Err(e) = tray.set_menu(Some(new_menu)) {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::error!("更新托盘菜单失败: {}", e);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 发射事件到前端,通知供应商已切换
|
|
|
|
|
|
let event_data = serde_json::json!({
|
|
|
|
|
|
"appType": app_type_str,
|
|
|
|
|
|
"providerId": provider_id_clone
|
|
|
|
|
|
});
|
|
|
|
|
|
if let Err(e) = app.emit("provider-switched", event_data) {
|
2025-09-13 15:38:01 +08:00
|
|
|
|
log::error!("发射供应商切换事件失败: {}", e);
|
2025-09-06 16:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 更新托盘菜单的Tauri命令
|
|
|
|
|
|
#[tauri::command]
|
|
|
|
|
|
async fn update_tray_menu(
|
|
|
|
|
|
app: tauri::AppHandle,
|
|
|
|
|
|
state: tauri::State<'_, AppState>,
|
|
|
|
|
|
) -> Result<bool, String> {
|
|
|
|
|
|
if let Ok(new_menu) = create_tray_menu(&app, state.inner()) {
|
|
|
|
|
|
if let Some(tray) = app.tray_by_id("main") {
|
|
|
|
|
|
tray.set_menu(Some(new_menu))
|
|
|
|
|
|
.map_err(|e| format!("更新托盘菜单失败: {}", e))?;
|
|
|
|
|
|
return Ok(true);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
Ok(false)
|
|
|
|
|
|
}
|
2025-08-23 20:15:10 +08:00
|
|
|
|
|
2025-08-23 19:57:42 +08:00
|
|
|
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
|
|
|
|
|
pub fn run() {
|
2025-09-23 10:02:23 +08:00
|
|
|
|
let mut builder = tauri::Builder::default();
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "macos", target_os = "windows", target_os = "linux"))]
|
|
|
|
|
|
{
|
2025-09-26 20:18:11 +08:00
|
|
|
|
builder = builder.plugin(tauri_plugin_single_instance::init(|app, _args, _cwd| {
|
|
|
|
|
|
if let Some(window) = app.get_webview_window("main") {
|
|
|
|
|
|
let _ = window.unminimize();
|
|
|
|
|
|
let _ = window.show();
|
|
|
|
|
|
let _ = window.set_focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
}));
|
2025-09-23 10:02:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let builder = builder
|
2025-09-26 20:18:11 +08:00
|
|
|
|
// 拦截窗口关闭:根据设置决定是否最小化到托盘
|
2025-09-14 16:07:51 +08:00
|
|
|
|
.on_window_event(|window, event| match event {
|
|
|
|
|
|
tauri::WindowEvent::CloseRequested { api, .. } => {
|
2025-09-26 20:18:11 +08:00
|
|
|
|
let settings = crate::settings::get_settings();
|
|
|
|
|
|
|
|
|
|
|
|
if settings.minimize_to_tray_on_close {
|
|
|
|
|
|
api.prevent_close();
|
|
|
|
|
|
let _ = window.hide();
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
|
{
|
|
|
|
|
|
let _ = window.set_skip_taskbar(true);
|
|
|
|
|
|
}
|
2025-09-29 17:03:13 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
{
|
|
|
|
|
|
apply_tray_policy(&window.app_handle(), false);
|
|
|
|
|
|
}
|
2025-09-26 20:18:11 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
window.app_handle().exit(0);
|
|
|
|
|
|
}
|
2025-09-14 16:07:51 +08:00
|
|
|
|
}
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
})
|
2025-09-08 16:58:41 +08:00
|
|
|
|
.plugin(tauri_plugin_process::init())
|
2025-09-20 21:20:07 +08:00
|
|
|
|
.plugin(tauri_plugin_dialog::init())
|
2025-08-25 20:16:29 +08:00
|
|
|
|
.plugin(tauri_plugin_opener::init())
|
2025-08-23 20:15:10 +08:00
|
|
|
|
.setup(|app| {
|
2025-09-08 16:58:41 +08:00
|
|
|
|
// 注册 Updater 插件(桌面端)
|
|
|
|
|
|
#[cfg(desktop)]
|
2025-09-09 10:28:34 +08:00
|
|
|
|
{
|
|
|
|
|
|
if let Err(e) = app
|
|
|
|
|
|
.handle()
|
|
|
|
|
|
.plugin(tauri_plugin_updater::Builder::new().build())
|
|
|
|
|
|
{
|
|
|
|
|
|
// 若配置不完整(如缺少 pubkey),跳过 Updater 而不中断应用
|
|
|
|
|
|
log::warn!("初始化 Updater 插件失败,已跳过:{}", e);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-25 23:06:54 +08:00
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
{
|
|
|
|
|
|
// 设置 macOS 标题栏背景色为主界面蓝色
|
|
|
|
|
|
if let Some(window) = app.get_webview_window("main") {
|
|
|
|
|
|
use objc2::rc::Retained;
|
2025-08-27 11:00:53 +08:00
|
|
|
|
use objc2::runtime::AnyObject;
|
2025-08-25 23:06:54 +08:00
|
|
|
|
use objc2_app_kit::NSColor;
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-25 23:06:54 +08:00
|
|
|
|
let ns_window_ptr = window.ns_window().unwrap();
|
2025-08-27 11:00:53 +08:00
|
|
|
|
let ns_window: Retained<AnyObject> =
|
|
|
|
|
|
unsafe { Retained::retain(ns_window_ptr as *mut AnyObject).unwrap() };
|
|
|
|
|
|
|
2025-08-25 23:06:54 +08:00
|
|
|
|
// 使用与主界面 banner 相同的蓝色 #3498db
|
|
|
|
|
|
// #3498db = RGB(52, 152, 219)
|
|
|
|
|
|
let bg_color = unsafe {
|
|
|
|
|
|
NSColor::colorWithRed_green_blue_alpha(
|
2025-08-27 11:00:53 +08:00
|
|
|
|
52.0 / 255.0, // R: 52
|
|
|
|
|
|
152.0 / 255.0, // G: 152
|
|
|
|
|
|
219.0 / 255.0, // B: 219
|
|
|
|
|
|
1.0, // Alpha: 1.0
|
2025-08-25 23:06:54 +08:00
|
|
|
|
)
|
|
|
|
|
|
};
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-25 23:06:54 +08:00
|
|
|
|
unsafe {
|
|
|
|
|
|
use objc2::msg_send;
|
|
|
|
|
|
let _: () = msg_send![&*ns_window, setBackgroundColor: &*bg_color];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-23 20:15:10 +08:00
|
|
|
|
// 初始化日志
|
|
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
|
|
app.handle().plugin(
|
|
|
|
|
|
tauri_plugin_log::Builder::default()
|
|
|
|
|
|
.level(log::LevelFilter::Info)
|
|
|
|
|
|
.build(),
|
|
|
|
|
|
)?;
|
|
|
|
|
|
}
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-24 23:04:55 +08:00
|
|
|
|
// 初始化应用状态(仅创建一次,并在本函数末尾注入 manage)
|
2025-08-23 20:15:10 +08:00
|
|
|
|
let app_state = AppState::new();
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-09-04 16:16:51 +08:00
|
|
|
|
// 首次启动迁移:扫描副本文件,合并到 config.json,并归档副本;旧 config.json 先归档
|
2025-08-23 20:15:10 +08:00
|
|
|
|
{
|
2025-09-04 16:16:51 +08:00
|
|
|
|
let mut config_guard = app_state.config.lock().unwrap();
|
|
|
|
|
|
let migrated = migration::migrate_copies_into_config(&mut *config_guard)?;
|
|
|
|
|
|
if migrated {
|
|
|
|
|
|
log::info!("已将副本文件导入到 config.json,并完成归档");
|
2025-08-23 20:15:10 +08:00
|
|
|
|
}
|
2025-09-04 16:16:51 +08:00
|
|
|
|
// 确保两个 App 条目存在
|
|
|
|
|
|
config_guard.ensure_app(&app_config::AppType::Claude);
|
|
|
|
|
|
config_guard.ensure_app(&app_config::AppType::Codex);
|
2025-08-23 20:15:10 +08:00
|
|
|
|
}
|
2025-08-27 11:00:53 +08:00
|
|
|
|
|
2025-08-30 21:54:11 +08:00
|
|
|
|
// 保存配置
|
|
|
|
|
|
let _ = app_state.save();
|
|
|
|
|
|
|
2025-09-06 16:21:21 +08:00
|
|
|
|
// 创建动态托盘菜单
|
|
|
|
|
|
let menu = create_tray_menu(&app.handle(), &app_state)?;
|
|
|
|
|
|
|
2025-09-14 21:55:41 +08:00
|
|
|
|
// 构建托盘
|
|
|
|
|
|
let mut tray_builder = TrayIconBuilder::with_id("main")
|
2025-09-14 16:07:51 +08:00
|
|
|
|
.on_tray_icon_event(|_tray, event| match event {
|
|
|
|
|
|
// 左键点击已通过 show_menu_on_left_click(true) 打开菜单,这里不再额外处理
|
|
|
|
|
|
TrayIconEvent::Click { .. } => {}
|
|
|
|
|
|
_ => log::debug!("unhandled event {event:?}"),
|
2025-09-06 16:21:21 +08:00
|
|
|
|
})
|
|
|
|
|
|
.menu(&menu)
|
|
|
|
|
|
.on_menu_event(|app, event| {
|
|
|
|
|
|
handle_tray_menu_event(app, &event.id.0);
|
|
|
|
|
|
})
|
2025-09-14 21:55:41 +08:00
|
|
|
|
.show_menu_on_left_click(true);
|
|
|
|
|
|
|
|
|
|
|
|
// 统一使用应用默认图标;待托盘模板图标就绪后再启用
|
|
|
|
|
|
tray_builder = tray_builder.icon(app.default_window_icon().unwrap().clone());
|
|
|
|
|
|
|
|
|
|
|
|
let _tray = tray_builder.build(app)?;
|
2025-08-24 23:04:55 +08:00
|
|
|
|
// 将同一个实例注入到全局状态,避免重复创建导致的不一致
|
|
|
|
|
|
app.manage(app_state);
|
2025-08-23 20:15:10 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
|
})
|
|
|
|
|
|
.invoke_handler(tauri::generate_handler![
|
|
|
|
|
|
commands::get_providers,
|
|
|
|
|
|
commands::get_current_provider,
|
|
|
|
|
|
commands::add_provider,
|
|
|
|
|
|
commands::update_provider,
|
|
|
|
|
|
commands::delete_provider,
|
|
|
|
|
|
commands::switch_provider,
|
|
|
|
|
|
commands::import_default_config,
|
|
|
|
|
|
commands::get_claude_config_status,
|
2025-08-30 21:54:11 +08:00
|
|
|
|
commands::get_config_status,
|
2025-08-23 20:15:10 +08:00
|
|
|
|
commands::get_claude_code_config_path,
|
2025-09-20 21:20:07 +08:00
|
|
|
|
commands::get_config_dir,
|
2025-08-23 20:15:10 +08:00
|
|
|
|
commands::open_config_folder,
|
2025-09-20 21:20:07 +08:00
|
|
|
|
commands::pick_directory,
|
2025-08-23 20:15:10 +08:00
|
|
|
|
commands::open_external,
|
2025-09-07 11:36:09 +08:00
|
|
|
|
commands::get_app_config_path,
|
|
|
|
|
|
commands::open_app_config_folder,
|
|
|
|
|
|
commands::get_settings,
|
|
|
|
|
|
commands::save_settings,
|
|
|
|
|
|
commands::check_for_updates,
|
2025-09-24 11:25:33 +08:00
|
|
|
|
commands::is_portable_mode,
|
2025-10-01 21:23:55 +08:00
|
|
|
|
commands::get_claude_plugin_status,
|
|
|
|
|
|
commands::read_claude_plugin_config,
|
|
|
|
|
|
commands::apply_claude_plugin_config,
|
|
|
|
|
|
commands::is_claude_plugin_applied,
|
2025-10-08 22:34:58 +08:00
|
|
|
|
// Claude MCP management
|
|
|
|
|
|
commands::get_claude_mcp_status,
|
|
|
|
|
|
commands::read_claude_mcp_config,
|
|
|
|
|
|
commands::upsert_claude_mcp_server,
|
|
|
|
|
|
commands::delete_claude_mcp_server,
|
|
|
|
|
|
commands::validate_mcp_command,
|
2025-10-09 21:08:42 +08:00
|
|
|
|
// New MCP via config.json (SSOT)
|
|
|
|
|
|
commands::get_mcp_config,
|
|
|
|
|
|
commands::upsert_mcp_server_in_config,
|
|
|
|
|
|
commands::delete_mcp_server_in_config,
|
|
|
|
|
|
commands::set_mcp_enabled,
|
|
|
|
|
|
commands::sync_enabled_mcp_to_claude,
|
|
|
|
|
|
commands::import_mcp_from_claude,
|
feat: Implement Speed Test Function
* feat: add unified endpoint speed test for API providers
Add a comprehensive endpoint latency testing system that allows users to:
- Test multiple API endpoints concurrently
- Auto-select the fastest endpoint based on latency
- Add/remove custom endpoints dynamically
- View latency results with color-coded indicators
Backend (Rust):
- Implement parallel HTTP HEAD requests with configurable timeout
- Handle various error scenarios (timeout, connection failure, invalid URL)
- Return structured latency data with status codes
Frontend (React):
- Create interactive speed test UI component with auto-sort by latency
- Support endpoint management (add/remove custom endpoints)
- Extract and update Codex base_url from TOML configuration
- Integrate with provider presets for default endpoint candidates
This feature improves user experience when selecting optimal API endpoints,
especially useful for users with multiple provider options or proxy setups.
* refactor: convert endpoint speed test to modal dialog
- Transform EndpointSpeedTest component into a modal dialog
- Add "Advanced" button next to base URL input to open modal
- Support ESC key and backdrop click to close modal
- Apply Linear design principles: minimal styling, clean layout
- Remove unused showBaseUrlInput variable
- Implement same modal pattern for both Claude and Codex
* fix: prevent modal cascade closing when ESC is pressed
- Add state checks to prevent parent modal from closing when child modals (endpoint speed test or template wizard) are open
- Update ESC key handler dependencies to track all modal states
- Ensures only the topmost modal responds to ESC key
* refactor: unify speed test panel UI with project design system
UI improvements:
- Update modal border radius from rounded-lg to rounded-xl
- Unify header padding from px-6 py-4 to p-6
- Change speed test button color to blue theme (bg-blue-500) for consistency
- Update footer background from bg-gray-50 to bg-gray-100
- Style "Done" button as primary action button with blue theme
- Adjust footer button spacing and hover states
Simplify endpoint display:
- Remove endpoint labels (e.g., "Current Address", "Custom 1")
- Display only URL for cleaner interface
- Clean up all label-related logic:
* Remove label field from EndpointCandidate interface
* Remove label generation in buildInitialEntries function
* Remove label handling in useEffect merge logic
* Remove label generation in handleAddEndpoint
* Remove label parameters from claudeSpeedTestEndpoints
* Remove label parameters from codexSpeedTestEndpoints
* refactor: improve endpoint list UI consistency
- Show delete button for all endpoints on hover for uniform UI
- Change selected state to use blue theme matching main interface:
* Blue border (border-blue-500) for selected items
* Light blue background (bg-blue-50/dark:bg-blue-900/20)
* Blue indicator dot (bg-blue-500/dark:bg-blue-400)
- Switch from compact list (space-y-px) to card-based layout (space-y-2)
- Add rounded corners to each endpoint item for better visual separation
* feat: persist custom endpoints to settings.json
- Extend AppSettings to store custom endpoints for Claude and Codex
- Add Tauri commands: get/add/remove/update custom endpoints
- Update frontend API with endpoint persistence methods
- Modify EndpointSpeedTest to load/save custom endpoints via API
- Track endpoint last used time for future sorting/cleanup
- Store endpoints per app type in settings.json instead of localStorage
* - feat(types): add Provider.meta and ProviderMeta (snake_case) with custom_endpoints map
- feat(provider-form): persist custom endpoints on provider create by merging EndpointSpeedTest’s custom URLs into meta.custom_endpoints on submit
- feat(endpoint-speed-test): add onCustomEndpointsChange callback emitting normalized custom URLs; wire it for both Claude/Codex modals
- fix(api): send alias param names (app/appType/app_type and provider_id/providerId) in Tauri invokes to avoid “missing providerId” with older backends
- storage: custom endpoints are stored in ~/.cc-switch/config.json under providers[<id>].meta.custom_endpoints (not in settings.json)
- behavior: edit flow remains immediate writes; create flow now writes once via addProvider, removing the providerId dependency during creation
* feat: add endpoint candidates support and code formatting improvements
- Add endpointCandidates field to ProviderPreset and CodexProviderPreset interfaces
- Integrate preset endpoint candidates into speed test endpoint selection
- Add multiple endpoint options for PackyCode providers (Claude & Codex)
- Apply consistent code formatting (trailing commas, line breaks)
- Improve template value type safety and readability
* refactor: improve endpoint management button UX
Replace ambiguous "Advanced" text with intuitive "Manage & Test" label accompanied by Zap icon, making the endpoint management panel entry point more discoverable and self-explanatory for both Claude and Codex configurations.
* - merge: merge origin/main, resolve conflicts and preserve both feature sets
- feat(tauri): register import/export and file dialogs; keep endpoint speed test and custom endpoints
- feat(api): add updateTrayMenu and onProviderSwitched; wire import/export APIs
- feat(types): extend global API declarations (import/export)
- chore(presets): GLM preset supports both new and legacy model keys
- chore(rust): add chrono dependency; refresh lockfile
---------
Co-authored-by: Jason <farion1231@gmail.com>
2025-10-07 19:14:32 +08:00
|
|
|
|
// ours: endpoint speed test + custom endpoint management
|
|
|
|
|
|
commands::test_api_endpoints,
|
|
|
|
|
|
commands::get_custom_endpoints,
|
|
|
|
|
|
commands::add_custom_endpoint,
|
|
|
|
|
|
commands::remove_custom_endpoint,
|
|
|
|
|
|
commands::update_endpoint_last_used,
|
|
|
|
|
|
// theirs: config import/export and dialogs
|
2025-10-05 23:33:07 +08:00
|
|
|
|
import_export::export_config_to_file,
|
|
|
|
|
|
import_export::import_config_from_file,
|
|
|
|
|
|
import_export::save_file_dialog,
|
|
|
|
|
|
import_export::open_file_dialog,
|
2025-09-06 16:21:21 +08:00
|
|
|
|
update_tray_menu,
|
2025-09-16 10:42:59 +08:00
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
let app = builder
|
|
|
|
|
|
.build(tauri::generate_context!())
|
2025-08-23 20:15:10 +08:00
|
|
|
|
.expect("error while running tauri application");
|
2025-09-16 10:42:59 +08:00
|
|
|
|
|
|
|
|
|
|
app.run(|app_handle, event| {
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
|
|
// macOS 在 Dock 图标被点击并重新激活应用时会触发 Reopen 事件,这里手动恢复主窗口
|
|
|
|
|
|
match event {
|
|
|
|
|
|
RunEvent::Reopen { .. } => {
|
|
|
|
|
|
if let Some(window) = app_handle.get_webview_window("main") {
|
2025-09-26 20:18:11 +08:00
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
|
|
{
|
|
|
|
|
|
let _ = window.set_skip_taskbar(false);
|
|
|
|
|
|
}
|
2025-09-16 10:42:59 +08:00
|
|
|
|
let _ = window.unminimize();
|
|
|
|
|
|
let _ = window.show();
|
|
|
|
|
|
let _ = window.set_focus();
|
2025-09-29 17:03:13 +08:00
|
|
|
|
apply_tray_policy(app_handle, true);
|
2025-09-16 10:42:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "macos"))]
|
|
|
|
|
|
{
|
|
|
|
|
|
let _ = (app_handle, event);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-08-24 23:04:55 +08:00
|
|
|
|
}
|