Verified Commit 15aa1475 authored by Jakob Moser's avatar Jakob Moser
Browse files

Send double click event if person is doubly clicked

Initial revision by the LLM, but I considerably adjusted the code

Co-authored-by: default avatarBig Pickle <big-pickle@opencode.ai>
parent 6d3d48f9
Loading
Loading
Loading
Loading
+55 −4
Changes for poolpay/admin/PersonsTable.py: 55 added lines, 4 removed lines.
Original line number Diff line number Diff line
import re
from decimal import Decimal
from enum import StrEnum, auto

from babel.numbers import NumberFormatError, format_currency, parse_decimal
from rich.text import Text
from textual import events
from textual.coordinate import Coordinate
from textual.message import Message
from textual.reactive import reactive
from textual.widgets import DataTable
from textual.widgets.data_table import ColumnKey

from poolpay.admin.money import format_balance
from poolpay.admin.money import format_balance, to_decimal_if_numeric
from poolpay.model.Person import Person


@@ -31,12 +33,24 @@ def to_row(p: Person) -> tuple[str, str, Text, str]:
    )


class PersonsTableColumn(StrEnum):
    name = auto()
    cl_account = auto()
    balance = auto()
    card_id = auto()


class PersonsTable(DataTable):
    sort_key: reactive[ColumnKey | None] = reactive(None)
    sort_reversed: reactive[bool] = reactive(False)

    def on_mount(self) -> None:
        self.add_columns("Name", "CL-Account", "Kontostand", "Karten-ID")
        self.add_columns(
            ("Name", PersonsTableColumn.name.value),
            ("CL-Account", PersonsTableColumn.cl_account.value),
            ("Kontostand", PersonsTableColumn.balance.value),
            ("Karten-ID", PersonsTableColumn.card_id.value),
        )
        self.add_all_persons()

    def add_all_persons(self) -> None:
@@ -64,3 +78,40 @@ class PersonsTable(DataTable):
        self.sort_key = event.column_key

        self.sort(self.sort_key, key=to_decimal_if_numeric, reverse=self.sort_reversed)

    class DoubleClick(Message):
        def __init__(self, person: Person, column: PersonsTableColumn) -> None:
            self.person = person
            self.column = column
            super().__init__()

    def on_click(self, event: events.Click) -> None:
        """
        Post message when the user double clicks on a person cell.
        """
        if event.chain != 2:
            return

        row_index = event.style.meta.get("row")
        column_index = event.style.meta.get("column")
        assert isinstance(row_index, int) and isinstance(column_index, int)

        coordinate = Coordinate(row_index, column_index)
        if not self.is_valid_coordinate(coordinate):
            # Coordinates could also indicate a header cell (row -1)
            return

        row_key, column_key = self.coordinate_to_cell_key(coordinate)
        assert row_key.value, "Rows in PersonsTable have keys"
        assert column_key.value, "Columns in PersonsTable have keys"

        person = Person.get_only(row_key.value)
        if person is None:
            return

        self.post_message(
            self.DoubleClick(
                person,
                PersonsTableColumn(column_key.value),
            )
        )