Skip to content

Commit

Permalink
persist hover after table updates
Browse files Browse the repository at this point in the history
  • Loading branch information
TomJGooding committed Dec 29, 2023
1 parent 2d5e5c3 commit 0af9260
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
40 changes: 19 additions & 21 deletions src/textual/widgets/_month_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
"""
Expand Down Expand Up @@ -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:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_month_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 0af9260

Please sign in to comment.