rho-coding-agent 3.12.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
//! The viewport a session's tabs render at: the agent's ([`VIEWPORT`])
//! while it drives, so its screenshots and coordinates stay the same from
//! call to call, and the size the person views them at while a person
//! drives ([`crate::browser::SessionBrowser::viewport`]). Every tab takes
//! the same one, and a tab opens at it.

use super::{answer, SessionTabs, Tabs};
use crate::browser::VIEWPORT;
use futures_util::future::join_all;
use rho_browser::tab::Viewport;

/// The viewport the session's tabs were last fit to, which new tabs open
/// at.
#[derive(Debug, Clone, Copy)]
pub(super) struct Sizing(Viewport);

impl Default for Sizing {
    fn default() -> Self {
        Self(VIEWPORT)
    }
}

impl Sizing {
    pub(super) fn viewport(self) -> Viewport {
        self.0
    }
}

impl Tabs {
    /// Renders every tab at `viewport`, asking only those last asked for
    /// another, all at once. A tab that does not answer is not asked again:
    /// a page busy in a script holds `Emulation.setDeviceMetricsOverride`
    /// unanswered for as long as it runs (over 15 s in a loop,
    /// chrome-headless-shell 158), so asking on every call would hold up
    /// every call, and the browser applies the size once the page lets
    /// it. Each frame of a watch says the viewport it shows either way.
    pub(super) async fn fit(&mut self, viewport: Viewport) {
        self.sizing = Sizing(viewport);
        let unfit = self.open.iter_mut().filter(|open| open.size != viewport);
        let resized = unfit.map(|open| async move {
            open.size = viewport;
            if let Err(error) = answer(open.tab.resize(viewport)).await {
                tracing::warn!(tab = %open.id, %error, "could not resize a browser tab");
            }
        });
        join_all(resized).await;
    }
}

impl SessionTabs {
    /// Fits the tabs to the viewport the session's browser is sized for,
    /// once no call holds them. Tabs of a context the browser no longer
    /// has are left for the next call, which forgets them.
    pub(in crate::tools::browser) async fn fit_running(&self) {
        let mut tabs = self.tabs.lock().await;
        let Some(context) = self.browser.running_context().await else {
            return;
        };
        if tabs.context.as_ref() == Some(context.id()) {
            tabs.fit(self.browser.viewport()).await;
        }
    }
}