Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
320 changes: 317 additions & 3 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ tauri = { version = "2", features = [
tauri-plugin-opener = "2"
tauri-plugin-process = "2"
tauri-plugin-updater = "2"
tauri-plugin-clipboard-manager = "2"
serde = { version = "1", features = [ "derive" ] }
serde_json = "1"
async-trait = "0.1"
Expand Down
3 changes: 2 additions & 1 deletion src-tauri/capabilities/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dialog:default",
"dialog:allow-save",
"updater:default",
"deep-link:default"
"deep-link:default",
"clipboard-manager:allow-write-text"
]
}
249 changes: 223 additions & 26 deletions src-tauri/src/desktop.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,161 @@
use std::sync::Mutex;
use std::time::Duration;

use tauri::menu::{MenuBuilder, MenuItem};
use tauri::tray::TrayIconBuilder;
use tauri::{App, AppHandle, Manager, WindowEvent};
use tauri::tray::{TrayIcon, TrayIconBuilder};
use tauri::{App, AppHandle, Manager, WindowEvent, Wry};
use tauri_plugin_clipboard_manager::ClipboardExt;

use crate::mcp_bridge::BridgeStopReason;

const MAIN_WINDOW: &str = "main";
const SHOW_WINDOW: &str = "tray-show-window";
const UNLOCK_BRIDGE: &str = "tray-unlock-bridge";
const COPY_BRIDGE_URL: &str = "tray-copy-bridge-url";
const QUIT: &str = "tray-quit";
const STATUS_REFRESH: Duration = Duration::from_secs(5);

/// Localized tray copy. Product nouns ("MCP Bridge", backend client
/// names) stay untranslated; only the status phrasing is localized.
struct TrayLabels {
show_window: &'static str,
quit: &'static str,
copy_url: &'static str,
/// "{}" → port
bridge_running: &'static str,
bridge_stopped: &'static str,
bridge_stopped_gate: &'static str,
/// Action item shown under the gated-stop status; "…" = opens a dialog.
unlock: &'static str,
}

impl TrayLabels {
fn bridge_running_line(&self, port: u16) -> String {
self.bridge_running.replace("{}", &port.to_string())
}
}

fn tray_labels(locale: &str) -> TrayLabels {
let normalized = locale.to_ascii_lowercase();
let mut locale_parts = normalized.split(['-', '_']);
match locale_parts.next() {
Some("zh") if locale_parts.any(|part| matches!(part, "hant" | "tw" | "hk" | "mo")) => {
TrayLabels {
show_window: "顯示視窗",
quit: "結束",
copy_url: "複製 Bridge 地址",
bridge_running: "MCP Bridge:運行中(連接埠 {})",
bridge_stopped: "MCP Bridge:已停止",
bridge_stopped_gate: "MCP Bridge:已停止(需要訂閱)",
unlock: "解鎖 MCP Bridge…",
}
}
Some("zh") => TrayLabels {
show_window: "显示窗口",
quit: "退出",
copy_url: "复制 Bridge 地址",
bridge_running: "MCP 桥接:运行中(端口 {})",
bridge_stopped: "MCP 桥接:已停止",
bridge_stopped_gate: "MCP 桥接:已停止(需要订阅)",
unlock: "解锁 MCP 桥接…",
},
Some("pt") => TrayLabels {
show_window: "Mostrar janela",
quit: "Sair",
copy_url: "Copiar URL do Bridge",
bridge_running: "Ponte MCP: em execução (porta {})",
bridge_stopped: "Ponte MCP: parado",
bridge_stopped_gate: "Ponte MCP: parado (assinatura necessária)",
unlock: "Desbloquear Ponte MCP…",
},
Some("ja") => TrayLabels {
show_window: "ウィンドウを表示",
quit: "終了",
copy_url: "Bridge URL をコピー",
bridge_running: "MCPブリッジ:実行中(ポート {})",
bridge_stopped: "MCPブリッジ:停止中",
bridge_stopped_gate: "MCPブリッジ:停止中(サブスクリプションが必要)",
unlock: "MCPブリッジを解錠…",
},
Some("ko") => TrayLabels {
show_window: "창 표시",
quit: "종료",
copy_url: "Bridge URL 복사",
bridge_running: "MCP 브리지: 실행 중 (포트 {})",
bridge_stopped: "MCP 브리지: 중지됨",
bridge_stopped_gate: "MCP 브리지: 중지됨 (구독 필요)",
unlock: "MCP 브리지 잠금 해제…",
},
Some("es") => TrayLabels {
show_window: "Mostrar ventana",
quit: "Salir",
copy_url: "Copiar URL del Bridge",
bridge_running: "Puente MCP: en ejecución (puerto {})",
bridge_stopped: "Puente MCP: detenido",
bridge_stopped_gate: "Puente MCP: detenido (se requiere suscripción)",
unlock: "Desbloquear Puente MCP…",
},
_ => TrayLabels {
show_window: "Show Window",
quit: "Quit",
copy_url: "Copy Bridge URL",
bridge_running: "MCP Bridge: Running (port {})",
bridge_stopped: "MCP Bridge: Stopped",
bridge_stopped_gate: "MCP Bridge: Stopped (subscription required)",
unlock: "Unlock MCP Bridge…",
},
}
}

