From 1d418ce1bae44e947f02bc17af22f6a979a22146 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Iv=C3=A1n?= Date: Thu, 1 Aug 2024 11:41:48 -0600 Subject: [PATCH] event-editor: Introduce GcalCalendarRow This widget displays the calendar and shows whether it is visible or not. The widget displays the color of the calendar at the left of the label and the visibility state at the right. --- .../event-editor/event-editor.gresource.xml | 1 + src/gui/event-editor/gcal-calendar-row.c | 192 ++++++++++++++++++ src/gui/event-editor/gcal-calendar-row.h | 33 +++ src/gui/event-editor/gcal-calendar-row.ui | 32 +++ src/gui/event-editor/meson.build | 1 + 5 files changed, 259 insertions(+) create mode 100644 src/gui/event-editor/gcal-calendar-row.c create mode 100644 src/gui/event-editor/gcal-calendar-row.h create mode 100644 src/gui/event-editor/gcal-calendar-row.ui diff --git a/src/gui/event-editor/event-editor.gresource.xml b/src/gui/event-editor/event-editor.gresource.xml index 38165c51..c875c9e2 100644 --- a/src/gui/event-editor/event-editor.gresource.xml +++ b/src/gui/event-editor/event-editor.gresource.xml @@ -2,6 +2,7 @@ gcal-alarm-row.ui + gcal-calendar-row.ui gcal-date-chooser.ui gcal-date-chooser-day.ui gcal-date-selector.ui diff --git a/src/gui/event-editor/gcal-calendar-row.c b/src/gui/event-editor/gcal-calendar-row.c new file mode 100644 index 00000000..2dcb57bc --- /dev/null +++ b/src/gui/event-editor/gcal-calendar-row.c @@ -0,0 +1,192 @@ +/* gcal-calendar-row.c + * + * Copyright 2024 Diego Iván M.E + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define G_LOG_DOMAIN "GcalCalendarRow" + +#include "config.h" +#include "gcal-calendar-row.h" +#include "gcal-debug.h" +#include "gcal-utils.h" + +#include + +struct _GcalCalendarRow +{ + AdwActionRow parent; + + GtkImage *color_image; + GtkImage *visibility_image; + GcalCalendar *calendar; +}; + +G_DEFINE_TYPE (GcalCalendarRow, gcal_calendar_row, ADW_TYPE_PREFERENCES_ROW) + +enum +{ + PROP_0, + PROP_CALENDAR, + N_PROPS, +}; + +static GParamSpec *properties [N_PROPS] = { NULL, }; + +static gboolean +paintable_from_gdk_rgb (GBinding *binding, + const GValue *from_value, + GValue *to_value, + gpointer user_data) +{ + GdkRGBA *rgba = g_value_get_boxed (from_value); + g_value_take_object (to_value, get_circle_paintable_from_color (rgba, 16)); + return TRUE; +} + +static void +updated_visibility_cb (GcalCalendarRow *self) +{ + g_autofree gchar *accessible_description = NULL; + + GCAL_ENTRY; + gtk_widget_set_visible (GTK_WIDGET (self->visibility_image), !gcal_calendar_get_visible (self->calendar)); + + if (!gcal_calendar_get_visible (self->calendar)) + accessible_description = g_strdup (_("This calendar is hidden")); + + gtk_accessible_update_property (GTK_ACCESSIBLE (self), + GTK_ACCESSIBLE_PROPERTY_DESCRIPTION, accessible_description, -1); + GCAL_EXIT; +} + +static void +set_calendar (GcalCalendarRow *self, + GcalCalendar *calendar) +{ + g_return_if_fail (GCAL_IS_CALENDAR (calendar)); + g_set_object (&self->calendar, calendar); + + g_object_bind_property_full (self->calendar, "color", + self->color_image, "paintable", + G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE, + paintable_from_gdk_rgb, + NULL, + NULL, + NULL); + g_signal_connect_object (self->calendar, + "notify::visible", (GCallback) updated_visibility_cb, + self, G_CONNECT_SWAPPED); + + g_object_bind_property (calendar, "name", self, "title", G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE); + + gtk_actionable_set_action_name (GTK_ACTIONABLE (self), "event-editor-dialog.select-calendar"); + gtk_actionable_set_action_target (GTK_ACTIONABLE (self), "s", gcal_calendar_get_id (calendar)); + + updated_visibility_cb (self); +} + +/* + * GObject overrides + */ + +static void +gcal_calendar_row_finalize (GObject *object) +{ + GcalCalendarRow *self = (GcalCalendarRow *)object; + + g_clear_object (&self->calendar); + + G_OBJECT_CLASS (gcal_calendar_row_parent_class)->finalize (object); +} + +static void +gcal_calendar_row_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + GcalCalendarRow *self = GCAL_CALENDAR_ROW (object); + + switch (prop_id) + { + case PROP_CALENDAR: + g_value_set_object (value, self->calendar); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +gcal_calendar_row_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + GcalCalendarRow *self = GCAL_CALENDAR_ROW (object); + + switch (prop_id) + { + case PROP_CALENDAR: + g_assert (self->calendar == NULL); + set_calendar (self, g_value_get_object (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + } +} + +static void +gcal_calendar_row_class_init (GcalCalendarRowClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + object_class->finalize = gcal_calendar_row_finalize; + object_class->get_property = gcal_calendar_row_get_property; + object_class->set_property = gcal_calendar_row_set_property; + + properties[PROP_CALENDAR] = g_param_spec_object ("calendar", + "Calendar", + "Calendar", + GCAL_TYPE_CALENDAR, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS); + + g_object_class_install_properties (object_class, N_PROPS, properties); + + gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/ui/event-editor/gcal-calendar-row.ui"); + + gtk_widget_class_bind_template_child (widget_class, GcalCalendarRow, color_image); + gtk_widget_class_bind_template_child (widget_class, GcalCalendarRow, visibility_image); +} + +static void +gcal_calendar_row_init (GcalCalendarRow *self) +{ + gtk_widget_init_template (GTK_WIDGET (self)); +} + +GtkWidget* +gcal_calendar_row_new(GcalCalendar *calendar) +{ + return g_object_new (GCAL_TYPE_CALENDAR_ROW, + "calendar", calendar, + NULL); +} diff --git a/src/gui/event-editor/gcal-calendar-row.h b/src/gui/event-editor/gcal-calendar-row.h new file mode 100644 index 00000000..cbbc1c88 --- /dev/null +++ b/src/gui/event-editor/gcal-calendar-row.h @@ -0,0 +1,33 @@ +/* gcal-calendar-row.h + * + * Copyright 2024 Diego Iván M.E + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include +#include "gcal-calendar.h" +G_BEGIN_DECLS + +#define GCAL_TYPE_CALENDAR_ROW (gcal_calendar_row_get_type()) + +G_DECLARE_FINAL_TYPE (GcalCalendarRow, gcal_calendar_row, GCAL, CALENDAR_ROW, AdwPreferencesRow) + +GtkWidget* gcal_calendar_row_new (GcalCalendar *calendar); + +G_END_DECLS diff --git a/src/gui/event-editor/gcal-calendar-row.ui b/src/gui/event-editor/gcal-calendar-row.ui new file mode 100644 index 00000000..97d3e347 --- /dev/null +++ b/src/gui/event-editor/gcal-calendar-row.ui @@ -0,0 +1,32 @@ + + + + diff --git a/src/gui/event-editor/meson.build b/src/gui/event-editor/meson.build index 1c9707ff..857b9ef1 100644 --- a/src/gui/event-editor/meson.build +++ b/src/gui/event-editor/meson.build @@ -8,6 +8,7 @@ built_sources += gnome.compile_resources( sources += files( 'gcal-alarm-row.c', + 'gcal-calendar-row.c', 'gcal-date-chooser.c', 'gcal-date-chooser-day.c', 'gcal-date-selector.c',