fix(browser): the bot opens its pages in windows of their own behind, so it never takes the focus
On the shared screen a tab brought to the front makes Chrome bring its window forward and take the keyboard focus from the game or program the user is in (seen on the game PC). The botBrowser tool now opens each person's page in a window of its own, behind and at the place and size of the bot browser's window (Target.createTarget with newWindow and background), and closes it again if the connection to it fails. While the browser runs on the virtual monitor, the app looks for new Chrome windows every 200 ms and settles each like the first (tool window, fitted to the work area, without activating it), the newest in front of the older ones, so the shared screen shows the page the bot opened last. Checked on the game PC: the bot's window loading a public page comes in front of the first, both out of the taskbar and Alt+Tab, the focus staying in Discord also with the user idle for hours. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -330,26 +330,26 @@ mod win {
|
||||
1
|
||||
}
|
||||
|
||||
/// Make process `pid`'s top-level Chrome windows tool windows (not in Alt+Tab, Win+Tab or the
|
||||
/// taskbar), fill their monitor's work area (Windows may give the virtual monitor a taskbar of
|
||||
/// its own, which would cover the page's bottom) and put them behind all others, all without
|
||||
/// activating them. Returns how many it made tool windows.
|
||||
/// Make process `pid`'s new top-level Chrome windows tool windows (not in Alt+Tab, Win+Tab or
|
||||
/// the taskbar) and fill their monitor's work area (Windows may give the virtual monitor a
|
||||
/// taskbar of its own, which would cover the page's bottom), all without activating them. The
|
||||
/// bot's windows stay behind all others, the newest in front of the older ones. Returns how many
|
||||
/// windows were new.
|
||||
pub fn settle_windows(pid: u32) -> usize {
|
||||
let mut found = Found { pid, windows: vec![] };
|
||||
// SAFETY: `collect` only reads windows and writes into `found`, which lives through the call.
|
||||
unsafe { EnumWindows(Some(collect), &mut found as *mut Found as LPARAM) };
|
||||
let mut changed = 0;
|
||||
for h in found.windows {
|
||||
// SAFETY: a plain query on a window handle EnumWindows gave.
|
||||
let (old, new): (Vec<HWND>, Vec<HWND>) = found.windows.into_iter()
|
||||
.partition(|&h| unsafe { GetWindowLongPtrW(h, GWL_EXSTYLE) } as u32 & WS_EX_TOOLWINDOW != 0);
|
||||
for &h in &new {
|
||||
// SAFETY: style changes on a window of our own child process; hiding and showing it
|
||||
// again is what makes the taskbar notice the new style.
|
||||
unsafe {
|
||||
let ex = GetWindowLongPtrW(h, GWL_EXSTYLE) as u32;
|
||||
if ex & WS_EX_TOOLWINDOW == 0 {
|
||||
ShowWindow(h, SW_HIDE);
|
||||
SetWindowLongPtrW(h, GWL_EXSTYLE, ((ex | WS_EX_TOOLWINDOW) & !WS_EX_APPWINDOW) as isize);
|
||||
ShowWindow(h, SW_SHOWNOACTIVATE);
|
||||
changed += 1;
|
||||
}
|
||||
ShowWindow(h, SW_HIDE);
|
||||
SetWindowLongPtrW(h, GWL_EXSTYLE, ((ex | WS_EX_TOOLWINDOW) & !WS_EX_APPWINDOW) as isize);
|
||||
ShowWindow(h, SW_SHOWNOACTIVATE);
|
||||
let mut mi = MONITORINFO { cbSize: std::mem::size_of::<MONITORINFO>() as u32, ..Default::default() };
|
||||
if GetMonitorInfoW(MonitorFromWindow(h, MONITOR_DEFAULTTONEAREST), &mut mi) != 0 {
|
||||
let w = mi.rcWork;
|
||||
@@ -359,7 +359,14 @@ mod win {
|
||||
}
|
||||
}
|
||||
}
|
||||
changed
|
||||
if !new.is_empty() {
|
||||
// Top first, each to the bottom: they keep their order, all behind the new ones.
|
||||
for &h in &old {
|
||||
// SAFETY: a z-order change of our own child process's window, without activating it.
|
||||
unsafe { SetWindowPos(h, HWND_BOTTOM, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE) };
|
||||
}
|
||||
}
|
||||
new.len()
|
||||
}
|
||||
|
||||
/// The root devices with the driver's hardware ID, in any class (a device whose driver install
|
||||
|
||||
Reference in New Issue
Block a user