Mouse selection carries on after the widgets it started in are removed
Nobody has claimed this yet.
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 85/100
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Textualize/textual
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6790 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 80/100
Textualize/textual#6789 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6787 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Textualize/textual#6782 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Textualize/textual#6777 · 1 comment ·
All issues in Textualize/textual
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
GoogleCloudPlatform/professional-services-data-validator#1899 ·
Maintainers usually reply within 4 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
mohanraj00/entelos#200 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 68/100
-
sweep
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
bketelsen/truenas-mcp#77 ·
Maintainers usually reply within 1 day
-
Console writes URL credentials to the local log, though AGENTS.md says secrets never reach diskOpen
Difficulty 1/5 Under an hour Newbie friendliness 86/100
sandbox-quantum/switch#2163 ·
Maintainers usually reply within 1 day