From 0af9260d132961b5906963395b3090af8e7dd061 Mon Sep 17 00:00:00 2001 From: TomJGooding <101601846+TomJGooding@users.noreply.github.com> Date: Fri, 29 Dec 2023 14:45:00 +0000 Subject: [PATCH] persist hover after table updates --- src/textual/widgets/_month_calendar.py | 40 ++++++++++++-------------- tests/test_month_calendar.py | 24 ++++++++++++++++ 2 files changed, 43 insertions(+), 21 deletions(-) diff --git a/src/textual/widgets/_month_calendar.py b/src/textual/widgets/_month_calendar.py index ac812bfde5..d661952e7e 100644 --- a/src/textual/widgets/_month_calendar.py +++ b/src/textual/widgets/_month_calendar.py @@ -80,34 +80,31 @@ def next_month(self) -> None: self.date += relativedelta(months=1) def _on_mount(self, _: Mount) -> None: - self._update_week_header() - self._update_calendar_days() - self._update_cursor_coordinate() + self._update_calendar_table(update_week_header=True) - def _update_week_header(self) -> None: + def _update_calendar_table(self, update_week_header: bool) -> None: table = self.query_one(DataTable) + old_hover_coordinate = table.hover_coordinate table.clear() - old_columns = table.columns.copy() - for old_column in old_columns: - table.remove_column(old_column) - day_names = calendar.day_abbr - for day in self._calendar.iterweekdays(): - table.add_column(day_names[day]) + if update_week_header: + old_columns = table.columns.copy() + for old_column in old_columns: + table.remove_column(old_column) + + day_names = calendar.day_abbr + for day in self._calendar.iterweekdays(): + table.add_column(day_names[day]) - def _update_calendar_days(self) -> None: - table = self.query_one(DataTable) - table.clear() with self.prevent(DataTable.CellHighlighted): for week in self.calendar_dates: table.add_row(*[self._format_day(date) for date in week]) - def _update_cursor_coordinate(self) -> None: - with self.prevent(DataTable.CellHighlighted): - table = self.query_one(DataTable) date_coordinate = self._get_date_coordinate(self.date) table.cursor_coordinate = date_coordinate + table.hover_coordinate = old_hover_coordinate + @property def calendar_dates(self) -> list[list[datetime.date]]: """ @@ -143,16 +140,17 @@ def watch_date(self, old_date: datetime.date, new_date: datetime.date) -> None: if not self.is_mounted: return if (new_date.month != old_date.month) or (new_date.year != old_date.year): - self._update_calendar_days() - self._update_cursor_coordinate() + self._update_calendar_table(update_week_header=False) + else: + table = self.query_one(DataTable) + date_coordinate = self._get_date_coordinate(self.date) + table.cursor_coordinate = date_coordinate def watch_first_weekday(self) -> None: self._calendar = calendar.Calendar(self.first_weekday) if not self.is_mounted: return - self._update_week_header() - self._update_calendar_days() - self._update_cursor_coordinate() + self._update_calendar_table(update_week_header=True) def watch_show_cursor(self, show_cursor: bool) -> None: if not self.is_mounted: diff --git a/tests/test_month_calendar.py b/tests/test_month_calendar.py index 05d83db333..878cb1c871 100644 --- a/tests/test_month_calendar.py +++ b/tests/test_month_calendar.py @@ -269,3 +269,27 @@ async def test_cell_highlighted_updates_date(): await pilot.press("left") expected_date = datetime.date(2021, 6, 10) assert month_calendar.date == expected_date + + +async def test_hover_coordinate_persists_after_month_changes(): + app = MonthCalendarApp() # MonthCalendar date is 2021-06-03 + async with app.run_test() as pilot: + month_calendar = pilot.app.query_one(MonthCalendar) + table = month_calendar.query_one(DataTable) + await pilot.hover(MonthCalendar, offset=(3, 3)) + assert table.hover_coordinate == Coordinate(2, 0) + + month_calendar.date = datetime.date(year=2022, month=10, day=2) + assert table.hover_coordinate == Coordinate(2, 0) + + +async def test_hover_coordinate_persists_after_first_weekday_changes(): + app = MonthCalendarApp() # MonthCalendar date is 2021-06-03 + async with app.run_test() as pilot: + month_calendar = pilot.app.query_one(MonthCalendar) + table = month_calendar.query_one(DataTable) + await pilot.hover(MonthCalendar, offset=(3, 3)) + assert table.hover_coordinate == Coordinate(2, 0) + + month_calendar.first_weekday = 6 # Sunday + assert table.hover_coordinate == Coordinate(2, 0)