Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
#![cfg(target_os = "windows")]
use scap_targets::{Window, WindowId, platform::WindowListCache};
use std::{
io::{BufRead, BufReader, Write},
process::{Child, Command, Stdio},
sync::mpsc,
time::Duration,
};
use windows::{
Win32::{
Foundation::{HWND, LPARAM, WPARAM},
UI::{
HiDpi::{PROCESS_PER_MONITOR_DPI_AWARE, SetProcessDpiAwareness},
WindowsAndMessaging::{
CreateWindowExW, DestroyWindow, DispatchMessageW, IDI_APPLICATION, IDI_WARNING,
LoadIconW, MSG, PM_REMOVE, PeekMessageW, SW_MINIMIZE, SendMessageW, ShowWindow,
TranslateMessage, WINDOW_EX_STYLE, WINDOW_STYLE, WM_SETICON, WS_CHILD,
WS_EX_TOOLWINDOW, WS_OVERLAPPEDWINDOW, WS_POPUP, WS_VISIBLE,
},
},
},
core::w,
};
struct TestWindow(HWND);
impl TestWindow {
fn new(style: WINDOW_STYLE, extended: WINDOW_EX_STYLE, parent: Option<HWND>) -> Self {
Self(unsafe {
CreateWindowExW(
extended,
w!("STATIC"),
w!("Cap window discovery regression"),
style,
100,
100,
320,
240,
parent,
None,
None,
None,
)
.expect("create native test window")
})
}
fn id(&self) -> WindowId {
(self.0.0 as u64).to_string().parse().unwrap()
}
}
impl Drop for TestWindow {
fn drop(&mut self) {
let _ = unsafe { DestroyWindow(self.0) };
}
}
struct FixtureProcess(Child);
impl Drop for FixtureProcess {
fn drop(&mut self) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
#[test]
fn window_fixture_process() {
if std::env::var_os("CAP_WINDOW_DISCOVERY_FIXTURE").is_none() {
return;
}
unsafe { SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE) }
.expect("match CLI per-monitor DPI awareness");
let normal = TestWindow::new(WS_OVERLAPPEDWINDOW | WS_VISIBLE, WINDOW_EX_STYLE(0), None);
let popup = TestWindow::new(WS_POPUP | WS_VISIBLE, WINDOW_EX_STYLE(0), Some(normal.0));
for (window, resource) in [(&normal, IDI_APPLICATION), (&popup, IDI_WARNING)] {
let icon = unsafe { LoadIconW(None, resource) }.expect("load fixture icon");
unsafe {
SendMessageW(
window.0,
WM_SETICON,
Some(WPARAM(0)),
Some(LPARAM(icon.0 as isize)),
)
};
}
let hidden = TestWindow::new(WS_OVERLAPPEDWINDOW, WINDOW_EX_STYLE(0), None);
let child = TestWindow::new(WS_CHILD | WS_VISIBLE, WINDOW_EX_STYLE(0), Some(normal.0));
let tool = TestWindow::new(WS_OVERLAPPEDWINDOW | WS_VISIBLE, WS_EX_TOOLWINDOW, None);
let minimized = TestWindow::new(WS_OVERLAPPEDWINDOW | WS_VISIBLE, WINDOW_EX_STYLE(0), None);
let _ = unsafe { ShowWindow(minimized.0, SW_MINIMIZE) };
println!(
"WINDOW_FIXTURE {} {} {} {} {} {}",
normal.id(),
popup.id(),
hidden.id(),
child.id(),
tool.id(),
minimized.id()
);
std::io::stdout().flush().unwrap();
let (stop_tx, stop_rx) = mpsc::channel();
std::thread::spawn(move || {
let mut line = String::new();
let _ = std::io::stdin().read_line(&mut line);
let _ = stop_tx.send(());
});
let started = std::time::Instant::now();
while started.elapsed() < Duration::from_secs(30) && stop_rx.try_recv().is_err() {
let mut message = MSG::default();
while unsafe { PeekMessageW(&mut message, None, 0, 0, PM_REMOVE) }.as_bool() {
let _ = unsafe { TranslateMessage(&message) };
unsafe { DispatchMessageW(&message) };
}
std::thread::sleep(Duration::from_millis(10));
}
}
#[test]
fn discovers_foreign_top_level_windows_and_preserves_target_filters() {
unsafe { SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE) }
.expect("match CLI per-monitor DPI awareness");
let mut fixture = FixtureProcess(
Command::new(std::env::current_exe().unwrap())
.args(["--exact", "window_fixture_process", "--nocapture"])
.env("CAP_WINDOW_DISCOVERY_FIXTURE", "1")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
.expect("start native window fixture"),
);
let output = fixture.0.stdout.take().unwrap();
let (ready_tx, ready_rx) = mpsc::channel();
std::thread::spawn(move || {
for line in BufReader::new(output).lines() {
let Ok(line) = line else { break };
if let Some((_, ids)) = line.split_once("WINDOW_FIXTURE ") {
let _ = ready_tx.send(ids.to_string());
}
}
});
let ids: Vec<WindowId> = ready_rx
.recv_timeout(Duration::from_secs(15))
.expect("native window fixture must become ready")
.split_whitespace()
.map(|id| id.parse().unwrap())
.collect();
assert_eq!(ids.len(), 6);
let own = TestWindow::new(WS_OVERLAPPEDWINDOW | WS_VISIBLE, WINDOW_EX_STYLE(0), None);
let windows = Window::list();
let mut cache = WindowListCache::default();
let cached_windows = cache.list();
assert!(!windows.iter().any(|window| window.id() == own.id()));
for id in &ids[..2] {
let window = windows
.iter()
.find(|window| &window.id() == id)
.expect("visible top-level and owned popup windows must be discoverable");
assert!(window.raw_handle().is_valid());
assert!(window.raw_handle().is_on_screen());
assert!(window.name().is_some_and(|name| !name.is_empty()));
assert!(window.owner_name().is_some());
assert!(cached_windows.iter().any(|cached| cached.id() == *id));
assert_eq!(cache.is_valid(window), window.raw_handle().is_valid());
assert_eq!(
cache.is_on_screen(window),
window.raw_handle().is_on_screen()
);
assert_eq!(cache.owner_name(window), window.owner_name());
assert_eq!(cache.app_icon(window), window.app_icon());
assert!(window.display().is_some());
assert!(window.display_relative_logical_bounds().is_some());
assert_eq!(
window.raw_handle().inner().0 as u64,
id.to_string().parse::<u64>().unwrap()
);
let parsed: WindowId = id.to_string().parse().unwrap();
assert_eq!(
Window::from_id(&parsed)
.expect("resolve listed window ID")
.id(),
*id
);
}
let eligible: Vec<_> = windows
.iter()
.filter(|window| window.raw_handle().is_valid() && window.raw_handle().is_on_screen())
.map(Window::id)
.collect();
for id in &ids[2..] {
assert!(
!eligible.contains(id),
"ineligible window {id} must stay excluded"
);
}
let cached_eligible: Vec<_> = cached_windows
.iter()
.filter(|window| cache.is_valid(window) && cache.is_on_screen(window))
.map(Window::id)
.collect();
for id in &ids {
assert_eq!(cached_eligible.contains(id), eligible.contains(id));
}
let icons: Vec<_> = ids[..2]
.iter()
.map(|id| {
let window = windows.iter().find(|window| window.id() == *id).unwrap();
cache.app_icon(window).expect("fixture window icon")
})
.collect();
assert_ne!(icons[0], icons[1]);
fixture
.0
.stdin
.take()
.unwrap()
.write_all(b"stop\n")
.unwrap();
assert!(fixture.0.wait().unwrap().success());
}