From 21eba8ea0a89fd15b7a571754a617dfad83d8ca9 Mon Sep 17 00:00:00 2001 From: Novi-Novi Date: Tue, 31 Jan 2017 13:42:43 +0000 Subject: [PATCH] event-widget: show tooltip with event details In month view and year view, when the mouse is hovering over an event, nothing is shown. A tooltip with the event information would be much more useful in this scenario. Fix that by showing a tooltip displaying the event's title, time and date, and, if present, the location and notes of the event. Long notes are truncated and ellipsized. https://bugzilla.gnome.org/show_bug.cgi?id=774952 --- src/gcal-event-widget.c | 143 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) diff --git a/src/gcal-event-widget.c b/src/gcal-event-widget.c index 0e316b04..4879bf4e 100644 --- a/src/gcal-event-widget.c +++ b/src/gcal-event-widget.c @@ -20,12 +20,14 @@ #define G_LOG_DOMAIN "GcalEventWidget" #include +#include #include "gcal-event-widget.h" #include "gcal-utils.h" -#define INTENSITY(c) ((c->red) * 0.30 + (c->green) * 0.59 + (c->blue) * 0.11) -#define ICON_SIZE 16 +#define DESC_MAX_CHAR 200 +#define INTENSITY(c) ((c->red) * 0.30 + (c->green) * 0.59 + (c->blue) * 0.11) +#define ICON_SIZE 16 struct _GcalEventWidget { @@ -240,6 +242,141 @@ update_color (GcalEventWidget *self) g_free (color_str); } +static void +gcal_event_widget_set_event_tooltip (GcalEventWidget *self, + GcalEvent *event) +{ + g_autoptr (GDateTime) tooltip_start, tooltip_end; + g_autofree gchar *start, *end; + gboolean allday, multiday, is_ltr; + GString *tooltip_desc, *tooltip_mesg; + + tooltip_mesg = g_string_new (NULL); + g_string_append_printf (tooltip_mesg, "%s", gcal_event_get_summary (event)); + + tooltip_start = g_date_time_to_local (gcal_event_get_date_start (event)); + tooltip_end = g_date_time_to_local (gcal_event_get_date_end (event)); + + allday = gcal_event_get_all_day (event); + multiday = gcal_event_is_multiday (event); + + is_ltr = gtk_widget_get_direction (GTK_WIDGET (self)) != GTK_TEXT_DIR_RTL; + + if (allday) + { + if (multiday) + { + start = g_date_time_format (tooltip_start, "%x"); + end = g_date_time_format (tooltip_end, "%x"); + } + else + { + start = g_date_time_format (tooltip_start, "%x"); + end = NULL; + } + } + else + { + if (multiday) + { + if (self->clock_format_24h) + { + if (is_ltr) + { + start = g_date_time_format (tooltip_start, "%x %R"); + end = g_date_time_format (tooltip_end, "%x %R"); + } + else + { + start = g_date_time_format (tooltip_start, "%R %x"); + end = g_date_time_format (tooltip_end, "%R %x"); + } + } + else + { + if (is_ltr) + { + start = g_date_time_format (tooltip_start, "%x I:%M %P"); + end = g_date_time_format (tooltip_end, "%x I:%M %P"); + } + else + { + start = g_date_time_format (tooltip_start, "%P %M:%I %x"); + end = g_date_time_format (tooltip_end, "%P %M:%I %x"); + } + } + } + else + { + if (self->clock_format_24h) + { + if (is_ltr) + { + start = g_date_time_format (tooltip_start, "%x, %R"); + end = g_date_time_format (tooltip_end, "%R"); + } + else + { + start = g_date_time_format (tooltip_start, "%R ,%x"); + end = g_date_time_format (tooltip_end, "%R"); + } + } + else + { + if (is_ltr) + { + start = g_date_time_format (tooltip_start, "%x, I:%M %P"); + end = g_date_time_format (tooltip_end, "I:%M %P"); + } + else + { + start = g_date_time_format (tooltip_start, "%P %M:%I ,%x"); + end = g_date_time_format (tooltip_end, "%P %M:%I"); + } + } + } + } + + if (allday && !multiday) + { + g_string_append_printf (tooltip_mesg, + "\n%s", + start); + } + else + { + g_string_append_printf (tooltip_mesg, + "\n%s - %s", + is_ltr ? start : end, + is_ltr ? end : start); + } + + /* Append event location */ + if (g_utf8_strlen (gcal_event_get_location (event), -1) > 0) + { + g_string_append (tooltip_mesg, "\n"); + g_string_append_printf (tooltip_mesg, + _("At %s"), + gcal_event_get_location (event)); + } + + tooltip_desc = g_string_new (gcal_event_get_description (event)); + + /* Truncate long descriptions at a white space and ellipsize */ + if (tooltip_desc->len > 0) + { + g_string_truncate (tooltip_desc, DESC_MAX_CHAR - 1); + g_string_append (tooltip_desc, "…"); + + g_string_append_printf (tooltip_mesg, "\n%s", tooltip_desc->str); + } + + gtk_widget_set_tooltip_markup (GTK_WIDGET (self), tooltip_mesg->str); + + g_string_free (tooltip_desc, TRUE); + g_string_free (tooltip_mesg, TRUE); +} + static void gcal_event_widget_set_event_internal (GcalEventWidget *self, GcalEvent *event) @@ -275,6 +412,8 @@ gcal_event_widget_set_event_internal (GcalEventWidget *self, "notify::summary", G_CALLBACK (gtk_widget_queue_draw), self); + + gcal_event_widget_set_event_tooltip (self, event); } static void