Skip to content

Windows: a tray icon and close-to-tray behind an opt-in setting (stage 1) - #893

Open
unRekable wants to merge 3 commits into
mainfrom
draft/802-tray
Open

unRekable wants to merge 3 commits into
mainfrom
draft/802-tray

Conversation

@unRekable

Copy link
Copy Markdown
Collaborator

What changed and why

Windows gets a tray icon (issue #802, stage 1 of the plan). Turning on "Keep tinyclient in the
tray when I close the window" (Settings › General, G3 — off by default, so nothing changes
until it is on) makes closing the window hide it instead of quitting: chatd keeps receiving,
a one-time balloon says where the app went, and the icon's click brings the window back. The
icon's menu has Open tinyclient and Quit tinyclient, and Quit ends chatd exactly like ⌘Q does.

Type

  • feature
  • fix
  • perf
  • refactor
  • chore
  • docs

Platforms

  • windows
  • macos
  • linux
  • android
  • ios

The tray is Windows only in this stage (notifier::tray has one backend). macOS, Linux,
Android and iOS code paths are cfg-gated around, not changed: macOS and Linux still quit on
close as today, G3 is absent there, and the honest search line stays for their tray words.

Proof

Scenario: e2e/scenarios/tray-close-keeps-running

Lab run:

The driver closes the window with its X and proves the process stays and chatd still answers,
clicks the real tray icon and proves the window comes back, then chooses Quit in the icon's
menu and proves tinyclient and its chatd are gone. On the base build the first close quits the
process, so the base run fails.

Compatibility

No protocol change, no server involvement: the setting lives in the window's shell.prefs,
whose additive contract (unknown lines kept verbatim) means an older build never erases a
newer one's tray choice, and a missing line reads as off. Older clients and servers are
unaffected; nothing is removed or renamed. Off — the shipped default — is byte-for-byte
today's behavior, and the tray icon exists only while the choice is on.

Risk and rollback

