Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The three DataTable *Selected methods report wrong selection location when caused by mouse selection #1723

Closed
davep opened this issue Feb 6, 2023 · 2 comments · Fixed by #1638
Assignees
Labels
bug Something isn't working Task

Comments

@davep
Copy link
Contributor

davep commented Feb 6, 2023

This was first raised by a user on our Discord server, and in testing the issue some more with this code:

from textual.app        import App, ComposeResult
from textual.containers import Vertical
from textual.widgets    import Header, Footer, TextLog, DataTable
from textual.binding    import Binding

class TestDataTable( App[ None ] ):

    BINDINGS = [
        Binding( "c", "cursor( 'cell' )", "Cell" ),
        Binding( "r", "cursor( 'row' )", "Row" ),
        Binding( "o", "cursor( 'column' )", "Column" ),
    ]

    CSS = """
    DataTable {
        height: 1fr;
        border: round green;
    }

    DataTable:focus {
        border: double green;
    }

    TextLog {
        height: 1fr;
        border: round red;
    }

    TextLog:focus {
        border: double red;
    }
    """

    def compose(self) -> ComposeResult:
        yield Header()
        self.dt = DataTable()
        yield Vertical( self.dt, TextLog() )
        yield Footer()

    def on_mount(self) -> None:
        self.dt.cursor_type = "row"
        self.dt.add_columns( "Row", *[ f"Column {n+1}" for n in range( 10 ) ] )
        for row in range( 50 ):
            self.dt.add_row( *[ str( row ), *[ str( n ) for n in range( 10 ) ] ] )
        self.dt.focus()

    def action_cursor( self, cursor: str ) -> None:
        self.dt.cursor_type = cursor

    def on_data_table_cell_highlighted( self, event: DataTable.CellHighlighted ) -> None:
        self.query_one( TextLog ).write( event )

    def on_data_table_cell_selected( self, event: DataTable.CellSelected ) -> None:
        self.query_one( TextLog ).write( event )

    def on_data_table_row_selected( self, event: DataTable.RowSelected ) -> None:
        self.query_one( TextLog ).write( event )

    def on_data_table_row_highlighted( self, event: DataTable.RowHighlighted ) -> None:
        self.query_one( TextLog ).write( event )

    def on_data_table_column_selected( self, event: DataTable.ColumnSelected ) -> None:
        self.query_one( TextLog ).write( event )

    def on_data_table_column_highlighted( self, event: DataTable.ColumnHighlighted ) -> None:
        self.query_one( TextLog ).write( event )

if __name__ == "__main__":
    TestDataTable().run()

it appears that if a *Selected event is initiated from the keyboard, everything works fine. On the other hand, if a *Selected event is initiated by a mouse click, the resulting event object will refer to the cell/row/column that was selected before the click took place, not the one that was clicked on.

Tested with both 0.10.1 and main.

@davep davep added bug Something isn't working Task labels Feb 6, 2023
@darrenburns darrenburns linked a pull request Feb 6, 2023 that will close this issue
3 tasks
@darrenburns
Copy link
Member

I've got the fix for this in #1638.

@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants