[gnome-calendar] event-widget: Add the timestamp-policy property



commit cb439095a5f60cfbeb339810a7827f51285099c2
Author: Adrien Plazas <kekun plazas laposte net>
Date:   Tue May 10 12:38:02 2022 +0200

    event-widget: Add the timestamp-policy property
    
    This gives some control on what time is displayed on the widget. It will
    be used to show the time of the end of an event in the agenda view.

 src/gui/gcal-event-widget.c     | 99 +++++++++++++++++++++++++++++++++--------
 src/gui/gcal-event-widget.h     |  3 ++
 src/gui/gcal-event-widget.ui    |  4 +-
 src/gui/views/gcal-month-view.c |  4 ++
 src/gui/views/gcal-week-grid.c  |  1 +
 5 files changed, 91 insertions(+), 20 deletions(-)
---
diff --git a/src/gui/gcal-event-widget.c b/src/gui/gcal-event-widget.c
index 3961480a..9b4ac8e4 100644
--- a/src/gui/gcal-event-widget.c
+++ b/src/gui/gcal-event-widget.c
@@ -53,7 +53,7 @@ struct _GcalEventWidget
   /* widgets */
   GtkWidget          *color_box;
   GtkWidget          *horizontal_box;
-  GtkWidget          *hour_label;
+  GtkWidget          *timestamp_label;
   GtkWidget          *squeezer;
   GtkWidget          *summary_label;
   GtkWidget          *vertical_box;
@@ -68,6 +68,8 @@ struct _GcalEventWidget
 
   GtkOrientation      orientation;
 
+  GcalTimestampPolicy timestamp_policy;
+
   gint                old_width;
   gint                old_height;
 
@@ -81,6 +83,7 @@ enum
   PROP_DATE_END,
   PROP_DATE_START,
   PROP_EVENT,
+  PROP_TIMESTAMP_POLICY,
   PROP_ORIENTATION,
   NUM_PROPS
 };
@@ -376,25 +379,37 @@ gcal_event_widget_set_event_tooltip (GcalEventWidget *self,
   g_string_free (tooltip_mesg, TRUE);
 }
 
-static gchar*
-get_hour_label (GcalEventWidget *self)
+static void
+gcal_event_widget_update_timestamp (GcalEventWidget *self)
 {
-  g_autoptr (GDateTime) local_start_time;
+  g_autofree gchar *timestamp_str = NULL;
 
-  local_start_time = g_date_time_to_local (gcal_event_get_date_start (self->event));
+  if (GCAL_IS_EVENT (self->event) &&
+      self->timestamp_policy != GCAL_TIMESTAMP_POLICY_NONE)
+    {
+      g_autoptr (GDateTime) time = NULL;
 
-  if (self->clock_format_24h)
-    return g_date_time_format (local_start_time, "%R");
-  else
-    return g_date_time_format (local_start_time, "%I:%M %P");
+      if (self->timestamp_policy == GCAL_TIMESTAMP_POLICY_START)
+        time = g_date_time_to_local (gcal_event_get_date_start (self->event));
+      else
+        time = g_date_time_to_local (gcal_event_get_date_end (self->event));
+
+      if (gcal_event_get_all_day (self->event) || gcal_event_is_multiday (self->event))
+        timestamp_str = g_date_time_format (time, "%a %B %e");
+      else if (self->clock_format_24h)
+        timestamp_str = g_date_time_format (time, "%R");
+      else
+        timestamp_str = g_date_time_format (time, "%I:%M %P");
+    }
+
+  gtk_widget_set_visible (self->timestamp_label, timestamp_str != NULL);
+  gtk_label_set_label (GTK_LABEL (self->timestamp_label), timestamp_str);
 }
 
 static void
 gcal_event_widget_set_event_internal (GcalEventWidget *self,
                                       GcalEvent       *event)
 {
-  g_autofree gchar *hour_str = NULL;
-
   /*
    * This function is called only once, since the property is
    * set as CONSTRUCT_ONLY. Any other attempt to set an event
@@ -432,18 +447,15 @@ gcal_event_widget_set_event_internal (GcalEventWidget *self,
   /* Tooltip */
   gcal_event_widget_set_event_tooltip (self, event);
 
-  /* Hour label */
-  hour_str = get_hour_label (self);
-
-  gtk_widget_set_visible (self->hour_label, !gcal_event_get_all_day (event));
-  gtk_label_set_label (GTK_LABEL (self->hour_label), hour_str);
-
   /* Summary label */
   g_object_bind_property (event,
                           "summary",
                           self->summary_label,
                           "label",
                           G_BINDING_DEFAULT | G_BINDING_SYNC_CREATE);
+
+  gcal_event_widget_update_style (self);
+  gcal_event_widget_update_timestamp (self);
 }
 
 static void
@@ -560,10 +572,15 @@ gcal_event_widget_set_property (GObject      *object,
       gcal_event_widget_set_event_internal (self, g_value_get_object (value));
       break;
 
+    case PROP_TIMESTAMP_POLICY:
+      gcal_event_widget_set_timestamp_policy (self, g_value_get_enum (value));
+      break;
+
     case PROP_ORIENTATION:
       self->orientation = g_value_get_enum (value);
       gtk_widget_set_visible (self->vertical_box, self->orientation == GTK_ORIENTATION_VERTICAL);
       gcal_event_widget_update_style (self);
+      gcal_event_widget_update_timestamp (self);
       g_object_notify (object, "orientation");
       break;
 
@@ -598,6 +615,10 @@ gcal_event_widget_get_property (GObject      *object,
       g_value_set_object (value, self->event);
       break;
 
+    case PROP_TIMESTAMP_POLICY:
+      g_value_set_enum (value, self->timestamp_policy);
+      break;
+
     case PROP_ORIENTATION:
       g_value_set_enum (value, self->orientation);
       break;
@@ -704,6 +725,23 @@ gcal_event_widget_class_init (GcalEventWidgetClass *klass)
                                                         GCAL_TYPE_EVENT,
                                                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
 
+  /**
+   * GcalEventWidget::timestamp-policy:
+   *
+   * The policy for this widget's timestamp.
+   *
+   * Whether to show the start time, end time, or no time for the
+   * event. Depending on the event's kind, it will be an hour or a day.
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_TIMESTAMP_POLICY,
+                                   g_param_spec_enum ("timestamp-policy",
+                                                      "Timestamp Policy",
+                                                      "The policy for this widget's timestamp",
+                                                      GCAL_TYPE_TIMESTAMP_POLICY,
+                                                      GCAL_TIMESTAMP_POLICY_NONE,
+                                                      G_PARAM_READWRITE));
+
   /**
    * GcalEventWidget::orientation:
    *
@@ -725,7 +763,7 @@ gcal_event_widget_class_init (GcalEventWidgetClass *klass)
   gtk_widget_class_bind_template_child (widget_class, GcalEventWidget, color_box);
   gtk_widget_class_bind_template_child (widget_class, GcalEventWidget, drag_source);
   gtk_widget_class_bind_template_child (widget_class, GcalEventWidget, horizontal_box);
-  gtk_widget_class_bind_template_child (widget_class, GcalEventWidget, hour_label);
+  gtk_widget_class_bind_template_child (widget_class, GcalEventWidget, timestamp_label);
   gtk_widget_class_bind_template_child (widget_class, GcalEventWidget, squeezer);
   gtk_widget_class_bind_template_child (widget_class, GcalEventWidget, summary_label);
   gtk_widget_class_bind_template_child (widget_class, GcalEventWidget, vertical_box);
@@ -880,6 +918,31 @@ gcal_event_widget_set_date_start (GcalEventWidget *self,
     }
 }
 
+/**
+ * gcal_event_widget_set_timestamp_policy:
+ * @self: a #GcalEventWidget
+ * @policy: the timestamp policy
+ *
+ * Sets whether to show the start time, end time, or no time for the
+ * event. Depending on the event's kind, it will be an hour or a day.
+ */
+void
+gcal_event_widget_set_timestamp_policy (GcalEventWidget     *self,
+                                        GcalTimestampPolicy  policy)
+{
+  g_return_if_fail (GCAL_IS_EVENT_WIDGET (self));
+  g_return_if_fail (policy >= GCAL_TIMESTAMP_POLICY_NONE && policy <= GCAL_TIMESTAMP_POLICY_END);
+
+  if (self->timestamp_policy != policy)
+    {
+      self->timestamp_policy = policy;
+
+      gcal_event_widget_update_timestamp (self);
+
+      g_object_notify (G_OBJECT (self), "timestamp-policy");
+    }
+}
+
 /**
  * gcal_event_widget_show_preview:
  * @self: a #GcalEventWidget
diff --git a/src/gui/gcal-event-widget.h b/src/gui/gcal-event-widget.h
index 6300e840..c57ab91b 100644
--- a/src/gui/gcal-event-widget.h
+++ b/src/gui/gcal-event-widget.h
@@ -57,6 +57,9 @@ void                 gcal_event_widget_set_date_end              (GcalEventWidge
 void                 gcal_event_widget_set_read_only             (GcalEventWidget    *event,
                                                                   gboolean            read_only);
 
+void                 gcal_event_widget_set_timestamp_policy      (GcalEventWidget     *event,
+                                                                  GcalTimestampPolicy  timestamp_policy);
+
 void                 gcal_event_widget_show_preview              (GcalEventWidget          *self,
                                                                   GcalEventPreviewCallback  callback,
                                                                   gpointer                  user_data);
diff --git a/src/gui/gcal-event-widget.ui b/src/gui/gcal-event-widget.ui
index 2af888b2..2a260d42 100644
--- a/src/gui/gcal-event-widget.ui
+++ b/src/gui/gcal-event-widget.ui
@@ -47,7 +47,7 @@
                     <property name="request-mode">width-for-height</property>
                     <child>
                       <object class="GtkLabel">
-                        <property name="label" bind-source="hour_label" bind-property="label" 
bind-flags="default" />
+                        <property name="label" bind-source="timestamp_label" bind-property="label" 
bind-flags="default" />
                         <property name="xalign">0.0</property>
                         <style>
                           <class name="dim-label" />
@@ -91,7 +91,7 @@
                   </object>
                 </child>
                 <child>
-                  <object class="GtkLabel" id="hour_label">
+                  <object class="GtkLabel" id="timestamp_label">
                     <style>
                       <class name="dim-label" />
                     </style>
diff --git a/src/gui/views/gcal-month-view.c b/src/gui/views/gcal-month-view.c
index 8e7892c9..cc83f0fa 100644
--- a/src/gui/views/gcal-month-view.c
+++ b/src/gui/views/gcal-month-view.c
@@ -1860,6 +1860,8 @@ gcal_month_view_add_event (GcalTimelineSubscriber *subscriber,
 
   event_widget = gcal_event_widget_new (self->context, event);
   gcal_event_widget_set_read_only (GCAL_EVENT_WIDGET (event_widget), gcal_calendar_is_read_only (calendar));
+  if (!gcal_event_get_all_day (event) && !gcal_event_is_multiday (event))
+    gcal_event_widget_set_timestamp_policy (GCAL_EVENT_WIDGET (event_widget), GCAL_TIMESTAMP_POLICY_START);
 
   add_event_widget (self, event_widget);
 
@@ -1894,6 +1896,8 @@ gcal_month_view_update_event (GcalTimelineSubscriber *subscriber,
 
   /* Create and add the new event widget */
   new_widget = gcal_event_widget_new (self->context, event);
+  if (!gcal_event_get_all_day (event) && !gcal_event_is_multiday (event))
+    gcal_event_widget_set_timestamp_policy (GCAL_EVENT_WIDGET (new_widget), GCAL_TIMESTAMP_POLICY_START);
   add_event_widget (self, new_widget);
 
   self->needs_reallocation = TRUE;
diff --git a/src/gui/views/gcal-week-grid.c b/src/gui/views/gcal-week-grid.c
index bb0aeaf3..50f721ef 100644
--- a/src/gui/views/gcal-week-grid.c
+++ b/src/gui/views/gcal-week-grid.c
@@ -944,6 +944,7 @@ gcal_week_grid_add_event (GcalWeekGrid *self,
                          "context", self->context,
                          "event", event,
                          "orientation", GTK_ORIENTATION_VERTICAL,
+                         "timestamp-policy", GCAL_TIMESTAMP_POLICY_START,
                          NULL);
 
   gcal_range_tree_add_range (self->events,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]