agentmux_mcp/
window_blackout.rs1use anyhow::Result;
16use serde::Deserialize;
17
18#[derive(Debug, Clone, Copy, Deserialize, Default, PartialEq)]
20pub(crate) struct MaskRect {
21 pub(crate) x: f64,
22 pub(crate) y: f64,
23 pub(crate) w: f64,
24 pub(crate) h: f64,
25}
26
27#[derive(Debug, Clone, Deserialize, Default, PartialEq)]
29pub(crate) struct WindowMask {
30 pub(crate) window_id: String,
31 pub(crate) title: String,
32 pub(crate) width: f64,
33 pub(crate) height: f64,
34 pub(crate) scale: f64,
35 #[serde(default)]
36 pub(crate) rects: Vec<MaskRect>,
37 #[serde(default)]
38 pub(crate) moving: bool,
39 #[serde(default)]
40 pub(crate) changed: bool,
41 #[serde(default)]
42 pub(crate) refuse: Option<String>,
43}
44
45const INSTEAD: &str = "try again in a moment, or use UI screenshot of your own pane";
47
48pub(crate) const UNSANDBOXED_SUFFIX: &str = " — Sandbox Disabled";
51
52fn same_title(title: &str, page: &str) -> bool {
54 title == page || title.strip_suffix(UNSANDBOXED_SUFFIX) == Some(page)
55}
56
57pub(crate) fn unique_title(same_title: usize, title: &str) -> Result<()> {
60 if same_title > 1 {
61 anyhow::bail!(
62 "{same_title} AgentMux windows have the title {title:?}, so their private panes can't be told apart and neither is captured: rename one, or use UI screenshot of your own pane"
63 );
64 }
65 Ok(())
66}
67
68pub(crate) fn before<'a>(masks: &'a [WindowMask], title: &str) -> Result<&'a WindowMask> {
70 let mine: Vec<&WindowMask> = masks.iter().filter(|m| same_title(title, &m.title)).collect();
71 let mask = match mine.as_slice() {
72 [] => anyhow::bail!("this AgentMux window did not give the rectangles of its private panes, so it is not captured: {INSTEAD}"),
73 [one] => *one,
74 _ => anyhow::bail!(
75 "{} AgentMux windows have the title {title:?}, so their private panes can't be told apart and neither is captured: rename one, or use UI screenshot of your own pane",
76 mine.len()
77 ),
78 };
79 if let Some(why) = &mask.refuse {
80 anyhow::bail!("this AgentMux window can't be captured now: {why}; {INSTEAD}");
81 }
82 if mask.moving {
83 anyhow::bail!("this AgentMux window's layout is moving, so it is not captured: {INSTEAD}");
84 }
85 Ok(mask)
86}
87
88pub(crate) fn after(begin: &WindowMask, ends: &[WindowMask], width: u32, height: u32) -> Result<()> {
91 let Some(end) = ends.iter().find(|m| m.window_id == begin.window_id) else {
92 anyhow::bail!("this AgentMux window did not answer after the frame was taken, so it is not captured: {INSTEAD}");
93 };
94 if end.changed || end.moving || end.refuse.is_some() || end.rects != begin.rects {
95 anyhow::bail!("this AgentMux window's layout changed while the frame was taken, so it is not captured: {INSTEAD}");
96 }
97 let same_geometry = end.width == begin.width && end.height == begin.height && end.scale == begin.scale;
98 let near = |frame: u32, css: f64| (frame as f64 - css * begin.scale).abs() <= 2.0;
99 if !same_geometry || !near(width, begin.width) || !near(height, begin.height) {
100 anyhow::bail!(
101 "the frame ({width}×{height}) is not the size or scale the rectangles were measured for ({:.0}×{:.0} at {}), so it is not captured: {INSTEAD}",
102 begin.width * begin.scale,
103 begin.height * begin.scale,
104 begin.scale
105 );
106 }
107 Ok(())
108}
109
110pub(crate) fn black_out(image: &mut image::RgbaImage, mask: &WindowMask) {
112 let (w, h) = (image.width() as i64, image.height() as i64);
113 for r in &mask.rects {
114 let x0 = ((r.x * mask.scale).floor() as i64 - 1).clamp(0, w);
115 let y0 = ((r.y * mask.scale).floor() as i64 - 1).clamp(0, h);
116 let x1 = (((r.x + r.w) * mask.scale).ceil() as i64 + 1).clamp(0, w);
117 let y1 = (((r.y + r.h) * mask.scale).ceil() as i64 + 1).clamp(0, h);
118 for y in y0..y1 {
119 for x in x0..x1 {
120 image.put_pixel(x as u32, y as u32, image::Rgba([0, 0, 0, 255]));
121 }
122 }
123 }
124}
125
126pub(crate) const OTHER_INSTANCE: &str = "that window belongs to another AgentMux, which this one can't ask for the rectangles of its private panes, so it is not captured: capture it from an agent of that AgentMux, or use UI screenshot of your own pane";
128
129pub(crate) const IN_CONTAINER: &str = "a container agent can't capture the host's windows: use UI screenshot of your own pane, or of a browser pane you opened";
131
132#[cfg(test)]
133#[path = "tests/window_blackout_tests.rs"]
134mod tests;