pub fn release_scratchpad<H: Handle, C: Config, SERVER: DisplayServer<H>>(
    window: ReleaseScratchPadOption<H>,
    tag: Option<TagId>,
    manager: &mut Manager<H, C, SERVER>,
) -> Option<bool> {
    let destination_tag =
        tag.or_else(|| manager.state.focus_manager.tag_history.front().copied())?;

    // If `None`, replace with current window
    let window = if window == ReleaseScratchPadOption::None {
        ReleaseScratchPadOption::Handle(
            manager
                .state
                .focus_manager
                .window_history
                .front()?
                .as_ref()
                .copied()?,
        )
    } else {
        window
    };

    match window {
        ReleaseScratchPadOption::Handle(window_handle) => {
            // Check if window is in active scratchpad
            let window = manager
                .state
                .windows
                .iter_mut()
                .find(|w| w.handle == window_handle)?;

            let scratchpad_name: ScratchPadName = manager
                .state
                .active_scratchpads
                .iter_mut()
                .find(|(_, id)| window.pid.as_ref() == id.front())
                .map(|(name, _)| name.clone())?;

            tracing::debug!(
                "Releasing scratchpad {:?} to tag {}",
                scratchpad_name,
                destination_tag
            );

            // If we found window in scratchpad, remove it from active_scratchpads
            if let Some(windows) = manager.state.active_scratchpads.get_mut(&scratchpad_name) {
                if windows.len() > 1 {
                    // If more than 1, pop of the stack
                    tracing::debug!("Removed 1 window from scratchpad {:?}", &scratchpad_name);
                    windows.remove(
                        windows
                            .iter()
                            .position(|w| Some(w) == window.pid.as_ref())?,
                    );
                } else {
                    // If only 1, remove entire vec, not needed anymore
                    tracing::debug!(
                        "Empty scratchpad {:?}, removing from active_scratchpads",
                        &scratchpad_name
                    );
                    manager.state.active_scratchpads.remove(&scratchpad_name);
                }
            }

            Some(manager.command_handler(&Command::SendWindowToTag {
                window: Some(window_handle),
                tag: destination_tag,
            }))
        }
        ReleaseScratchPadOption::ScratchpadName(scratchpad_name) => {
            // Remove and get value from active_scratchpad
            let window_pid = manager
                .state
                .active_scratchpads
                .get_mut(&scratchpad_name)
                .and_then(|pids| {
                    next_valid_scratchpad_pid(pids, &manager.state.windows, Direction::Forward)
                })?;
            manager // We found already a working pid, discard from scratchpad
                .state
                .active_scratchpads
                .get_mut(&scratchpad_name)?
                .pop_front();

            let window_handle = manager
                .state
                .windows
                .iter()
                .find(|w| w.pid == Some(window_pid))
                .map(|w| w.handle);

            tracing::debug!(
                "Releasing scratchpad {:?} to tag {}",
                scratchpad_name,
                destination_tag
            );

            Some(manager.command_handler(&Command::SendWindowToTag {
                window: window_handle,
                tag: destination_tag,
            }))
        }
        ReleaseScratchPadOption::None => unreachable!(), // Should not be possible
    }
}
Release a scratchpad to become a normal window. When tag is None, use current active tag as the
destination. Window can be a handle to select a specific window, the name of a scratchpad or
none to select the current window.
leftwm-core/src/handlers/command_handler/scratchpad_handler.rs