Add mini-calendar sidebar to Calendar TUI
- Add MonthCalendar widget as a collapsible sidebar (toggle with 's') - Sidebar syncs with main week grid (week highlight, selected date) - Click dates in sidebar to navigate week grid to that date - Click month navigation arrows to change displayed month - Add goto_date() method to WeekGrid for date navigation
This commit is contained in:
@@ -240,3 +240,41 @@ class MonthCalendar(Widget):
|
||||
self.display_month = date(year, month, 1)
|
||||
self.post_message(self.MonthChanged(self.display_month))
|
||||
self.refresh()
|
||||
|
||||
def on_click(self, event) -> None:
|
||||
"""Handle mouse clicks on the calendar."""
|
||||
# Row 0 is the month header (< Month Year >)
|
||||
# Row 1 is day names (Mo Tu We ...)
|
||||
# Row 2+ are the week rows
|
||||
y = event.y
|
||||
x = event.x
|
||||
|
||||
if y == 0:
|
||||
# Month header - check for navigation arrows
|
||||
if x < 2:
|
||||
self.prev_month()
|
||||
elif x >= self.size.width - 2:
|
||||
self.next_month()
|
||||
return
|
||||
|
||||
if y == 1:
|
||||
# Day names row - ignore
|
||||
return
|
||||
|
||||
# Week row - calculate which date was clicked
|
||||
week_idx = y - 2
|
||||
weeks = self._weeks
|
||||
if week_idx < 0 or week_idx >= len(weeks):
|
||||
return
|
||||
|
||||
week = weeks[week_idx]
|
||||
# Each day takes 3 characters ("DD "), so find which day column
|
||||
day_col = x // 3
|
||||
if day_col < 0 or day_col >= 7:
|
||||
return
|
||||
|
||||
clicked_date = week[day_col]
|
||||
if clicked_date is not None:
|
||||
self.selected_date = clicked_date
|
||||
self.post_message(self.DateSelected(clicked_date))
|
||||
self.refresh()
|
||||
|
||||
@@ -761,3 +761,20 @@ class WeekGrid(Vertical):
|
||||
event = self.get_event_at_cursor()
|
||||
if event:
|
||||
self.post_message(self.EventSelected(event))
|
||||
|
||||
def goto_date(self, target_date: date) -> None:
|
||||
"""Navigate to a specific date.
|
||||
|
||||
Sets the week to contain the target date and places cursor on that day.
|
||||
"""
|
||||
# Get the week start for the target date
|
||||
week_start_date = get_week_start_for_date(target_date)
|
||||
|
||||
if self.week_start != week_start_date:
|
||||
self.week_start = week_start_date
|
||||
|
||||
# Set cursor column to the target date
|
||||
col = get_day_column_for_date(target_date, self.week_start)
|
||||
if not self.include_weekends and col >= 5:
|
||||
col = 4 # Last weekday if weekend
|
||||
self.cursor_col = col
|
||||
|
||||
Reference in New Issue
Block a user