Mouse selection carries on after the widgets it started in are removed

Open Beginner friendly
#6,778 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
85/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
frontend

Research direction

Start from Screen._watch__select_state and the guard that checks select_state.is_attached_to_dom; it needs to call the method (is_attached_to_dom()) so removed widgets clear _select_state. Run the provided repro to confirm the current behavior, then add a regression test covering removal of the drag-start widget. Done looks like _select_state being None and get_selected_text() empty after the replacement container is mounted and the pointer moves over it.

Written by the indexing model from the issue text.

Description

Hi! I hit this while writing generated tests against Textual's widgets.

Screen._watch__select_state has a guard for widgets removed during a selection:

if not select_state.is_attached_to_dom:
    # Widgets may have been removed in the interim
    self._select_state = None
    return

SelectState.is_attached_to_dom is a method, not a property, so the bound method is always truthy and the guard never fires. If the widgets a drag started in are removed, the drag goes on selecting text in whatever widgets are now under the pointer.

Repro (start a drag in one container, replace the container, keep dragging):

import asyncio

import textual
from textual.app import App, ComposeResult
from textual.containers import Vertical
from textual.widgets import Static

class SelectApp(App):
    def compose(self) -> ComposeResult:
        with Vertical(id="old"):
            yield Static("first line of old text", id="a")
            yield Static("second line of old text", id="b")

async def main() -> None:
    app = SelectApp()
    async with app.run_test() as pilot:
        await pilot.mouse_down("#a", offset=(2, 0))
        await pilot.hover("#b", offset=(5, 0))
        await pilot.pause()
        print("selected before removal:", {w.id: s for w, s in app.screen.selections.items()})
        old = app.query_one("#old")
        await old.remove()
        await app.mount(
            Vertical(Static("new text one", id="c"), Static("new text two", id="d"), id="new")
        )
        await pilot.pause()
        await pilot.hover("#d", offset=(4, 0))
        await pilot.pause()
        print("select state cleared:", app.screen._select_state is None)
        print("selected text:", repr(app.screen.get_selected_text()))
        print(
            "selections after removal:",
            {(w.id, w.is_attached): s for w, s in app.screen.selections.items()},
        )
    print("Textual", textual.__version__)

asyncio.run(main())

Output on Textual 8.2.7 (and the same on main at 5e5b7ef):

selected before removal: {'a': Selection(start=Offset(x=2, y=0), end=None), 'b': Selection(start=None, end=Offset(x=5, y=0))}
select state cleared: False
selected text: 'new '
selections after removal: {('a', False): Selection(start=Offset(x=2, y=0), end=None), ('d', True): Selection(start=None, end=Offset(x=4, y=0))}

I expected the selection to end once its start widget was gone, as the comment says: _select_state cleared, and nothing selected in #d, which the drag never started in. With the call added (is_attached_to_dom()) that's what happens:

select state cleared: True
selected text: ''

I have a one-line fix with a test, if that's welcome.

Dominant language
Python
Stars
37.7k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Textualize/textual

All issues in Textualize/textual

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.