feat(update): huge changes
This commit is contained in:
98 files changed
+9636
-5575
No files matched your search
@@ -1,11 +1,13 @@
|
||||
import calendar
|
||||
from datetime import datetime
|
||||
import gi
|
||||
import utils.icons as icons
|
||||
from fabric.widgets.label import Label
|
||||
from fabric.widgets.box import Box
|
||||
from fabric.widgets.centerbox import CenterBox
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from gi.repository import Gtk, GLib
|
||||
import utils.icons as icons
|
||||
|
||||
|
||||
class Calendar(Gtk.Box):
|
||||
@@ -17,36 +19,47 @@ class Calendar(Gtk.Box):
|
||||
self.current_year = datetime.now().year
|
||||
self.current_month = datetime.now().month
|
||||
self.current_day = datetime.now().day
|
||||
# Store previous month/year to determine transition direction.
|
||||
self.previous_key = (self.current_year, self.current_month)
|
||||
|
||||
# Cache threshold in number of months away from the current view.
|
||||
self.cache_threshold = 1 # Allow current month +/- 1 month
|
||||
self.month_views = {}
|
||||
|
||||
self.header = Gtk.Box(spacing=4, name="header")
|
||||
self.pack_start(self.header, False, False, 0)
|
||||
# Dictionary to store built views for each month.
|
||||
self.month_views = {}
|
||||
|
||||
self.prev_month_button = Gtk.Button(
|
||||
name="prev-month-button",
|
||||
child=Label(name="month-button-label", markup=icons.chevron_left),
|
||||
)
|
||||
self.prev_month_button.connect("clicked", self.on_prev_month_clicked)
|
||||
self.header.pack_start(self.prev_month_button, False, False, 0)
|
||||
|
||||
self.month_label = Gtk.Label(name="month-label")
|
||||
self.header.pack_start(self.month_label, True, True, 0)
|
||||
|
||||
self.next_month_button = Gtk.Button(
|
||||
name="next-month-button",
|
||||
child=Label(name="month-button-label", markup=icons.chevron_right),
|
||||
)
|
||||
self.next_month_button.connect("clicked", self.on_next_month_clicked)
|
||||
self.header.pack_start(self.next_month_button, False, False, 0)
|
||||
|
||||
self.header = CenterBox(
|
||||
spacing=4,
|
||||
name="header",
|
||||
start_children=[self.prev_month_button],
|
||||
center_children=[
|
||||
self.month_label,
|
||||
],
|
||||
end_children=[self.next_month_button],
|
||||
)
|
||||
|
||||
self.add(self.header)
|
||||
|
||||
self.weekday_row = Gtk.Box(spacing=4, name="weekday-row")
|
||||
self.pack_start(self.weekday_row, False, False, 0)
|
||||
|
||||
# Create a stack to hold month days views.
|
||||
self.stack = Gtk.Stack(name="calendar-stack")
|
||||
self.stack.set_transition_duration(500)
|
||||
self.stack.set_transition_duration(250)
|
||||
self.pack_start(self.stack, True, True, 0)
|
||||
|
||||
self.update_header()
|
||||
@@ -54,14 +67,15 @@ class Calendar(Gtk.Box):
|
||||
GLib.timeout_add_seconds(60, self.check_date_change)
|
||||
|
||||
def update_header(self):
|
||||
# Update header month label and weekday row.
|
||||
self.month_label.set_text(
|
||||
datetime(self.current_year, self.current_month, 1)
|
||||
.strftime("%B %Y")
|
||||
.capitalize()
|
||||
)
|
||||
# Clear existing children from weekday_row.
|
||||
for child in self.weekday_row.get_children():
|
||||
self.weekday_row.remove(child)
|
||||
|
||||
days = self.get_weekday_initials()
|
||||
for day in days:
|
||||
label = Gtk.Label(label=day.upper(), name="weekday-label")
|
||||
@@ -70,14 +84,15 @@ class Calendar(Gtk.Box):
|
||||
|
||||
def update_calendar(self):
|
||||
new_key = (self.current_year, self.current_month)
|
||||
|
||||
# Set the transition type based on whether we're moving to a later or earlier month.
|
||||
if new_key > self.previous_key:
|
||||
self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT)
|
||||
elif new_key < self.previous_key:
|
||||
self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_RIGHT)
|
||||
|
||||
# Update previous key for next comparison.
|
||||
self.previous_key = new_key
|
||||
|
||||
# Create the month view if it doesn't exist.
|
||||
if new_key not in self.month_views:
|
||||
month_view = self.create_month_view(self.current_year, self.current_month)
|
||||
self.month_views[new_key] = month_view
|
||||
@@ -86,24 +101,25 @@ class Calendar(Gtk.Box):
|
||||
f"{self.current_year}_{self.current_month}",
|
||||
f"{self.current_year}_{self.current_month}",
|
||||
)
|
||||
|
||||
# Switch the visible child in the stack.
|
||||
self.stack.set_visible_child_name(f"{self.current_year}_{self.current_month}")
|
||||
self.update_header()
|
||||
self.stack.show_all()
|
||||
|
||||
# Purge any cached month views that are too far away.
|
||||
self.prune_cache()
|
||||
|
||||
def prune_cache(self):
|
||||
# Compute a numerical value for a (year, month) key.
|
||||
def month_index(key):
|
||||
year, month = key
|
||||
return year * 12 + (month - 1)
|
||||
|
||||
current_index = month_index((self.current_year, self.current_month))
|
||||
keys_to_remove = [
|
||||
key
|
||||
for key in self.month_views
|
||||
if abs(month_index(key) - current_index) > self.cache_threshold
|
||||
]
|
||||
|
||||
keys_to_remove = []
|
||||
for key in self.month_views:
|
||||
if abs(month_index(key) - current_index) > self.cache_threshold:
|
||||
keys_to_remove.append(key)
|
||||
for key in keys_to_remove:
|
||||
widget = self.month_views.pop(key)
|
||||
self.stack.remove(widget)
|
||||
@@ -167,6 +183,7 @@ class Calendar(Gtk.Box):
|
||||
return grid
|
||||
|
||||
def get_weekday_initials(self):
|
||||
# Returns localized weekday initials.
|
||||
return [datetime(2023, 1, i + 1).strftime("%a")[:1] for i in range(7)]
|
||||
|
||||
def on_prev_month_clicked(self, widget):
|
||||
@@ -197,3 +214,4 @@ class Calendar(Gtk.Box):
|
||||
self.current_year = now.year
|
||||
self.update_calendar()
|
||||
return True
|
||||
|
||||
Reference in new issue
Block a user