/// Tray widgets the refresh loop and the copy handler share. Managed as Tauri
/// state so the menu event handler can reach the last known port without
/// touching the filesystem.
struct TrayStatusItems {
tray: TrayIcon<Wry>,
bridge_line: MenuItem<Wry>,
unlock_line: MenuItem<Wry>,
copy_url: MenuItem<Wry>,
labels: TrayLabels,
app_name: String,
/// Last seen bridge port, kept fresh by the refresh loop.
port: Mutex<Option<u16>>,
}

pub(crate) fn setup(app: &App) {
let tray_ready = match create_tray(app) {
Ok(()) => true,
let tray = match create_tray(app) {
Ok(items) => {
app.manage(items);
true
}
Err(error) => {
eprintln!("Could not create the system tray: {error}");
false
}
};

if tray {
let app_handle = app.handle().clone();
if let Some(items) = app_handle.try_state::<TrayStatusItems>() {
// Paint the first status lines synchronously; the loop only has to
// keep them fresh afterwards.
refresh_status_lines(&app_handle, &items);
tauri::async_runtime::spawn(async move {
loop {
tokio::time::sleep(STATUS_REFRESH).await;
if let Some(items) = app_handle.try_state::<TrayStatusItems>() {
refresh_status_lines(&app_handle, &items);
}
}
});
}
}

if let Some(window) = app.get_webview_window(MAIN_WINDOW) {
let main_window = window.clone();
window.on_window_event(move |event| {
if let WindowEvent::CloseRequested { api, .. } = event {
api.prevent_close();
if tray_ready && main_window.hide().is_ok() {
if tray && main_window.hide().is_ok() {
return;
}
if let Err(error) = main_window.minimize() {
Expand All @@ -43,46 +178,108 @@ pub(crate) fn show_main_window(app: &AppHandle) {
}
}

fn create_tray(app: &App) -> tauri::Result<()> {
fn create_tray(app: &App) -> Result<TrayStatusItems, Box<dyn std::error::Error>> {
#[cfg(target_os = "macos")]
let icon = tauri::include_image!("icons/tray-template.png");
#[cfg(not(target_os = "macos"))]
let icon = tauri::include_image!("icons/tray.png");

let locale = tauri_plugin_os::locale()
.unwrap_or_default()
.to_ascii_lowercase();
let mut locale_parts = locale.split(['-', '_']);
let (show_label, quit_label) = match locale_parts.next() {
Some("zh") if locale_parts.any(|part| matches!(part, "hant" | "tw" | "hk" | "mo")) => {
("顯示視窗", "結束")
}
Some("zh") => ("显示窗口", "退出"),
Some("pt") => ("Mostrar janela", "Sair"),
Some("ja") => ("ウィンドウを表示", "終了"),
Some("ko") => ("창 표시", "종료"),
Some("es") => ("Mostrar ventana", "Salir"),
_ => ("Show Window", "Quit"),
};
let locale = tauri_plugin_os::locale().unwrap_or_default();
let labels = tray_labels(&locale);
let app_name = app.package_info().name.clone();

let show = MenuItem::with_id(app, SHOW_WINDOW, show_label, true, None::<&str>)?;
let quit = MenuItem::with_id(app, QUIT, quit_label, true, None::<&str>)?;
// Inert status line — its id is never matched; the unlock item below carries the click.
let bridge_line = MenuItem::with_id(app, "tray-bridge-status", "", false, None::<&str>)?;
let unlock_line = MenuItem::with_id(app, UNLOCK_BRIDGE, labels.unlock, false, None::<&str>)?;
let copy_url = MenuItem::with_id(app, COPY_BRIDGE_URL, labels.copy_url, true, None::<&str>)?;
let show = MenuItem::with_id(app, SHOW_WINDOW, labels.show_window, true, None::<&str>)?;
let quit = MenuItem::with_id(app, QUIT, labels.quit, true, None::<&str>)?;
let menu = MenuBuilder::new(app)
.item(&bridge_line)
.item(&unlock_line)
.item(&copy_url)
.separator()
.item(&show)
.separator()
.item(&quit)
.build()?;
TrayIconBuilder::with_id(app.config().identifier.as_str())

let tray = TrayIconBuilder::with_id(app.config().identifier.as_str())
.icon(icon)
.icon_as_template(cfg!(target_os = "macos"))
.tooltip(&app.package_info().name)
.tooltip(&app_name)
.menu(&menu)
.show_menu_on_left_click(true)
.on_menu_event(|app, event| match event.id().as_ref() {
SHOW_WINDOW => show_main_window(app),
UNLOCK_BRIDGE => {
show_main_window(app);
use tauri::Emitter;
let _ = app.emit("mcp-bridge://subscription", ());
}
COPY_BRIDGE_URL => copy_bridge_url(app),
QUIT => app.exit(0),
_ => {}
})
.build(app)?;
Ok(())

Ok(TrayStatusItems {
tray,
bridge_line,
unlock_line,
copy_url,
labels,
app_name,
port: Mutex::new(None),
})
}

fn refresh_status_lines(app: &AppHandle, items: &TrayStatusItems) {
let Ok(app_data_dir) = app.path().app_data_dir() else {
return;
};
let running_port = crate::mcp_bridge::get_actual_port(&app_data_dir);
*items.port.lock().unwrap() = running_port;

let bridge_text = match running_port {
Some(port) => items.labels.bridge_running_line(port),
None => {
let gated = app
.try_state::<crate::mcp_bridge::BridgeStopStatus>()
.is_some_and(|status| {
*status.reason.lock().unwrap() == Some(BridgeStopReason::SubscriptionRequired)
});
if gated {
items.labels.bridge_stopped_gate.to_string()
} else {
items.labels.bridge_stopped.to_string()
}
}
};
// The "Unlock …" action item appears only under the gated-stop status —
// the status line itself stays inert; the action item carries the click.
let gated_stop = running_port.is_none() && bridge_text == items.labels.bridge_stopped_gate;
let _ = items.bridge_line.set_text(&bridge_text);
let _ = items.unlock_line.set_enabled(gated_stop);
let _ = items.copy_url.set_enabled(running_port.is_some());
let tooltip = match running_port {
Some(port) => format!("{} — MCP Bridge on :{port}", items.app_name),
None => items.app_name.clone(),
};
let _ = items.tray.set_tooltip(Some(tooltip));
}

fn copy_bridge_url(app: &AppHandle) {
let Some(items) = app.try_state::<TrayStatusItems>() else {
return;
};
let Some(port) = *items.port.lock().unwrap() else {
return;
};
if let Err(error) = app
.clipboard()
.write_text(format!("http://127.0.0.1:{port}"))
{
eprintln!("Could not copy the bridge URL: {error}");
}
}
5 changes: 5 additions & 0 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub fn run() {
.plugin(tauri_plugin_deep_link::init())
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_window_state::Builder::default().build())
.plugin(tauri_plugin_clipboard_manager::init())
.manage(AppState::new())
.manage(store.clone())
.setup(move |app| {
Expand All @@ -167,6 +168,10 @@ pub fn run() {
store.set_app_handle(handle.clone()).await;
});
menu::create_menu(app)?;
// Tray-visible MCP state must exist before desktop::setup spawns
// the status refresh loop.
app.manage(crate::mcp_bridge::McpSessionRegistry::default());
app.manage(crate::mcp_bridge::BridgeStopStatus::default());
#[cfg(desktop)]
desktop::setup(app);

Expand Down
Loading
Loading