Skip to content

armgit / clipy

Public Imported from GitHub
0

The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.

3.6 KB
Raw Blame
//! The full Settings window (`#settings`): the popover's Settings in a
//! resizable window with a sidebar, opened from "Open full settings" in the
//! popover's Settings, ⌘, in the popover, and "Settings…" in the tray menu.
//!
//! The page renders the same `SettingsSheet` as the popover, and the
//! popover's webview keeps owning the settings (`settingsRemote.ts`): the
//! two talk over app events, so nothing here reads or writes a setting.

use tauri::{AppHandle, Emitter, Manager, Runtime, WebviewUrl, WebviewWindowBuilder};

pub const LABEL: &str = "settings";
/// Asks an open window to show a tab.
const EVENT_TAB: &str = "settings-window:tab";
/// The tabs the page knows. Anything else opens it on its first tab.
const TABS: [&str; 5] = ["enhance", "video", "transcription", "preferences", "shortcuts"];

#[tauri::command]
pub async fn show_settings_window(app: AppHandle, tab: Option<String>) -> Result<(), String> {
    open_window(&app, tab)
}

/// The tray menu's entry. Spawned: building a window must not run on the
/// main thread (see `whats_new::open_from_menu`).
pub fn open_from_menu<R: Runtime>(app: &AppHandle<R>) {
    let app = app.clone();
    tauri::async_runtime::spawn(async move {
        if let Err(err) = open_window(&app, None) {
            tracing::warn!("open Settings: {err}");
        }
    });
}

fn known_tab(tab: Option<String>) -> Option<String> {
    tab.filter(|tab| TABS.contains(&tab.as_str()))
}

fn open_window<R: Runtime>(app: &AppHandle<R>, tab: Option<String>) -> Result<(), String> {
    // Nothing comes forward over a live capture (the same rule as the
    // popover and What's new).
    if crate::tray::capture_in_progress(app) {
        return Err("Finish the recording first.".into());
    }
    let tab = known_tab(tab);
    if let Some(window) = app.get_webview_window(LABEL) {
        window.show().map_err(|e| e.to_string())?;
        window.unminimize().ok();
        window.set_focus().ok();
        if let Some(tab) = tab {
            let _ = app.emit_to(LABEL, EVENT_TAB, tab);
        }
        return Ok(());
    }
    let route = match &tab {
        Some(tab) => format!("index.html#settings/{tab}"),
        None => "index.html#settings".to_string(),
    };
    let builder = WebviewWindowBuilder::new(app, LABEL, WebviewUrl::App(route.into()))
        .title(crate::i18n::t("windowTitles.settings"))
        .inner_size(880.0, 640.0)
        .min_inner_size(680.0, 460.0)
        .resizable(true)
        .center();
    // The same full-height layout as setup and What's new: the page leaves
    // room for the traffic lights and makes its sidebar a drag region.
    #[cfg(target_os = "macos")]
    let builder = builder
        .title_bar_style(tauri::TitleBarStyle::Overlay)
        .hidden_title(true);
    match builder.build() {
        Ok(window) => {
            window.set_focus().ok();
            Ok(())
        }
        // Two callers raced to create it: the other one won, use that.
        Err(err) => match app.get_webview_window(LABEL) {
            Some(window) => {
                window.show().map_err(|e| e.to_string())?;
                window.set_focus().ok();
                Ok(())
            }
            None => Err(format!("open Settings: {err}")),
        },
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn only_known_tabs_are_passed_to_the_page() {
        assert_eq!(known_tab(Some("transcription".into())).as_deref(), Some("transcription"));
        assert_eq!(known_tab(Some("shortcuts".into())).as_deref(), Some("shortcuts"));
        assert_eq!(known_tab(Some("../../etc".into())), None);
        assert_eq!(known_tab(None), None);
    }
}