utils: Turn number of weekdays into a macro

While "7" may be obvious in the context of calendaring that
it represents the number of week days, it often gives me the
impression of it being a magic number when I'm hyperfocused on certain
parts of the codebase for several consecutive hours. Turning the
number into a macro helps me scan functions much more easily.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-calendar/-/merge_requests/674>
This commit is contained in:
Hari Rana
2026-03-08 21:53:40 -04:00
parent da7b625e18
commit 50e048c7e0
14 changed files with 109 additions and 107 deletions
+1 -1
View File
@@ -199,7 +199,7 @@ gcal_date_chooser_day_set_date (GcalDateChooserDay *self,
now = g_date_time_new_now (g_date_time_get_timezone (date));
today = gcal_date_time_compare_date (date, now) == 0;
weekday = g_date_time_get_day_of_week (date) % 7;
weekday = g_date_time_get_day_of_week (date) % N_WEEKDAYS;
if (G_UNLIKELY (today))
gtk_widget_add_css_class (widget, "today");
+18 -19
View File
@@ -34,8 +34,7 @@
#include <stdlib.h>
#define ROWS 6
#define COLS 7
#define DAYS 42
#define DAYS ROWS * N_WEEKDAYS
struct _GcalDateChooser
{
@@ -49,9 +48,9 @@ struct _GcalDateChooser
GtkWidget *day_grid;
GtkWidget *corner;
GtkWidget *cols[COLS];
GtkWidget *cols[N_WEEKDAYS];
GtkWidget *rows[ROWS];
GtkWidget *days[ROWS][COLS];
GtkWidget *days[ROWS][N_WEEKDAYS];
GtkWidget *week[ROWS];
GDateTime *date;
@@ -222,9 +221,9 @@ calendar_compute_days (GcalDateChooser *self)
first_day = g_date_time_get_day_of_week (date);
g_date_time_unref (date);
first_day = (first_day + COLS - self->week_start) % COLS;
first_day = (first_day + N_WEEKDAYS - self->week_start) % N_WEEKDAYS;
if (first_day == 0)
first_day = COLS;
first_day = N_WEEKDAYS;
/* Compute days of previous month */
if (month > 1)
@@ -252,8 +251,8 @@ calendar_compute_days (GcalDateChooser *self)
}
/* Compute days of current month */
row = first_day / COLS;
col = first_day % COLS;
row = first_day / N_WEEKDAYS;
col = first_day % N_WEEKDAYS;
for (day = 1; day <= ndays_in_month; day++)
{
@@ -264,7 +263,7 @@ calendar_compute_days (GcalDateChooser *self)
g_date_time_unref (date);
col++;
if (col == COLS)
if (col == N_WEEKDAYS)
{
row++;
col = 0;
@@ -284,7 +283,7 @@ calendar_compute_days (GcalDateChooser *self)
day = 1;
for (; row < ROWS; row++)
{
for (; col < COLS; col++)
for (; col < N_WEEKDAYS; col++)
{
d = GCAL_DATE_CHOOSER_DAY (self->days[row][col]);
date = g_date_time_new_local (other_year, other_month, day, 0, 0, 0);
@@ -301,7 +300,7 @@ calendar_compute_days (GcalDateChooser *self)
{
gchar *text;
d = GCAL_DATE_CHOOSER_DAY (self->days[row][COLS - 1]);
d = GCAL_DATE_CHOOSER_DAY (self->days[row][N_WEEKDAYS - 1]);
date = gcal_date_chooser_day_get_date (d);
text = g_strdup_printf ("%d", g_date_time_get_week_of_year (date));
gtk_label_set_label (GTK_LABEL (self->rows[row]), text);
@@ -382,9 +381,9 @@ calendar_get_weekday_name (gint i)
static void
calendar_init_weekday_display (GcalDateChooser *self)
{
for (gint i = 0; i < COLS; i++)
for (gint i = 0; i < N_WEEKDAYS; i++)
{
const char *text = calendar_get_weekday_name ((i + self->week_start) % COLS);
const char *text = calendar_get_weekday_name ((i + self->week_start) % N_WEEKDAYS);
gtk_label_set_label (GTK_LABEL (self->cols[i]), text);
}
}
@@ -463,7 +462,7 @@ calendar_update_selected_day_display (GcalDateChooser *self)
{
gboolean row_selected = FALSE;
for (col = 0; col < COLS; col++)
for (col = 0; col < N_WEEKDAYS; col++)
{
gboolean day_selected;
@@ -508,7 +507,7 @@ update_event_indicators (GcalDateChooser *self)
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
for (col = 0; col < N_WEEKDAYS; col++)
{
GDateTime *date;
g_autoptr (GcalRange) range = NULL;
@@ -1071,7 +1070,7 @@ show_week_number_to_column_span_cb (GBinding *binding,
GValue *to_value,
gpointer user_data)
{
g_value_set_int (to_value, g_value_get_boolean (from_value) ? COLS + 1 : COLS);
g_value_set_int (to_value, g_value_get_boolean (from_value) ? N_WEEKDAYS + 1 : N_WEEKDAYS);
return TRUE;
}
@@ -1102,7 +1101,7 @@ gcal_date_chooser_init (GcalDateChooser *self)
gtk_widget_init_template (GTK_WIDGET (self));
for (col = 0; col < COLS; col++)
for (col = 0; col < N_WEEKDAYS; col++)
{
self->cols[col] = gtk_label_new ("");
@@ -1183,11 +1182,11 @@ gcal_date_chooser_init (GcalDateChooser *self)
"column-spacing", 2,
"visible", TRUE,
NULL);
gtk_grid_attach (GTK_GRID (self->grid), self->day_grid, 0, 0, COLS, ROWS);
gtk_grid_attach (GTK_GRID (self->grid), self->day_grid, 0, 0, N_WEEKDAYS, ROWS);
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
for (col = 0; col < N_WEEKDAYS; col++)
{
self->days[row][col] = gcal_date_chooser_day_new ();