Skip to content

Focus Lost in Chat Text Box After Alt+Tab #2189

@libertyteeth

Description

@libertyteeth

App Version

3.11.3

API Provider

OpenRouter

Model Used

Any router, any model

Actual vs. Expected Behavior

What happened?

When a user is typing in the Roo Code chat text box, switches away from VSCode using Alt+Tab, and then switches back using Alt+Tab again, focus is lost from the chat text box. This forces the user to manually click inside the text box to continue typing, disrupting workflow and keyboard-based navigation.

A workaround exists: after switching back, if the user presses Ctrl+V to paste, the text box regains focus. However, this is still an interruption to the natural workflow.

Expected behavior

After switching back to VSCode with Alt+Tab, focus should remain in the chat text box, allowing the user to continue typing immediately without requiring a mouse click.

Detailed Steps to Reproduce

  1. Open VSCode with Roo Code extension
  2. Start typing in the Roo Code chat text box
  3. Press Alt+Tab to switch to another application
  4. Press Alt+Tab again to switch back to VSCode
  5. Try to continue typing - the text box is no longer focused

Relevant API Request Output

Additional Context

Technical Analysis

The issue occurs because the ChatView component doesn't properly handle window focus events. When the window loses focus and then regains it, the text area doesn't automatically regain focus.

The fix implemented

Added a window focus event listener in the ChatView.tsx component that explicitly refocuses the text area when the window regains focus:

// Effect to restore focus to the text area when the window regains focus
useEffect(() => {
  const handleWindowFocus = () => {
    // Check if the view is visible and the text area should be focusable
    if (!isHidden && !textAreaDisabled && !enableButtons) {
      // Use setTimeout to ensure focus happens after the event loop cycle
      setTimeout(() => {
        textAreaRef.current?.focus()
      }, 0)
    }
  }

  window.addEventListener("focus", handleWindowFocus)

  // Cleanup listener on component unmount
  return () => {
    window.removeEventListener("focus", handleWindowFocus)
  }
  // Dependencies ensure the effect re-runs if these conditions change
}, [isHidden, textAreaDisabled, enableButtons])

The listener checks if the text area is eligible for focus (visible, not disabled, and no buttons are enabled) and, if so, explicitly focuses it when the window regains focus. The setTimeout ensures the focus action happens after the event loop cycle for better reliability.

Environment

  • Version: 3.11.3
  • Provider: Any (not provider-specific)
  • Model: Any (not model-specific)
  • OS: All (Windows, macOS, Linux)

Additional Context

This issue disrupts the flow for keyboard-focused users who frequently switch between VSCode and other applications while working with Roo Code. The implementation is minimal and follows React best practices for effect handling and focus management.

The fix has been tested and works as expected. Unit tests within roo-code/ did show failures in TerminalRegistry.test.ts, but these appear unrelated to the focus fix applied in the webview UI code.

Note: Previous attempts to fix this issue may have used different approaches (such as using useMount or a different useEffect with setTimeout), but the current implementation with the window focus event listener is the most effective solution that specifically addresses the Alt+Tab focus issue.

Metadata

Metadata

Assignees

Labels

Issue - In ProgressSomeone is actively working on this. Should link to a PR soon.bugSomething isn't working

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions