Tauri command API
Covers:
Overview
src-tauri/…/cmd.rs defines the Rust commands registered with Tauri and exposed to the frontend through invoke(...). The commands are registered in src-tauri/…/main.rs with tauri::generate_handler![cmd::view_reload, cmd::view_url, cmd::view_go_forward, cmd::view_go_back, cmd::set_view_ask, cmd::get_app_conf, cmd::window_pin, cmd::ask_sync, cmd::ask_send, cmd::set_theme], so the frontend calls them by those snake-case names.
Most commands operate on the window labeled core and its webviews. view_reload, view_go_forward, and view_go_back fetch the main webview and run JavaScript (window.location.reload(), window.history.forward(), or window.history.back()) with eval(...). view_url also targets the main webview but returns its current tauri::Url. These commands assume the core window and main webview already exist; failures are unwrapped.
Configuration commands use AppConf from src-tauri/…/conf.rs, which persists JSON under the Tauri config directory. get_app_conf returns the loaded AppConf to initialize frontend state. window_pin writes stay_on_top and then calls set_always_on_top(pin) on the core window. set_theme writes the selected theme and immediately calls app.restart(), so theme changes are applied by a full app restart rather than by mutating the existing webviews.
The ask-mode commands coordinate the main and ask webviews. ask_sync receives a JSON-formatted JavaScript argument string from the frontend and evaluates ChatAsk.sync(<message>) in the main webview. ask_send evaluates ChatAsk.submit() in main, then after 500 ms focuses the webview labeled ask via __TAURI__.webview.Webview.getByLabel('ask')?.setFocus(). src/…/Ask.tsx calls ask_sync on debounced text changes and calls ask_send when submitting, although ask_send currently has no Rust message parameter and ignores the extra frontend payload.
set_view_ask toggles ask mode and persists ask_mode through AppConf. It recalculates the layout for main, titlebar, and ask webviews using ASK_HEIGHT and TITLEBAR_HEIGHT from src-tauri/…/constant.rs, the core window scale factor, and the current inner size. When enabling ask mode it focuses the ask webview; when disabling it focuses main. On macOS, main starts below the titlebar and ask is placed at the bottom; on other platforms, main starts at the top, while titlebar and ask are placed at the bottom of the window. Position/size failures are logged to stderr instead of panicking, but missing windows, missing webviews, config load/save failures, scale-factor failures, and focus failures are still unwrapped.
Frontend call patterns are visible in src/…/Titlebar.tsx: it initializes titlebar state with get_app_conf, calls navigation commands from the titlebar controls, calls window_pin after optimistically updating pin state, calls set_view_ask after optimistically toggling ask mode, and calls set_theme without updating local theme state because the Rust command restarts the app.
Coupling
- If any window or webview label changes, update every lookup in src-tauri/…/cmd.rs and the frontend callers that assume those labels. The command code repeatedly unwraps
app.get_window("core")andget_webview("main" | "titlebar" | "ask"), so a label drift becomes a native panic rather than a recoverable command error. - If
AppConffields change in src-tauri/…/conf.rs, updatewindow_pin,set_theme,get_app_conf, andset_view_askin src-tauri/…/cmd.rs together with the frontend state initialized in src/…/Titlebar.tsx; these commands amend JSON by field name and depend on matching serialized keys. - If the ask webview JavaScript contract changes, update
ask_syncandask_sendin src-tauri/…/cmd.rs with the implementation that definesChatAsk.syncandChatAsk.submitin themainwebview. The Rust side injects raw JavaScript calls rather than using a typed event or command boundary. - If
TITLEBAR_HEIGHTorASK_HEIGHTin src-tauri/…/constant.rs changes, verifyset_view_askon macOS and non-macOS. The command mixes logical positions with physical sizes and applies different vertical stacking rules by target OS.
Risks
- src-tauri/…/cmd.rs uses
unwrap()for all window/webview lookup, JavaScript evaluation, config persistence, and most focus/size reads. If the command is invoked before thecore,main,titlebar, oraskwebviews exist, or if config I/O fails, the app can panic instead of returning an error to the frontend. - src-tauri/…/cmd.rs builds
ChatAsk.sync({message})withformat!and expects callers such as src/…/Ask.tsx to pass a JSON-encoded JavaScript expression. If a caller sends an unescaped raw string instead ofJSON.stringify(message), JavaScript injection or syntax errors can break ask synchronization. - src-tauri/…/cmd.rs defines
ask_send(app: AppHandle)without a message parameter while src/…/Ask.tsx invokes it with{ message: JSON.stringify(message) }. Tauri ignores the extra payload today, but future changes that expect Rust to receive the message could silently fail unless the command signature and frontend call are changed together.