Repository: mediar-ai/screenpipe
File: screenpipe-vision/src/capture_screenshot_by_window.rs (Rust)

Code before:
                .iter()
                .any(|ignore| app_name_lower.contains(ignore) || title_lower.contains(ignore))
        {
            return false;
        }

        false
    }
}

pub async fn capture_all_visible_windows(
    monitor: &SafeMonitor,
    window_filters: &WindowFilters,
    capture_unfocused_windows: bool,
) -> Result<Vec<CapturedWindow>, Box<dyn Error>> {
    let mut all_captured_images = Vec::new();

    // Get windows and immediately extract the data we need
    let windows_data = Window::all()?
        .into_iter()
        .filter_map(|window| {
            // Extract all necessary data from the window while in the main thread
            let app_name = window.app_name().unwrap();
            let title = window.title().unwrap();
            let is_focused = window.is_focused().unwrap();
            let process_id = window.pid().unwrap();
            // Capture image immediately while we have access to the window
            match window.capture_image() {
                Ok(buffer) => Some((app_name, title, is_focused, buffer, process_id)),
                Err(_) => None,
            }
        })
        .collect::<Vec<_>>();

    if windows_data.is_empty() {
        return Err(Box::new(CaptureError::NoWindows));
    }

    // Process the captured data
    for (app_name, window_name, is_focused, buffer, process_id) in windows_data {
        // Convert to DynamicImage
        let image = DynamicImage::ImageRgba8(
            image::ImageBuffer::from_raw(buffer.width(), buffer.height(), buffer.into_raw())
                .unwrap(),
        );

        // Apply filters
        let is_valid = !SKIP_APPS.contains(app_name.as_str())
            && !SKIP_TITLES.contains(window_name.as_str())
            && (capture_unfocused_windows || (is_focused && monitor.id() == monitor.id()))
            && window_filters.is_valid(&app_name, &window_name);

Review comment (suggestion):
```suggestion
            let app_name = window.app_name().unwrap_or_default().to_string();
            let title = window.title().unwrap_or_default().to_string();
            let is_focused = window.is_focused().unwrap_or(false);
            let process_id = window.pid().unwrap_or(0);
```

Code after:
                .iter()
                .any(|ignore| app_name_lower.contains(ignore) || title_lower.contains(ignore))
        {
            return false;
        }

        false
    }
}

pub async fn capture_all_visible_windows(
    monitor: &SafeMonitor,
    window_filters: &WindowFilters,
    capture_unfocused_windows: bool,
) -> Result<Vec<CapturedWindow>, Box<dyn Error>> {
    let mut all_captured_images = Vec::new();

    // Get windows and immediately extract the data we need
    let windows_data = Window::all()?
        .into_iter()
        .filter_map(|window| {
            // Extract all necessary data from the window while in the main thread
            let app_name = window.app_name().unwrap_or_default().to_string();
            let title = window.title().unwrap_or_default().to_string();
            let is_focused = window.is_focused().unwrap_or(false);
            let process_id = window.pid().unwrap_or(0);
            // Capture image immediately while we have access to the window
            match window.capture_image() {
                Ok(buffer) => Some((app_name, title, is_focused, buffer, process_id)),
                Err(_) => None,
            }
        })
        .collect::<Vec<_>>();

    if windows_data.is_empty() {
        return Err(Box::new(CaptureError::NoWindows));
    }

    // Process the captured data
    for (app_name, window_name, is_focused, buffer, process_id) in windows_data {
        // Convert to DynamicImage
        let image = DynamicImage::ImageRgba8(
            image::ImageBuffer::from_raw(buffer.width(), buffer.height(), buffer.into_raw())
                .unwrap(),
        );

        // Apply filters
        let is_valid = !SKIP_APPS.contains(app_name.as_str())
            && !SKIP_TITLES.contains(window_name.as_str())
            && (capture_unfocused_windows || (is_focused && monitor.id() == monitor.id()))
            && window_filters.is_valid(&app_name, &window_name);