The changed behavior is one branch in close_window (menubar.rs) that only runs when the
new choice is on and the icon exists; with the choice off, closing quits as before. If it is
wrong, the setting is the rollback for users (turn it off), and git revert of this PR is the
rollback for the code — no migration, no protocol, no server piece. Known residual risks are
listed in the plan (windows-rs signature check in CI; a hard process death leaves the icon
until hover, as every tray app's does).

Release note

Keep tinyclient running in the tray when you close its window (Windows): the icon in the
notification area brings the window back, and Quit there ends everything, like ⌘Q. Off by
default; find it in Settings › General.

@unRekable unRekable left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

🟡 5 findings (4 blocking).

Stage 1 of G3: a Windows tray icon behind an opt-in shell.prefs line, close-into-tray with a one-time balloon, and a Quit that reuses menubar::quit so chatd stops as on every other way out. The design holds: the hidden helper window rides GPUI's message pump, the quit ask crosses threads only as an atomic drained by the existing 500 ms loop, the prefs line is additive with junk-reads-off and old builds keep the line, and the platform cfg arms are complete. What fails is the windows-rs API layer: four 0.62 signature mismatches, verified against the published windows 0.62.2 the lockfile resolves, that cannot compile on the one platform this targets. One further gap: the icon is never re-added on the shell's TaskbarCreated broadcast, so after an explorer restart the close path hides the window with no way back in.

Residual risk

  • main_window() returns the first top-level, unowned, titled window of the process in Z-order, and the Settings window (gui/settings/mod.rs:321) is titled too, so it is an equally qualified candidate; which window the tray click restores after a hide depends on where Windows keeps a hidden window in the Z-order. Remembering GPUI's own HWND instead of enumerating would remove the guess.
  • The tray's Quit is delivered by the 500 ms loop through let _ = cx.update(...); a Quit asked in the same tick as some other teardown is dropped silently. Harmless with today's loop body, but the swallow is worth remembering if the loop grows more work.

Comment thread crates/notifier/src/tray.rs Outdated
};
use windows::Win32::UI::WindowsAndMessaging::{
AppendMenuW, CreatePopupMenu, CreateWindowExW, DefWindowProcW, DestroyMenu, DestroyWindow, EnumWindows, GetCursorPos,
GetModuleHandleW, GetWindow, GetWindowTextW, GetWindowThreadProcessId, HICON, IsIconic, LoadIconW, MF_STRING, PostMessageW,

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocker: GetModuleHandleW is not in Win32::UI::WindowsAndMessaging: windows-rs 0.62.2 generates it only in Win32::System::LibraryLoader, so this import does not resolve and the Windows build cannot compile. No workspace crate enables the Win32_System_LibraryLoader feature on the windows 0.62 line (login and mediad list their features without it), so the fix needs both the import moved here and the feature added to crates/notifier/Cargo.toml.

Comment thread crates/notifier/src/tray.rs Outdated
unsafe {
let _ = GetCursorPos(&mut at);
let _ = SetForegroundWindow(window);
let picked = TrackPopupMenu(items, TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, at.x, at.y, 0, window, None);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocker: windows-rs 0.62 declares TrackPopupMenu's fifth parameter as nreserved: Option, so the bare 0 is a type error and this cannot compile; pass Some(0).

Suggested change
let picked = TrackPopupMenu(items, TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, at.x, at.y, 0, window, None);
let picked = TrackPopupMenu(items, TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, at.x, at.y, Some(0), window, None);

Comment thread crates/notifier/src/tray.rs Outdated
let _ = GetCursorPos(&mut at);
let _ = SetForegroundWindow(window);
let picked = TrackPopupMenu(items, TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, at.x, at.y, 0, window, None);
let _ = PostMessageW(window, WM_NULL, WPARAM(0), LPARAM(0));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocker: windows-rs 0.62 declares PostMessageW's hwnd as Option, so a bare HWND is a type error and this cannot compile.

Suggested change
let _ = PostMessageW(window, WM_NULL, WPARAM(0), LPARAM(0));
let _ = PostMessageW(Some(window), WM_NULL, WPARAM(0), LPARAM(0));

Comment thread crates/notifier/src/tray.rs Outdated
fn icon() -> Option<HICON> {
// SAFETY: reads our own module's resource table.
let module = unsafe { GetModuleHandleW(None) }.ok()?;
let icon = unsafe { LoadIconW(HINSTANCE(module.0), PCWSTR(1 as _)) }.ok()?;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Blocker: windows-rs 0.62 declares LoadIconW's first parameter as Option, so a bare HINSTANCE is a type error and this cannot compile.

Suggested change
let icon = unsafe { LoadIconW(HINSTANCE(module.0), PCWSTR(1 as _)) }.ok()?;
let icon = unsafe { LoadIconW(Some(HINSTANCE(module.0)), PCWSTR(1 as _)) }.ok()?;

}
}

unsafe extern "system" fn wnd_proc(window: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Major: The icon is never re-added after the shell restarts: explorer.exe broadcasts the registered TaskbarCreated message to every top-level window, and tray apps re-add on it, but this wnd_proc answers everything except clicks with DefWindowProcW. After a shell restart, WINDOW and active() still report the icon present, so a close hides the window with no icon and no balloon, and there is no way back in or out: a hidden window has no taskbar button, and watch_for_vanished_window stays quiet because the window keeps its title. Smallest fix: RegisterWindowMessageW("TaskbarCreated") at add() and call add() again when wnd_proc receives that message.

@unRekable
unRekable marked this pull request as ready for review November 18, 2026 15:49
@unRekable
unRekable requested a review from itz4blitz as a code owner November 18, 2026 15:49
@github-actions

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Tiny Code Review

⚠️ Code review unavailable · run

the AI provider rate-limited the request

This is advice, not a gate. Re-run with the agent:review label or a /review comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant