agentmux_srv/server/streamed_browser/screen/
wm.rs1use std::path::Path;
11
12pub const RC_FILE: &str = "rc.xml";
14pub const MENU_FILE: &str = "menu.xml";
15
16pub fn rc(menu: &Path) -> String {
18 format!(
19 r#"<?xml version="1.0" encoding="UTF-8"?>
20<openbox_config xmlns="http://openbox.org/3.4/rc">
21 <focus><focusNew>yes</focusNew><followMouse>no</followMouse><focusLast>yes</focusLast></focus>
22 <placement><policy>Smart</policy></placement>
23 <desktops><number>1</number></desktops>
24 <keyboard></keyboard>
25 <mouse><dragThreshold>8</dragThreshold><doubleClickTime>500</doubleClickTime></mouse>
26 <menu><file>{}</file></menu>
27 <applications><application class="*"><decor>no</decor></application></applications>
28</openbox_config>
29"#,
30 menu.display()
31 )
32}
33
34pub const MENU: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
36<openbox_menu xmlns="http://openbox.org/3.4/menu">
37 <menu id="root-menu" label="AgentMux"></menu>
38</openbox_menu>
39"#;
40
41pub fn write(dir: &Path) -> Result<Vec<String>, String> {
43 std::fs::create_dir_all(dir).map_err(|e| format!("cannot make the window manager's folder: {e}"))?;
44 let (rc_path, menu_path) = (dir.join(RC_FILE), dir.join(MENU_FILE));
45 std::fs::write(&menu_path, MENU).map_err(|e| format!("cannot write the window manager's menu: {e}"))?;
46 std::fs::write(&rc_path, rc(&menu_path)).map_err(|e| format!("cannot write the window manager's settings: {e}"))?;
47 Ok(vec!["--config-file".to_string(), rc_path.display().to_string()])
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn the_window_manager_binds_nothing_a_client_could_use_to_run_a_program_or_move_a_window() {
56 let text = rc(Path::new("/tmp/wm/menu.xml"));
57 for bound in ["<keybind", "<mousebind", "<action", "Execute", "ShowMenu", "Move"] {
58 assert!(!text.contains(bound), "{bound} must not be in the settings");
59 }
60 assert!(text.contains("<keyboard></keyboard>"), "the keyboard section is there and empty (an absent one may fall back to defaults)");
61 assert!(text.contains("<decor>no</decor>"), "no window has a frame to drag");
62 assert!(text.contains("/tmp/wm/menu.xml"));
63 assert!(!MENU.contains("<item") && !MENU.contains("Execute"), "the menu is empty");
64 }
65
66 #[test]
67 fn the_files_are_written_and_the_arguments_name_the_settings() {
68 let dir = tempfile::tempdir().unwrap();
69 let args = write(&dir.path().join("wm")).unwrap();
70 assert_eq!(args[0], "--config-file");
71 assert!(Path::new(&args[1]).is_file() && dir.path().join("wm").join(MENU_FILE).is_file());
72 }
73}