Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
96 changes: 3 additions & 93 deletions crates/devstack-hub/src/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>, String> {
Expand All @@ -14,7 +14,7 @@ pub fn pick_folder() -> Result<Option<String>, String> {
#[cfg(windows)]
mod imp {
pub fn pick() -> Result<Option<String>, 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);
Expand All @@ -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<Self> {
let class: Vec<u16> = "STATIC\0".encode_utf16().collect();
let title: Vec<u16> = "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::<INPUT>() 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<WindowHandle<'_>, 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<DisplayHandle<'_>, HandleError> {
Ok(DisplayHandle::windows())
}
}
}
99 changes: 99 additions & 0 deletions crates/devstack-hub/src/foreground.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
let class: Vec<u16> = "STATIC\0".encode_utf16().collect();
let title: Vec<u16> = "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::<INPUT>() 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<WindowHandle<'_>, 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<DisplayHandle<'_>, HandleError> {
Ok(DisplayHandle::windows())
}
}
2 changes: 2 additions & 0 deletions crates/devstack-hub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions crates/devstack-hub/src/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
15 changes: 11 additions & 4 deletions docs/adr/0046-the-hub-opens-a-worktree-in-the-users-programs.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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).
Expand Down