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

Aperta Adatta ai principianti
#6,778 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
Meno di un'ora
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
frontend

Direzione di ricerca

Parti da Screen._watch__select_state e dalla guardia che controlla select_state.is_attached_to_dom; deve chiamare il metodo (is_attached_to_dom()) in modo che i widget rimossi azzerino _select_state. Esegui il repro fornito per confermare il comportamento attuale, poi aggiungi un test di regressione che copra la rimozione del widget da cui è iniziato il trascinamento. È completato quando _select_state è None e get_selected_text() è vuoto dopo che il contenitore sostitutivo è stato montato e il puntatore si è spostato sopra di esso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
Python
Stelle
37.7k
Fork
1.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/textual

Tutte le issue di Textualize/textual

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.