textual: Regression: mouse support broken in Visual Studio Code terminal

Mouse support is broken in the VS Code terminal. When you click on a widget, that widget is not focused. Worse, if it was focused using keyboard shortcuts, it will be blurred. It turns out that the mouse coordinates are incorrectly calculated and that if you move your mouse over the top-left corner of the terminal, the reported mouse coordinates cover the entire area. So the exact top-left is reported as (0.0, 0.0, but if you move a bit to the right and a bit down it already reports (17.0, 12.0) and so on.

I ran a git bisect and the offending commit is (unsurprisingly) 9f18dfe (smooth scrolling). I tested using this code:

from textual import on
from textual.app import App, ComposeResult
from textual.events import MouseMove
from textual.widgets import RichLog


class MouseMoveApp(App[None]):
    def compose(self) -> ComposeResult:
        yield RichLog(markup=True)

    @on(MouseMove)
    def record_mouse_position(self, event: MouseMove) -> None:
        self.query_one(RichLog).write(event)


MouseMoveApp().run()

I ran textual diagnose in that same terminal:

<!-- This is valid Markdown, please paste the following directly in to a GitHub issue -->
# Textual Diagnostics

## Versions

| Name    | Value  |
|---------|--------|
| Textual | 2.0.1  |
| Rich    | 13.9.4 |

## Python

| Name           | Value                                                           |
|----------------|-----------------------------------------------------------------|
| Version        | 3.13.2                                                          |
| Implementation | CPython                                                         |
| Compiler       | Clang 16.0.0 (clang-1600.0.26.6)                                |
| Executable     | /Users/david/work/software/playground-textual/.venv/bin/python3 |

## Operating System

| Name    | Value                                                                                                 |
|---------|-------------------------------------------------------------------------------------------------------|
| System  | Darwin                                                                                                |
| Release | 24.3.0                                                                                                |
| Version | Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6020 |

## Terminal

| Name                 | Value           |
|----------------------|-----------------|
| Terminal Application | vscode (1.97.2) |
| TERM                 | xterm-256color  |
| COLORTERM            | truecolor       |
| FORCE_COLOR          | *Not set*       |
| NO_COLOR             | *Not set*       |

## Rich Console options

| Name           | Value                |
|----------------|----------------------|
| size           | width=123, height=27 |
| legacy_windows | False                |
| min_width      | 1                    |
| max_width      | 123                  |
| is_terminal    | False                |
| encoding       | utf-8                |
| max_height     | 27                   |
| justify        | None                 |
| overflow       | None                 |
| no_wrap        | False                |
| highlight      | None                 |
| markup         | None                 |
| height         | None                 |

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 15 (10 by maintainers)

Most upvoted comments

Looks like an easy fix. Just need to detect iTerm via LC_TERMINAL

I can confirm on my Raspberry Pi. VS Code + ssh works fine, Ghostty + ssh works fine, iTerm2 + ssh is broken in a very similar fashion to the original issue with the VS Code terminal.

That is very nice, thanks!

I see the issue. It is a bug in Textual. Fix incoming.