Expose window evidence snapshots

This commit is contained in:
2026-05-25 20:57:41 -04:00
parent 1b8b6cabf0
commit 92cc023b4d
3 changed files with 72 additions and 10 deletions
+23 -9
View File
@@ -1,10 +1,28 @@
use super::WindowSnapshot;
use crate::domain::EvidenceHealth;
use regex::Regex; use regex::Regex;
use std::{process::Command, process::Output, str}; use std::{process::Command, process::Output, str};
pub fn get_title_clean() -> String { pub fn get_title_clean() -> String {
let title = get_window_info().title; get_snapshot().title
}
pub fn get_snapshot() -> WindowSnapshot {
let window_info = get_window_info();
let re = Regex::new(r"-?\d+([:.]\d+)+%?").unwrap(); let re = Regex::new(r"-?\d+([:.]\d+)+%?").unwrap();
re.replace_all(&title, "").to_string() let title = re.replace_all(&window_info.title, "").to_string();
let class = (window_info.class != "none").then_some(window_info.class);
let health = if window_info.wid.is_empty() {
EvidenceHealth::Unavailable("xdotool active window unavailable".to_string())
} else {
EvidenceHealth::Available
};
WindowSnapshot {
title,
class,
health,
}
} }
pub fn minimize_other(title: &str) { pub fn minimize_other(title: &str) {
@@ -16,7 +34,7 @@ pub fn minimize_other(title: &str) {
struct WindowInfo { struct WindowInfo {
title: String, title: String,
_class: String, class: String,
wid: String, wid: String,
} }
@@ -46,7 +64,7 @@ fn run(cmd: &str) -> Option<String> {
fn get_window_info() -> WindowInfo { fn get_window_info() -> WindowInfo {
let none = WindowInfo { let none = WindowInfo {
title: "none".to_string(), title: "none".to_string(),
_class: "none".to_string(), class: "none".to_string(),
wid: "".to_string(), wid: "".to_string(),
}; };
@@ -62,9 +80,5 @@ fn get_window_info() -> WindowInfo {
return none; return none;
}; };
WindowInfo { WindowInfo { title, class, wid }
title,
_class: class,
wid,
}
} }
+36
View File
@@ -1,3 +1,22 @@
use crate::domain::EvidenceHealth;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct WindowSnapshot {
pub title: String,
pub class: Option<String>,
pub health: EvidenceHealth,
}
impl WindowSnapshot {
pub fn unavailable(reason: impl Into<String>) -> Self {
Self {
title: "none".to_string(),
class: None,
health: EvidenceHealth::Unavailable(reason.into()),
}
}
}
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
mod windows; mod windows;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
@@ -7,3 +26,20 @@ pub use windows::*;
mod linux; mod linux;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub use linux::*; pub use linux::*;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unavailable_snapshot_records_reason_without_evidence() {
assert_eq!(
WindowSnapshot::unavailable("no active window"),
WindowSnapshot {
title: "none".to_string(),
class: None,
health: EvidenceHealth::Unavailable("no active window".to_string()),
}
);
}
}
+13 -1
View File
@@ -1,12 +1,24 @@
use super::WindowSnapshot;
use crate::domain::EvidenceHealth;
use regex::Regex; use regex::Regex;
use std::{ffi::OsString, os::windows::ffi::OsStringExt}; use std::{ffi::OsString, os::windows::ffi::OsStringExt};
use winapi::shared::windef::HWND; use winapi::shared::windef::HWND;
use winapi::um::winuser::{GetForegroundWindow, GetWindowTextW, ShowWindow, SW_MINIMIZE}; use winapi::um::winuser::{GetForegroundWindow, GetWindowTextW, ShowWindow, SW_MINIMIZE};
pub fn get_title_clean() -> String { pub fn get_title_clean() -> String {
get_snapshot().title
}
pub fn get_snapshot() -> WindowSnapshot {
let title = get_window_info().title; let title = get_window_info().title;
let re = Regex::new(r"-?\d+([:.]\d+)+%?").unwrap(); let re = Regex::new(r"-?\d+([:.]\d+)+%?").unwrap();
re.replace_all(&title, "").to_string() WindowSnapshot {
title: re.replace_all(&title, "").to_string(),
class: None,
health: EvidenceHealth::Degraded(
"window class unavailable on current Windows adapter".to_string(),
),
}
} }
pub fn minimize_other(title: &str) { pub fn minimize_other(title: &str) {