fn process_window()

in crates/fig_desktop/src/platform/linux/x11.rs [147:254]


fn process_window(
    conn: &RustConnection,
    x11_state: &X11State,
    proxy: &EventLoopProxy,
    platform_state: &Arc<PlatformStateImpl>,
) -> anyhow::Result<()> {
    let hide = || {
        proxy.send_event(Event::WindowEvent {
            window_id: AUTOCOMPLETE_ID.clone(),
            window_event: WindowEvent::Hide,
        })
    };

    let mut window = get_input_focus(conn)?.reply()?.focus;

    let (active_window, wm_class) = 'win: loop {
        if window == 0 {
            hide()?;
            return Ok(());
        }

        let wm_class = WmClass::get(conn, window)?.reply();

        let wm_class = String::from_utf8_lossy(&match wm_class {
            Ok(Some(class_raw)) => class_raw.class().to_owned(),
            // hide if missing wm class
            Ok(None) => {
                debug!("No wm class");
                hide()?;
                return Ok(());
            },
            Err(err) => {
                debug!("Error getting wm class: {err:?}");
                hide()?;
                return Ok(());
            },
        })
        .to_string();

        if wm_class == "FocusProxy" {
            window = query_tree(conn, window)?.reply()?.parent;
        } else {
            break 'win (window, wm_class);
        }
    };

    debug!("active window id: {active_window}");
    debug!("focus changed to {}", wm_class);

    let window_reply = window_geometry(conn, active_window);

    let old_window_data = x11_state.active_window.lock().replace(X11WindowData {
        id: active_window,
        window_geometry: window_reply.ok().map(|window_reply| Rect {
            position: PhysicalPosition {
                x: window_reply.x,
                y: window_reply.y,
            }
            .into(),
            size: PhysicalSize {
                width: window_reply.width,
                height: window_reply.height,
            }
            .into(),
        }),
    });

    if wm_class == DESKTOP_APP_WM_CLASS {
        // get wm_role
        let reply = get_property(
            conn,
            false,
            active_window,
            x11_state.atom_b2a(conn, WM_WINDOW_ROLE)?,
            AtomEnum::STRING,
            0,
            2048,
        )?
        .reply()?;

        if &reply.value != b"autocomplete" {
            // hide if not an autocomplete window
            hide()?;
        }

        return Ok(());
    }

    debug!("Selected window is not Fig");

    if let Some(terminal) = WM_CLASS_ALLOWLIST.get(&wm_class.as_str()) {
        *platform_state.active_terminal.lock() = Some(terminal.clone());
    }

    if let Some(old_window_data) = old_window_data {
        if old_window_data.id != active_window {
            hide()?;
            return Ok(());
        }
    }

    if !WM_CLASS_ALLOWLIST.contains_key(&wm_class.as_str()) {
        hide()?;
        return Ok(());
    }

    Ok(())
}