Skip to main content

agentmux_srv/server/streamed_browser/screen/
watch.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The screen's windows, as the person would count them. A window the browser opens by itself (a pop-up) shows on the screen as it
5//! would on a person's desktop; what srv adds is what the streamed pane does for the same event (`windows.rs`): a notice, and the
6//! keys held for a moment so what is being typed does not go to the new window by mistake.
7
8use std::time::Instant;
9
10use super::super::windows::{NoticeLimiter, KEY_GAP};
11
12/// Did one more window open between a look that saw `before` and one that saw `count`? The browser's first window is the pane itself coming
13/// up, not a new window.
14pub fn opened_one(before: usize, count: usize) -> bool {
15    before > 0 && count > before
16}
17
18#[derive(Default)]
19pub struct Watch {
20    /// How many windows were there at the last look (0 until the browser's first window has come up).
21    count: usize,
22    held_until: Option<Instant>,
23    limiter: NoticeLimiter,
24}
25
26impl Watch {
27    /// A look at the screen at `now` saw `count` windows. The notice to show when one more window opened.
28    pub fn seen(&mut self, count: usize, now: Instant) -> Option<String> {
29        let before = std::mem::replace(&mut self.count, count);
30        if !opened_one(before, count) {
31            return None;
32        }
33        self.held_until = Some(now + KEY_GAP);
34        self.limiter.allow(now).then(|| "A new window opened in this browser. Typing was held for half a second so that it did not go to the new window by mistake.".to_string())
35    }
36
37    /// Are keys held at `now`?
38    pub fn keys_held(&self, now: Instant) -> bool {
39        self.held_until.is_some_and(|t| now < t)
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::*;
46    use std::time::Duration;
47
48    #[test]
49    fn the_browsers_first_window_is_not_news() {
50        let mut w = Watch::default();
51        let t = Instant::now();
52        assert_eq!(w.seen(0, t), None);
53        assert_eq!(w.seen(1, t), None);
54        assert!(!w.keys_held(t));
55    }
56
57    #[test]
58    fn only_a_window_after_the_first_that_adds_to_the_count_is_news() {
59        assert!(!opened_one(0, 1), "the pane's own first window");
60        assert!(!opened_one(2, 2) && !opened_one(2, 1), "no more windows, or fewer");
61        assert!(opened_one(1, 2) && opened_one(2, 4));
62    }
63
64    #[test]
65    fn a_second_window_is_told_and_holds_the_keys_for_half_a_second() {
66        let mut w = Watch::default();
67        let t = Instant::now();
68        w.seen(1, t);
69        let notice = w.seen(2, t).expect("a notice for a new window");
70        assert!(notice.contains("new window"), "{notice}");
71        assert!(w.keys_held(t) && w.keys_held(t + Duration::from_millis(499)));
72        assert!(!w.keys_held(t + KEY_GAP));
73    }
74
75    #[test]
76    fn a_window_that_closes_says_nothing_and_a_burst_is_one_notice_though_each_holds_keys() {
77        let mut w = Watch::default();
78        let t = Instant::now();
79        w.seen(1, t);
80        assert!(w.seen(2, t).is_some());
81        assert_eq!(w.seen(1, t + Duration::from_secs(1)), None);
82        let later = t + Duration::from_secs(1);
83        assert_eq!(w.seen(2, later), None, "throttled: a notice at most every few seconds");
84        assert!(w.keys_held(later), "but the keys are held again");
85        assert!(w.seen(3, t + Duration::from_secs(10)).is_some(), "after the pause a notice is allowed again");
86    }
87}