Skip to main content

soma_tauri_shell/
app.rs

1//! `AppHandle`-level helpers: resolving a window by label, toggling its
2//! visibility, and emitting a frontend event without letting the failure
3//! propagate as a command error.
4
5use tauri::{AppHandle, Emitter, Manager, WebviewWindow};
6
7use crate::{command::CommandResult, window};
8
9/// Resolve `label`'s webview window, or a descriptive error if it isn't
10/// open.
11pub fn get_window(app: &AppHandle, label: &str) -> CommandResult<WebviewWindow> {
12    app.get_webview_window(label)
13        .ok_or_else(|| format!("{label} window not found"))
14}
15
16/// Show `label`'s window if hidden, hide it if visible.
17pub fn toggle_window_visibility(app: &AppHandle, label: &str) -> CommandResult<()> {
18    let Some(handle) = app.get_webview_window(label) else {
19        return Ok(());
20    };
21    if window::is_visible(&handle) {
22        window::hide(&handle)
23    } else {
24        window::show_and_focus(&handle)
25    }
26}
27
28/// Emit `event` with `payload` on `window`, logging (rather than
29/// propagating) a failure. Frontend event delivery is best-effort — a failed
30/// emit shouldn't fail the command that triggered it.
31pub fn emit_or_warn<S: serde::Serialize + Clone>(window: &WebviewWindow, event: &str, payload: S) {
32    if let Err(err) = window.emit(event, payload) {
33        tracing::warn!("failed to emit {event}: {err}");
34    }
35}