diff --git a/CHANGELOG.md b/CHANGELOG.md index d7d5003c..05b24b05 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,13 @@ and listed with their assets on the ## [Unreleased] +### Fixed + +- **A tree opened in a program comes to the front on Windows** (ADR-0046): + Explorer, the terminals, the editors, the agents and Orca opened behind + the browser, as the hub runs in the background. The hub takes the + foreground first and hands it to the program. + ## [0.16.0] - 2026-10-21 The hub tells the desktop what needs attention (a failed build, a held diff --git a/crates/devstack-hub/src/dialog.rs b/crates/devstack-hub/src/dialog.rs index 6d6ab808..e9dcdd68 100644 --- a/crates/devstack-hub/src/dialog.rs +++ b/crates/devstack-hub/src/dialog.rs @@ -3,8 +3,8 @@ //! Windows: the Explorer folder picker (`IFileOpenDialog`), Linux: the //! desktop's dialog through the XDG portal (no GTK linked in). The hub has no //! window of its own and runs in the background, so on Windows the dialog gets -//! a small owner window that is brought to the front first; without one it -//! would open behind the browser that asked for it. +//! an owner window that is brought to the front first (`foreground::Front`); +//! without one it would open behind the browser that asked for it. /// The chosen folder, `None` when cancelled, `Err` when no dialog can be shown. pub fn pick_folder() -> Result, String> { @@ -14,7 +14,7 @@ pub fn pick_folder() -> Result, String> { #[cfg(windows)] mod imp { pub fn pick() -> Result, String> { - let owner = super::owner::Owner::new(); + let owner = crate::foreground::Front::take(); let mut d = rfd::FileDialog::new().set_title("Choose the repository folder (devstack)"); if let Some(o) = &owner { d = d.set_parent(o); @@ -37,93 +37,3 @@ mod imp { .map(|p| p.to_string_lossy().into_owned())) } } - -#[cfg(windows)] -mod owner { - //! An invisible, topmost owner window in the foreground. A background - //! process may not take the foreground; the usual way around it is a - //! synthetic Alt key event, after which `SetForegroundWindow` is allowed. - use raw_window_handle::{ - DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawWindowHandle, - Win32WindowHandle, WindowHandle, - }; - use std::num::NonZeroIsize; - use windows_sys::Win32::Foundation::HWND; - use windows_sys::Win32::UI::Input::KeyboardAndMouse::{ - INPUT, INPUT_0, INPUT_KEYBOARD, KEYBDINPUT, KEYEVENTF_KEYUP, SendInput, VK_MENU, - }; - use windows_sys::Win32::UI::WindowsAndMessaging::{ - CreateWindowExW, DestroyWindow, SW_SHOW, SetForegroundWindow, ShowWindow, WS_EX_TOOLWINDOW, - WS_EX_TOPMOST, WS_POPUP, - }; - - pub struct Owner(HWND); - - impl Owner { - pub fn new() -> Option { - let class: Vec = "STATIC\0".encode_utf16().collect(); - let title: Vec = "devstack\0".encode_utf16().collect(); - // SAFETY: plain Win32 calls; the window is destroyed in Drop. - unsafe { - let hwnd = CreateWindowExW( - WS_EX_TOPMOST | WS_EX_TOOLWINDOW, - class.as_ptr(), - title.as_ptr(), - WS_POPUP, - 0, - 0, - 0, - 0, - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null_mut(), - std::ptr::null(), - ); - if hwnd.is_null() { - return None; - } - let alt = |flags| INPUT { - r#type: INPUT_KEYBOARD, - Anonymous: INPUT_0 { - ki: KEYBDINPUT { - wVk: VK_MENU, - wScan: 0, - dwFlags: flags, - time: 0, - dwExtraInfo: 0, - }, - }, - }; - let inputs = [alt(0), alt(KEYEVENTF_KEYUP)]; - SendInput(2, inputs.as_ptr(), std::mem::size_of::() as i32); - ShowWindow(hwnd, SW_SHOW); - SetForegroundWindow(hwnd); - Some(Owner(hwnd)) - } - } - } - - impl Drop for Owner { - fn drop(&mut self) { - // SAFETY: our own window. - unsafe { - DestroyWindow(self.0); - } - } - } - - impl HasWindowHandle for Owner { - fn window_handle(&self) -> Result, HandleError> { - let h = NonZeroIsize::new(self.0 as isize).ok_or(HandleError::Unavailable)?; - let raw = RawWindowHandle::Win32(Win32WindowHandle::new(h)); - // SAFETY: the window lives as long as `self`. - Ok(unsafe { WindowHandle::borrow_raw(raw) }) - } - } - - impl HasDisplayHandle for Owner { - fn display_handle(&self) -> Result, HandleError> { - Ok(DisplayHandle::windows()) - } - } -} diff --git a/crates/devstack-hub/src/foreground.rs b/crates/devstack-hub/src/foreground.rs new file mode 100644 index 00000000..92f420cc --- /dev/null +++ b/crates/devstack-hub/src/foreground.rs @@ -0,0 +1,99 @@ +//! The foreground on Windows, for a hub that runs in the background: a +//! window it opens (the folder dialog) or a program it starts (ADR-0046) +//! would appear behind the browser that asked for it, as Windows lets a +//! background process neither take the foreground nor give it. +//! +//! [`Front`] is an invisible, topmost window brought to the foreground. The +//! usual way around the lock is a synthetic Alt key event, after which +//! `SetForegroundWindow` is allowed. While the hub has the foreground, it +//! lets any process take it next (`AllowSetForegroundWindow(ASFW_ANY)`, +//! until the user's next input): the program it starts, or the running one +//! a link is handed to (VS Code, Orca). + +use raw_window_handle::{ + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawWindowHandle, + Win32WindowHandle, WindowHandle, +}; +use std::num::NonZeroIsize; +use windows_sys::Win32::Foundation::HWND; +use windows_sys::Win32::UI::Input::KeyboardAndMouse::{ + INPUT, INPUT_0, INPUT_KEYBOARD, KEYBDINPUT, KEYEVENTF_KEYUP, SendInput, VK_MENU, +}; +use windows_sys::Win32::UI::WindowsAndMessaging::{ + ASFW_ANY, AllowSetForegroundWindow, CreateWindowExW, DestroyWindow, SW_SHOW, + SetForegroundWindow, ShowWindow, WS_EX_TOOLWINDOW, WS_EX_TOPMOST, WS_POPUP, +}; + +/// The hub's window in the foreground, until dropped. Created and dropped on +/// the same thread (a window belongs to the thread that created it). +pub struct Front(HWND); + +impl Front { + /// `None`: no window could be created; what follows may open behind. + pub fn take() -> Option { + let class: Vec = "STATIC\0".encode_utf16().collect(); + let title: Vec = "devstack\0".encode_utf16().collect(); + // SAFETY: plain Win32 calls; the window is destroyed in Drop. + unsafe { + let hwnd = CreateWindowExW( + WS_EX_TOPMOST | WS_EX_TOOLWINDOW, + class.as_ptr(), + title.as_ptr(), + WS_POPUP, + 0, + 0, + 0, + 0, + std::ptr::null_mut(), + std::ptr::null_mut(), + std::ptr::null_mut(), + std::ptr::null(), + ); + if hwnd.is_null() { + return None; + } + let alt = |flags| INPUT { + r#type: INPUT_KEYBOARD, + Anonymous: INPUT_0 { + ki: KEYBDINPUT { + wVk: VK_MENU, + wScan: 0, + dwFlags: flags, + time: 0, + dwExtraInfo: 0, + }, + }, + }; + let inputs = [alt(0), alt(KEYEVENTF_KEYUP)]; + SendInput(2, inputs.as_ptr(), std::mem::size_of::() as i32); + ShowWindow(hwnd, SW_SHOW); + SetForegroundWindow(hwnd); + AllowSetForegroundWindow(ASFW_ANY); + Some(Front(hwnd)) + } + } +} + +impl Drop for Front { + fn drop(&mut self) { + // SAFETY: our own window. + unsafe { + DestroyWindow(self.0); + } + } +} + +impl HasWindowHandle for Front { + fn window_handle(&self) -> Result, HandleError> { + let h = NonZeroIsize::new(self.0 as isize).ok_or(HandleError::Unavailable)?; + let raw = RawWindowHandle::Win32(Win32WindowHandle::new(h)); + // SAFETY: the window lives as long as `self`. + Ok(unsafe { WindowHandle::borrow_raw(raw) }) + } +} + +impl HasDisplayHandle for Front { + fn display_handle(&self) -> Result, HandleError> { + Ok(DisplayHandle::windows()) + } +} diff --git a/crates/devstack-hub/src/lib.rs b/crates/devstack-hub/src/lib.rs index 90632337..dce0710a 100644 --- a/crates/devstack-hub/src/lib.rs +++ b/crates/devstack-hub/src/lib.rs @@ -34,6 +34,8 @@ use tokio_stream::StreamExt; static UI: Dir<'_> = include_dir!("$OUT_DIR/ui"); mod dialog; +#[cfg(windows)] +mod foreground; mod guests; mod notify; mod open; diff --git a/crates/devstack-hub/src/open.rs b/crates/devstack-hub/src/open.rs index f47abe3f..b356aba4 100644 --- a/crates/devstack-hub/src/open.rs +++ b/crates/devstack-hub/src/open.rs @@ -114,6 +114,10 @@ pub fn open(id: &str, tree: &Path, repo: &Path, stack_file: &Path) -> Result<(), .find(|f| f.opener.id == id) .ok_or(OpenError::Unknown)?; let path = display(tree); + // Windows: the hub is in the background, the program would open behind + // the browser; held until it is started or handed the link. + #[cfg(windows)] + let _front = crate::foreground::Front::take(); match &f.how { How::Folder => imp::shell_open(&path), How::Link(scheme) => imp::shell_open(&link(scheme, &path)), diff --git a/docs/adr/0046-the-hub-opens-a-worktree-in-the-users-programs.md b/docs/adr/0046-the-hub-opens-a-worktree-in-the-users-programs.md index 0cd2755d..f771726a 100644 --- a/docs/adr/0046-the-hub-opens-a-worktree-in-the-users-programs.md +++ b/docs/adr/0046-the-hub-opens-a-worktree-in-the-users-programs.md @@ -1,6 +1,6 @@ # ADR-0046: The hub opens a stack's worktree in the user's own programs, from a fixed list -- **Status:** Accepted (2026-10-21) +- **Status:** Accepted (2026-10-21), amended 2026-10-21 (the program opens in front on Windows) - **Date:** 2026-10-21 - **Deciders:** project owner - **Related:** [ADR-0011](0011-platform-windows-native-first.md), [ADR-0019](0019-control-api-security.md), [ADR-0021](0021-a-hub-serves-the-dashboard.md), [ADR-0024](0024-a-lane-is-a-supervisor-in-the-main-trees-daemon.md), [ADR-0040](0040-a-shared-lane-can-be-watched-like-ones-own.md) @@ -87,13 +87,20 @@ Windows Terminal is the `wt.exe` app alias on `PATH`. - A program the list does not know (JetBrains IDEs, Visual Studio, Sublime) is not offered; adding one is a code change. JetBrains' scheme (`jetbrains://`) needs Toolbox and a project name, not a path. -- The hub runs in the background: a window it starts may open behind the - browser where Windows does not let it take the foreground. +- The hub runs in the background, and Windows lets a background process + neither take the foreground nor give it. *Amended 2026-10-21:* the + programs opened behind the browser there. The hub now takes the foreground + first with a window of its own (the synthetic Alt key event its folder + dialog uses) and, while it has it, lets any process take it next + (`AllowSetForegroundWindow(ASFW_ANY)`): the program it starts, or the + running one a link is handed to. A desktop on Linux that prevents focus + stealing may still open it behind. - macOS lists nothing until #66. ## Enforced by -- `crates/devstack-hub/src/open.rs` (the list, finding, starting), routes +- `crates/devstack-hub/src/open.rs` (the list, finding, starting), + `crates/devstack-hub/src/foreground.rs` (the foreground on Windows), routes `/hub/openers` and `/hub/open` in `crates/devstack-hub/src/lib.rs`. - `crates/devstack-daemon/src/lanes.rs` (`worktree_of`). - `ui/src/components/StackHeader.vue` (the path's menu).