pub fn toggle_scratchpad<H: Handle, C: Config, SERVER: DisplayServer<H>>(
    manager: &mut Manager<H, C, SERVER>,
    name: &ScratchPadName,
) -> Option<bool> {
    let current_tag = &manager.state.focus_manager.tag(0)?;

    // Check if there is a valid scratchpad, if so handle it and return immediately
    if let Some(id) = manager.state.active_scratchpads.get_mut(name) {
        if let Some(first_in_scratchpad) =
            next_valid_scratchpad_pid(id, &manager.state.windows, Direction::Forward)
        {
            if let Some((is_visible, window_handle)) = manager
                .state
                .windows
                .iter()
                .find(|w| w.pid == Some(first_in_scratchpad))
                .map(|w| (w.has_tag(current_tag), w.handle))
            {
                let action_result = if is_visible {
                    // Window is visible => Hide the scratchpad.
                    hide_scratchpad(manager, &window_handle)
                } else {
                    // Window is hidden => show the scratchpad
                    show_scratchpad(manager, &window_handle)
                };

                // Report the result of hiding/showing the scratchpad
                return match action_result {
                    Ok(()) => Some(true),
                    Err(msg) => {
                        tracing::error!("{}", msg);
                        return Some(false);
                    }
                };
            }
        }
    }

    let scratchpad = manager
        .state
        .scratchpads
        .iter()
        .find(|s| name == &s.name)?
        .clone();

    tracing::debug!(
        "No active scratchpad found for name {:?}. Creating a new one",
        name
    );
    tracing::debug!("Args for scratchpad: {:?}", &scratchpad.args);

    let pid: ChildID = exec_shell_with_args(
        &scratchpad.value,
        scratchpad.args.unwrap_or_else(Vec::new),
        &mut manager.children,
    )?;

    match manager.state.active_scratchpads.get_mut(name) {
        Some(windows) => {
            windows.push_front(pid);
        }
        None => {
            manager
                .state
                .active_scratchpads
                .insert(scratchpad.name, VecDeque::from([pid]));
        }
    }

    None
}
Handle the command to toggle the scratchpad
leftwm-core/src/handlers/command_handler/scratchpad_handler.rs